Seccomp
基本信息
Seccomp,全称Secure Computing mode,是Linux内核的安全功能,旨在过滤系统调用。它将进程限制在一组有限的系统调用上(exit()、sigreturn()、read()和write(),用于已打开的文件描述符)。如果进程尝试调用其他内容,内核将使用SIGKILL或SIGSYS终止该进程。该机制不会虚拟化资源,而是将进程与资源隔离开来。
有两种激活seccomp的方式:通过prctl(2)系统调用使用PR_SET_SECCOMP,或者对于Linux内核3.17及以上版本,使用seccomp(2)系统调用。通过向/proc/self/seccomp写入以启用seccomp的旧方法已被弃用,推荐使用prctl()。
一种增强功能seccomp-bpf,增加了使用伯克利数据包过滤器(BPF)规则自定义策略来过滤系统调用的能力。此扩展被软件如OpenSSH、vsftpd以及Chrome OS和Linux上的Chrome/Chromium浏览器所利用,用于灵活高效地过滤系统调用,提供了对于Linux中现在不再支持的systrace的替代方案。
原始/严格模式
在此模式下,Seccomp仅允许系统调用exit()、sigreturn()、read()和write()用于已打开的文件描述符。如果进行任何其他系统调用,进程将被使用SIGKILL终止。
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <linux/seccomp.h>
#include <sys/prctl.h>
//From https://sysdig.com/blog/selinux-seccomp-falco-technical-discussion/
//gcc seccomp_strict.c -o seccomp_strict
int main(int argc, char **argv)
{
int output = open("output.txt", O_WRONLY);
const char *val = "test";
//enables strict seccomp mode
printf("Calling prctl() to set seccomp strict mode...\n");
prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
//This is allowed as the file was already opened
printf("Writing to an already open file...\n");
write(output, val, strlen(val)+1);
//This isn't allowed
printf("Trying to open file for reading...\n");
int input = open("output.txt", O_RDONLY);
printf("You will not see this message--the process will be killed first\n");
}Seccomp-bpf
这种模式允许使用使用伯克利数据包过滤器规则实现的可配置策略来过滤系统调用。
Docker中的Seccomp
Seccomp-bpf由Docker支持,用于限制容器中的syscalls,有效减少攻击面。您可以在https://docs.docker.com/engine/security/seccomp/找到默认情况下被阻止的syscalls,并且可以在此处找到默认的seccomp配置文件:https://github.com/moby/moby/blob/master/profiles/seccomp/default.json。 您可以使用以下命令以不同的seccomp策略运行docker容器:
如果您想例如禁止容器执行一些系统调用,比如 uname,您可以从https://github.com/moby/moby/blob/master/profiles/seccomp/default.json下载默认配置文件,然后只需从列表中删除 uname 字符串。
如果您想确保某个二进制文件在 Docker 容器中无法运行,您可以使用 strace 列出二进制文件正在使用的系统调用,然后禁止它们。
在以下示例中,发现了 uname 的系统调用:
示例 Seccomp 策略
为了说明 Seccomp 功能,让我们创建一个 Seccomp 配置文件,禁用 "chmod" 系统调用,如下所示。
在上述配置文件中,我们将默认操作设置为“允许”,并创建了一个黑名单来禁用“chmod”。为了更安全,我们可以将默认操作设置为“拒绝”,并创建一个白名单来有选择性地启用系统调用。 以下输出显示了“chmod”调用返回错误,因为在seccomp配置文件中已禁用了它。
以下输出显示了“docker inspect”显示的配置文件:
在Docker中停用它
使用标志启动一个容器:--security-opt seccomp=unconfined
截至Kubernetes 1.19,所有Pod默认启用seccomp。然而,应用于Pod的默认seccomp配置文件是由容器运行时(例如Docker、containerd)提供的“RuntimeDefault”配置文件。这个“RuntimeDefault”配置文件允许大多数系统调用,同时阻止一些被认为是危险的或容器通常不需要的系统调用。
最后更新于