How to get userID when writing Linux kernel module

后端 未结 4 2072
伪装坚强ぢ
伪装坚强ぢ 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:01

    Getting the UID without using the getuid syscall hook:

    #include "linux/cred.h"
    
    static inline uid_t get_uid(void) {
    #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
    #include "linux/uidgid.h"
        // current_uid() returns struct in newer kernels
        return __kuid_val(current_uid());
    #else
        return 0 == current_uid();
    #endif
    }
    

    You should also look through linux/uidgid.h for useful macros that define ROOT uid/gid as well as inline comparison functions to avoid directly calling __kuid_val().

    For example, a common use would be to check if the user is root:

    static inline bool is_root_uid(void) {
    #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
    #include "linux/uidgid.h"
        // current_uid() returns struct in newer kernels
        return uid_eq(current_uid(), GLOBAL_ROOT_UID);
    #else
        return 0 == current_uid();
    #endif
    }
    

提交回复
热议问题