Sub topic 12
Stronger isolation with gVisor
This chapter assumes you have read the earlier chapters on container primitives and hardening. You should know that a normal container shares the host kernel, and you should be comfortable with docker run flags.
The problem gVisor addresses
A hardened container still sends every system call from the agent straight to the host kernel. Namespaces, cgroups, capabilities, and seccomp limit what the agent can do. A bug in the kernel's handling of an allowed call can still give an attacker code execution on the host. An agent that runs untrusted code, or that follows instructions hidden in a downloaded file, makes that risk more likely.
gVisor reduces the risk by putting a second kernel between the agent and the host. It is written in Go and runs in user space.
How it works
gVisor's runtime is called runsc. It has two main parts:
- The Sentry is the user-space kernel. It implements most Linux system calls itself, including file systems, networking, and process management.
- The Gofer is a helper process that opens files on the agent's behalf and passes them to the Sentry.
The Sentry makes only a small, filtered set of calls to the host kernel. The agent never reaches the host kernel's full interface directly.
An exploit now has to compromise the Sentry first. It then needs a second flaw to get from the Sentry to the host. The Sentry itself runs under a tight seccomp filter.
Running a container under gVisor
Install gVisor, register it with Docker, and restart the Docker daemon. After that, you select it with one flag:
sudo runsc install
sudo systemctl restart docker
docker run --rm -it --runtime=runsc python:3.12-slim bashTo confirm the container is under gVisor, run dmesg inside it. gVisor prints its own simulated boot messages, not the host's.
docker run --rm --runtime=runsc python:3.12-slim dmesgKeep all the hardening flags from the earlier chapters. gVisor adds a layer and does not replace read-only roots, dropped capabilities, resource limits, or --network none. Add --runtime=runsc to the same command:
docker run --rm -it \
--runtime=runsc \
--user 1000:1000 \
--read-only \
--tmpfs /tmp \
--cap-drop=ALL \
--security-opt no-new-privileges \
--pids-limit 256 \
--memory 2g \
--network none \
-v "$PWD":/workspace \
-w /workspace \
python:3.12-slim bashTrade-offs
| Aspect | Standard container | gVisor |
|---|---|---|
| Kernel exposed to the agent | Host kernel | Sentry, with a filtered path to the host |
| System call overhead | Low | Higher |
| File and network speed | Close to native | Often slower |
| Linux compatibility | Complete | Most programs, not every system call |
Compatibility is the usual source of surprises. A tool that relies on an unimplemented system call or an unusual /proc entry may fail. Heavy file operations, such as npm install on a large tree, can be noticeably slower. Test your agent's actual workload before you commit to it.
Common errors
unknown or invalid runtime name: runsc: gVisor is installed but not registered, or Docker was not restarted. Check/etc/docker/daemon.jsonfor aruntimesentry.- A tool fails with "function not implemented": the program uses a system call the Sentry does not support. Check gVisor's compatibility notes, or run that task outside the agent sandbox.
- Assuming gVisor makes other hardening unnecessary: a mounted secret or an open network is exposed in the same way as before.
Exercise
A teammate says that running the agent under --runtime=runsc means you can remove --network none and --cap-drop=ALL. Is that right? Explain.
Show answer
No. gVisor protects the host kernel from the agent's system calls. It does not decide what the agent may reach. Without --network none, the agent can still send data out or contact internal services. Dropping capabilities still shrinks what the agent can ask the Sentry to do. gVisor adds a layer on top of the other controls and does not replace them.