Kubernetes 最小化微服务漏洞 安全沙箱运行容器:gVisor介绍与安装

网友投稿 1704 2022-11-30

Kubernetes 最小化微服务漏洞 安全沙箱运行容器:gVisor介绍与安装

Kubernetes 最小化微服务漏洞 安全沙箱运行容器:gVisor介绍与安装

现在容器通过什么技术实现的隔离

1.使用Linux namespace和cgroup技术隔离容器

2.Linux内核是共享的

使用的容器不是强隔离的环境,和虚拟机是无法媲美的,虚拟机从硬件到软件,再到内核是完全的隔离,而容器只隔离了一部分。

降低容器的安全风险:

(1)AppArmor:限制容器中的进程访问系统文件权限

(2)Seccomp:过滤容器当中的进程对linux的系统调用

(3)Capabilities:限制容器中的进程对Linux的内核系统调用能力(root权限进行逻辑划分,针对划分的能力配置给进程)

(4)非root用户运行进程,比如软件安装也是限制的

(5)安全上下文是基于容器已有的能力判断有没有正确配置,如果想要推行这个安全策略,那么配置psp

(6)设置文件系统只读

安全沙箱运行容器:gVisor介绍

所知,容器的应用程序可以直接访问Linux内核的系统调用,容器在安全隔离上还是比较弱,虽然

内核在不断地增强自身的安全特性,但由于内核自身代码极端复杂,CVE 漏洞层出不穷。

所以要想减少这方面安全风险,就是做好安全隔离,阻断容器内程序对物理机内核的依赖。

Google开源的一种gVisor容器沙箱技术就是采用这种思路,gVisor隔离容器内应用和内核之间访

问,提供了大部分Linux内核的系统调用, 巧妙的将容器内进程的系统调用转化为对gVisor的访问。

提供了一个虚拟的空间,让容器去访问Linux内核的时候,要经过这个虚拟的程序帮你去做转换,如果不是常规的Linux调用,那么都屏蔽出去。

gVisor兼容OCI,与docker和K8s无缝集成,很方面使用。

项目地址:由 3 个组件构成:

• Runsc 是一种 Runtime 引擎,负责容器的创建与销毁。

• Sentry 负责容器内程序的系统调用处理。(容器发出的系统调用都先交给它处理)

• Gofer 负责文件系统的操作代理,IO 请求都会由它转接到 Host 上。

在使用任何一个关于中间人的时候,都会产生一定的性能额外消耗。比如虚拟化和hyper层,所以虚拟机性能没有物理机性能好。同理gVisor道理是一样的

安全沙箱运行容器:gVisor与Docker集成

[root@master ~]# uname -r3.10.0-693.el7.x86_64

gVisor内核要求:Linux 3.17+   如果用的是CentOS7则需要升级内核,Ubuntu不需要。

CentOS7内核升级步骤:

rpm --import -Uvh --enablerepo=elrepo-kernel install kernel-ml-devel kernel-ml –ygrub2-set-default 0rebootuname -r

1、准备gVisor二进制文件

sha512sum -c runsc.sha512rm -f *.sha512chmod a+x runscmv runsc /usr/local/bin[root@k8s-master ~]# rm -f *.sha512[root@k8s-master ~]# chmod a+x runsc[root@k8s-master ~]# mv runsc /usr/local/bin

2、Docker配置使用gVisor,使用runsc

下面可以看到有runc和runsc都可以对容器进行创建和管理

runsc install # 查看加的配置/etc/docker/daemon.jsonsystemctl restart docker[root@k8s-master ~]# runsc install2021/07/14 04:34:46 Added runtime "runsc" with arguments [] to "/etc/docker/daemon.json".[root@k8s-master ~]# systemctl restart docker[root@k8s-master ~]# cat /etc/docker/daemon.json{ "registry-mirrors": [ " ], "runtimes": { "runsc": { "path": "/usr/local/bin/runsc" } }}[root@k8s-node1 ~]# docker info containerd version: 8fba4e9a7d01810a393d5d25a3621dc101981175 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd Runtimes: runc runsc Default Runtime: runc

使用runsc运行容器:

docker run -d --runtime=runsc nginx

使用dmesg验证:

[root@k8s-node1 ~]# docker run -d --runtime=runsc nginxUnable to find image 'nginx:latest' locallylatest: Pulling from library/nginxb4d181a07f80: Pull complete 66b1c490df3f: Pull complete d0f91ae9b44c: Pull complete baf987068537: Pull complete 6bbc76cbebeb: Pull complete 32b766478bc2: Pull complete Digest: sha256:353c20f74d9b6aee359f30e8e4f69c3d7eaea2f610681c4a95849a2fd7c497f9Status: Downloaded newer image for nginx:latest25ba1c32b09fa04cad0d7397b21fcaac254ab9d539d28ab392ae90b1b38a3483[root@k8s-node1 ~]# docker run --runtime=runsc -it nginx dmesg[ 0.000000] Starting gVisor...[ 0.257800] Committing treasure map to memory...[ 0.323427] Digging up root...[ 0.549272] Consulting tar man page...[ 0.727058] Feeding the init monster...[ 1.106393] Searching for needles in stacks...[ 1.209076] Generating random numbers by fair dice roll...[ 1.551108] Accelerating teletypewriter to 9600 baud...[ 1.858969] Reading process obituaries...[ 1.927673] Rewriting operating system in Javascript...[ 2.180562] Verifying that no non-zero bytes made their way into /dev/zero...[ 2.198676] Ready!#可以看到不适用runsc去运行使用runc,这里没有这个命令,因为docker将默认的一部分系统调用屏蔽掉了,demesg也在其中,那么就调用不了Linux内核的功能[root@k8s-node2 ~]# docker run nginx demesg/docker-entrypoint.sh: 38: exec: demesg: not found

