Reading / dev / cpu / * / msr from user space: operation not allowed

I am trying to write a simple application that can read msr registers and I am running this application from user space.

I downloaded the msr module and gave read permissions for each in / dev / cpu / * / msr. But still, the user cannot access these files, but root can.

Access rights are as follows:

crw-r--r-- 1 root root 202, 0 sep 6 17:55 /dev/cpu/0/msr crw-r--r-- 1 root root 202, 1 sep 6 17:55 /dev/cpu/1/msr crw-r--r-- 1 root root 202, 2 sep 6 17:55 /dev/cpu/2/msr crw-r--r-- 1 root root 202, 3 sep 6 17:55 /dev/cpu/3/msr 

I keep getting the “Operation not allowed” error message when I try to read these files from user space, but it works fine when root tries to access them. What am I doing wrong? I'm on Ubuntu 13.04 with kernel version 3.11.0.

+6
source share
2 answers

Changes in the main Linux kernel starting from 3.7 now require that the executable has the ability CAP_SYS_RAWIO to open the MSR device file [2]. In addition to loading the MSR kernel module and setting the appropriate permissions on the msr file file, you must provide the CAP_SYS_RAWIO option to any executable file for the user who needs access to the MSR driver using the following command:

 sudo setcap cap_sys_rawio=ep <user_executable> 
+6
source

You can see vfs_read:

 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos) { ret = rw_verify_area(READ, file, pos, count); if (ret >= 0) { ... if (file->f_op->read) // your driver read . ret = file->f_op->read(file, buf, count, pos); else ret = do_sync_read(file, buf, count, pos); .... } // here, if the ret is 13. your error will be occur. return ret; } 
0
source

Source: https://habr.com/ru/post/953340/


All Articles