What a microVM snapshot really contains, and the three ways restoring one goes wrong
Snapshots are one of the most useful things a virtual machine can do for agent workloads. Set up an environment once, with dependencies installed and a language server warmed up, save it, and start every later run from t
Snapshots are one of the most useful things a virtual machine can do for agent workloads. Set up an environment once, with dependencies installed and a language server warmed up, save it, and start every later run from that point instead of from scratch. Or pause a sandbox between conversation turns and resume it later with everything still in memory.
They also have sharp edges that are easy to miss, because a snapshot does not just save files. Depending on the kind, it saves a running computer, frozen mid-thought, including things that were never meant to exist twice. This post explains what is inside, using Firecracker's snapshot documentation as the reference, and the three classic ways restores go wrong.
Two kinds of "snapshot"
The word gets used for two different things, and the risks differ.
A filesystem snapshot saves the disk, or part of it. Restoring it gives you the same files, with a fresh boot and fresh processes. This is close to a container image layer.
A full machine snapshot saves the guest's memory and the state of its virtual hardware as well. Restoring it resumes execution exactly where it stopped: the same processes, the same open files, the same values in every variable. Firecracker's documentation says its snapshots save the full state of the guest memory and the emulated hardware state, producing a guest memory file and a microVM state file, with disk files managed separately by the user.
The pitfalls below are mostly about the second kind. They apply whenever a machine's memory is resumed, and become serious when it is resumed more than once.
Pitfall 1: things that should be unique are not
A running Linux system holds plenty of state that assumes it exists once: the kernel's random number generator state, random seeds cached in user-space libraries, UUIDs generated at startup, TLS session keys, nonces, temporary tokens.
Firecracker's documentation calls this out under "Snapshot security and uniqueness". When a guest's state is resumed from more than once, information assumed to be unique may not be, including identifiers, random numbers and seeds, the guest's entropy pool and cryptographic tokens. Without a mechanism that guarantees unique things stay unique, the documentation says it considers resuming from the same state more than once insecure.
It gives three examples. Booting a VM, snapshotting it, terminating it and resuming the snapshot exactly once is the secure pattern. Resuming the original and also starting a clone from the snapshot is potentially insecure. Starting several clones from one snapshot is potentially insecure too.
There is a partial fix. Firecracker always enables a VMGenID device, which changes a random 16-byte generation identifier on resume and notifies the guest. Linux 5.18 and later uses that to reseed its in-kernel random number generator. The documentation is explicit about the limit: state other than the guest kernel's entropy pool, such as unique identifiers, cached random numbers and cryptographic tokens, will still be replicated across microVMs resumed from the same snapshot. User-space libraries that keep their own pools are on you. A companion document, entropy for clones, goes deeper and recommends against using such libraries in pre-snapshot logic.
What to do: take golden snapshots before anything generates long-lived unique values. Do not create keys, session tokens or IDs in the setup phase you snapshot. Generate them after restore. In application code, prefer asking the kernel for randomness (getrandom, /dev/urandom) at the moment you need it, rather than seeding a generator once at startup.
Pitfall 2: the clock jumps
A resumed machine wakes up believing it is still the moment the snapshot was taken. Firecracker's documentation notes that a restored microVM resumes with the guest's wall clock continuing from snapshot creation, and that the wall clock should be updated on the guest side. An optional restore setting can advance the clock, with the warning that the sudden jump may itself cause issues in the guest.
Consequences in practice: TLS certificate checks fail or pass when they should not, short-lived tokens look valid when they have expired, build tools decide nothing has changed because timestamps went backwards, and logs from the first moments after resume carry the wrong time.
What to do: sync the clock as the first step after resume, before anything time-sensitive runs. Treat any credential cached before the snapshot as expired, whatever its expiry field says.
Pitfall 3: secrets get frozen into the image
Everything in memory goes into a memory snapshot. If a secret was in an environment variable, a process's heap or a file in a RAM disk when the snapshot was taken, it is now in the memory file too, for as long as that file exists. Firecracker's documentation is direct about responsibility: the host and snapshot files are trusted by Firecracker, and users need to secure snapshot files with authentication and encryption when managing them or moving them across a trust boundary.
The same applies to filesystem snapshots. A .env file or a credentials file written to disk during setup ends up in every sandbox started from that snapshot.
What to do: keep secrets out of the setup phase entirely. Inject them after restore, scoped to the run, with a short lifetime. If a snapshot was taken with a secret inside, treat the secret as exposed for as long as the snapshot exists and rotate it.
A pattern that avoids all three
The pattern that works for agent sandboxes looks like this:
- Build the environment with no secrets and no long-lived unique values: install dependencies, warm caches.
- Snapshot that clean state.
- Start each run from the snapshot as a fresh sandbox.
- After start: sync the clock, then hand the run its secrets and generate any per-run identifiers.
- Stop the sandbox when the run ends; do not snapshot a sandbox that has held secrets unless you mean to keep them.
How it works on Sandscale
On Sandscale, pausing keeps a sandbox's memory and files, and resuming brings it back where it stopped; every plan can pause and resume. Snapshots work at the filesystem level: according to the API guide, a snapshot saves the sandbox's /workspace so you can start a new sandbox from it later with snapshot_id, and snapshots use storage. Starting several sandboxes from one snapshot therefore restores files, not a frozen process, so the random-seed and clock problems above are about pause and resume rather than clones. The secret problem still applies to files: anything written into /workspace before a snapshot is in every sandbox started from it.
Secrets from Vault reach a sandbox as environment variables when it starts, and are kept out of prompts, logs and the API's answers. The vault guide shows how to reference them per sandbox, which fits step 4 of the pattern: the snapshot carries your setup, and each run gets its secrets fresh.
Originally published on the Sandscale blog.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.