OpenBSD 内核代码中 uint64_t ps_pledge 变量的用途是什么?

What is the purpose of uint64_t ps_pledge varibale in OpenBSD kernel code?

我正在玩 OpenBSD 内核代码,尤其是这个文件 sys/kern/sched_bsd.c

    void
    schedcpu(void *arg)
    {
......

......

    LIST_FOREACH(p, &allproc, p_list) {
        /*
         * Increment sleep time (if sleeping). We ignore overflow.
         */
        if (p->p_stat == SSLEEP || p->p_stat == SSTOP)
            p->p_slptime++;
        p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
        /*
         * If the process has slept the entire second,
         * stop recalculating its priority until it wakes up.
         */
        if (p->p_slptime > 1)
            continue;
        SCHED_LOCK(s);
        /*
         * p_pctcpu is only for diagnostic tools such as ps.
         */
....

....
LIST_FOREACH(TYPE *var, LIST_HEAD *head, LIST_ENTRY NAME);
The macro LIST_FOREACH traverses the list referenced by head in the forward direction, assigning each element in turn to var.

现在,p 将包含文件

中每个进程的 struct proc 结构的地址

sys/sys/proc.h

现在,这个结构再次包含另一个 struct process *p_p 结构,它表示每个进程的属性,如 pidflagsthreads

    struct proc {
        TAILQ_ENTRY(proc) p_runq;
        LIST_ENTRY(proc) p_list;    /* List of all threads. */

        struct  process *p_p;       /* The process of this thread. */
        TAILQ_ENTRY(proc) p_thr_link;   /* Threads in a process linkage. */

        TAILQ_ENTRY(proc) p_fut_link;   /* Threads in a futex linkage. */
        struct  futex   *p_futex;   /* Current sleeping futex. */

        /* substructures: */
        struct  filedesc *p_fd;     /* copy of p_p->ps_fd */
        struct  vmspace *p_vmspace; /* copy of p_p->ps_vmspace */
#define p_rlimit    p_p->ps_limit->pl_rlimit

    ....

    ....

现在,结构 struct process 包含 uint64_t ps_plegde

struct process {
    /*
     * ps_mainproc is the original thread in the process.
     * It's only still special for the handling of p_xstat and
     * some signal and ptrace behaviors that need to be fixed.
     */
    struct  proc *ps_mainproc;
    struct  ucred *ps_ucred;    /* Process owner's identity. */

....

....

    u_short ps_acflag;      /* Accounting flags. */

    uint64_t ps_pledge;
    uint64_t ps_execpledge; 

....

....

现在,我在void schedcpu()函数代码中写了一些修改。

void
schedcpu(void *arg)
{
    pid_t pid;
    uint64_t pledge_bit;
....

....


    LIST_FOREACH(p, &allproc, p_list) {

    pid=p->p_p->pid;
    pledge_bit=p->p_p->ps_pledge;

    if (pledge_bit) {
            printf("pid: %10d pledge_bit: %10llu pledge_xbit:%10llx\n",pid,pledge_bit,pledge_bit);
}

        /*
         * Increment sleep time (if sleeping). We ignore overflow.
         */
        if (p->p_stat == SSLEEP || p->p_stat == SSTOP)
            p->p_slptime++;
        p->p_pctcpu = (p->p_pctcpu * ccpu) >> FS 
....

....

Here, Kernel log

pid:      37846 pledge_bit:     393359 pledge_xbit:      6008f
pid:      96037 pledge_bit:     393544 pledge_xbit:      60148
pid:      86032 pledge_bit:     264297 pledge_xbit:      40869
pid:      72264 pledge_bit:     393480 pledge_xbit:      60108
pid:      40102 pledge_bit:          8 pledge_xbit:          8
pid:        841 pledge_bit: 2148162527 pledge_xbit:   800a5bdf
pid:      49970 pledge_bit: 2148096143 pledge_xbit:   8009588f
pid:      68505 pledge_bit:         40 pledge_xbit:         28
pid:      46106 pledge_bit:         72 pledge_xbit:         48
pid:      77690 pledge_bit:     537161 pledge_xbit:      83249
pid:      44005 pledge_bit:     262152 pledge_xbit:      40008
pid:      82731 pledge_bit: 2148096143 pledge_xbit:   8009588f
pid:      71609 pledge_bit:     262472 pledge_xbit:      40148
pid:      54330 pledge_bit:     662063 pledge_xbit:      a1a2f
pid:      77764 pledge_bit:    1052776 pledge_xbit:     101068
pid:        699 pledge_bit: 2148096143 pledge_xbit:   8009588f
pid:      84265 pledge_bit:    1052776 pledge_xbit:     101068

....

....

现在,是否可以通过查看我从上面的输出中获得的 pledge_bit(十进制或十六进制值)来知道哪个进程保证了哪些权限?

我获取了 dhclient 进程的十六进制值,即 0x8009588f,然后,我用 pledge("STDIO",NULL); 编写了一个示例 hello world 程序,我再次查看 dmesg 并得到相同的 pledge_bit你好世界,即 0x8009588f

然后,这次查看dhclient源码发现,dhclient代码质押pledge("stdio inet dns route proc", NULL)

但是,那么,不同的质押参数如何得到相同的质押十六进制位呢?

看了一遍又一遍的源码,终于有了答案。

所以,我只是想贡献我的知识,这样未来的开发人员就不会遇到任何关于这个问题的问题或困惑。

初识:

我是这样写hello world的,

void
main() {
pledge("STDIO", NULL);   /* wrong use of pledge call */
printf("Hello world\n");
}

以上代码更正:

void
main() {

if (pledge("stdio", NULL) == -1) {
        printf("Error\n");
}
printf("Hello world\n");
}

我忘了检查质押 () 中的 return 值。

二次理解:

dhclient.c 代码包含质押 () 调用为:

int
main(int argc, char *argv[])
{
    struct ieee80211_nwid    nwid;
    struct ifreq         ifr;
    struct stat      sb;
    const char      *tail_path = "/etc/resolv.conf.tail";
....

....


fork_privchld(ifi, socket_fd[0], socket_fd[1]);

....

....


    if ((cmd_opts & OPT_FOREGROUND) == 0) {
        if (pledge("stdio inet dns route proc", NULL) == -1)
            fatal("pledge");
    } else {
        if (pledge("stdio inet dns route", NULL) == -1)
            fatal("pledge");
    }

....

....

void
fork_privchld(struct interface_info *ifi, int fd, int fd2)
{
    struct pollfd    pfd[1];
    struct imsgbuf  *priv_ibuf;
    ssize_t      n;
    int      ioctlfd, routefd, nfds, rslt;

    switch (fork()) {
    case -1:
        fatal("fork");
        break;
    case 0:
        break;
    default:
        return;
    }
....

....

}

现在,我编写了示例 hello world 代码,其中包含与 dhclient 相同的参数:

void
main() {

if(pledge("stdio inet proc route dns", NULL) == -1) {
        printf("Error\n");
}

while(1) {}
}

现在,我测试了上面的示例 hello world 代码,得到 pledge_bit 作为十六进制的 0x101068,这是正确的。

但是,正如我之前在问题中告诉你的,当我看到 dhclient 的不同 pledge_bit 时,我感到很困惑。因为,正如你们所知,两者在质押 () 中具有相同的参数。

那怎么可能呢?

现在,问题来了,

在不断查看dhclient源码后,发现了一个函数,名字为fork_privchld().

这个函数在dhclient中是在质押之前调用的,所以,这个函数是在质押之前调用的,就像没有质押一样。

所以,为了再次验证,我编写了示例 hello world 代码,但这次没有任何 pledge() 系统调用。

void
main() {
printf("hello\n");
}

而且,你猜怎么着,我得到了与 0x8009588f 相同的 pledge_bit。

所以,在这个验证之后,验证了 fork_privchld() 函数没有设置任何质押位,因为它在 dhclient.

质押之前被调用

此函数为 dhclient 创建一个 [priv] 子进程。

# ps aux|grep dhclient
root     26416  0.0  0.1   608   544 ??  Is     8:36AM    0:00.00 dhclient: em0 [priv] (dhclient)
_dhcp    33480  0.0  0.1   744   716 ??  Isp    8:36AM    0:00.00 dhclient: em0 (dhclient)

而且,我不知道为什么我只看第一个过程,即 [priv] (dhclient)。此进程是由 fork_privchld() 函数创建的进程。

这就是为什么这个过程有 0x8009588f 质押位(由于在质押之前调用,所以此时没有质押)。

而且,当我检查第二个过程时,即 _dhcp,然后我得到了我预期的 pledge_bit,即 0x101068(由于认捐)。

所以,我希望阅读本文后事情会清楚。

注意:请随时更新我,如果我忘记或错过了什么。