c - 为什么加载seccomp过滤器后,普通用户PING不通

标签 c linux seccomp

我使用 seccomp 记录 'ping' 使用的系统调用。当我运行它时,它总是注意到

socket: Operation not permitted.

我可以在 bash 中很好地运行 ping,但是在程序中加载 seccomp 过滤器后无法运行。

但如果我以 root 运行相同的程序,它会运行得很好。

这是在具有 4.15.0-54 通用内核的 Ubuntu 18.04 中运行。

我试过用root用户运行程序,然后在child progress中,我用setuid(1000)设置为普通用户,还是不行。

如果我不使用 fork,它仍然会注意到 no premitted。

如果我将 seccomp 默认操作更改为 SCMP_ACT_ALLOW,它仍然无效。

这是一个简单的 C 代码。

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <signal.h>
#include <seccomp.h>
#include <unistd.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>

void child() {
        setuid(1000);
        scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_LOG);

        if (seccomp_load(ctx) != 0) {
                printf("SCMP LAOD ERR!");
        } else {
                seccomp_release(ctx);
        }
        execl("/bin/ping", "ping", "-c", "1", "172.16.1.1", NULL);
        printf("EXEC FAIL");
}
int main(){

        int p = fork();
        if (p < 0) {
                printf("Frok ERROR!");
                exit(1);
        }
        if ( p == 0 ) {
                child();
        } else {
                struct rusage usage;
                int status;
                if (wait4(p, &status, WSTOPPED, &usage) == -1) {
                        kill(p, SIGKILL);
                }
        }
}

我使用 gcc main.c -o main.out -lseccomp 来编译它。

英语不是我的第一语言,我对我的语法感到抱歉。

最佳答案

ping 只能作为 root 使用。通常它以 root 身份运行,因为它在其文件权限中设置了 setuid 位:

-rwsr-xr-x 1 root root 44168 May  8  2014 /bin/ping
   ^         ^^^^
   |
this 's' is called 'setuid' and means it wants to run as the user which owns it, which is root

除非你是 root,否则你不能使用 seccomp,或者你设置了 no_new_privs flag .您不是直接使用 seccomp,而是通过一个库。看来图书馆正在为您设置标志。

no_new_privs 标志意味着您不能运行 setuid 程序。好吧,您可以运行它们,但它们不会是 setuid。他们将作为您的用户运行。它没有按照 ping 要求的方式发送特殊数据包的权限。所以 ping 失败,因为它没有 ping 权限。

关于c - 为什么加载seccomp过滤器后,普通用户PING不通,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57487618/

相关文章:

c - 'including *.c files' 风格的 C 编程中可能存在的缺陷

c++ - 如果我使用相同的消息和相同的段落,为什么 wparam 会改变?

Mysql Replication Slave 未连接 "ERROR 1200 (HY000)"

c++ - 并行程序不会使

python - 在 Python 中启用 seccomp 后如何干净退出?

kubernetes - 无法创建Pod沙箱:rpc错误:代码=未知desc =内核中未启用seccomp,无法与配置文件一起运行

c - 使用 C 预处理器创建多个表布局

linux - 遍历 bash 主文件夹内子文件夹中的文件

c - 如何使用 SECCOMP_RET_DATA 和 PTRACE_GETEVENTMSG 获取系统调用的返回码

c - "alignment trap"错误是什么意思?