# Building your own sandboxes

Alfonso Campodonico · 2026-10-17 · https://alfonsocampodonico.com/posts/building-your-own-sandboxes

> MicroVMs for untrusted code, built from the bottom up.

## Why build your own

A sandbox makes one promise: code you don't trust runs, and nothing it does reaches anything you care about. You can rent that from a dozen vendors today[^vendors]. I wanted to know what it takes to keep that promise myself, so I built one from the bottom up:

- **vmkit**: microVMs on Firecracker and Cloud Hypervisor behind one API. Every VMM runs in a rootless sandbox with a default-deny network.
- **kiln**: turns OCI images into microVM images and boots them.

*Interactive diagram on the page: kiln on vmkit, over Firecracker, Cloud Hypervisor and KVM.*

The short version: the microVM is the easy part. The hard part is everything around it. Who runs the VMM, what it can open, where its packets go, and what the guest is allowed to tell you.

## What we're defending

One rule drives everything below: images, guests and whatever runs inside them are untrusted. Nothing the guest reports (an exit code, a digest, a size) drives a security decision.

What's worth protecting: the host, the other VMs on it, credentials the host can reach (cloud metadata, registry tokens), the local image store, and even your terminal. That turns into a few hard guarantees:

| # | Guarantee |
| --- | --- |
| T5 | Guests can't reach link-local (including cloud metadata), private, loopback or host addresses, or other VMs. They can't spoof addresses. |
| T6 | VMMs run unprivileged, and only see the files and devices their VM needs. Nothing runs as root. |
| T8 | Every string from the guest or the image is sanitised before it's printed. |
| T9 | Guest messages are size-bounded and schema-validated. A protocol violation ends the VM. |

Why a microVM and not a container? A container shares the host kernel, so every syscall is attack surface. A microVM has its own kernel, so an escape needs a guest kernel bug and a KVM or VMM bug. But that only holds if the VMM itself is locked down. Firecracker's answer is its jailer, and the jailer needs root. I wanted neither root nor a weaker bar.

## Layer one: a rootless jail for any process

The foundation is vmkit-sandbox, a small helper that builds a jail out of Linux namespaces as a normal user. It refuses to run as root. It works in three stages:

1. `run` unshares user and PID namespaces and maps your uid.
2. Inside the new user namespace, it sets `max_user_namespaces` to 0, so nothing inside can create its own, and marks itself undumpable.
3. `init` becomes PID 1, unshares mount, network, IPC and UTS, builds the root, pivot_roots into it, then execs the VMM or supervises the program.

If a stage dies, the next one gets SIGKILL. Nothing outlives its parent.

A VMM sees almost nothing:

- a read-only 1 MiB tmpfs with `/dev/kvm`, the VMM binary, the kernel, its disks and one socket directory;
- fixed paths (`/vm/disk/0`), so nothing depends on where files live on the host;
- files opened by descriptor, never through a symlink, so a compromised VMM can't redirect them;
- tight rlimits, no core dumps, and a cgroup scope when one is available.

On top of that, Firecracker applies its own seccomp filters, and Cloud Hypervisor gets Landlock rules for exactly its own files.

*Interactive diagram on the page: Nested isolation layers around a sandboxed process.*

The same helper runs any program, not just a VMM. When code only needs to be contained, not booted, a namespace jail costs a fraction of a VM:

```rust
let spec = Spec {
    command: Command {
        program: Program::Path("/usr/bin/python3".into()),
        args: vec!["untrusted.py".into()],
        ..
    },
    root: Root::Overlay { lowers: vec![base], upper, work },
    ids: Ids::Subordinate { count: 65536 },
    network: Network::Egress(NetSpec::default()),
    nest: true,
    seccomp: true,
    ..
};
let status = sandbox::spawn(&spec, Stdio::inherit())?.wait()?;
```

Two flags do most of the work. `nest` gives the program its own user namespace: it's root over its files, but it can't mount, change routes or touch the firewall. `seccomp` blocks mounts, unshare, setns, BPF, io_uring and friends. One trick worth calling out: `clone3` returns `ENOSYS`, so libc falls back to `clone`, whose flags seccomp can actually inspect.

## Layer two: two VMMs, one API

