Sub topic 13
Virtual machines and microVMs for strong isolation
This chapter covers the strongest common isolation boundary for coding agents: running the agent inside its own virtual machine (VM). It assumes you know the container basics from earlier chapters, including namespaces, cgroups, capabilities, and seccomp. It also assumes you are comfortable on the Linux command line and know what a kernel is.
Why a VM is a different kind of boundary
A container shares the host kernel. Every system call the agent makes is handled by the same kernel that runs your editor, your browser, and your SSH agent. A kernel bug can therefore let code inside the container escape. Hardening reduces that risk but does not remove the shared kernel.
A virtual machine changes what is shared. The guest runs its own kernel on virtual hardware. The host kernel and the hypervisor, the software that creates and schedules VMs, expose a much narrower interface to the guest than the full system call table does.
| Property | Hardened container | gVisor | VM or microVM |
|---|---|---|---|
| Kernel that handles guest syscalls | Host kernel | User-space kernel (Sentry) | Guest kernel |
| Main attack surface toward the host | Host syscall interface | Small set of host syscalls used by the sandbox | Hypervisor and virtual devices |
| Start time | Milliseconds | Milliseconds to low seconds | Roughly 100 ms to several seconds |
| Memory overhead | Very low | Low | Tens of MB or more per guest |
| Can run a different kernel | No | No | Yes |
A VM escape needs a bug in the hypervisor or in an emulated device. Those bugs exist, but the code surface is far smaller than the Linux syscall interface.
How hardware virtualization works
Modern CPUs provide virtualization extensions (Intel VT-x, AMD-V, ARM virtualization). On Linux, the KVM module exposes these through the device /dev/kvm. A user-space program, called a virtual machine monitor (VMM), opens /dev/kvm, allocates guest memory, and runs guest CPU instructions directly on the hardware. When the guest touches something it should not touch directly, such as a disk or network card, the CPU traps to the VMM, which emulates the device.
Two points matter for sandboxing:
- The guest's memory is separate from the host's. The guest cannot read host memory unless a bug or a deliberate shared mechanism allows it.
- Anything the VMM emulates is attack surface. Fewer emulated devices means less code to exploit.
You can check whether a Linux machine supports KVM:
ls -l /dev/kvm
grep -E -c '(vmx|svm)' /proc/cpuinfoThe first command shows the device if the KVM module is loaded. The second prints a count above zero if the CPU advertises virtualization support. Many cloud instances do not allow nested virtualization, so a VM inside a VM may be unavailable. Check this before you design around it.
Full VMs versus microVMs
A traditional VM (for example QEMU with a full PC machine model) emulates a lot: BIOS, PCI bus, graphics, USB, sound, and several disk and network controllers. This is flexible but large.
A microVM is a VM stripped to the minimum needed to run a Linux workload. It typically has:
- A minimal VMM with few emulated devices, often only a virtio network device, a virtio block device, and a serial console.
- No BIOS or graphical hardware. The VMM boots a kernel directly.
- A small guest kernel and a minimal root filesystem.
- Fast boot and low memory overhead.
Firecracker, written in Rust and originally built for AWS Lambda and Fargate, is the best-known example. Cloud Hypervisor and QEMU's microvm machine type follow similar ideas. Kata Containers takes a different route. It runs standard container images but starts each pod inside a lightweight VM, so you keep the container workflow with a VM boundary underneath.
The diagram below shows the layers each option places between the agent and the host hardware.
Running an agent in a microVM
Firecracker is configured through a REST API on a Unix socket, or through a JSON config file. The following config boots a guest with one vCPU, 1 GiB of memory, a root filesystem image, and no network interface. The kernel and rootfs paths are placeholders you must replace with real files.
{
"boot-source": {
"kernel_image_path": "/srv/vm/vmlinux",
"boot_args": "console=ttyS0 reboot=k panic=1 pci=off"
},
"drives": [
{
"drive_id": "rootfs",
"path_on_host": "/srv/vm/agent-rootfs.ext4",
"is_root_device": true,
"is_read_only": false
}
],
"machine-config": {
"vcpu_count": 1,
"mem_size_mib": 1024
}
}Start it with:
firecracker --api-sock /tmp/fc.sock --config-file vmconfig.jsonLeaving out the network-interfaces section gives the guest no network device at all. That is stronger than a container's --network none, because there is no virtual interface to misconfigure.
Writing your own kernel and rootfs pipeline is real work. Many teams use a managed or open-source layer that does it for them, such as Kata Containers, a hosted sandbox service, or a local tool such as Lima or Colima for a developer laptop VM.
Using Kata Containers with Docker
If Kata is installed and registered as a runtime, the container workflow stays familiar:
docker run --rm -it --runtime=kata-runtime \
--network none \
--memory 2g \
python:3.12-slim bashThe runtime name depends on your installation. Some setups register kata, kata-qemu, or kata-fc. Check docker info for the list of registered runtimes.
Getting project files into the guest
Containers use bind mounts. A VM has to share files across a hardware boundary, and the method you choose affects both security and performance.
| Method | How it works | Security notes |
|---|---|---|
| Copy in, copy out | Copy the project into the guest disk, then copy results back | Strongest. Nothing is shared live. You review changes before applying them. |
| Block device image | Attach a disk image containing the project | Simple and isolated. The host must not trust the guest's filesystem contents when mounting it afterward. |
| virtio-fs or 9p | The guest mounts a host directory | Convenient and fast. It adds shared-filesystem attack surface and live write access to the host directory. |
| Network transfer (git, rsync, SSH) | Push and pull over a controlled channel | Requires a network path, so restrict it to one host. |
For untrusted agents, copy in and copy out fits the disposable workflow from earlier chapters. Treat anything coming out of the guest as untrusted input. In particular, do not mount a guest-written disk image on the host without checks, since a crafted filesystem can exploit host filesystem code. Prefer extracting a tar archive or a git patch and reviewing it.
Networking for VMs
A microVM with a network device usually connects to a host TAP interface. Control egress at the host, outside the guest's reach:
- Create a TAP device per VM so guests cannot see each other's traffic.
- Place the TAP on its own bridge or routed subnet.
- Add host firewall rules (nftables or iptables) that allow only the destinations you want, for example a package mirror or a model API endpoint.
- Block access to the cloud metadata address
169.254.169.254and to internal subnets.
Firewall rules inside the guest do not count as a control, because the agent can change them. The rules must live on the host or on a separate gateway.
Hardening the VMM itself
The hypervisor boundary is strong, but the VMM is still a process on the host. If an attacker escapes the guest into the VMM, that process becomes the next barrier. Defense in depth means limiting what the VMM can do:
- Run the VMM as an unprivileged user, never root.
- Apply seccomp filters to the VMM process. Firecracker ships with its own filters and a companion tool called the jailer.
- Use the jailer or equivalent to put the VMM in its own chroot, namespaces, and cgroup.
- Give each VM its own user ID, so one escaped VMM cannot read another VM's files.
- Keep the hypervisor, VMM, and host kernel patched.
This is the same container hardening you learned earlier, now applied around the VMM.
Trade-offs and failure modes
- Startup cost and image management. You maintain a kernel and root filesystem, and rebuild them when you patch. Snapshots can cut boot time, but a restored snapshot reuses memory state, including random number generator state. Reseed entropy after restore, or two clones can produce the same "random" values.
- Nested virtualization. Many cloud VMs do not expose
/dev/kvm. You may need bare-metal instances or a provider that supports nesting. - Resource accounting. A guest has a fixed memory size. Memory ballooning and overcommit help density but can let one guest pressure the host.
- Shared devices. Passing a GPU or USB device through to a guest weakens isolation, because the guest then talks to real hardware and its drivers.
- Side channels. CPU cache and speculative-execution attacks can leak data across VM boundaries on shared hardware. For hostile workloads, keep different trust levels on separate hosts and apply the mitigations your kernel and CPU vendor recommend.
- Debugging and tooling. Some development tools expect a normal Docker socket or host paths. A VM adds friction there.
A VM does not change what the agent can do with the access you give it. If you place an API token in the guest, a hostile agent can still use that token over any allowed network path. Isolation limits blast radius on the host. It does not limit misuse of what you deliberately handed over.
A practical rule of thumb
Use a microVM when the agent runs code you do not trust, when many users share a host, or when a kernel escape would be costly. Use a hardened container or gVisor when the code is mostly trusted and the speed and convenience matter more. The next chapter turns these considerations into a decision process.
Exercise
A team runs agent-generated code in Firecracker microVMs. To move results out, they attach the guest's disk image to the host with mount and copy files from it. Name the risk this creates and give a safer alternative.
Show answer
Mounting a filesystem written by the guest makes the host kernel parse untrusted data, so a crafted filesystem can exploit a bug in the host's filesystem code and defeat the VM boundary. A safer approach is to have the guest produce a tar archive or git patch, copy that out over a controlled channel such as vsock or a restricted transfer, and review or extract it as untrusted input in an unprivileged, separate environment.
Exercise
Why is a firewall rule set inside the guest not enough to restrict a microVM's network egress, and where should the rules live?
Show answer
The agent may have root inside the guest and can change or remove any rule there. Egress rules should be enforced on the host (for example nftables on the TAP interface) or on a separate gateway the guest cannot modify.