Definition
A sandbox escape is an event in which code running inside a sandbox §2.30 causes an effect that the sandbox’s policy forbids, such as reading or writing host memory outside its permitted regions, running host code it may not call, or using an object after its permission ended. A sandbox is an environment that runs untrusted code while limiting its effects. An escape needs a path to a forbidden effect that is unchecked or checked wrongly. The term names the broken limit, not a symptom: an escape may crash nothing.
The idea
A sandbox’s fence is a set of checks that people wrote, in the virtual machine §2.3 (VM) that runs the code, in its loader and in its helper functions §2.19. The author of the guest program controls every value those checks see: every register, offset and argument. An attacker therefore looks for one value that a check misjudges, or one path that no check guards. One mistake is enough, because the attacker can produce the triggering value on every run.
Picture a prison with sound walls and a laundry van that leaves through the gate every day. Nobody climbs the wall if the guard waves the van through unopened. In software the van is a helper function: native host code acting for the guest with the host’s full access. When it trusts an address or length that the guest supplied, the guest has it make the forbidden write; this pattern is a confused deputy §1.25. The picture stops being accurate in one respect: software can apply one exact check to every van, and showing that every route passes such a check is what makes a sandbox trustworthy.
How it works
Escapes follow a few routes, each breaking an assumption that a runtime bounds check §2.27 or the sandbox relies on.
Arithmetic that wraps. A check adds an offset and a length in fixed-width unsigned integers; the true sum is too large, wraps to a small number (unsigned modular arithmetic §4.16) and passes. A related slip mixes signed and unsigned values: a length of is small as a signed number but enormous when used as an unsigned size.
Checking one value, using another. The check tests the base register while the access adds an offset; or a helper checks a value in memory that the guest changes before the helper uses it, a time-of-check to time-of-use §9.26 (TOCTOU) error.
A path with no check. An atomic instruction, a rare load size, code produced by a just-in-time (JIT §2.14) compiler, or a helper that receives a pointer is never tested. Complete mediation §1.16 requires a check on every path.
A helper that trusts the guest writes through a guest-supplied pointer, or uses a guest-supplied length, without testing the whole span.
A pointer that exceeds or outlives its object: a helper returns a host pointer, and the VM grants more bytes than the object has, or keeps the grant after the object is freed.
The VM’s own state: an interpreter §2.13 keeps the guest’s registers in a host array §14.5, and a register number beyond the array, if the loader let it through, reaches host memory.
A verifier that disagrees with the engine. A static verifier proves bounds with its own model of each instruction. If that model, or its tracking of value ranges, differs from what the engine does, the proof covers a different program. Such errors in the Linux eBPF §2.1 (extended Berkeley Packet Filter) verifier have repeatedly let accepted programs read and write kernel memory.
Real attacks often chain escapes: an attack on a web browser may first take over the process that renders a page and then escape the operating-system sandbox around that process.
Worked example
Wraparound in a helper. On a host with 32-bit addresses, a helper copies guest data into a 256-byte host buffer at address . The guest chooses an offset and a length , both 32-bit unsigned, and the helper refuses if . The guest passes 0xFFFFFFF0 and . The true sum, , is stored as 16, and is false, so the helper accepts. Its 32-bit address addition also wraps, to . The 32 bytes land at through , and the first 16 overwrite host memory just below the buffer. Nothing crashes. An overflow-safe range check §4.17 tests first, which fails here; for an offset that passes, it then tests , which cannot wrap.
A pointer returned by a helper. A helper finds one 8-byte entry in a host table of 64 entries, bytes in all, and returns the entry’s address . If the VM then permits the whole table, the guest can also read the other bytes, which belong to other users, although every access stays in a permitted region. If the VM permits exactly the 8 bytes to but keeps the permission after an update deletes the entry and reuses its memory, the guest reads the new occupant: a temporal escape. The correct grant is those 8 bytes for the entry’s lifetime only; a helper that copies the entry into guest memory avoids handing out at all.
Stated precisely
Let be the sandbox’s policy, the set of effects that the guest may cause. A run escapes when it causes an effect outside . Demonstrating an escape takes four facts: a guest program the loader accepts, an input, the path through the VM or a helper by which a run reaches a native effect, and the fact that this effect is outside .
A sandbox has no escape when, for every accepted program and every input, every native effect of every run is in . That holds when two conditions hold together.
Complete mediation: every path from the guest to a protected effect, through any instruction, compiled code or helper, passes a check.
Correct checks: each check tests the exact address and width that the access uses, with arithmetic that cannot wrap, against the bounds of the object the guest was given, during that object’s lifetime.
Both conditions assume that the trusted code, meaning the VM, loader and helpers, has no other memory error of its own.
Limits and common mistakes
A fault that the VM catches and reports is the sandbox working: a guest that tries an out-of-bounds access and is stopped has not escaped.
A crash is a symptom, not a verdict. A host crash caused by an out-of-bounds write §4.4 is evidence of an escape; a slowdown caused by using resources that the policy allowed is a weakness of the policy. The dangerous escapes are silent, so testing that watches only for crashes misses them.
One defense does not cover another route. A helper allowlist §2.20 decides which helpers can be called, not whether an allowed helper handles its arguments safely.
On processors that execute speculatively, guessing a branch’s outcome and running ahead before it is known, a correct bounds check can be passed briefly. The out-of-bounds load runs during the guess and leaves a trace in the cache that the attacker can time. This is Spectre variant 1, also called bounds check bypass: a side channel §1.29 that leaks data but writes nothing. Defenses either force the index into range with arithmetic, so that even a guessed load stays in bounds, or insert an instruction that stops the processor from running ahead past the check.
Questions answered
Can integer wraparound or a helper pointer bypass region checks?
Yes, both can bypass region checks. Integer wraparound defeats a check that adds an offset and a length in fixed-width unsigned arithmetic. With 32-bit values, offset 0xFFFFFFF0 plus length 32 is , stored as 16, which passes “offset plus length at most 256” for a 256-byte buffer, and the copy then lands 16 bytes before the buffer because the address addition wraps the same way. The repair is to test the offset against the size first, then the length against the size minus the offset.
A helper pointer bypasses region checks because helper functions are native host code outside the VM’s per-instruction checks. A helper that writes through a guest-supplied pointer without testing the whole span writes wherever the guest points it. A helper that returns a host pointer can lead the VM to grant more bytes than the object has, or to keep the grant after the object’s memory is reused. The repairs: every helper tests every pointer and length it receives against the guest’s permitted regions, and a returned pointer grants exactly the object’s bytes for exactly the object’s lifetime, or the helper copies the data instead.
Related entries: Runtime bounds check (§2.27), Helper contract (§2.21), Trust boundary (§1.3), Sandbox (§2.30), Confused deputy (§1.25), Overflow-safe range check (§4.17), Complete mediation (§1.16).