seccomp-bpf如何过滤系统调用?

8
我正在调查seccomp-bpf的实现细节,这是自3.5版以来引入Linux中的系统调用过滤机制。我查看了Linux 3.10内核/seccomp.c的源代码,并想问一些有关此事的问题。
从seccomp.c中可以看出,__secure_computing()会调用seccomp_run_filters()测试当前进程调用的系统调用。但是,在查看seccomp_run_filters()时,传递的系统调用号并没有在任何地方使用。
似乎sk_run_filter()是BPF过滤器机器的实现,但从seccomp_run_filters()调用sk_run_filter()时,第一个参数(要运行过滤器的缓冲区)为NULL。
我的问题是:seccomp_run_filters()如何在不使用参数的情况下过滤系统调用?
以下是seccomp_run_filters()的源代码:
/**
 * seccomp_run_filters - evaluates all seccomp filters against @syscall
 * @syscall: number of the current system call
 *
 * Returns valid seccomp BPF response codes.
 */
static u32 seccomp_run_filters(int syscall)
{
        struct seccomp_filter *f;
        u32 ret = SECCOMP_RET_ALLOW;

        /* Ensure unexpected behavior doesn't result in failing open. */
        if (WARN_ON(current->seccomp.filter == NULL))
                return SECCOMP_RET_KILL;

        /*
         * All filters in the list are evaluated and the lowest BPF return
         * value always takes priority (ignoring the DATA).
         */
        for (f = current->seccomp.filter; f; f = f->prev) {
                u32 cur_ret = sk_run_filter(NULL, f->insns);
                if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
                        ret = cur_ret;
        }
        return ret;
}
1个回答

3
当用户进程进入内核时,寄存器集将被存储到内核变量中。函数sk_run_filter实现了过滤器语言的解释器。seccomp过滤器的相关指令是BPF_S_ANC_SECCOMP_LD_W。每个指令都有一个常数k,在这种情况下,它指定要读取的单词的索引。
#ifdef CONFIG_SECCOMP_FILTER
            case BPF_S_ANC_SECCOMP_LD_W:
                    A = seccomp_bpf_load(fentry->k);
                    continue;
#endif

seccomp_bpf_load函数使用用户线程的当前寄存器集合来确定系统调用信息。


谢谢,Juho。我已经理解了指令BPF_S_ANC_SECCOMP_LD_W的用法,它告诉解释器从struct seccomp_data中加载系统调用信息。 - user2875834

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