|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: "Kubernetes 1.30:对 Pod 使用用户命名空间的支持进阶至 Beta" |
| 4 | +date: 2024-04-22 |
| 5 | +slug: userns-beta |
| 6 | +author: > |
| 7 | + Rodrigo Campos Catelin (Microsoft), |
| 8 | + Giuseppe Scrivano (Red Hat), |
| 9 | + Sascha Grunert (Red Hat) |
| 10 | +translator: > |
| 11 | + Michael Yao (DaoCloud) |
| 12 | +--- |
| 13 | +<!-- |
| 14 | +layout: blog |
| 15 | +title: "Kubernetes 1.30: Beta Support For Pods With User Namespaces" |
| 16 | +date: 2024-04-22 |
| 17 | +slug: userns-beta |
| 18 | +author: > |
| 19 | + Rodrigo Campos Catelin (Microsoft), |
| 20 | + Giuseppe Scrivano (Red Hat), |
| 21 | + Sascha Grunert (Red Hat) |
| 22 | +--> |
| 23 | + |
| 24 | +<!-- |
| 25 | +Linux provides different namespaces to isolate processes from each other. For |
| 26 | +example, a typical Kubernetes pod runs within a network namespace to isolate the |
| 27 | +network identity and a PID namespace to isolate the processes. |
| 28 | +
|
| 29 | +One Linux namespace that was left behind is the [user |
| 30 | +namespace](https://man7.org/linux/man-pages/man7/user_namespaces.7.html). This |
| 31 | +namespace allows us to isolate the user and group identifiers (UIDs and GIDs) we |
| 32 | +use inside the container from the ones on the host. |
| 33 | +--> |
| 34 | +Linux 提供了不同的命名空间来将进程彼此隔离。 |
| 35 | +例如,一个典型的 Kubernetes Pod 运行在一个网络命名空间中可以隔离网络身份,运行在一个 PID 命名空间中可以隔离进程。 |
| 36 | + |
| 37 | +Linux 有一个以前一直未被容器化应用所支持的命名空间是[用户命名空间](https://man7.org/linux/man-pages/man7/user_namespaces.7.html)。 |
| 38 | +这个命名空间允许我们将容器内使用的用户标识符和组标识符(UID 和 GID)与主机上的标识符隔离开来。 |
| 39 | + |
| 40 | +<!-- |
| 41 | +This is a powerful abstraction that allows us to run containers as "root": we |
| 42 | +are root inside the container and can do everything root can inside the pod, |
| 43 | +but our interactions with the host are limited to what a non-privileged user can |
| 44 | +do. This is great for limiting the impact of a container breakout. |
| 45 | +--> |
| 46 | +这是一个强大的抽象,允许我们以 “root” 身份运行容器: |
| 47 | +我们在容器内部有 root 权限,可以在 Pod 内执行所有 root 能做的操作, |
| 48 | +但我们与主机的交互仅限于非特权用户可以执行的操作。这对于限制容器逃逸的影响非常有用。 |
| 49 | + |
| 50 | +<!-- |
| 51 | +A container breakout is when a process inside a container can break out |
| 52 | +onto the host using some unpatched vulnerability in the container runtime or the |
| 53 | +kernel and can access/modify files on the host or other containers. If we |
| 54 | +run our pods with user namespaces, the privileges the container has over the |
| 55 | +rest of the host are reduced, and the files outside the container it can access |
| 56 | +are limited too. |
| 57 | +--> |
| 58 | +容器逃逸是指容器内的进程利用容器运行时或内核中的某些未打补丁的漏洞逃逸到主机上, |
| 59 | +并可以访问/修改主机或其他容器上的文件。如果我们以用户命名空间运行我们的 Pod, |
| 60 | +容器对主机其余部分的特权将减少,并且此容器可以访问的容器外的文件也将受到限制。 |
| 61 | + |
| 62 | +<!-- |
| 63 | +In Kubernetes v1.25, we introduced support for user namespaces only for stateless |
| 64 | +pods. Kubernetes 1.28 lifted that restriction, and now, with Kubernetes 1.30, we |
| 65 | +are moving to beta! |
| 66 | +--> |
| 67 | +在 Kubernetes v1.25 中,我们仅为无状态 Pod 引入了对用户命名空间的支持。 |
| 68 | +Kubernetes 1.28 取消了这一限制,目前在 Kubernetes 1.30 中,这个特性进阶到了 Beta! |
| 69 | + |
| 70 | +<!-- |
| 71 | +## What is a user namespace? |
| 72 | +
|
| 73 | +Note: Linux user namespaces are a different concept from [Kubernetes |
| 74 | +namespaces](/docs/concepts/overview/working-with-objects/namespaces/). |
| 75 | +The former is a Linux kernel feature; the latter is a Kubernetes feature. |
| 76 | +--> |
| 77 | +## 什么是用户命名空间? {#what-is-a-user-namespace} |
| 78 | + |
| 79 | +注意:Linux 用户命名空间与 |
| 80 | +[Kubernetes 命名空间](/zh-cn/docs/concepts/overview/working-with-objects/namespaces/)是不同的概念。 |
| 81 | +前者是一个 Linux 内核特性;后者是一个 Kubernetes 特性。 |
| 82 | + |
| 83 | +<!-- |
| 84 | +User namespaces are a Linux feature that isolates the UIDs and GIDs of the |
| 85 | +containers from the ones on the host. The identifiers in the container can be |
| 86 | +mapped to identifiers on the host in a way where the host UID/GIDs used for |
| 87 | +different containers never overlap. Furthermore, the identifiers can be mapped |
| 88 | +to unprivileged, non-overlapping UIDs and GIDs on the host. This brings two key |
| 89 | +benefits: |
| 90 | +--> |
| 91 | +用户命名空间是一个 Linux 特性,它将容器的 UID 和 GID 与主机上的隔离开来。 |
| 92 | +容器中的标识符可以被映射为主机上的标识符,并且保证不同容器所使用的主机 UID/GID 不会重叠。 |
| 93 | +此外,这些标识符可以被映射到主机上没有特权的、非重叠的 UID 和 GID。这带来了两个关键好处: |
| 94 | + |
| 95 | +<!-- |
| 96 | +* _Prevention of lateral movement_: As the UIDs and GIDs for different |
| 97 | +containers are mapped to different UIDs and GIDs on the host, containers have a |
| 98 | +harder time attacking each other, even if they escape the container boundaries. |
| 99 | +For example, suppose container A runs with different UIDs and GIDs on the host |
| 100 | +than container B. In that case, the operations it can do on container B's files and processes |
| 101 | +are limited: only read/write what a file allows to others, as it will never |
| 102 | +have permission owner or group permission (the UIDs/GIDs on the host are |
| 103 | +guaranteed to be different for different containers). |
| 104 | +--> |
| 105 | +* __防止横向移动__:由于不同容器的 UID 和 GID 被映射到主机上的不同 UID 和 GID, |
| 106 | + 即使这些标识符逃出了容器的边界,容器之间也很难互相攻击。 |
| 107 | + 例如,假设容器 A 在主机上使用的 UID 和 GID 与容器 B 不同。 |
| 108 | + 在这种情况下,它对容器 B 的文件和进程的操作是有限的:只能读取/写入某文件所允许的操作, |
| 109 | + 因为它永远不会拥有文件所有者或组权限(主机上的 UID/GID 保证对不同容器是不同的)。 |
| 110 | + |
| 111 | +<!-- |
| 112 | +* _Increased host isolation_: As the UIDs and GIDs are mapped to unprivileged |
| 113 | +users on the host, if a container escapes the container boundaries, even if it |
| 114 | +runs as root inside the container, it has no privileges on the host. This |
| 115 | +greatly protects what host files it can read/write, which process it can send |
| 116 | +signals to, etc. Furthermore, capabilities granted are only valid inside the |
| 117 | +user namespace and not on the host, limiting the impact a container |
| 118 | +escape can have. |
| 119 | +--> |
| 120 | +* __增加主机隔离__:由于 UID 和 GID 被映射到主机上的非特权用户,如果某容器逃出了它的边界, |
| 121 | + 即使它在容器内部以 root 身份运行,它在主机上也没有特权。 |
| 122 | + 这大大保护了它可以读取/写入的主机文件,它可以向哪个进程发送信号等。 |
| 123 | + 此外,所授予的权能仅在用户命名空间内有效,而在主机上无效,这就限制了容器逃逸的影响。 |
| 124 | + |
| 125 | +<!-- |
| 126 | +{{< figure src="/images/blog/2024-04-22-userns-beta/userns-ids.png" alt="Image showing IDs 0-65535 are reserved to the host, pods use higher IDs" title="User namespace IDs allocation" >}} |
| 127 | +--> |
| 128 | +{{< figure src="/images/blog/2024-04-22-userns-beta/userns-ids.png" alt="此图显示了 ID 0-65535 为主机预留,Pod 使用更大的 ID" title="用户命名空间 ID 分配" >}} |
| 129 | + |
| 130 | +<!-- |
| 131 | +Without using a user namespace, a container running as root in the case of a |
| 132 | +container breakout has root privileges on the node. If some capabilities |
| 133 | +were granted to the container, the capabilities are valid on the host too. None |
| 134 | +of this is true when using user namespaces (modulo bugs, of course 🙂). |
| 135 | +--> |
| 136 | +如果不使用用户命名空间,容器逃逸时以 root 运行的容器在节点上将具有 root 特权。 |
| 137 | +如果某些权能授权给了此容器,这些权能在主机上也会有效。 |
| 138 | +如果使用用户命名空间,就不会是这种情况(当然,除非有漏洞 🙂)。 |
| 139 | + |
| 140 | +<!-- |
| 141 | +## Changes in 1.30 |
| 142 | +
|
| 143 | +In Kubernetes 1.30, besides moving user namespaces to beta, the contributors |
| 144 | +working on this feature: |
| 145 | +--> |
| 146 | +## 1.30 的变化 {#changes-in-1.30} |
| 147 | + |
| 148 | +在 Kubernetes 1.30 中,除了将用户命名空间进阶至 Beta,参与此特性的贡献者们还: |
| 149 | + |
| 150 | +<!-- |
| 151 | +* Introduced a way for the kubelet to use custom ranges for the UIDs/GIDs mapping |
| 152 | + * Have added a way for Kubernetes to enforce that the runtime supports all the features |
| 153 | + needed for user namespaces. If they are not supported, Kubernetes will show a |
| 154 | + clear error when trying to create a pod with user namespaces. Before 1.30, if |
| 155 | + the container runtime didn't support user namespaces, the pod could be created |
| 156 | + without a user namespace. |
| 157 | + * Added more tests, including [tests in the |
| 158 | + cri-tools](https://github.com/kubernetes-sigs/cri-tools/pull/1354) |
| 159 | + repository. |
| 160 | +--> |
| 161 | +* 为 kubelet 引入了一种使用自定义范围进行 UID/GID 映射的方式 |
| 162 | +* 为 Kubernetes 添加了一种强制执行的方式让运行时支持用户命名空间所需的所有特性。 |
| 163 | + 如果不支持这些特性,Kubernetes 在尝试创建具有用户命名空间的 Pod 时,会显示一个明确的错误。 |
| 164 | + 在 1.30 之前,如果容器运行时不支持用户命名空间,Pod 可能会在没有用户命名空间的情况下被创建。 |
| 165 | +* 新增了更多的测试,包括在 [cri-tools](https://github.com/kubernetes-sigs/cri-tools/pull/1354) 仓库中的测试。 |
| 166 | + |
| 167 | +<!-- |
| 168 | +You can check the |
| 169 | +[documentation](/docs/concepts/workloads/pods/user-namespaces/#set-up-a-node-to-support-user-namespaces) |
| 170 | +on user namespaces for how to configure custom ranges for the mapping. |
| 171 | +--> |
| 172 | +你可以查阅有关用户命名空间的[文档](/zh-cn/docs/concepts/workloads/pods/user-namespaces/#set-up-a-node-to-support-user-namespaces), |
| 173 | +了解如何配置映射的自定义范围。 |
| 174 | + |
| 175 | +<!-- |
| 176 | +## Demo |
| 177 | +
|
| 178 | +A few months ago, [CVE-2024-21626][runc-cve] was disclosed. This **vulnerability |
| 179 | +score is 8.6 (HIGH)**. It allows an attacker to escape a container and |
| 180 | +**read/write to any path on the node and other pods hosted on the same node**. |
| 181 | +
|
| 182 | +Rodrigo created a demo that exploits [CVE 2024-21626][runc-cve] and shows how |
| 183 | +the exploit, which works without user namespaces, **is mitigated when user |
| 184 | +namespaces are in use.** |
| 185 | +--> |
| 186 | +## 演示 {#demo} |
| 187 | + |
| 188 | +几个月前,[CVE-2024-21626][runc-cve] 被披露。 |
| 189 | +这个 **漏洞评分为 8.6(高)**。它允许攻击者让容器逃逸,并**读取/写入节点上的任何路径以及同一节点上托管的其他 Pod**。 |
| 190 | + |
| 191 | +Rodrigo 创建了一个滥用 [CVE 2024-21626][runc-cve] 的演示, |
| 192 | +演示了此漏洞在没有用户命名空间时的工作方式,而在使用用户命名空间后 **得到了缓解**。 |
| 193 | + |
| 194 | +<!-- |
| 195 | +{{< youtube id="07y5bl5UDdA" title="Mitigation of CVE-2024-21626 on Kubernetes by enabling User Namespace support" class="youtube-quote-sm" >}} |
| 196 | +--> |
| 197 | +{{< youtube id="07y5bl5UDdA" title="通过启用用户命名空间支持来在 Kubernetes 上缓解 CVE-2024-21626" class="youtube-quote-sm" >}} |
| 198 | + |
| 199 | +<!-- |
| 200 | +Please note that with user namespaces, an attacker can do on the host file system |
| 201 | +what the permission bits for "others" allow. Therefore, the CVE is not |
| 202 | +completely prevented, but the impact is greatly reduced. |
| 203 | +--> |
| 204 | +请注意,使用用户命名空间时,攻击者可以在主机文件系统上执行“其他”权限位所允许的操作。 |
| 205 | +因此,此 CVE 并没有完全被修复,但影响大大降低。 |
| 206 | + |
| 207 | +[runc-cve]: https://github.com/opencontainers/runc/security/advisories/GHSA-xr7r-f8xq-vfvv |
| 208 | + |
| 209 | +<!-- |
| 210 | +## Node system requirements |
| 211 | +
|
| 212 | +There are requirements on the Linux kernel version and the container |
| 213 | +runtime to use this feature. |
| 214 | +
|
| 215 | +On Linux you need Linux 6.3 or greater. This is because the feature relies on a |
| 216 | +kernel feature named idmap mounts, and support for using idmap mounts with tmpfs |
| 217 | +was merged in Linux 6.3. |
| 218 | +--> |
| 219 | +## 节点系统要求 {#node-system-requirements} |
| 220 | + |
| 221 | +使用此特性对 Linux 内核版本和容器运行时有一些要求。 |
| 222 | + |
| 223 | +在 Linux 上,你需要 Linux 6.3 或更高版本。 |
| 224 | +这是因为此特性依赖于一个名为 idmap 挂载的内核特性,而支持 idmap 挂载与 tmpfs 一起使用的特性是在 Linux 6.3 中合并的。 |
| 225 | + |
| 226 | +<!-- |
| 227 | +Suppose you are using [CRI-O][crio] with crun; as always, you can expect support for |
| 228 | +Kubernetes 1.30 with CRI-O 1.30. Please note you also need [crun][crun] 1.9 or |
| 229 | +greater. If you are using CRI-O with [runc][runc], this is still not supported. |
| 230 | +
|
| 231 | +Containerd support is currently targeted for [containerd][containerd] 2.0, and |
| 232 | +the same crun version requirements apply. If you are using containerd with runc, |
| 233 | +this is still not supported. |
| 234 | +--> |
| 235 | +假设你使用 [CRI-O][crio] 和 crun;就像往常一样,你可以期待 CRI-O 1.30 支持 Kubernetes 1.30。 |
| 236 | +请注意,你还需要 [crun][crun] 1.9 或更高版本。如果你使用的是 CRI-O 和 [runc][runc],则仍然不支持用户命名空间。 |
| 237 | + |
| 238 | +containerd 对此特性的支持目前锁定为 [containerd][containerd] 2.0,同样 crun 也有适用的版本要求。 |
| 239 | +如果你使用的是 containerd 和 runc,则仍然不支持用户命名空间。 |
| 240 | + |
| 241 | +<!-- |
| 242 | +Please note that containerd 1.7 added _experimental_ support for user |
| 243 | +namespaces, as implemented in Kubernetes 1.25 and 1.26. We did a redesign in |
| 244 | +Kubernetes 1.27, which requires changes in the container runtime. Those changes |
| 245 | +are not present in containerd 1.7, so it only works with user namespaces |
| 246 | +support in Kubernetes 1.25 and 1.26. |
| 247 | +--> |
| 248 | +请注意,正如在 Kubernetes 1.25 和 1.26 中实现的那样,containerd 1.7 增加了对用户命名空间的**实验性**支持。 |
| 249 | +我们曾在 Kubernetes 1.27 中进行了重新设计,所以容器运行时需要做一些变更。 |
| 250 | +而 containerd 1.7 并未包含这些变更,所以它仅在 Kubernetes 1.25 和 1.26 中支持使用用户命名空间。 |
| 251 | + |
| 252 | +<!-- |
| 253 | +Another limitation of containerd 1.7 is that it needs to change the |
| 254 | +ownership of every file and directory inside the container image during Pod |
| 255 | +startup. This has a storage overhead and can significantly impact the |
| 256 | +container startup latency. Containerd 2.0 will probably include an implementation |
| 257 | +that will eliminate the added startup latency and storage overhead. Consider |
| 258 | +this if you plan to use containerd 1.7 with user namespaces in |
| 259 | +production. |
| 260 | +
|
| 261 | +None of these containerd 1.7 limitations apply to CRI-O. |
| 262 | +--> |
| 263 | +containerd 1.7 的另一个限制是,它需要在 Pod 启动期间变更容器镜像内的每个文件和目录的所有权。 |
| 264 | +这会增加存储开销,并可能显著影响容器启动延迟。containerd 2.0 可能会包含一个实现,以消除增加的启动延迟和存储开销。 |
| 265 | +如果你计划在生产环境中使用 containerd 1.7 和用户命名空间,请考虑这一点。 |
| 266 | + |
| 267 | +containerd 1.7 的这些限制均不适用于 CRI-O。 |
| 268 | + |
| 269 | +[crio]: https://cri-o.io/ |
| 270 | +[crun]: https://github.com/containers/crun |
| 271 | +[runc]: https://github.com/opencontainers/runc/ |
| 272 | +[containerd]: https://containerd.io/ |
| 273 | + |
| 274 | +<!-- |
| 275 | +## How do I get involved? |
| 276 | +
|
| 277 | +You can reach SIG Node by several means: |
| 278 | +- Slack: [#sig-node](https://kubernetes.slack.com/messages/sig-node) |
| 279 | +- [Mailing list](https://groups.google.com/forum/#!forum/kubernetes-sig-node) |
| 280 | +- [Open Community Issues/PRs](https://github.com/kubernetes/community/labels/sig%2Fnode) |
| 281 | +--> |
| 282 | +## 如何参与? {#how-do-i-get-involved} |
| 283 | + |
| 284 | +你可以通过以下方式联系 SIG Node: |
| 285 | + |
| 286 | +- Slack:[#sig-node](https://kubernetes.slack.com/messages/sig-node) |
| 287 | +- [邮件列表](https://groups.google.com/forum/#!forum/kubernetes-sig-node) |
| 288 | +- [提交社区 Issue/PR](https://github.com/kubernetes/community/labels/sig%2Fnode) |
| 289 | + |
| 290 | +<!-- |
| 291 | +You can also contact us directly: |
| 292 | +- GitHub: @rata @giuseppe @saschagrunert |
| 293 | +- Slack: @rata @giuseppe @sascha |
| 294 | +--> |
| 295 | +你也可以通过以下方式直接联系我们: |
| 296 | + |
| 297 | +- GitHub:@rata @giuseppe @saschagrunert |
| 298 | +- Slack:@rata @giuseppe @sascha |
0 commit comments