k8s 的pod服务开启seccomp实验测试

k8s 的pod服务开启seccomp

定义:

最简单和最容易理解的定义 seccomp 可能是 “系统调用防火墙”。 seccomp 本质上是一种限制进程可能进行的系统调用的机制, 因此就像阻止来自某些 IP 的数据包一样,也可以阻止进程向 CPU 发送系统调用

用处:Linux 内核有很多系统调用(几百个),但是任何给定进程都不需要它们中的大多数。但是,如果进程可能会受到损害并被欺骗使用其中一些系统调用,那么它可能会导致整个系统出现严重的安全问题。因此,限制哪些系统调用进程可以大大减少内核的攻击面。

前提:

系统内核是否开启seccomp 功能:

grep SECCOMP /boot/config-$(uname -r)

CONFIG_HAVE_ARCH_SECCOMP_FILTER=y
CONFIG_SECCOMP_FILTER=y
CONFIG_SECCOMP=y

docker是否开启seccomp 功能

如果您正在运行任何最新版本的 Docker(1.10 或更高版本),那么您已经在使用 seccomp. 您可以使用 docker info 或通过查看

docker info

 Security Options:
  seccomp
   Profile: default

seccomp 的相关参数

#SCMP_ACT_KILL_THREAD (or SCMP_ACT_KILL)
Does not execute the syscall and terminate the thread that attempted making the call. Note that depending on the application being enforced (i.e. multi-threading) and its error handling, syscalls blocked using this action may do so silently which may result in side effects on the overall application.
#####
不执行系统调用并终止尝试进行调用的线程。 请注意,根据正在实施的应用程序(即多线程)及其错误处理,使用此操作阻止的系统调用可能会静默执行,这可能会对整个应用程序产生副作用。

#SCMP_ACT_TRAP
Does not execute the syscall. The kernel will send a thread-directed SIGSYS signal to the thread that attempted making the call.
#####
不执行系统调用。 内核将向试图进行调用的线程发送一个线程导向的 SIGSYS 信号。

#SCMP_ACT_ERRNO
Does not execute the syscall, returns error instead. Note that depending on the error handling of the application being enforced, syscalls blocked using this action may do so silently which may result in side effects on the overall application.
#####
不执行系统调用,而是返回错误。 请注意,根据正在执行的应用程序的错误处理,使用此操作阻止的系统调用可能会静默执行,这可能会对整个应用程序产生副作用。

#SCMP_ACT_TRACE
The decision on whether or not to execute the syscall will come from a tracer. If no tracer is present behaves like SECCOMP_RET_ERRNO.
This can be used to automate profile generation and also can be used to change the syscall being made. Not recommended when trying to enforce seccomp to line of business applications.
#####
是否执行系统调用的决定将来自跟踪器。 如果不存在跟踪器,则行为类似于 SECCOMP_RET_ERRNO。
这可用于自动生成配置文件,也可用于更改正在进行的系统调用。 尝试将 seccomp 强制执行到业务线应用程序时不推荐使用。

#SCMP_ACT_ALLOW
Executes the syscall.
#####
执行系统调用。

#SCMP_ACT_LOG (since Linux 4.14)
Executes the syscall. Useful for running seccomp in "complain-mode", logging the syscalls that are mapped (or catch-all) and not blocking their execution. It can be used together with other action types to provide an allow and deny list approach.
#####
执行系统调用。 对于在“投诉模式”下运行 seccomp、记录映射(或全部捕获)的系统调用并且不阻止它们的执行很有用。 它可以与其他操作类型一起使用,以提供允许和拒绝列表方法。

#SCMP_ACT_KILL_PROCESS (since Linux 4.14)
Does not execute the syscall and terminates the entire process with a core dump. Very useful when automating the profile generation.
#####
不执行系统调用并使用核心转储终止整个进程。 在自动生成配置文件时非常有用。

pod调用seccomp

选择的seccomp 方式

k8s默认情况下不使用系统调用,并且上方seccomp 参数中多个参数需要linux4.14以上的内核版本,故针对此漏洞推荐以下两种seccomp配置。

1、SCMP_ACT_ALLOW

不阻止任何系统调用

2、 RuntimeDefault

使用docker的默认seccomp 配置

调用方式

(pod调用外挂seccomp文件时会默认到pod容器的/var/lib/kubelet/seccomp/寻找改外挂文件 )

1、SCMP_ACT_ALLOW
#在所有节点创建外挂seccomp配置文件
vim /var/lib/kubelet/seccomp/audit.json
{
    "defaultAction": "SCMP_ACT_LOG"
}

#在pod yaml文件中添加挂载和seccomp配置内容
##示例:
apiVersion: v1
kind: Pod
metadata:
  namespace: szxc
  name: default-seccomp
  labels:
    app: default-seccomp
spec:
  securityContext:								#
    seccompProfile:								#
      type: Localhost							#
      localhostProfile: audit.json				#
  containers:
  - name: test-container
    image: r.j3ss.co/amicontained
    command: [ "/bin/sh", "-c", "--" ]
    args: [ "amicontained" ]
    securityContext:
      allowPrivilegeEscalation: false
    volumeMounts:								#
      - name: seccomp							#
        mountPath: /var/lib/kubelet/seccomp/	#
  volumes:										#
    - name: seccomp								#
      hostPath:									#
        path: /var/lib/kubelet/seccomp/			#

2、 RuntimeDefault
apiVersion: v1
kind: Pod
metadata:
  namespace: szxc
  name: default-seccomp
  labels:
    app: default-seccomp
spec:
  securityContext:
    seccompProfile:
      type: RuntimeDefault
  containers:
  - name: test-container
    image: r.j3ss.co/amicontained
    command: [ "/bin/sh", "-c", "--" ]
    args: [ "amicontained" ]
    securityContext:
      allowPrivilegeEscalation: false
    volumeMounts:
      - name: seccomp
        mountPath: /var/lib/kubelet/seccomp/
  volumes:
    - name: seccomp
      hostPath:
        path: /var/lib/kubelet/seccomp/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值