linux – seccomp-bpf如何过滤系统调用?
发布时间:2020-12-13 23:25:05 所属栏目:Linux 来源:网络整理
导读:我正在调查seccomp-bpf的实现细节,这是自3.5版以来引入 Linux的系统调用过滤机制. 我查看了Linux 3.10中kernel / seccomp.c的源代码,并想询问一些有关它的问题. 从seccomp.c开始,似乎从__secure_computing()调用seccomp_run_filters()来测试当前进程调用的系
我正在调查seccomp-bpf的实现细节,这是自3.5版以来引入
Linux的系统调用过滤机制.
我查看了Linux 3.10中kernel / seccomp.c的源代码,并想询问一些有关它的问题. 从seccomp.c开始,似乎从__secure_computing()调用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; } 解决方法
当用户进程进入内核时,寄存器集将存储到内核变量中.
函数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使用用户线程的当前寄存器集来确定系统调用信息. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |