如何在编写Linux内核模块时获取userID

Jun*_*aid 5 c linux-kernel

这是我的内核模块中的函数,我在后面的阶段使用insmod命令插入make.我正在努力 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);
}
Run Code Online (Sandbox Code Playgroud)

我想捕获系统调用并找出哪些用户进行了这些系统调用.但是当我运行'make'时,它会抛出我的错误.

/home/mohsin/LKM/trapcall.c:245: error: implicit declaration of function 'getuid'
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激.

Mat*_*son 6

你也许可以用这个:

 #include <include/linux/cred.h>

 static int getuid()
 {
     return current_uid();
 }
Run Code Online (Sandbox Code Playgroud)

cred代表"凭据",此宏返回当前活动凭据的用户ID.但请记住,"当前用户ID"在Linux中可能意味着多种用途.

[dan3显然没有像我一样挖掘相同数量的代码 - 或者他在我之前开始!]


Jun*_*aid 6

花了两天之后,我终于想出了如何获得进行系统调用的进程.我将提供我在不同链接上找到的所有建议,以便如果我的解决方案不起作用,其他一个可能会起作用.

1)告诉我垫,

#include <include/linux/cred.h>

 static int getuid()
 {
     return current_uid();
 }
Run Code Online (Sandbox Code Playgroud)

你调用这个函数来获取uid但它给了我负数等-943124788.

2)

uid_t credd_uid ;
const struct cred *cred = current_cred();
credd_uid = current->cred->uid; 
Run Code Online (Sandbox Code Playgroud)

相同的输出像大负数.

3)

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

struct_uid = get_uid(u);
Run Code Online (Sandbox Code Playgroud)

4)工作解决方案

它实际上是在这里给出的.

i)在顶部声明函数原型

asmlinkage int (*getuid_call)();
Run Code Online (Sandbox Code Playgroud)

ii)将以下行添加到init_module()函数

/*获取getuid的系统调用*/

  getuid_call = sys_call_table[__NR_getuid];
Run Code Online (Sandbox Code Playgroud)

iii)调用被困系统调用函数中的函数以获得类似的功能

uid_t uid = getuid_call();
Run Code Online (Sandbox Code Playgroud)


dan*_*an3 5

你需要调用在linux/cred.h中定义的current_uid()(从2.6开始,以前是current-> uid).请参阅有关凭据内核文档

当前是一个,BTW.