Sub topic 14
Choosing the right isolation level
This chapter assumes you have read the earlier chapters on the isolation spectrum, container hardening, and network and secrets controls. You should know what a shared kernel means and be able to read a docker run command with hardening flags. Here you will turn those pieces into a decision. The question is which boundary to put around a given agent, not how to configure one.
Start from the threat, not the technology
Every isolation level answers one question: what can a hostile process in the sandbox reach if it tries hard? Choosing well means writing down what you are defending against before you look at tools.
Four questions drive most decisions:
- Who wrote the code the agent will run? Your own repository is different from a random package or a pull request from a stranger.
- Can the agent be steered by untrusted text? Issues, web pages, READMEs, and dependency docs can all contain prompt injection. If the agent reads them, treat its commands as untrusted even when you trust the model.
- What is worth stealing or damaging on the host? A throwaway CI runner holds little. A developer laptop holds SSH keys, browser sessions, and source for other projects.
- Does one sandbox host several users or tasks? If so, a failure in one can leak into the others.
Most real agent incidents are not kernel exploits. They are an agent deleting the wrong directory, reading a credential file, or sending data to an unexpected host. Those failures are stopped by filesystem scoping, network egress control, and secret handling. They are not stopped by a stronger boundary. The isolation level matters when the attacker is trying to escape the sandbox, not when the agent is merely careless.
What each level shares with the host
The deciding difference is how much of the host's attack surface sits between the agent's code and the machine.
| Level | Kernel the agent talks to | Attack surface exposed | Typical overhead | Compatibility |
|---|---|---|---|---|
| Hardened container (runc) | The host kernel, filtered by seccomp and capabilities | Hundreds of system calls and the kernel code behind them | Near zero | Highest. Runs ordinary Linux software. |
| User-space kernel (gVisor, `runsc`) | A kernel written in user space that makes a small set of host calls | A much narrower set of host system calls | Low CPU cost, noticeable on syscall-heavy and file-heavy work | Good, but some syscalls and `/proc` or `/sys` features are missing |
| MicroVM (Firecracker, Cloud Hypervisor, Kata with a microVM backend) | A separate guest kernel | The hypervisor and virtual devices | Higher memory use and slower start than containers | Good, with a full guest kernel. Needs hardware virtualization. |
| Full VM | A separate guest kernel with a full device model | The hypervisor and a larger device model | Highest | Highest, including nested containers and custom kernels |
A container escape needs a kernel bug reachable from the allowed system calls. A gVisor escape needs a bug in gVisor and then a bug in the few host calls it makes. A VM escape needs a hypervisor or device-emulation bug. Each step down the table shrinks the reachable code that attackers have found bugs in before. It never reaches zero.
A decision flow
The flow below captures the logic most teams settle on. It is a starting point to adjust, not a rule.
Every branch ends at the same box. The isolation level is one layer. Scoped mounts, egress control, and resource limits apply at every level.
Choosing a hardened container
A hardened container is the right default when the main risk is a mistake, not an attack on the kernel. That fits these cases:
- You run the agent on your own repository, on your own machine, and watch it work.
- The agent does not read arbitrary web content or third-party issues.
- The host holds nothing the agent could reach through the mounted directory.
Use the full set of flags from earlier chapters: non-root user, read-only root filesystem, dropped capabilities, no-new-privileges, resource limits, no network or an internal network, and only the project directory mounted. A container configured this way is a strong guard against accidents. It is a weak guard against a determined attacker with a kernel exploit, because the kernel is shared.
Do not choose a container if you need to run untrusted code from many parties on one host. A single kernel bug then exposes every tenant.
Choosing gVisor
gVisor suits the middle ground. The code is not fully trusted, or the agent reads untrusted text, but you want container-like startup and tooling. It changes one flag:
docker run --rm -it --runtime=runsc python:3.12-slim bashThe runtime must be installed and registered with Docker first. Keep all the hardening flags from the earlier chapters on this command as well. gVisor adds a layer and does not replace them.
Check these trade-offs before you commit:
- Compatibility gaps. Programs that use uncommon system calls,
perf, someptraceuses, or eBPF may fail. Run your actual workload, such as the test suite and the build, underrunscbefore you rely on it. - I/O cost. File-heavy work like
npm installor a large compile makes many system calls, and each one is handled in user space. Measure it. - Not a VM. The gVisor sandbox process still runs on the host kernel. It reduces the exposed surface and does not remove it.
Choosing a microVM or VM
Pick a microVM when the attacker is realistic and the stakes are high. Examples:
- A service that runs agents for many customers.
- An agent that processes content from the open internet and holds credentials worth stealing.
- A sandbox that must run Docker, systemd, or privileged tooling inside it, which would be unsafe in a shared-kernel container.
A microVM gives each sandbox its own kernel, so a guest kernel exploit stays inside the guest. The costs are real:
- Hardware support. The host needs KVM or an equivalent. Many cloud instance types do not allow nested virtualization, so running Firecracker inside an ordinary cloud VM may not work. Bare-metal or nested-virtualization-enabled instances are required.
- Operations. You manage kernel images, root filesystem images, and guest networking. A container image does not carry over unchanged.
- Memory. Each guest has its own kernel and page cache, so density per host drops.
- File sharing. Mounting a host directory into a VM goes through a virtual filesystem or a copy. Both are slower than a bind mount, and a shared directory is still a channel between guest and host.
Full VMs are the same idea with a bigger device model and slower start. Use them when you need a complete operating system or must run something the microVM stack does not support.
Encoding the decision
A written policy beats an ad hoc choice. The function below maps a few facts about a task to a runtime. Adapt the rules to your threat model.
def choose_isolation(
own_code: bool,
reads_untrusted_text: bool,
multi_tenant: bool,
needs_kernel_features: bool,
) -> str:
"""Return the minimum isolation level for a task."""
if multi_tenant or needs_kernel_features:
return "microvm"
if reads_untrusted_text or not own_code:
return "gvisor"
return "hardened-container"
cases = {
"personal repo, local": (True, False, False, False),
"agent triages public issues": (True, True, False, False),
"hosted agents for customers": (False, True, True, False),
"needs Docker inside sandbox": (True, False, False, True),
}
for name, args in cases.items():
print(f"{name}: {choose_isolation(*args)}")The output is hardened-container, gvisor, microvm, and microvm for the four cases, in that order. The point of writing it down is that a reviewer can argue with the rules, and the runtime stops depending on whoever set up the sandbox that day.
Common mistakes
- Treating a stronger boundary as a substitute for scoping. A microVM with your SSH keys mounted inside leaks those keys just as well as a container does.
- Choosing by habit. Teams pick containers because they are familiar, or microVMs because they sound safer. Pick from the threat.
- Not testing the real workload. gVisor and microVMs both break some tools. Find out in a trial, not during an incident.
- Mixing levels inconsistently. If some tasks run in microVMs and others in containers with the same credentials, an attacker uses the weakest path.
- Forgetting that the boundary can be reconfigured. A container started with
--privilegedor with the Docker socket mounted has almost no isolation, whatever its runtime.
Exercise
A team runs an agent that reads public GitHub issues, edits a private repository, and runs its test suite. The host is a developer laptop that holds SSH keys. The team has no other users sharing the sandbox, and the tests need only ordinary Linux system calls. Which isolation level would you choose, and what must you do regardless of the level?
Show answer
gVisor is a reasonable choice. The agent reads untrusted text from public issues, so prompt injection is a realistic path to hostile commands, which rules out a plain hardened container as the only layer. Nothing is shared between tenants and the tests use ordinary system calls, so a microVM's extra cost is not clearly justified (a microVM is also defensible if the keys are very valuable). Regardless of level, mount only the project directory, keep SSH keys and other credentials out of the sandbox, restrict network egress, and set CPU, memory, and process limits.