gvisor-exec: Running Untrusted Commands in a Throwaway gVisor Sandbox

I run curl | sh more often than I should, and I read what sh is about to get less often than I should. Go tools ship install instructions as a pipe into a shell, forum answers suggest a paragraph-long bash -c, and an LLM asked for a build script hands one over with no expectation that I’ll audit it. Usually that’s fine. Sometimes it isn’t, and you have a bad morning.

I wanted something between the two current options. Running the script as myself is the default and what actually happens. Spinning up a VM means a disk image, a kernel, a network bridge and ten minutes before I can even ask whether the command does what it claims. I wanted a command I could put in front of any other command that runs it in a constrained, throwaway sandbox and cleans up afterwards.

That’s gvisor-exec:

$ gvisor-exec -- uname -a
Linux gvisor-exec 4.4.0 #1 SMP Sun Jan 10 15:06:54 PST 2016 x86_64 GNU/Linux

The 4.4.0 gives it away. gVisor’s Sentry has a spoofed kernel version built in and answers every uname() with 4.4.0, whatever the host runs. The guest’s syscalls never reach the host kernel; they’re intercepted and reimplemented in a userspace kernel written in Go. What reaches the host are the Sentry’s own requests on the guest’s behalf, which run under a seccomp filter that blocks most of the interesting syscalls.

No Docker, no containerd, no root. The binary wraps runsc in rootless mode, builds an OCI bundle in a temp directory, runs the command, and deletes everything.

What it does

gvisor-exec is one Go binary of about five hundred lines, half of it the OCI spec builder and its validation. It wraps runsc --rootless in a CLI built for one-shot sandboxing. The defaults:

  • the host / is visible read-only;
  • writes go to a temporary overlay that’s discarded on exit;
  • network is off;
  • all capabilities are dropped;
  • the sandbox gets its own PID, mount, IPC, UTS and network namespaces.

Typical uses:

# A script from a forum, piped in.
cat sus.sh | gvisor-exec -- /bin/sh

# A build that reads from the current directory.
gvisor-exec -bind "$PWD:/mnt" -cwd /mnt -- make

# The same build, returning its output as a tarball on stdout.
gvisor-exec -ro-bind "$PWD:/mnt" -- sh -c 'cd /mnt && make && tar c build/' > out.tar

The implementation is boring. It turns a Config struct into OCI runtime-spec JSON, writes it to /tmp/gvisor-exec-XXXX/config.json, execs runsc --rootless ... run --bundle $dir $id, forwards stdio, waits, returns the exit code and removes the bundle. The value is in the defaults, and finding them was the work. The rest of this post is how I got to each one, in the order it happened.

gVisor isn’t usable as a library

I started out expecting to import gvisor.dev/gvisor/pkg/sentry/... and drive the Sentry from my own program. That isn’t really possible. The gVisor packages aren’t built for outside users: their interfaces assume coordination with the rest of gVisor, the build system is Bazel, and the glue between the Sentry and its platform isn’t public. The code is worth reading, but not worth linking against.

The practical route is to treat runsc as a black box with a stable CLI and shell out to it. Every OCI-level integration I looked at does the same: the containerd-shim-runsc-v1 that gVisor ships for containerd, various systemd experiments, CI runners that need a sandbox per job. runsc is the supported entry point and the Sentry is an implementation detail.

User namespaces: run as uid 0

runsc --rootless sets up a user namespace with one entry: host uid 1000 maps to sandbox uid 0. The sandbox’s root is me. That’s not privilege escalation, just my own identity under a different number.

At first I ran the sandbox process as uid 1000, on the theory that a sandboxed process shouldn’t look like root even to itself. The sandbox started and could read files in my bind-mounted directory, but writing to them returned EACCES. It took me longer than I’d like to admit to see why:

  • A host file is owned by uid 1000 (me).
  • Inside the namespace, that file appears to be owned by uid 0, the only uid the namespace can map.
  • The sandbox process runs as uid 1000, which isn’t the file’s apparent owner or in its group, so permission checks treat it as “other”.
  • Mode 0755 gives “other” r-x. No write bit, so EACCES.

The fix was to default the sandbox uid and gid to 0. Then the process owns the files it wants to write, which it always really did; the namespace had just relabeled my identity. The mapping is the same single-entry identity map, and the process now runs as the uid the mapping expects.

The overlay has to cover every mount

runsc’s overlay behavior is set by --overlay2, which takes a {mount}:{medium} pair. The first half is root (overlay only the rootfs) or all (overlay every mount in the bundle). The second picks storage: self keeps the upper layer in a filestore inside the mount’s own host directory, memory uses sandbox memory, and so on.

I started with root:self, assuming the rootfs overlay was what I wanted and bind mounts would just work. They didn’t. Writes to the bind-mounted directory returned EINVAL on openat(..., O_CREAT, ...). strace showed the syscall was legal, the target directory existed and the flags were reasonable, but the Sentry still returned EINVAL.

After some time in runsc’s debug logs I switched to all:self. The failures stopped, every mount got its own overlay, and writes went to temp storage as intended. I didn’t chase the original bug to its source. I suspect root:self plus writable bind mounts is an edge case in rootless mode that nobody has sent the maintainers a reproducer for. My workaround cost nothing, so I didn’t file an issue, but I did write the decision down.

