跳到正文
DEV Community· Sandscale·· 4 小時前AI 評分67

四宗容器逃逸漏洞説明執行 AI 智能體程式碼的安全風險

Four container escapes and what they teach about running agent code

AI 導讀

Sandscale 分析四宗已修復的公開漏洞,指出容器執行時或共用主機核心的缺陷,均曾讓容器內程式碼取得主機存取權。文章建議以非 root 身分執行、使用限制較嚴的 seccomp 設定,並及時修補 runc 和主機核心;Firecracker microVM 增加了隔離層,但本身仍可能有漏洞。

正文

"Just run it in a container" is the default answer when someone asks where an AI agent's code should execute. Containers are fast, familiar and already in your CI. For code your own team wrote, they are often the right answer.

The question changes when the code was written by a model that just read an issue from a stranger. Now the code is untrusted, and the container is a security boundary rather than a packaging format. It is worth looking at how that boundary has failed in practice. Here are four public vulnerabilities, what each one did, and the lesson for anyone running agent code.

None of this is a reason to panic about containers. All four were fixed. The point is the pattern: a container shares the host's kernel and its runtime, and bugs in either have let code inside reach the host.

1. CVE-2019-5736: overwriting the runtime from inside

In February 2019, a flaw in runc, the low-level runtime used by Docker, containerd and others, was disclosed. According to the NVD entry, runc through 1.0-rc6, as used in Docker before 18.09.2, let an attacker who could run a command as root inside a container overwrite the host's runc binary and so gain root on the host. The root cause was file-descriptor mishandling related to /proc/self/exe.

Two conditions made it exploitable: a new container started from an attacker-controlled image, or an existing container the attacker could write to and that someone later attached to with docker exec.

The lesson for agent code: "root inside the container" mattered. Many agent setups run commands as root in the container because it is easier: package installs work, file permissions never get in the way. Running as root inside a shared-kernel container hands untrusted code more of the attack surface than it needs. If you do use containers, run agent commands as an unprivileged user and use user namespaces.

2. CVE-2024-21626 "Leaky Vessels": a file descriptor left open

Five years later, runc had another escape. The NVD entry describes it: in runc 1.1.11 and earlier, an internal file descriptor leaked into newly spawned container processes. An attacker could make a process started with runc exec get a working directory in the host's filesystem namespace, giving it access to the host filesystem. A malicious image could do the same through runc run, and variants could overwrite host binaries for a full escape. Snyk, which found it, published a write-up of this and related build-time issues. The fix shipped in runc 1.1.12.

One of the attack paths needed nothing more exotic than a WORKDIR line in an image.

The lesson for agent code: the image is code. If an agent can choose or build the image its code runs in, for example by writing a Dockerfile as part of the task and having your pipeline build it, the image is attacker-controlled input to your container runtime. Treat image builds from agent-written files with the same suspicion as the code itself.

3. CVE-2022-0847 "Dirty Pipe": a kernel bug, shared by everyone

In March 2022, Max Kellermann published Dirty Pipe, a Linux kernel bug. The NVD entry explains that a flags field in the pipe buffer structure was not properly initialized, so an unprivileged local user could write to pages in the page cache backed by read-only files, and use that to escalate privileges.

This one was not a container bug at all. It was a bug in the kernel every container on the host shares. Read-only files in the page cache include files that are shared between containers and the host, so "read-only" stopped meaning read-only for every process on the machine.

The lesson for agent code: with containers, your isolation is only as current as the host kernel. Kernel patches need a reboot or live patching, and fleets lag. Every unpatched day is a day when any container on the host can try the published exploit. With a virtual machine, the guest kernel can still have this bug, but exploiting it gets you root inside a VM that holds nothing else.

4. CVE-2022-0185: user namespaces widen the kernel surface

Earlier in 2022, a heap overflow in the kernel's filesystem context code was disclosed. Per the NVD entry, the legacy_parse_param function did not check a length correctly, and a local user could use it to escalate privileges. The important detail is the condition: it was reachable by an unprivileged user when unprivileged user namespaces were enabled, because inside a user namespace a process holds CAP_SYS_ADMIN for that namespace.

The lesson for agent code: hardening options interact. User namespaces make containers safer in one way (root in the container is not root on the host) and expose more kernel code in another (namespaced capabilities reach code paths that were previously root-only). Seccomp profiles that block unshare and the mount-related syscalls an agent never needs close that path. The default Docker seccomp profile blocks many of them; a custom or disabled profile may not.

The pattern

Look at what the four have in common:

  • Two were bugs in the container runtime. The runtime sits at the boundary and handles attacker-influenced input (images, exec requests).
  • Two were bugs in the shared kernel. Every container on the host exposes the same kernel to its code, and the Linux syscall surface is large.
  • All four turned "code execution inside the container" into "access to the host".

For an AI agent, "code execution inside the container" is not an attack. It is the feature. You are deliberately running code you did not write, chosen by a model that may have been steered by text you did not write either. That puts you in the position these CVEs assume as their starting point.

What changes with a microVM

A microVM such as Firecracker moves the boundary. The code runs on its own guest kernel inside a virtual machine. To reach the host it has to get through the guest kernel and then the virtual machine monitor, which exposes a small set of emulated devices rather than the whole syscall interface. Firecracker's design also runs the monitor process under a jailer with seccomp filters, as a second layer.

That is a smaller target, not an unbreakable one. Virtual machine monitors have bugs too: Firecracker itself had CVE-2019-18960, a buffer overflow in its vsock implementation in versions 0.18.0 and 0.19.0. The host kernel and KVM still matter. You patch them like anything else.

Our earlier post on containers, gVisor and Firecracker covers the trade-offs in cost and startup in more depth.

If you stay on containers

Plenty of teams will, and that can be reasonable for internal code. If you run agent code in containers, the CVEs above suggest a short list:

  • Run commands as a non-root user inside the container.
  • Keep a restrictive seccomp profile; never run agent code with --privileged or extra capabilities.
  • Do not build images from agent-written Dockerfiles on the same hosts that run anything sensitive.
  • Patch runc and the host kernel quickly, and know how long your fleet takes to roll a kernel update.
  • Give each run a fresh container and nothing mounted from the host.
  • Turn off outbound network by default, so an escape that does happen has less to send and nowhere to send it.

Where Sandscale fits

Sandscale runs each agent run in its own Firecracker microVM with its own kernel, so a bug in a shared kernel or container runtime is not the only thing between agent code and other work. That is a stronger default than a shared-kernel container, not a guarantee. The security page describes the rest of the controls around it.

Originally published on the Sandscale blog.

來源:DEV Community · dev.to