Skip to content

Commit 7dcfa3c

Browse files
authored
Create 容器libseccomp限制操作.md
1 parent 210540f commit 7dcfa3c

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# 容器安全特性文档
2+
3+
本文档介绍了 Docker 容器的三类核心安全特性:**seccomp、capabilities、SELinux**,包括场景说明、使用限制和配置指导。
4+
5+
---
6+
7+
## 1. seccomp 安全配置
8+
9+
### 1.1 场景说明
10+
11+
`seccomp`(secure computing mode)是 Linux 内核自 2.6.23 版本引入的一种简洁的 sandboxing 机制。
12+
在一些特定场景下,用户希望在容器中执行一些“特权”操作,但又不希望启动特权容器,这时可以通过精细化控制系统调用权限来实现。
13+
14+
**示例场景**
15+
16+
* 容器挂载宿主机某目录,该目录包含普通用户无法执行的二进制文件。
17+
* 在容器中对二进制文件设置 `chmod 4777` 加入 SUID 标志位,使得容器内运行该二进制时可以提权。
18+
* 为防止提权风险,可通过 `seccomp` 限制相关系统调用,如 `chmod``fchmod``fchmodat`
19+
20+
---
21+
22+
### 1.2 使用限制
23+
24+
* `seccomp` 会对性能有一定影响,应评估必要性后启用。
25+
* 仅对特定系统调用生效,对容器内逻辑进行精细化保护。
26+
27+
---
28+
29+
### 1.3 使用指导
30+
31+
Docker 支持通过 `--security-opt` 引入 seccomp 配置文件:
32+
33+
```bash
34+
docker run -it --rm --security-opt seccomp=/path/to/seccomp/profile.json busybox
35+
```
36+
37+
说明:
38+
39+
* `/path/to/seccomp/profile.json` 必须为绝对路径。
40+
* 默认配置文件位于 `/etc/docker/seccomp.json`
41+
* 设置 `--security-opt seccomp=unconfined` 时,不过滤系统调用。
42+
43+
#### 获取默认 seccomp 配置:
44+
45+
```bash
46+
cat /etc/docker/seccomp.json | python3 -m json.tool > profile.json
47+
```
48+
49+
可在此基础上定制系统调用过滤规则。
50+
51+
#### 自定义 seccomp 配置示例:
52+
53+
```json
54+
{
55+
"defaultAction": "SCMP_ACT_ALLOW",
56+
"syscalls": [
57+
{
58+
"name": "chmod",
59+
"action": "SCMP_ACT_ERRNO",
60+
"args": null
61+
}
62+
]
63+
}
64+
```
65+
66+
说明:
67+
68+
* `defaultAction`:未在 `syscalls` 中明确指定的系统调用默认行为。
69+
* `action` 可选值:
70+
71+
* `"SCMP_ACT_ALLOW"`:允许调用
72+
* `"SCMP_ACT_ERRNO"`:禁止调用并返回错误
73+
* `args` 可用于对系统调用参数进行精细控制。
74+
75+
---
76+
77+
## 2. capabilities 安全配置
78+
79+
### 2.1 场景说明
80+
81+
Linux 内核自 2.2 版本引入 **capabilities**,将 root 权限拆分为独立能力,允许以更小粒度授予容器进程权限,避免不必要的 root 权限。
82+
83+
**示例场景**
84+
85+
* 容器挂载宿主机目录时默认包含 `CAP_SETUID``CAP_FSETID`,可能被用于提权。
86+
* 如果不需要写入宿主机审计日志,可移除 `CAP_AUDIT_WRITE` 以降低风险。
87+
88+
---
89+
90+
### 2.2 使用限制
91+
92+
* 增加 capabilities 会扩大容器权限和暴露更多系统调用接口。
93+
* 默认容器 capabilities:
94+
95+
```
96+
CAP_CHOWN, CAP_DAC_OVERRIDE, CAP_FSETID, CAP_FOWNER,
97+
CAP_MKNOD, CAP_NET_RAW, CAP_SETGID, CAP_SETUID,
98+
CAP_SETFCAP, CAP_SETPCAP, CAP_NET_BIND_SERVICE,
99+
CAP_SYS_CHROOT, CAP_KILL, CAP_AUDIT_WRITE
100+
```
101+
102+
---
103+
104+
### 2.3 使用指导
105+
106+
Docker 支持通过 `--cap-add` / `--cap-drop` 增删容器 capabilities:
107+
108+
```bash
109+
# 增加所有 capabilities,同时去掉 SYS_ADMIN
110+
docker run -it --rm --cap-add all --cap-drop SYS_ADMIN busybox
111+
112+
# 去掉不必要的默认 capabilities
113+
docker run -it --rm --cap-drop AUDIT_WRITE busybox
114+
```
115+
116+
---
117+
118+
## 3. SELinux 安全配置
119+
120+
### 3.1 场景说明
121+
122+
SELinux (Security-Enhanced Linux) 是 Linux 内核安全模块,可通过访问控制策略对容器进程打标签,限制访问宿主机资源,降低提权攻击风险。
123+
124+
* Docker 采用 MCS(Multi-Category Security)策略对容器进程打标签。
125+
* 容器对宿主机文件系统访问受限,挂载卷需打标签。
126+
127+
---
128+
129+
### 3.2 使用限制
130+
131+
* 确保宿主机启用 SELinux,并已加载容器策略(推荐 container-selinux)。
132+
* 对性能有一定影响,应根据安全需求评估是否启用。
133+
* 挂载卷源目录不允许为 `/`, `/usr`, `/etc`, `/tmp`, `/home`, `/run`, `/var`, `/root` 等系统目录。
134+
135+
---
136+
137+
### 3.3 使用指导
138+
139+
#### 启用 SELinux
140+
141+
Docker daemon 需启用 SELinux 支持:
142+
143+
```json
144+
// /etc/docker/daemon.json
145+
{
146+
"selinux-enabled": true
147+
}
148+
```
149+
150+
重启 Docker daemon 后生效。
151+
152+
#### 容器启动配置 SELinux 上下文
153+
154+
```bash
155+
# 设置安全上下文类型和等级
156+
docker run -itd --security-opt label=type:container_t --security-opt label=level:s0:c1,c2 centos
157+
158+
# 禁用 SELinux 配置
159+
docker run -itd --security-opt label=disable centos
160+
```
161+
162+
#### 为挂载卷打标签
163+
164+
* `:z` 表示共享卷,`:Z` 表示私有卷
165+
166+
```bash
167+
docker run -itd -v /test:/test:z centos
168+
ls -Z /test
169+
# 输出示例:
170+
# system_u:object_r:container_file_t:s0 file
171+
```
172+
173+
---
174+
175+
### 参考资料
176+
177+
1. [Docker Security Options](https://docs.docker.com/engine/security/security/)
178+
2. [Linux capabilities man page](https://man7.org/linux/man-pages/man7/capabilities.7.html)
179+
3. [SELinux Project](https://selinuxproject.org/page/Main_Page)
180+
4. [Seccomp Documentation](https://www.kernel.org/doc/html/latest/userspace-api/seccomp_filter.html)
181+
182+
---

0 commit comments

Comments
 (0)