How to get user ID when writing Linux kernel module

Here is my function in my kernel module, which I insert with the insmod command after make in later steps. I am working on goldfish (2.6.29)

 asmlinkage long our_sys_read(unsigned int fd, char *buf, size_t count) { printk("------->> our_sys_read getuid() ---------- %d\n", getuid()); return original_call_read(fd,buf,count); } 

I want to catch system calls and find out which user made these system calls. But when I run make, it calls me after an error.

 /home/mohsin/LKM/trapcall.c:245: error: implicit declaration of function 'getuid' 

Any suggestion would be appreciated.

+4
source share
3 answers

After spending two days, I finally figured out how to get the uid of the process that made the system call. I will give all the suggestions that I found on different links, so that if my solution does not work, one of the others can work.

1) As Mats told me,

 #include <include/linux/cred.h> static int getuid() { return current_uid(); } 

You call this function to get uid, but it gave me negative numbers like -943124788 etc.

2)

 uid_t credd_uid ; const struct cred *cred = current_cred(); credd_uid = current->cred->uid; 

Same result as large negative numbers.

3)

 uid_t struct_uid; struct user_struct *u = current_user(); struct_uid = get_uid(u); 

4) working solution

It is given here .

i) Declare a function prototype on top, for example

 asmlinkage int (*getuid_call)(); 

ii) Add the following line to the init_module () function

/ * Get a system call for getuid * /

  getuid_call = sys_call_table[__NR_getuid]; 

iii) Call the function in your system calls to get uid as

 uid_t uid = getuid_call(); 
+5
source

Perhaps you can use this:

  #include <include/linux/cred.h> static int getuid() { return current_uid(); } 

cred means "credentials" and this macro returns the user ID of the currently active credentials. Keep in mind, however, that “current user ID” can mean several things on Linux.

[dan3 obviously didn't need to dig as much code as me to find this, or he crossed in front of me!]

+6
source

You need to call current_uid () defined in linux / cred.h (since version 2.6, previously used for current-> uid). See Account Document Document.

current - macro , BTW.

+5
source

All Articles