Task
I have a small kernel module I wrote for my RaspBerry Pi 2 which implements an additional system call for generating power consumption metrics. I would like to modify the system call so that it only gets invoked if a special user (such as "root" or user "pi") issues it. Otherwise, the call just skips the bulk of its body and returns success.
Background Work
I've read into the issue at length, and I've found a similar question on SO, but there are numerous problems with it, from my perspective (noted below).
Question
The linked question notes thatstruct task_struct
contains a pointer element tostruct cred
, as defined inlinux/sched.h
andlinux/cred.h
. The latter of the two headers doesn't exist on my system(s), and the former doesn't show any declaration of a pointer to astruct cred
element. Does this make sense?- Silly mistake. This is present in its entirety in the kernel headers (ie:
/usr/src/linux-headers-$(uname -r)/include/linux/cred.h
), I was searching in gcc-build headers in/usr/include/linux
.
- Silly mistake. This is present in its entirety in the kernel headers (ie:
Even if the above worked, it doesn't mention if I would be getting the the real, effective, or saved UID for the process. Is it even possible to get each of these three values from within the system call?cred.h
already contains all of these.
Is there a safe way in the kernel module to quickly determine which groups the user belongs to without parsing/etc/group
?cred.h
already contains all of these.
Update
So, the only valid question remaining is the following:
Note, that iterating through processes and reading process's credentials should be done under RCU-critical section.
...
how do I ensure my check is run in this critical section? Are there any working examples of how to accomplish this? I've found some existing kernel documentation that instructs readers to wrap the relevant code withrcu_read_lock()
andrcu_read_unlock()
. Do I just need to wrap an read operations against thestruct cred
and/orstruct task_struct
data structures?