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 be the object’s size in bytes and the address of its first byte. Let be the offset of an access, its distance in bytes from , and let be the width of the access in bytes. The access covers offsets to . The check refuses the access if . All three numbers are unsigned 32-bit integers: they are never negative, and the largest value they can hold is . The program, and so possibly an attacker, chooses . The question is whether the check itself can fail.
Short answer
Yes. The machine computes in 32 bits. When the true sum exceeds , 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 and . 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 : the check computes as 2, accepts the access, and the read starts 2 bytes before the object. The setting: a bounds check refuses an access of bytes at offset into a memory object of bytes that starts at address , if . All values are unsigned 32-bit integers, which run from 0 to , on a processor whose addresses are also 32 bits.
The true sum is .
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 : . This is unsigned wraparound §4.16.
The check asks whether . It is not, so the access is accepted.
The processor computes the address , again in 32 bits. Adding and discarding is the same as subtracting 2, so the address is . The 4-byte read covers to : two bytes before the object and its first two bytes.
The same mechanism in miniature: 2-bit unsigned numbers hold 0 to 3, and is stored as . A -bit unsigned sum is always computed modulo , meaning that only its remainder after division by is kept. The test is correct for true mathematical integers, but the machine evaluates a different expression whenever exceeds , and here the attacker chooses .
Give a safe offset test
Require and . Neither test forms the sum , so neither can wrap. The setting: an access of bytes at offset into a memory object of bytes, all three unsigned integers, must cover only offsets 0 to . For true mathematical integers that means .
guarantees that is at least 0, so the subtraction cannot wrap below zero. When and are fixed by the program’s design, this test can run once, before any request arrives.
states the same condition as , but it involves only numbers no larger than .
An equivalent order is first, then . The first test makes safe to compute, and the second compares the width with the number of bytes remaining from offset .
Worked with and , so that :
: , accepted; the access covers offsets 60 to 63.
: , refused; only bytes remain from offset 61.
: far greater than 60, refused, and no wrapped sum ever appears.
The test assumes that is unsigned. If arrives as a signed number, refuse negative values first, or convert 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 bytes at offset into a memory object that starts at address and holds bytes. The address form of the check computes and refuses the access if .
Wraparound. Addresses are fixed-width numbers too. On a processor with 32-bit addresses, with wraps around to . Then , which is not above , so the comparison accepts an access that starts before the object.
Undefined behavior §4.12 in C. The C standard defines only when the result points into the object or just past its last byte, and it defines a comparison with or of two pointers only when both point into the same object or just past it. Outside those cases the program has undefined behavior: the standard places no requirement on what happens. An optimizing compiler may therefore assume that pointer arithmetic §4.25 never wraps and delete a test such as
if (p + w < p)as always false, and widely used compilers do so.
The safe order is to compare integers first and form the pointer last. Start from a known object with a known size , test the integer offset and width with and , and only then compute , 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 bytes for elements of bytes each, reserves that many bytes, and then writes all elements. To keep the numbers small, let the program keep the size in an 8-bit unsigned variable, which holds 0 to 255.
With elements of bytes, the true size is bytes. In 8 bits, 320 is stored as . The program reserves 64 bytes and writes 320, so the last bytes overwrite memory that belongs to something else: a buffer overflow §4.4. The same happens at full size. In 32-bit arithmetic, elements of 8 bytes need bytes, which is stored as .
Test before multiplying. Let be the largest value the size type can hold and let . The product fits exactly when , where means rounded down to a whole number. In 8 bits, and : 31 elements need bytes, which fits, while 32 elements need 256 bytes, which already wraps to 0. So is refused. Fitting in the type is necessary but not sufficient: the count must also respect the space actually available, for example at most 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.
Signedness. The 8-bit pattern
0xFFis 255 read as unsigned and read as signed in two’s complement, the usual encoding of signed integers, in which the top bit of an 8-bit number counts as instead of , so that0xFFis . An eBPF register carries no sign; the instruction chooses.JGT(jump if greater than) compares two registers as unsigned numbers, andJSGT(jump if signed greater than) compares them as signed numbers.Extension. Widening copies a value into more bits. Zero extension §4.19 fills the new bits with 0, so
0xFFbecomes0x00000000000000FF, still 255. Sign extension fills them with copies of the top bit, so0xFFbecomes0xFFFFFFFFFFFFFFFF, still read as signed but read as unsigned. RFC 9669, the IETF specification of eBPF, fixes the choice for each instruction. An ordinary load zero-extends, a separate sign-extending load exists, and the 32-bit constant inside an instruction is a signed number, which a 64-bit operation widens by sign extension.Width of the operation. eBPF registers are 64 bits wide. The 32-bit arithmetic instructions compute in 32 bits and set the upper 32 bits of the result to zero.
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 . Computed in 64 bits, , where is the object’s start address, is , 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].