【问题标题】:How does seccomp-bpf filter syscalls?seccomp-bpf 如何过滤系统调用?
【发布时间】:2013-11-18 15:18:47
【问题描述】:

我正在研究 seccomp-bpf 的实现细节,这是从 3.5 版开始引入 Linux 的系统调用过滤机制。 我从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;
}

【问题讨论】:

    标签: linux security kernel system-calls seccomp


    【解决方案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使用用户线程的当前寄存器集来确定系统调用信息。

    【讨论】:

    • 谢谢,Juho。我已经了解 BPF_S_ANC_SECCOMP_LD_W 指令如何用于告诉解释器从 struct seccomp_dara 加载系统调用信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多