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

时间:2013-11-07 04:41:42

标签: linux security kernel system-calls seccomp

我正在调查seccomp-bpf的实现细节,这是自3.5版以来引入Linux的syscall过滤机制。 我查看了Linux 3.10中kernel / 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 个答案:

答案 0 :(得分:1)

当用户进程进入内核时,寄存器集将存储到内核变量中。 函数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使用用户线程的当前寄存器集来确定系统调用信息。