Sub topic 12
Stronger isolation with gVisor
This chapter covers gVisor, a user-space kernel that sits between a containerized agent and the host kernel. It assumes you can run Docker containers, know what a system call is, and have read the earlier chapters on container primitives and hardening. By the end you should be able to explain how gVisor changes the attack surface, run a container under it, and judge what it costs.
The problem gVisor addresses
A normal container shares the host kernel. Namespaces, cgroups, capabilities and seccomp restrict what a process can do, but every permitted system call still runs inside the host kernel. If an agent runs code that triggers a kernel bug, such as a flaw in a filesystem driver or a networking path, the result can be code execution in the host kernel. A hardened container reduces the number of reachable system calls, but the remaining ones are still handled by a very large, privileged code base.
gVisor takes a different approach. It puts a second, much smaller kernel written in Go between the container and the host. The agent's system calls go to that kernel, not to Linux.
How gVisor works
gVisor ships an OCI-compatible runtime called runsc. Docker can use it in place of the default runtime, runc. The main pieces are:
- The Sentry. This is the user-space kernel. It implements Linux system calls, memory management, process scheduling, signals and a TCP/IP network stack. It runs as an ordinary unprivileged process on the host.
- The Gofer. This is a separate helper process that handles file access. The Sentry does not open host files directly. It asks the Gofer, which is the only component with access to the mounted directories.
- The platform. This is the mechanism that intercepts the container's system calls and hands them to the Sentry. Current gVisor releases use the
systrapplatform by default. Older setups usedptraceorKVM.
The Sentry itself still needs the host kernel. It makes a much smaller set of host system calls, and it is confined with its own seccomp filter. An attacker who wants to reach the host kernel must first compromise the Sentry, then escape its filter, which is a harder path than exploiting one kernel bug directly.
Running a container under gVisor
Install gVisor and register runsc as a Docker runtime. The gVisor documentation provides the install steps for each distribution. The usual registration command is:
sudo runsc install
sudo systemctl reload dockerThis adds a runsc entry to /etc/docker/daemon.json. After that, select the runtime per container:
docker run --rm -it --runtime=runsc python:3.12-slim bashYou can confirm that you are inside gVisor by reading the kernel messages. gVisor prints a boot log that does not look like a Linux boot log:
docker run --rm --runtime=runsc python:3.12-slim dmesgThe output lists made-up startup steps, such as "Constructing the universe" and "Checking naughty and nice process list". A normal container either shows real host kernel messages or is denied access to them.
Combining gVisor with the hardening flags
gVisor does not replace the hardening from the earlier chapters. It adds a layer underneath them. Use the same flags as before and add the runtime:
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 \
--cpus 2 \
--network none \
-v "$PWD":/workspace \
-w /workspace \
python:3.12-slim bashThe reasons to keep every flag:
- A non-root user and dropped capabilities limit what the agent can do even inside the Sentry's view of the system.
- A read-only root filesystem and a scoped mount still decide which files the agent can change. gVisor does not know which files matter to you.
- Resource limits still apply, because they are enforced through cgroups on the host.
--network noneor an internal network still controls egress. gVisor's own network stack does not block anything by default.
If you use the Python wrapper from the earlier chapter, the change is one extra pair of arguments in the docker_cmd list: "--runtime", "runsc".
What you give up
gVisor is not free. The costs fall into three groups.
Performance
Every system call now passes through an interception step and the Sentry. Workloads that make few system calls, such as number crunching, run at close to native speed. Workloads that make many, such as installing packages, scanning a large source tree, or running a test suite that opens thousands of small files, can be noticeably slower. File access is the usual hot spot, because each operation goes through the Gofer. Measure your own agent's workload before you commit to it.
Compatibility
The Sentry implements most of Linux but not all of it. Some system calls, /proc and /sys entries, and ioctl operations are missing or only partly supported. Typical agent tasks, like running Python, Node, compilers and git, work well. Tools that depend on unusual kernel features can fail. Examples include some debuggers, perf, certain eBPF-based tools, and software that needs raw device access. Docker-in-Docker and anything that needs a privileged container are poor fits.
When a command fails under runsc but works under runc, the cause is often a missing syscall. The Sentry logs unsupported calls, and you can enable debug logging in the runtime configuration to see them.
Operational complexity
You must install and update runsc on every host, and gVisor releases track Linux behavior, so you should keep it current. Because gVisor is a separate code base, it has its own bugs. Its security value comes from having a smaller, memory-safe, purpose-built kernel, not from being flawless.
Security properties and limits
Compared with a hardened runc container, gVisor gives you these benefits:
- The host kernel exposes far fewer system calls and code paths to the agent.
- A kernel exploit aimed at Linux does not work directly, because the agent is not talking to Linux.
- The component that parses the agent's requests is written in a memory-safe language, which removes whole classes of bugs.
It does not give you these:
- Hardware-level separation. The Sentry and the host kernel still run on the same CPU and share it. Side-channel attacks and CPU bugs are outside gVisor's scope. A VM or microVM, covered in the next chapter, adds a hypervisor boundary.
- Policy. gVisor does not decide what the agent should be allowed to read, send or delete. Mounts, network rules and credentials remain your job.
- Protection of data you mount in. If you mount your home directory, the agent can read it, with or without gVisor.
| Property | runc container | gVisor (runsc) | VM or microVM |
|---|---|---|---|
| Kernel handling agent syscalls | Host kernel | Sentry in user space | Separate guest kernel |
| Host kernel attack surface | Large | Small, filtered | Hypervisor and device models |
| Typical overhead | Lowest | Moderate, syscall and file heavy loads | Higher startup and memory cost |
| Linux compatibility | Full | Most, not all | Full |
When gVisor is a good fit
Choose gVisor when the agent runs code you do not fully trust, you want stronger isolation than a hardened container, and you cannot or do not want to run a VM per task. It works well on Linux servers and in container platforms that already use Docker or Kubernetes, because switching is a runtime setting and not a rewrite. Stay with runc when you need full kernel compatibility or the performance hit on your workload is unacceptable, and consider a microVM when you need a hardware boundary.
Exercises
Exercise
Name the two main components of gVisor and say what each one does. Why does the split matter for security?
Show answer
The Sentry is the user-space kernel that implements system calls, memory management and networking for the container. The Gofer is a separate process that performs file access on the host on the Sentry's behalf. The split matters because the Sentry, which handles the agent's requests directly, has no direct access to host files and runs under a tight seccomp filter. An attacker who compromises the Sentry still has to get past that filter and the Gofer's narrow interface to reach the host.
Exercise
A teammate says that because the agent runs under --runtime=runsc, the flags --cap-drop=ALL, --read-only and --network none are no longer needed. Respond with two specific reasons this is wrong.
Show answer
First, gVisor limits how the agent reaches the host kernel, but it does not decide which files the agent can modify or which hosts it can contact. --read-only and a scoped mount control writes, and --network none controls egress, so removing them leaves the agent free to change or send out anything it can reach. Second, capabilities and the non-root user still limit what the agent can do inside the sandbox, and cgroup-based resource limits still protect the host from exhaustion. gVisor adds a layer. It does not replace these controls.
Exercise
An agent's test suite runs five times slower under runsc than under runc, and one tool fails with an unsupported system call. List two likely causes and one decision you could make.
Show answer
The slowdown is likely caused by a system-call-heavy or file-heavy workload, since every call passes through the Sentry and file operations go through the Gofer. The failure is likely caused by a Linux feature that the Sentry does not implement. You could keep runsc and change the workload, for example by caching dependencies or avoiding the incompatible tool. You could also keep runc with full hardening for this task if the risk is low, or move to a microVM if you need stronger isolation with full Linux compatibility.