Section 2.27

Runtime bounds check

Definition

A runtime bounds check is a test that the software running a program, such as a virtual machine’s §2.3 interpreter §2.13, makes just before each of the program’s memory accesses, to confirm that every byte the access touches lies in a region the program may use. Its inputs are the address, the number of bytes the access moves, and the start and length of each permitted region. Its output is allow or refuse; a refused access does not happen. A static check, such as a verifier, decides before the run instead.

The idea

A program in a virtual machine (VM), a computer defined in software, computes most of its addresses while it runs. In eBPF §2.1 (extended Berkeley Packet Filter), a load reads from the address in a register plus a constant offset, and the register may come from the program’s input or from arithmetic nobody foresaw. The VM itself is an ordinary program running on the host processor, so when it carries out the load it reads real host memory. Without a test, a program that sets the register to the address of a host secret reads that secret.

A runtime bounds check tests at the one moment when the address is certainly known: just before the access. A ticket inspector at a door is a fair picture: nobody predicts in advance who will come, because each person is checked on entry. The picture stops being accurate in one respect. A ticket admits a whole person, while an access covers several bytes, and the check must cover every one of them: a 4-byte read that starts at the last byte of a region touches 3 bytes beyond it.

How it works

An eBPF interpreter, the part of the VM that carries out one instruction after another, handles a load or store in five steps.

  1. Effective address. Compute the effective address aa, the address the instruction actually accesses: the base register’s value plus the instruction’s signed 16-bit offset, sign-extended to 64 bits so that a negative offset stays negative.

  2. Width. Read the width ww, the number of bytes moved: 1, 2, 4 or 8.

  3. Test. Find a permitted region, with start ss and length LL in bytes, that allows this kind of access (read or write) and contains the bytes aa to a+w−1a + w - 1.

  4. Refuse if no region passes: stop the program with a fault, a defined error result, without touching memory. The fault-to-verdict mapping §2.34 decides what the fault means for the host.

  5. Access otherwise, with exactly the address and width that were tested.

Step 3 is where mistakes happen. The obvious test is a≥sa \ge s and a+w≤s+La + w \le s + L. But an unsigned integer of nn bits keeps only the remainder after division by 2n2^{n}, so a sum above 2n−12^{n} - 1 wraps around to a small number (unsigned modular arithmetic §4.16), and a small wrapped end passes. The safe test never forms an end address: w≤Lands≤aanda−s≤L−w,w \le L \quad\text{and}\quad s \le a \quad\text{and}\quad a - s \le L - w, evaluated from left to right and stopped at the first comparison that fails. Each subtraction is then computed only after the comparison before it has shown its result is not negative, so nothing wraps. The overflow-safe range check §4.17 entry derives this form.

Forming the effective address can wrap too. eBPF registers are 64 bits wide, while many microcontroller §6.1 hosts have 32-bit addresses. Adding in 32 bits turns a register value of 8 and an offset of −16-16 into 232−82^{32} - 8, the address 0xFFFFFFF8; adding in 64 bits gives 264−82^{64} - 8. Widening does not avoid the wrap, because a register can already hold any 64-bit value. The VM need not prevent this wrap, only make it harmless, by two rules. First, compute the effective address once and give that same value to the test and to the access. Second, test it with the subtraction form, which gives the right answer for every value of aa, wrapped or not. A wrapped address is then judged like any other: the access is refused unless the bytes aa to a+w−1a + w - 1 lie inside a permitted region. A wrapped address can land inside one: the register value 264−4,0962^{64} - 4{,}096 plus the offset 8,192 wraps to a=4,096a = 4{,}096, and if a permitted region starts at address 4,096 and is at least ww bytes long, the access passes, and correctly so, because it touches only memory the program may use. Convert a 64-bit address to a 32-bit host address only after the test passes; the value is then below 2322^{32}, and nothing is lost.

In C, the language most VMs are written in, compute the candidate address with unsigned integers, not pointers. Moving a pointer outside its object is undefined behavior §4.12, so a compiler may assume it never happens and delete a test such as “is p+wp + w below pp”.

Worked example

A guest has one permitted region: a 16-byte input at s=s = 0x20000000, so L=16L = 16 and the valid addresses are 0x20000000 to 0x2000000F. The host has 32-bit addresses.

In 64 bits, a=264−8a = 2^{64} - 8, the obvious test wraps again because a+8=264a + 8 = 2^{64} is stored as 0, and the safe test again refuses. Wider arithmetic did not repair the obvious test; the subtraction form repaired both.

Stated precisely

Let aa be the effective address exactly as the access will use it, w≥1w \ge 1 the width in bytes, and let a region start at address ss and be LL bytes long, with s+Ls + L not beyond the end of the address space. Over the mathematical integers, the access lies inside the region exactly when s≤as \le a and a+w≤s+La + w \le s + L. In unsigned integers wide enough to hold aa, ss, LL and ww, the test w≤Lw \le L, then s≤as \le a, then a−s≤L−wa - s \le L - w, stopped at the first failure, gives the same answer for every value and never wraps.

Passing the test keeps the access in memory the program may use under four assumptions.

  1. Correct regions: each listed region is memory the program may use for as long as it stays listed.

  2. Same value: the access uses the tested aa and ww.

  3. Every path: every way of touching memory for the program passes the test, including atomic instructions, code from a just-in-time (JIT §2.14) compiler and helper functions §2.19 that receive addresses; this is complete mediation §1.16.

  4. No change in between: nothing alters the regions or the address between test and access, which would be a time-of-check to time-of-use §9.26 (TOCTOU) error.

Limits and common mistakes

Testing only the start address misses accesses that cross the region’s end.

A region check confines the program to a region, not each object to its own bytes, as the spatial memory safety §2.28 entry shows. Nor does it know about time: if a freed object’s memory stays in a permitted region, an old address still passes, which the temporal memory safety §2.29 entry explains.

In bounds is not aligned. On processors that require alignment §4.22, such as the Arm Cortex-M0, a 4-byte access at an address that is not a multiple of 4 causes a hardware fault although every byte is permitted. A VM on such a host must also test alignment, or move the bytes one at a time.

Every access pays a few comparisons. A static verifier, such as the eBPF verifier §3.3, can prove accesses in bounds before the run and drop the test. Software fault isolation §2.41 instead forces each address into the region, for example by masking its high bits, so a bad address is redirected rather than refused.

The interpreter’s test covers only instructions the interpreter carries out. A helper function is native host code and must test every address and length it receives.

Questions answered

How do you avoid overflow in address plus length?

Avoid overflow in address plus length by never computing the end address. For an address aa, a length ww and a permitted region with start ss and length LL, check w≤Lw \le L, then s≤as \le a, then a−s≤L−wa - s \le L - w. Each subtraction comes after a comparison that shows its result cannot be negative, so unsigned arithmetic never wraps. A wider integer type alone is not enough: a 64-bit eBPF (extended Berkeley Packet Filter) register can hold any value, so even 64-bit sums wrap. For a 16-byte region at 0x20000000 on a 32-bit host, an 8-byte access at 0xFFFFFFF8 gives a+w=232a + w = 2^{32}, stored as 0, which passes the test “end at most s+Ls + L”; the subtraction form finds a−s=3,758,096,376>8a - s = 3{,}758{,}096{,}376 > 8 and refuses. Finally, compute the address once and access exactly the address and width that passed.

Related entries: Overflow-safe range check (§4.17), Spatial memory safety (§2.28), Temporal memory safety (§2.29), Sandbox escape (§2.31), Unsigned modular arithmetic (§4.16), Software fault isolation (SFI) (§2.41).