Bind-mount targets must already exist

gVisor’s filesystem is served by a separate trusted process called the Gofer, which talks LISAFS to the Sentry over a socket and holds the only handles into the host filesystem. When the rootfs is the host /, as gvisor-exec sets it up, the Gofer can’t create directories on it. The host filesystem is read, not written.

So bind-mount destinations must already exist on the host. gvisor-exec -bind "$PWD:/work" fails, because /work doesn’t exist and the Gofer can’t create it. The error from runsc doesn’t help:

cannot read client sync file: waiting for sandbox to start: EOF

What actually happened: the Gofer died trying to mkdir("/proc/fs/root/work"), the Sentry couldn’t finish its handshake with a Gofer that was gone, and the handshake pipe reported EOF to whatever was still reading it.

Now gvisor-exec checks this up front. If the rootfs is /, every bind-mount destination is os.Stat’d before the spec goes to runsc, and a missing one produces my own error naming the path: bind destination "/work" does not exist on host. That’s something you can act on. The general habit: when a subprocess’s errors don’t tell you what went wrong, check the preconditions in your own code and write your own error.

Rootless networking barely works

runsc’s default network mode, sandbox, creates a veth pair and installs iptables rules to route traffic through the Sentry’s userspace network stack. A rootless user can’t do either, and it fails clearly:

failed to run /usr/bin/ip link add ve-runsc-...: exit status 2

--network=host is the other option, and on paper it puts the sandbox in the host’s network namespace. In rootless mode you get a shell of a network. The routing table inside the sandbox was effectively empty (/proc/net/route had only its header row), and DNS didn’t resolve a single query. Connecting to a literal IP might work with the right configuration; I didn’t dig into when it does. For a one-shot sandbox whose main job is containment, the right default is none, so that’s what gvisor-exec uses.

Tools that need real networking under something like rootless gVisor (modal.com’s runtime, for one) handle it out of band, usually with a privileged host process that creates the veth ahead of time, or a proxy the sandbox connects through. I kept -network host available, documented its limits, and moved on. For my use, a sandbox that can’t reach the internet is a feature.

Writes should be thrown away

I started out assuming writes should persist to the source directory by default. Between the EINVAL problem and the user-namespace permissions, getting that to work cleanly became a project of its own, and somewhere in it I realized I’d misread the requirement.

For most commands you’d run under a tool like this, throwaway writes are the right behavior. The untrusted script should think it wrote its output, but the output shouldn’t land on the host. If you want bytes back, you take them out on purpose: tar to stdout, a redirected file descriptor, or an explicit volume mount for that purpose. Everything else is discarded. The defaults are there to serve caution, not convenience, and caution is better served by forgetting than by remembering.

Once I committed to that, the CLI followed naturally. It ended up matching how people use firejail, bubblewrap and the various container-based scratch pads: you write into something that disappears, and pull out what you want deliberately.

The OCI spec is small

I’d been dreading the OCI runtime spec, picturing a committee document with a thousand optional fields.

The spec you need for a single sandboxed command is about thirty fields, and most are constants: an empty capability list, one rlimit, five namespaces, four default mounts, the process argv, a working directory and a terminal flag. It’s one Go struct with a few nested structs, a json.MarshalIndent, and a unit test that round-trips the result through json.Unmarshal to check that the JSON reads back.

That made it clear why runtimes other than runc and runsc (Kata, Youki, crun) can be written in a reasonable amount of code. The spec is small. The hard parts are around it: unpacking images and layering filesystems, CRI integration, intercepting syscalls. gvisor-exec skips all of that because runsc does it.

What I learned

The project is one directory: a Go binary, a few test files, a Makefile and five runnable examples. It’s at github.com/mtclinton/gvisor-exec, MIT-licensed, and needs runsc on the host (the static binary from gvisor.dev installs with one curl). It’s a wrapper, but building it taught me a few things:

  1. gVisor’s security comes from interception, not permissions. What matters about a sandboxed command isn’t the list of capabilities it lacks but that its syscalls never reach the host kernel. A kernel bug in write() doesn’t matter if the guest never makes the host run write(). That changes how to think about the defaults. I turn the network off not mainly to stop exfiltration, but because even the Sentry’s network stack is more surface than an untrusted command needs. Dropping all capabilities shrinks the set of behaviors the Sentry has to reimplement correctly.
  2. Throwaway state is a stronger control than permissions. A file the command wrote that’s gone when it exits is more tightly controlled than one written to a location I tried to permission correctly. The first can’t be left behind; with the second I have to reason about how it might be. In every sandbox I’ve built or configured, the cheapest safety came from things not persisting, not from things being forbidden.
  3. The hard work is in the defaults. uid=0, all:self, -network none, a read-only /, checking bind-mount targets in the wrapper: each one cost me a day of not understanding something, and each is now hidden behind a flag the user doesn’t have to set. That’s what a good wrapper is for.

The examples under examples/ are the fastest way to get a feel for the CLI. To see the defaults and the reasons for them where they actually live, start with spec.go.