0

http://man7.org/linux/man-pages/man3/seccomp_export_bpf.3.html如何将生成的代码加载到内核中?此功能有哪些可能的用例?

4

1 回答 1

3

如何将生成的代码加载到内核中?

如果您正在使用seccomp_export_bpf(const scmp_filter_ctx ctx, int fd),那么您已经有一个初始化scmp_filter_ctx对象ctx,在这种情况下,您可以简单地执行以下操作:

int rc = seccomp_load(ctx);

无需使用seccomp_export_bpf在内核中加载过滤器。

此功能有哪些可能的用例?

我猜seccomp_export_bpf当您想在磁盘上保留过滤器的副本以供将来使用时,它最有用。例如,您可以(来自手册页示例):

filter_fd = open("/tmp/seccomp_filter.bpf", O_WRONLY);
if (filter_fd == -1) {
     rc = -errno;
     goto out;
}

rc = seccomp_export_bpf(ctx, filter_fd);

然后将导出的过滤器加载到内核中,您可以执行以下操作:

char filter[4096];
int length = read(0, filter, 4096);
if (length < 0) {
    goto out;
}
struct sock_fprog bpf_prog = {
    .len = length / sizeof(struct sock_filter),
    .filter = filter,
};
rc = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &bpf_prog);
于 2019-08-12T08:09:38.580 回答