C 编写Linux内核模块时如何获取用户ID

C 编写Linux内核模块时如何获取用户ID,c,linux-kernel,C,Linux Kernel,这是我在内核模块中的函数,我在后面的make之后使用insmod命令插入它。我正在研究金鱼(2.6.29) 我想捕获系统调用并找出是哪个用户进行了这些系统调用。但当我运行“make”时,它会抛出以下错误 /home/mohsin/LKM/trapcall.c:245: error: implicit declaration of function 'getuid' 任何建议都将不胜感激。您需要调用linux/cred.h中定义的current\u uid()(从2.6开始,以前是current

这是我在内核模块中的函数,我在后面的
make
之后使用
insmod
命令插入它。我正在研究金鱼(2.6.29)

我想捕获系统调用并找出是哪个用户进行了这些系统调用。但当我运行“make”时,它会抛出以下错误

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

任何建议都将不胜感激。

您需要调用linux/cred.h中定义的current\u uid()(从2.6开始,以前是current->uid)。见


当前是a,顺便说一句。

您或许可以使用此选项:

 #include <include/linux/cred.h>

 static int getuid()
 {
     return current_uid();
 }
#包括
静态int getuid()
{
返回当前uid();
}
cred代表“凭据”,此宏返回当前活动凭据的用户id。但是请记住,“当前用户id”在Linux中可能意味着多个方面


[dan3显然不需要像我一样深入挖掘大量代码来找到这个-或者他比我先开始了!]

花了两天时间,我终于找到了如何获取进行系统调用的进程的uid。我会给出我在不同链接上找到的所有建议,这样,如果我的解决方案不起作用,其他解决方案中的一个可能会起作用

1) 正如Mats告诉我的

#include <include/linux/cred.h>

 static int getuid()
 {
     return current_uid();
 }
像大负数一样输出相同

(三)

4) 有效解决方案

事实上,它是给定的

i) 在顶部声明函数原型,如

asmlinkage int (*getuid_call)();
ii)在init_module()函数中添加以下行

/*获取getuid*的系统调用

  getuid_call = sys_call_table[__NR_getuid];
iii)调用系统调用函数中的函数,以获得类似uid的

uid_t uid = getuid_call();

不使用
getuid
syscall挂钩获取UID:

#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
}
您还应该通过
linux/uidgid.h
查找定义根uid/gid以及内联比较函数的有用宏,以避免直接调用
\uu kuid\u val()

例如,常见的用法是检查用户是否为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
}

我以前用过它,但我不能说我一下子就记住了:)我已经测试过这段代码,它编译得非常好。但当我使用insmod插入模块时,它会打印以下UID,我猜这是错误的。------->>我们的系统读取getuid()------------1035134260----------->我们的系统读取getuid()------------943124788Hmm。听起来有点不公平。你的实际用户的用户id是什么?嗯……如果我告诉你我正在使用Android内核,我相信你的答案不会有什么不同。我已签入packages.xml文件,该文件包含已安装应用程序的所有用户ID列表。它们都没有这两个用户ID。它们从10000开始,然后递增1。我确实相信用户id是用户id和“名称空间”的组合,但它不能解释你得到的数字-我认为。看起来很难。。看看这段代码是如何实现的:drivers/android/binder.c:binder\u ioctl\u set\u ctx\u mgr()
uid_t uid = getuid_call();
#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
}
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
}