使用 seccomp 过滤器获得“错误的系统调用” 调试允许最少的系统调用集拒绝系统调用而不是杀死进程结果

问题描述

我刚刚开始学习 seccomp 过滤器,我正在使用 libseccomp v2.4.4。我尝试编写一个基本的白名单过滤器,它只允许写入名为 file1文件,但我在 STDOUT 中收到“系统调用错误”消息。这是我的代码

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <seccomp.h>
#include <stdlib.h>
#include <errno.h>

int main(void)
{
        FILE *fil = fopen("file1","w");
        scmp_filter_ctx filter = seccomp_init(SCMP_ACT_KILL);

        if (filter==NULL || NULL==fil)
                goto End1;

        int chk1 = seccomp_rule_add(filter,SCMP_ACT_ALLOW,SCMP_SYS(write),1,SCMP_A0(SCMP_CMP_EQ,fileno(fil)));
        int chk2 = seccomp_load(filter);
        if (chk1<0 || chk2<0)
                goto End;

        fprintf(stdout,"Filter did not work correctly\n");
        fprintf(fil,"Filter worked correctly\n");

End:
        seccomp_release(filter); //releasing filter before closing file
        fclose(fil);

End1:
        return 0;
}

此外,我在 file1 中没有看到任何输出。我只是一个初学者,对这里的很多事情都不确定,因为我是根据自己的理解编写代码的,而不是从一些参考资料中编写的。似乎是什么问题?


编辑:我完全删除了过滤器并简单地执行

int main(void)
{
        FILE *fil = fopen("file1","w");

        fprintf(stdout,"Filter worked correctly\n");

End:
        fclose(fil);

End1:
        return 0;
}

跟踪这一点,我观察到系统调用 fstat,close 如@pchaigno 在下面的回答中提到的,另外还有 mmap,munmap调用。所以我必须允许这些系统调用用于预期的行为。

解决方法

调试

跟踪您的应用程序将揭示问题:

$ strace ./test
[...]
seccomp(SECCOMP_SET_MODE_FILTER,{len=12,filter=0x55ace60a6600}) = 0
fstat(1,<unfinished ...>)             = ?
+++ killed by SIGSYS (core dumped) +++
Bad system call (core dumped)

允许最少的系统调用集

您需要允许比 write(2) 多得多的系统调用,以便程序结束时不会出错。

您将需要 fstat(2)fprintf()close(2)fclose()exit_group(2) 才能结束该过程。

拒绝系统调用而不是杀死进程

此外,如果您希望程序在未经授权的系统调用后继续运行,则不应使用 SCMP_ACT_KILL 终止它。 SCMP_ACT_ERRNO(EPERM) 在这种情况下可能更合适。

结果

int main(void) {
        FILE *fil = fopen("file1","w");
        scmp_filter_ctx filter = seccomp_init(SCMP_ACT_ERRNO(EPERM));
        if (filter == NULL || NULL == fil)
                return 1;
        if (seccomp_rule_add(filter,SCMP_ACT_ALLOW,SCMP_SYS(write),1,SCMP_A0(SCMP_CMP_EQ,fileno(fil))) < 0)
                goto err;
        if (seccomp_rule_add(filter,SCMP_SYS(fstat),0) < 0)
                goto err;
        if (seccomp_rule_add(filter,SCMP_SYS(close),SCMP_SYS(exit_group),0) < 0)
                goto err;
        if (seccomp_load(filter) < 0)
                goto err;
        fprintf(stdout,"Filter did not work correctly\n");
        fprintf(fil,"Filter worked correctly\n");
err:
        seccomp_release(filter); //releasing filter before closing file
        fclose(fil);
        return 0;
}

给我们:

$ ./test; echo file1
Filter worked correctly