【问题标题】:load seccomp_export_bpf generated code into the kernel将 seccomp_export_bpf 生成的代码加载到内核中
【发布时间】:2019-08-12 07:50:50
【问题描述】:

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

【问题讨论】:

    标签: kernel bpf seccomp


    【解决方案1】:

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

    如果您使用的是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 在您想在磁盘上保留一份过滤器以供将来使用时最有用。例如,你可以这样做(来自the man page example):

    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);
    

    【讨论】:

    • 您好,我想应该是.len = length / sizeof(struct sock_filter),第2行有语法错误。
    • 已修复。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多