对于linux来说,用户层一切资源相关操作都需要通过系统调用来完成;系统调用实现技术层次上解耦,内核只关心系统调用Api的实现不关心谁调用的。
Seccomp(Secure computing mode)
是一个 Linux 内核安全模块,可用于应用进程允许使用的系统调用。 容器实际上是宿主机上运行的一个进程,共享宿主机内核,如果所有容器都具有任何系统调用的能力,那么容器如果被入侵,就很轻松绕过容器隔离更改宿主机系统权限或者进入宿主机。这就可以使用Seccomp机制限制容器系统调用,有效减少攻击面。
Seccomp在Kubernetes 1.3版本引入,在1.19版本成为GA版本,因此K8s中使用Seccomp可以通过以下两种方式:
1.19版本之前
annotations:seccomp.security.alpha.kubernetes.io/pod: "localhost/<profile>"
1.19版本+
apiVersion: v1
kind: Pod
metadata:
name: hello-seccomp
spec:
securityContext:
seccompProfile:
type: Localhost
localhostProfile: <profile> # Pod所在宿主机上策略文件名,默认目录:/var/lib/kubelet/seccomp
containers
示例:禁止容器使用chmod
mkdir /var/lib/kubelet/seccompvi /var/lib/kubelet/seccomp/chmod.json{"defaultAction": "SCMP_ACT_ALLOW","syscalls": [{"names": ["chmod"],"action": "SCMP_ACT_ERRNO"}]}
•
defaultAction:在syscalls部分未定义的任何系统调用默认动作为允许
•
syscalls
• names 系统调用名称,可以换行写多个
• SCMP_ACT_ERRNO 阻止系统调用