Section 20.6

Tree 6: “Can a bounds check itself overflow?”

Setting

A sensor gateway is a small device that collects readings from sensors and passes them on. The gateway in this invented example runs a small decision program, delivered through a phone from a publisher, the one party authorized to change the gateway’s policy. The program is eBPF §2.1 bytecode §2.4: instructions for a processor that exists only as a definition, not as hardware. The name eBPF began as an abbreviation of extended Berkeley Packet Filter and is no longer treated as an acronym. Part of the gateway’s software, the virtual machine §2.3 (VM), carries out the instructions. Before each memory access of the program, the VM checks the access against the memory object being read, a block of memory with a known start and size. The gateway’s processor works with 32-bit numbers and 32-bit addresses, and the check is written in C.

Let n=64n = 64 be the object’s size in bytes and base\mathrm{base} the address of its first byte. Let oo be the offset of an access, its distance in bytes from base\mathrm{base}, and let w=4w = 4 be the width of the access in bytes. The access covers offsets oo to o+w−1o + w - 1. The check refuses the access if o+w>no + w > n. All three numbers are unsigned 32-bit integers: they are never negative, and the largest value they can hold is 232−1=4,294,967,2952^{32} - 1 = 4{,}294{,}967{,}295. The program, and so possibly an attacker, chooses oo. The question is whether the check itself can fail.

Short answer

Yes. The machine computes o+wo + w in 32 bits. When the true sum exceeds 232−12^{32} - 1, the stored result wraps around to a small number, and the check accepts an offset billions of bytes past the object. An overflow-safe range check §4.17 never forms the sum: it requires w≤nw \le n and o≤n−wo \le n - w. Multiplication in size calculations wraps the same way, and every check must test the very value later used, at the same width and signedness.

Follow-up questions

Show the counterexample

Take o=232−2=4,294,967,294o = 2^{32} - 2 = 4{,}294{,}967{,}294: the check computes o+4o + 4 as 2, accepts the access, and the read starts 2 bytes before the object. The setting: a bounds check refuses an access of w=4w = 4 bytes at offset oo into a memory object of n=64n = 64 bytes that starts at address base\mathrm{base}, if o+w>no + w > n. All values are unsigned 32-bit integers, which run from 0 to 232−1=4,294,967,2952^{32} - 1 = 4{,}294{,}967{,}295, on a processor whose addresses are also 32 bits.

  1. The true sum is 4,294,967,294+4=4,294,967,2984{,}294{,}967{,}294 + 4 = 4{,}294{,}967{,}298.

  2. That sum does not fit in 32 bits. Unsigned 32-bit arithmetic, in C and on the processor, keeps only the remainder after division by 232=4,294,967,2962^{32} = 4{,}294{,}967{,}296: 4,294,967,298−4,294,967,296=24{,}294{,}967{,}298 - 4{,}294{,}967{,}296 = 2. This is unsigned wraparound §4.16.

  3. The check asks whether 2>642 > 64. It is not, so the access is accepted.

  4. The processor computes the address base+o\mathrm{base} + o, again in 32 bits. Adding 232−22^{32} - 2 and discarding 2322^{32} is the same as subtracting 2, so the address is base−2\mathrm{base} - 2. The 4-byte read covers base−2\mathrm{base} - 2 to base+1\mathrm{base} + 1: two bytes before the object and its first two bytes.

The same mechanism in miniature: 2-bit unsigned numbers hold 0 to 3, and 3+1=43 + 1 = 4 is stored as 4−4=04 - 4 = 0. A kk-bit unsigned sum is always computed modulo 2k2^{k}, meaning that only its remainder after division by 2k2^{k} is kept. The test o+w>no + w > n is correct for true mathematical integers, but the machine evaluates a different expression whenever o+wo + w exceeds 232−12^{32} - 1, and here the attacker chooses oo.

Give a safe offset test

Require w≤nw \le n and o≤n−wo \le n - w. Neither test forms the sum o+wo + w, so neither can wrap. The setting: an access of ww bytes at offset oo into a memory object of nn bytes, all three unsigned integers, must cover only offsets 0 to n−1n - 1. For true mathematical integers that means o+w≤no + w \le n.

  1. w≤nw \le n guarantees that n−wn - w is at least 0, so the subtraction cannot wrap below zero. When ww and nn are fixed by the program’s design, this test can run once, before any request arrives.

  2. o≤n−wo \le n - w states the same condition as o+w≤no + w \le n, but it involves only numbers no larger than nn.

An equivalent order is o≤no \le n first, then w≤n−ow \le n - o. The first test makes n−on - o safe to compute, and the second compares the width with the number of bytes remaining from offset oo.

Worked with n=64n = 64 and w=4w = 4, so that n−w=64−4=60n - w = 64 - 4 = 60:

