我正在研究 seccomp-bpf 的实现细节,这是从 3.5 版开始引入 Linux 的系统调用过滤机制。 我从 Linux 3.10 查看了 kernel/seccomp.c 的源代码,想问一些关于它的问题。
从 seccomp.c 来看,seccomp_run_filters() 似乎是从 __secure_computing() 调用的,以测试当前进程调用的系统调用。 但是查看 seccomp_run_filters(),作为参数传递的系统调用编号并未在任何地方使用。
看起来 sk_run_filter() 是 BPF 过滤器机器的实现,但是 sk_run_filter() 是从 seccomp_run_filters() 调用的,第一个参数(运行过滤器的缓冲区)为 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
使用用户线程的当前寄存器集来确定系统调用信息。
关于linux - seccomp-bpf 如何过滤系统调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19828141/