Linux将'ruid'和'euid'存储在哪里?

4

我想知道Linux内核在哪里存储'ruid'和'euid'。

以下是我所了解的:

当用户运行文件并将其转换为进程时,该进程会获得ruid和euid。

如果文件被设置为使用setuid,则进程的euid将更改为该文件所有者的用户ID。如果没有设置,则euid将不会更改,并且与ruid相同。

然后,Linux内核允许进程根据ruid和euid运行另一个进程或使用系统中的其他资源。

因此,我认为这意味着内核必须在RAM中某个地方保存每个进程的ruid和euid。

我以为“某个地方”在PCB中,但PCB块没有ruid和euid字段。

我试图在'/proc'目录的进程文件中找到它们,但失败了。

Linux在哪里保存正在运行进程的ruid和euid?

1个回答

3
以下是新内核中它如何工作的说明:
  • From user-space point of view, real and effective user ID can be changed using setreuid() syscall. See man 2 setreuid for usage details

  • Kernel is using struct cred for storing UID and EUID

  • Each process has its own struct cred; take a look at .cred field in struct task_struct

  • RUID is stored in .uid field of struct cred; see setreuid() syscall code:

      struct cred *new;
      kuid_t kruid, keuid;
      ...
      kruid = make_kuid(ns, ruid);
      keuid = make_kuid(ns, euid);
      ...
      new->uid = kruid;
      new->euid = keuid;
      ...
      return commit_creds(new);
    
  • commit_creds() function is actually sets RUID and EUID to current process

参见此答案,了解有关旧内核的线索:如何在Linux Kernel 4.2中获取当前进程的UID和EUID?


谢谢。根据您的回答和链接,Linux内核使用“struct cred”来存储UID和EUID,并且“struct cred”所在的内存区域是内核区域,因此用户只能通过系统调用(例如setreuid和getreuid)访问该空间。这是正确的吗? - Joon
我所知道的另一种获取该信息的方法是查看/proc/$pid/status文件。但那将是从内核读取的相同数据。因此,如果您想更改进程的UID,则必须在内核空间中执行此操作,而唯一的方法(最终)是使用系统调用。 - Sam Protsenko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接