1

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

套接字:不允许操作。

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

但是如果我用root运行同样的程序,它会运行得很好。

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

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

如果我不使用 fork,它仍然会注意到没有预设。

如果我将 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用来编译它。

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

4

1 回答 1

1

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 标志。您不是直接使用 seccomp,而是通过库。看来图书馆正在为您设置标志。

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

于 2019-08-14T04:58:01.910 回答