gVisor: Google's Userspace Kernel That Rewrites Linux in Go
Hook
Google runs a complete Linux kernel written in Go between your containers and the host—not a wrapper, not a filter, but 250,000 lines of syscall reimplementation that never touches kernel space.
Context
Container isolation has always been security theater. Linux namespaces and cgroups provide process isolation, but every syscall from your containerized app executes directly in the host kernel. A single kernel vulnerability—and there are dozens disclosed yearly—means game over. The kernel's attack surface spans 300+ syscalls, each with edge cases accumulated over decades. Traditional containers share the kernel with the host, so exploiting a kernel bug in your container means you own the entire machine.
The industry's answer has been virtual machines: run each container in a separate kernel using hardware virtualization. Kata Containers and Firecracker took this route, achieving strong isolation but paying the price in memory (150MB+ per container) and startup latency (1-2 seconds). For high-density multi-tenant platforms like serverless or hosted notebooks, these overheads are prohibitive. gVisor emerged from Google's need to run untrusted code at scale—Cloud Run executes arbitrary user containers, Google Colab runs user Python notebooks that could attempt privilege escalation. They needed VM-level isolation with container-like efficiency, so they built a userspace kernel that intercepts syscalls and reimplements them without ever executing in kernel space.
Technical Insight
gVisor's architecture revolves around the Sentry, an application kernel running as an unprivileged userspace process. When a containerized application makes a syscall like open(), it doesn't reach the host kernel. Instead, the Sentry intercepts it through either ptrace (for maximum compatibility) or KVM (for better performance), and gVisor's Go implementation handles the request. This isn't syscall filtering—it's complete reimplementation. The Sentry maintains its own process table, virtual filesystem, network stack, and memory management.
The interception mechanism is abstracted through a platform layer. In KVM mode, the Sentry runs guest code in a VM with ring0 disabled, causing syscalls to trap into the Sentry via VMEXIT. This sounds like virtualization, but crucially there's no separate guest kernel—the Sentry IS the kernel. In ptrace mode, the Sentry acts as a debugger, catching every syscall through PTRACE_SYSCALL. Here's how you'd configure runsc (gVisor's OCI runtime) to use KVM mode with Docker:
# Install runsc
sudo apt-get install runsc
# Configure Docker to use gVisor with KVM platform
sudo runsc install --runtime=runsc-kvm -- --platform=kvm
# Run a container with gVisor
docker run --runtime=runsc-kvm -it ubuntu bash
# Inside the container, verify you're in gVisor's kernel
uname -a # Shows Linux but it's gVisor's implementation
cat /proc/cpuinfo # Filtered view from gVisor
The critical security property: the Sentry only uses approximately 70 whitelisted host syscalls for its own operations, versus the 300+ that a normal container could invoke. Application syscalls never become host syscalls—they're translated into safe operations within the Sentry. If your app calls socket(), gVisor's Netstack (a complete TCP/IP stack written in Go) handles it entirely in userspace. The host kernel never sees your network packets.
The Gofer solves filesystem access without breaking isolation. The Sentry cannot hold direct file descriptors to host files—that would enable attacks through kernel filesystem bugs. Instead, a separate Gofer process with minimal capabilities mediates all filesystem operations through the 9P protocol. When your app opens /etc/hosts, the Sentry sends a 9P message to the Gofer, which performs the actual open() on the host and shuttles data back. This dual-process architecture means compromising the Sentry doesn't give you raw file descriptors to the host.
The implementation pushes Go's boundaries. While Go provides memory safety, gVisor's performance-critical paths use unsafe and go:linkname extensively. Here's a simplified example of how gVisor's architecture looks from the syscall perspective:
// Simplified representation of gVisor's syscall handling
// From the Sentry's perspective
func (s *Sentry) handleSyscall(regs *arch.Registers) error {
// Application made a syscall - we intercepted via KVM/ptrace
sysno := regs.Orig_rax // x86-64 syscall number
switch sysno {
case SYS_OPEN:
return s.handleOpen(regs)
case SYS_SOCKET:
return s.handleSocket(regs)
// ... 300+ more syscalls implemented
}
}
func (s *Sentry) handleOpen(regs *arch.Registers) error {
filename := s.readString(regs.Rdi)
flags := int(regs.Rsi)
// Don't actually call host open()!
// Instead, send 9P message to Gofer
req := &p9.OpenRequest{
Path: filename,
Flags: translateFlags(flags),
}
resp := s.goferClient.Send(req)
fd := s.createVirtualFD(resp.FileHandle)
regs.Rax = uint64(fd) // Return virtual FD to app
return nil
}
Google runs this in production at massive scale. GKE Sandbox uses gVisor for Kubernetes pods requiring isolation, and Cloud Run executes every container through gVisor. The project's security track record is documented—CVEs are disclosed publicly with patches, showing Google treats this as production security infrastructure, not research.
Netstack deserves special mention as independently valuable. It's a complete TCP/IP stack supporting TCP BBR congestion control, GSO/GRO offloading, raw sockets, and IPv6. Projects like Tailscale have adopted Netstack for userspace networking. You can use it outside gVisor for any application needing network stack control without kernel modules or eBPF complexity.
Gotcha
Performance overhead is real and workload-dependent. Syscall-heavy applications suffer because every syscall crosses multiple userspace boundaries (app → Sentry → possibly Gofer → host). Disk I/O benchmarks show 30-50% degradation because reads/writes involve 9P serialization and process hops. CPU-bound workloads with few syscalls see 10-20% overhead, primarily from KVM exits. Measure your actual workload—don't assume it's acceptable.
Linux compatibility is incomplete by design. gVisor implements the common subset of Linux syscalls, but obscure flags, esoteric ioctls, and edge cases are missing. Applications using FUSE filesystems, certain terminal emulation features, or specialized hardware interfaces often break. The compatibility layer prioritizes common server workloads (web services, Python notebooks, batch jobs) over comprehensive Linux emulation. You must test your specific stack—gVisor maintains a compatibility test suite, but it can't cover every application. Debugging is painful because your mental model breaks. Running strace on a gVisor container shows the Sentry's syscalls to the host, not your application's syscalls. Stack traces cross the Go/native boundary in confusing ways. Performance profiling tools like perf don't work because the kernel events they're measuring aren't the ones your app is generating. Google has built custom debugging tools, but the ecosystem is primitive compared to native debugging.
Verdict
Use if: You're running untrusted multi-tenant workloads (serverless platforms, hosted notebooks, CI/CD runners) where container escape is a real threat and you need stronger isolation than namespaces but can't afford VM memory overhead. Your workloads are typical stateless services, web APIs, or batch processing that aren't pathologically syscall-heavy. You're on modern infrastructure (Linux 5.6+, KVM available) and can spare 100-200MB per container. Skip if: You need bare-metal performance for high-throughput systems, run specialized software requiring obscure kernel features, or operate in memory-constrained environments. Skip if debugging complexity outweighs security benefits—if you're running trusted first-party code, the compatibility gaps and operational friction aren't worth it. Also skip for low-level systems programming, kernel module development, or anything that needs direct hardware access. The sweet spot is untrusted code in cloud platforms where security justifies the tradeoffs.