How to get userID when writing Linux kernel module

后端 未结 4 2075
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 16:13

Here is my function in my kernel module which I insert using insmod command after make at later stages. I am working on goldfish (2.6.29)

4条回答
  •  执念已碎
    2020-12-30 17:07

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

    1) As told me Mats,

    #include 
    
     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 output like large negative numbers.

    3)

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

    4) Worked Solution

    It's given here actually.

    i) Declare function prototype on the top like

    asmlinkage int (*getuid_call)();
    

    ii) Add following line to init_module() function

    /* Get the system call for getuid */

      getuid_call = sys_call_table[__NR_getuid];
    

    iii) Call the function in your trapped system call functions to get uid like

    uid_t uid = getuid_call();
    

提交回复
热议问题