A Tour of gVisor: Attack Surface, Overhead and Compatibility

I spent the past few weeks with gVisor, the userspace kernel Google uses to run untrusted code. I’d already built a small version of it, mini-sentry, which is to gVisor roughly what a model ship is to a ship. This time I looked at the real thing: what it buys you in security, what it costs, and what doesn’t work under it. I measured the costs with a small harness of my own, comparing gVisor against a normal container runtime on five workloads.

Short version: gVisor cuts the host-kernel attack surface from about 400 syscalls to 68. The price depends almost entirely on how syscall-heavy the workload is: 1.4x slower for hashing, about 6x for nginx, over 12x for tiny TCP round trips. The bigger practical limit is compatibility: perf, BPF and usually io_uring don’t work.

gVisor isn’t a container runtime

When I started I thought gVisor was another container runtime. It isn’t. A container runtime uses the kernel’s isolation features (namespaces, cgroups, seccomp) to keep processes from seeing each other, but the guest’s syscalls still land on the host kernel, which has to be both correct and trustworthy.

gVisor reimplements Linux instead. It runs in userspace as a Go binary. When a sandboxed process calls read, openat or socket, gVisor catches the call before the host kernel sees it, handles it in its own implementation of that subsystem, and returns the result. The process thinks it’s talking to Linux; it’s actually talking to about two hundred thousand lines of Go. The host kernel is exactly the component gVisor doesn’t want to depend on.

The attack-surface argument

Linux has roughly 400 syscalls, depending on how you count the subcommands of ioctl, prctl, fcntl and friends. From a hostile guest’s point of view, each one is a possible path into the kernel, and a reachable kernel bug anywhere along that surface is a way out.

The Sentry, the part of gVisor that pretends to be Linux, answers almost all of those calls itself without touching the host kernel. The Sentry’s own use of the host is cut down to about 68 syscalls, and a seccomp filter locks it to exactly that set. Going from about 400 to 68 is a roughly sixfold reduction in exposed surface, and it’s the main reason gVisor exists. The rest of this post is about what that reduction costs.

Three ways to catch syscalls

gVisor has had three platforms, meaning three mechanisms for catching the guest’s syscalls:

  • ptrace: the same mechanism a debugger uses. The tracer stops the child at every syscall, reads the registers and emulates the call. It’s simple but very slow, because every syscall costs two context switches whether or not the Sentry needs to see it. Tight syscall benchmarks show about 40x overhead versus native. gVisor shipped with this and has effectively retired it.
  • systrap (the current default): a seccomp filter returns SECCOMP_RET_TRAP, which raises a signal handled in-process by a small stub in the guest’s own address space, with no tracer. The stub hands the call to the Sentry over shared memory, so a syscall costs something closer to a function call than a full stop. This is fast enough to run in production.
  • KVM: runs the Sentry in guest ring 0 of a small hardware virtual machine, so the guest’s syscalls trap straight into the Sentry without a vmexit or a signal. It’s often the fastest, with the strictest deployment requirements.

Most of what’s written about gVisor is about systrap because that’s the default. All my measurements used systrap.

Benchmarks

I ran five workloads under runc (the standard OCI runtime) and under runsc (gVisor’s), and compared the times:

  • Coldstart: time for a container to start and print a message.
  • sha256sum of a 256 MB file: almost all CPU, with a read every 32 KB.
  • Redis PING, host network: a pipelined PING loop over TCP with gVisor using the host’s real network stack. The Sentry dispatches the socket syscalls, but the host kernel’s TCP does the work.
  • Redis PING, Netstack: the same loop in gVisor’s default network mode, where TCP goes through Netstack, a full TCP/IP stack written in Go inside the Sentry.
  • nginx under wrk: ten seconds of HTTP load.
Workloadrunsc / runc
sha256sum, 256 MB1.41x
Coldstart4.07x (about 500 ms vs 120 ms)
nginx under wrk5.79x
Redis PING, host network10.61x
Redis PING, Netstack12.40x

How to read these:

  • Hashing pays a small, visible tax for gVisor dispatching about eight thousand read calls, spread over a lot of computation.
  • Redis PING on the host network is the syscall-dense extreme: two sendto/recvfrom pairs per round trip and almost nothing in between. 10.61x is roughly the upper bound of what Sentry dispatch alone costs.
  • Netstack adds about 17% on top of that (12.40 vs 10.61), less than I expected for a userspace TCP stack.
  • nginx sits in between. It does a lot of socket work, but also parsing, response building, routing and logging, which are cheap under gVisor. If someone wants one number for a typical HTTP microservice under gVisor, this is the one I’d give.

Sorted by syscall density, the overhead rises as the workload gets more syscall-heavy. Computation between syscalls dilutes the per-syscall cost. That’s not surprising, but it’s useful to see it in actual numbers.

What doesn’t work

The Sentry’s syscall table has several hundred entries, and coverage of what ordinary servers use is very good. But some things aren’t there, and some can’t be:

  • perf_event_open returns ENODEV; hardware performance counters don’t exist inside gVisor.
  • io_uring is partially supported and, depending on version and policy, usually turned off.
  • bpf, including the eBPF calls that are now the normal way to write Linux tracing and networking extensions, isn’t supported.

So most of the tools you’d use to dig into a production problem (perf record, the bcc tools, bpftrace) don’t run inside a gVisor container.

That follows from what gVisor is. A userspace kernel can reimplement the parts of Linux that are mostly state machines: filesystems, sockets, pipes. It can’t really reimplement hardware interfaces (performance counters, hardware breakpoints) or host-wide facilities (attaching BPF programs, cgroup BPF). For most web services that doesn’t matter. For serious data-plane workloads it usually does.

The usual picture of gVisor on conference slides and vendor blogs is a secure drop-in replacement with an average overhead of “about 5x”. In practice it isn’t a drop-in. It fits workloads of a particular shape, and the shape is determined by which syscalls the workload depends on. An averaged ratio hides where the cost actually falls.

What I took away

  1. The attack-surface reduction is the real argument. The benchmarks and compatibility tables exist to tell you whether a given workload can afford it. Going from about 400 host syscalls to 68 is structural, not marketing, and it’s what makes gVisor interesting even when the benchmarks look bad. You can always make code faster; changing a threat model is harder.
  2. The overhead isn’t uniform, so don’t treat it as one number. 1.4x on hashing barely matters. 12x on localhost TCP rules gVisor out for some workloads and is irrelevant for others. The useful question isn’t how fast gVisor is, but how syscall-dense your workload is and how much of that is socket work.
  3. Compatibility gaps decide more cases than speed. Workloads that fail under gVisor usually fail because something they need (perf, bpf, io_uring) isn’t there, not because of the overhead. The benchmark harness was useful; actually launching a workload under runsc and checking that it did what it should was more useful.
  4. Building the toy first helped. Having written a small version before looking closely at the real one made the numbers make sense, because I’d written a reduced version of the code that produces them.