The test assumes that oo is unsigned. If oo arrives as a signed number, refuse negative values first, or convert oo to unsigned before the test and use that same converted value for the address. The tested value and the used value must be the same number at the same width.

Is address comparison enough?

No: comparing addresses means forming an address before knowing that it is valid, and both the machine and the C language can make that comparison meaningless. The setting: a bounds check for an access of ww bytes at offset oo into a memory object that starts at address base\mathrm{base} and holds nn bytes. The address form of the check computes p=base+op = \mathrm{base} + o and refuses the access if p+w>base+np + w > \mathrm{base} + n.

The safe order is to compare integers first and form the pointer last. Start from a known object with a known size nn, test the integer offset and width with w≤nw \le n and o≤n−wo \le n - w, and only then compute base+o\mathrm{base} + o, which the tests have shown points into the object. Code that must compare raw addresses, such as a memory manager that works with fixed address ranges, has to state and justify its own model of addresses, for example by converting them to unsigned integers as wide as an address and showing that no sum it forms can wrap.

What about multiplication?

Multiplication wraps too, and a wrapped size calculation is a classic cause of a buffer that is too small for the data written into it. The setting: a program computes the size of a buffer, a region of memory, as c×sc \times s bytes for cc elements of ss bytes each, reserves that many bytes, and then writes all cc elements. To keep the numbers small, let the program keep the size in an 8-bit unsigned variable, which holds 0 to 255.

With c=40c = 40 elements of s=8s = 8 bytes, the true size is 40×8=32040 \times 8 = 320 bytes. In 8 bits, 320 is stored as 320−256=64320 - 256 = 64. The program reserves 64 bytes and writes 320, so the last 320−64=256320 - 64 = 256 bytes overwrite memory that belongs to something else: a buffer overflow §4.4. The same happens at full size. In 32-bit arithmetic, c=536,870,913c = 536{,}870{,}913 elements of 8 bytes need 4,294,967,3044{,}294{,}967{,}304 bytes, which is stored as 4,294,967,304−4,294,967,296=84{,}294{,}967{,}304 - 4{,}294{,}967{,}296 = 8.

Test before multiplying. Let MM be the largest value the size type can hold and let s>0s > 0. The product c×sc \times s fits exactly when c≤⌊M/s⌋c \le \lfloor M / s \rfloor, where ⌊x⌋\lfloor x \rfloor means xx rounded down to a whole number. In 8 bits, M=255M = 255 and ⌊255/8⌋=31\lfloor 255 / 8 \rfloor = 31: 31 elements need 31×8=24831 \times 8 = 248 bytes, which fits, while 32 elements need 256 bytes, which already wraps to 0. So c=40c = 40 is refused. Fitting in the type is necessary but not sufficient: the count must also respect the space actually available, for example at most ⌊128/8⌋=16\lfloor 128 / 8 \rfloor = 16 elements if only 128 bytes are free. The C23 revision of the C standard adds ckd_mul in the header stdckdint.h, which computes a product and reports whether it wrapped.

Why do signedness and width matter to a VM?

Because the same bits mean different numbers depending on width and signedness, and every part of a virtual machine (VM) must give them the same meaning, or a proof about one number protects a different one. A VM here is the software that carries out bytecode, instructions for a processor that exists only as a definition. Its parts are a verifier, the checker that examines the program beforehand and records the possible range of each value; an interpreter §2.13, which performs the instructions one at a time; and possibly a just-in-time (JIT §2.14) compiler, which translates them into the processor’s own machine code. The bytecode here is eBPF.

A mismatch breaks the proof. Suppose the verifier treats a 1-byte load as zero-extending, records the range 0 to 255 for the loaded register, and therefore accepts a 1-byte read at that offset into a 256-byte object. If the interpreter sign-extends instead, the byte 0xFF becomes 264−12^{64} - 1. Computed in 64 bits, base+264−1\mathrm{base} + 2^{64} - 1, where base\mathrm{base} is the object’s start address, is base−1\mathrm{base} - 1, one byte before the object, although the verifier’s reasoning was right for the instruction meaning it assumed. An interpreter written in C can make this mistake silently: assigning a signed 8-bit C variable to an unsigned 64-bit one sign-extends, while assigning an unsigned 8-bit variable zero-extends. Every part of the VM must follow the specification’s choice for each instruction, and a value tested at one width and signedness must be used at that same width and signedness.

Related entries: Interpreter semantic fidelity (§2.38), Overflow-safe range check (§4.17), Integer promotion and conversion (§4.18), Unsigned modular arithmetic (§4.16), Truncation and extension (§4.19), Pointer arithmetic (§4.25), Buffer overflow (§4.4), Undefined behavior (UB) (§4.12), ALU32 versus ALU64 (§2.11), Signed versus unsigned comparison (§2.12), Tree 5: “Can one prove that a read stays inside its object?” (§20.5). Further reading: [78, 160].