我刚刚开始学习 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 did not work correctly\n");
fprintf(fil,"Filter worked correctly\n");
End:
fclose(fil);
End1:
return 0;
}
跟踪这一点,我观察到系统调用 fstat
, close
正如@pchaigno 在下面的回答中提到的,另外还有 mmap
, munmap
被调用。所以我必须允许这些系统调用用于预期的行为。
最佳答案
调试
跟踪您的应用程序将揭示问题:
$ strace ./test
[...]
seccomp(SECCOMP_SET_MODE_FILTER, 0, {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_ACT_ALLOW, SCMP_SYS(fstat), 0) < 0)
goto err;
if (seccomp_rule_add(filter, SCMP_ACT_ALLOW, SCMP_SYS(close), 0) < 0)
goto err;
if (seccomp_rule_add(filter, SCMP_ACT_ALLOW, 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
关于c - 使用 seccomp 过滤器获取 "Bad System Call",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67916946/