可以看到内核版本就不一样,使用runsc启动的容器内核被gvisor虚拟出来了,这是一个虚拟的内核。

#通过runsc运行的容器[root@k8s-node2 ~]# docker exec -it 6d6683fe1bab bashroot@6d6683fe1bab:/# uname -r4.4.0#使用runc启动[root@k8s-node2 ~]# docker exec -it 0e30fdb6992e bashroot@0e30fdb6992e:/# uname -r5.13.1-1.el7.elrepo.x86_64

这里可以看到是使用runsc去管理容器的,而正常是使用runc去管理容器

[root@k8s-node2 ~]# ps -ef | grep runscroot 17114 1004 0 20:08 ? 00:00:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/6d6683fe1bab8a122bdcc40b3ffe29e58c0534735b655db3bf5cb3b92c4416a0 -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runscroot 17129 17114 0 20:08 ? 00:00:00 runsc-gofer --root=/var/run/docker/runtime-runsc/moby --log=/run/containerd/io.containerd.runtime.v1.linux/moby/6d6683fe1bab8a122bdcc40b3ffe29e58c0534735b655db3bf5cb3b92c4416a0/log.json --log-format=json --log-fd=3 gofer --bundle /run/containerd/io.containerd.runtime.v1.linux/moby/6d6683fe1bab8a122bdcc40b3ffe29e58c0534735b655db3bf5cb3b92c4416a0 --spec-fd=4 --mounts-fd=5 --io-fds=6 --io-fds=7 --io-fds=8 --io-fds=9 --apply-caps=false --setup-root=false65534 17133 17114 0 20:08 ? 00:00:01 runsc-sandbox --root=/var/run/docker/runtime-runsc/moby --log=/run/containerd/io.containerd.runtime.v1.linux/moby/6d6683fe1bab8a122bdcc40b3ffe29e58c0534735b655db3bf5cb3b92c4416a0/log.json --log-format=json --log-fd=3 boot --bundle=/run/containerd/io.containerd.runtime.v1.linux/moby/6d6683fe1bab8a122bdcc40b3ffe29e58c0534735b655db3bf5cb3b92c4416a0 --controller-fd=4 --mounts-fd=5 --spec-fd=6 --start-sync-fd=7 --io-fds=8 --io-fds=9 --io-fds=10 --io-fds=11 --stdio-fds=12 --stdio-fds=13 --stdio-fds=14 --cpu-num 1 6d6683fe1bab8a122bdcc40b3ffe29e58c0534735b655db3bf5cb3b92c4416a0[root@k8s-node2 ~]# ps -ef | grep runcroot 1826 1004 0 19:46 ? 00:00:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/1513aca9d7cc807ebb8a48c999ffe18aba360d1b0dee6b18ccd8147244e36416 -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runcroot 1828 1004 0 19:46 ? 00:00:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/77bc2b32c40eda885eca918d08efc6aafa1d921eb3a643215a062d84eff045ac -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runcroot 1901 1004 0 19:46 ? 00:00:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/545aa9ffb5362589aa44bb9b55573ab4f6b53af9ef18222d7f13c3863f37edd4 -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runcroot 2221 1004 0 19:47 ? 00:00:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/52a806b60e5a43f29c381c38fcdb0b11be33822963127fc1f617b6346513cb93 -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runcroot 2460 1004 0 19:47 ? 00:00:06 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/c8049ebec32fa4f1b27dad6b4329877ac3c045ecb82f0210bace811810cdfaea -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runcroot 2607 1004 0 19:47 ? 00:00:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/31e7f22f9f7f2cde04e3ddfe777d4adf851fe248d5f3bdd4b7bae16aebfec7f0 -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runcroot 2622 1004 0 19:47 ? 00:00:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/1c430298d1a3d66919c1a9f763fa7aad897203a63f883f4a3a18b298a698db55 -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runcroot 3067 1004 0 19:47 ? 00:00:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/53b80b36a96fa7c3b63935007d5426652829220c547f71cef903b910481a7a62 -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runcroot 3926 1004 0 19:48 ? 00:00:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/4f730ddd354fe034acf48073c1fb4086932a99e9130612696c29c754785c0b14 -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runcroot 19149 1004 0 20:11 ? 00:00:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/0e30fdb6992e5e457bfd157a4b59fd93a15cc2fd1de7f0b72e61adb42146e995 -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runc

当容器发生系统调用的时候走的都是虚拟内核,如果要对内核做破坏那么也是对虚拟内核做破坏,对宿主机的内核没什么影响

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:最小化微服务漏洞 Secret存储敏感数据
下一篇:Kubernetes 最小化微服务漏洞 gVisor与Containerd集成
相关文章

 发表评论

暂时没有评论,来抢沙发吧~