vmkit drives Firecracker and Cloud Hypervisor behind one `Vmm` trait. I built against both from day one, because an abstraction designed against one backend always leaks it. Callers decide from `capabilities()`, never from the backend's name.

```rust
let vmm = backend.discover()?;
let mut vm = vmm.create(&VmSpec { kernel, disks, .. })?;
vm.start()?;
let end = vm.wait()?;
```

The differences that bit me:

| | Firecracker | Cloud Hypervisor |
| --- | --- | --- |
| Virtio devices | 17 on x86_64, 92 on aarch64 | 31, one taken by its RNG |
| How the guest ends the VM | `reboot` | `poweroff` |
| Console | `ttyS0` | `ttyAMA0` on aarch64 |

The exit row hides a real bug. On Cloud Hypervisor, a guest reset reboots the VM, so a workload can silently run twice. vmkit watches the VMM's event stream and kills it on a reset. If it can't read the stream, it kills the VMM anyway.

## The network: default deny, outside the guest

Most sandbox escapes that matter in the cloud aren't kernel exploits. They're a guest asking 169.254.169.254, the cloud metadata service, for the host's credentials.

Each VM gets its own network namespace, with an nftables chain the guest can't touch. It drops IPv6 and spoofed packets, allows what you list, and denies everything private by default: loopback, link-local (where the metadata service lives), private ranges, other VMs and the host itself. Traffic leaves through an unprivileged userspace proxy, so the host's firewall never changes.

*Interactive diagram on the page: Per-VM network namespace with the policy outside the guest.*

The test suite runs a hostile guest that tries every one of those. It has to fail at all of them.

## Images: kiln turns containers into microVMs

A VM that boots busybox is a demo. A sandbox has to run what people actually ship, and that's OCI images.

The core decision: one read-only erofs filesystem per OCI layer. kiln streams each layer's tar straight into erofs, without extracting it. That gets you three things:

- **Determinism.** Same input, same bytes, on any host. Identical layers dedupe across images for free.
- **Per-layer caching.** A warm convert of an unchanged image is one HEAD request.
- **It runs on a Mac.** Nothing is extracted, so it needs neither Linux ownership nor root.

```sh
kiln convert go:1.27-cli
kiln run alpine:3 -- sh -c 'echo hello'
```

At boot, kiln's own PID 1, kiln-init, mounts the layers, stacks them with overlayfs over a writable scratch disk, and runs the image's command. Host and guest talk over one schema-validated vsock channel.

*Interactive diagram on the page: From an OCI image to erofs layers to a booted guest.*

Here's where the device budget comes back. Every layer is a virtio disk, so kiln squashes the bottom layers when an image has more than 10.

Trust is explicit at boot. kiln pins the digests of the kernels and kiln-init builds it trusts, and it always supplies PID 1 itself. A malicious image can't swap the kernel or init.

## What's next

- **Lazy restore.** Snapshots as OCI artifacts, fetched in chunks on demand from below the VMM, so one mechanism works for both.
- **A containerd shim**, so kiln images run as a Kubernetes RuntimeClass and everything around them keeps working.
- **IPv6.** Guests only get IPv4 today, so the forward chain drops all IPv6. Letting it through needs an IPv6 deny list, anti-spoofing and neighbour discovery handled outside the guest.

## What I learned

- **The VMM is the smallest part.** Most of the code, and nearly all the bugs, are in who runs it, what it can open and where its packets go.
- **Put the policy outside the guest.** Firewall, kernel and init are decided on the host, where a compromised guest can't argue.

[^vendors]: Sandbox vendors at the time of writing: [E2B](https://e2b.dev), [Modal](https://modal.com), [Daytona](https://daytona.io), [Vercel Sandbox](https://vercel.com/docs/vercel-sandbox), [Cloudflare Sandbox](https://developers.cloudflare.com/sandbox/), [CodeSandbox SDK](https://codesandbox.io/sdk), [Fly.io Machines](https://fly.io/docs/machines/), [Northflank](https://northflank.com), [Runloop](https://runloop.ai), [Blaxel](https://blaxel.ai), [Morph Cloud](https://cloud.morph.so) and [Beam](https://beam.cloud).
