Sub topic 13
Virtual machines and microVMs for strong isolation
This chapter assumes you know how containers share the host kernel, and you have seen the hardening flags and gVisor from earlier chapters. Here you will see what a virtual machine adds, why microVMs make it practical for agents, and what it costs.
What a VM changes
A container and its host share one kernel. If an agent finds a kernel bug, it can escape to the host. A virtual machine (VM) gives the guest its own kernel. The hypervisor, which is the software that runs VMs, uses CPU virtualization features to keep the guest's memory and devices separate from the host.
The agent's system calls now go to the guest kernel. A kernel exploit inside the VM only compromises the guest. To reach the host, the attacker needs a second bug in the hypervisor or in a virtual device.
Why microVMs
A traditional VM emulates a full PC, with BIOS, many devices, and a slow boot. That is a large attack surface and a heavy start-up cost. A microVM is a VM with a minimal virtual machine monitor, a small set of devices, and a stripped-down guest kernel.
- Firecracker is a microVM monitor written in Rust. It exposes only a few devices, such as virtio network and block devices. It boots in a fraction of a second.
- Cloud Hypervisor and QEMU's microvm machine type follow a similar idea.
- Kata Containers runs each container or pod inside a lightweight VM. It plugs into Docker and Kubernetes through a runtime interface, so you keep the container workflow.
Fast boot matters for agents. You can start one fresh VM per task and throw it away, which fits the disposable workflow from earlier chapters.
Using Kata with Docker
Once Kata is installed, you register it as a Docker runtime:
{
"runtimes": {
"kata": {
"runtimeType": "io.containerd.kata.v2"
}
}
}Put this in /etc/docker/daemon.json, then restart the Docker daemon. After that, select the runtime per container:
docker run --rm -it --runtime=kata \
--network none \
--memory 2g \
-v "$PWD":/workspace \
-w /workspace \
python:3.12-slim bashThe uname -r output inside will show the guest kernel, not the host's. That is a quick check that you are really in a VM.
Trade-offs
| Property | Hardened container | gVisor | MicroVM |
|---|---|---|---|
| Kernel | Shared with host | User-space kernel in front of host | Own guest kernel |
| Start time | Milliseconds | Milliseconds | Hundreds of milliseconds |
| Memory overhead | Very low | Low | Tens of MB per VM |
| Syscall compatibility | Full | Partial | Full (guest kernel) |
| Needs hardware virtualization | No | No | Yes (KVM) |
The last row is the common obstacle. Firecracker and Kata need KVM, so they run on bare metal or on cloud instances that allow nested virtualization. Many standard cloud VMs do not.
File sharing is the weak spot
A VM cannot bind-mount your project directory the way a container can. The directory is shared through a file-sharing protocol such as virtio-fs, or the files are copied into a disk image. Both add latency, and virtio-fs adds attack surface. Copying files in and copying a diff out also gives you a natural review point.
When to choose a microVM
Choose a microVM when the code is untrusted or adversarial, when many tenants share one host, or when a host compromise would be severe. Prompt injection from web pages or third-party repositories is a good example of a situation that raises the risk. For a single developer running an agent on their own repository, a hardened container or gVisor is often enough.
Exercise
An agent runs inside a Firecracker microVM, and a kernel exploit succeeds inside the guest. Why is the host still not compromised, and what would the attacker need next?
Show answer
The exploit targets the guest kernel, which is separate from the host kernel. The attacker would need a second vulnerability in the hypervisor or one of its virtual devices to reach the host.