Skip to content
mentoor

Topic 3 of 5 · 4 min read

MicroVMs and hosted sandboxes for untrusted code

Goal: Choose between a container, a gVisor or microVM sandbox and a hosted sandbox for a workload and justify the choice by threat.

Pick the sandbox by how much you trust the code: a container for code you control, a user-space kernel or microVM for code you do not, and a hosted sandbox when you would rather not run the isolation yourself.

Check yourself · 1 of 3

What is the main security difference between a container and a Firecracker microVM?

Choose one answer

Why it matters

Some products run code that a model wrote on behalf of strangers: a data-analysis feature, a coding playground, an agent that executes whatever a user asks for. Anyone can steer that code, so you must assume it is hostile. A shared-kernel container may not be enough.

The options

  • Container. Fast and cheap. Shares the host kernel. Fine for your own team's agent on code you trust.
  • gVisor. A user-space kernel that intercepts the container's system calls and handles most of them itself, so the code talks to gVisor and not directly to the host kernel. It keeps container workflows and adds a layer, at some cost to speed on system-call-heavy work.
  • Firecracker microVM. A very small virtual machine with its own kernel, built for fast start and many instances. A kernel bug inside the guest does not directly give access to the host kernel. It is the base of many hosted sandboxes.
  • Hosted sandbox. A service runs the microVMs for you and gives you an API to start one, run code and read results. You trade control and data location for less work. Check where code and files go and how long they are kept.

Choosing

Ask four questions: Who can influence the code? What is on the machine if it escapes? Do you need fast start-up at scale? Who runs the isolation? Strangers plus valuable neighbours points to a VM boundary. One developer plus a throwaway checkout points to a container.

Before and after

Weak: "Run user-submitted notebooks in a Docker container on the same host that holds our customer database." Better: run each job in its own microVM or hosted sandbox, with no route to the database, a time limit, and a fresh disk that is destroyed afterwards.

What goes wrong

  • Isolation without cleanup. Reusing one sandbox between users carries files and secrets across.
  • Network left open. A microVM with open egress still sends data out.
  • Secrets inside. Passing production keys into the sandbox cancels the isolation.
  • Skipping limits. Without time and memory limits, a hostile job just hangs or exhausts the machine.

Examples

Comparing the options

Workload Fit
Your team's agent on your own repo Container
Public playground that runs model-written code microVM or gVisor
Small startup, no ops staff, strangers' code Hosted sandbox

Per-job lifecycle

Create a sandbox, copy in only the inputs, run with a 60-second limit, copy out the result, destroy the sandbox. Nothing from job one can be seen by job two.

Key points

  • Match the sandbox to who can influence the code and what sits next to it.
  • Containers share the host kernel; gVisor and microVMs add a stronger boundary.
  • Firecracker microVMs give each workload its own kernel with fast start-up.
  • A hosted sandbox saves effort but moves your data, so check where it runs and how long it keeps files.
  • Fresh sandbox per user, no secrets inside, a time limit and closed egress apply to every option.

Common mistakes

  • Running strangers' code in a plain container next to valuable services.
  • Reusing one sandbox across users or sessions.
  • Putting production credentials inside the sandbox.
  • Choosing a hosted sandbox without asking where code and files are stored.

Check yourself · 2 of 3

A public site runs code written by a model for anonymous users. Which isolation fits best?

Choose one answer

Check yourself · 3 of 3

What does gVisor do for a containerised workload?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic