Section 20.5

Tree 5: “Can one prove that a read stays inside its object?”

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. The gateway’s own software, called the host, carries out the instructions in a virtual machine §2.3 (VM) and offers the program helper functions §2.19, host routines that the program may call.

For one request the host hands the program a memory object: a block of bytes with a known start and size. Let n=64n = 64 be its size in bytes. Its bytes are numbered by offset, the distance in bytes from the object’s start, from 0 to 63. Let base\mathrm{base} be the address of the object’s first byte; base\mathrm{base} is a multiple of 4, which is called 4-byte aligned. The program wants to read a 4-byte value that starts at offset oo. Let w=4w = 4 be the width of the read in bytes. The value oo comes from the request, so an attacker may choose it. The program holds oo as an unsigned number, one that is never negative. The program runs these steps:

  1. if o>60o > 60, stop and answer DENY;

  2. if oo is not a multiple of 4, stop and answer DENY;

  3. otherwise read the 4 bytes at addresses base+o\mathrm{base} + o to base+o+3\mathrm{base} + o + 3.

Four facts about the object come from the host, and the arithmetic below cannot establish them: the object really is 64 bytes long; it exists for the whole read and has not been freed, meaning given back for reuse; all 64 bytes hold values that were written to them, which is called initialized; and the program is allowed to read it. The question asks for a proof that the read stays inside the object; the first follow-up question works through that proof, and the others change the premises one at a time.

Short answer

Every offset oo that passes both checks gives a read inside the object, at an aligned address. Because oo is unsigned, o≥0o \ge 0. The first check leaves o≤60o \le 60, so the last byte read, at offset o+3o + 3, is at most 63, the object’s last offset. The second check makes oo a multiple of 4, and base\mathrm{base} is one, so their sum is one too. The proof covers only the read’s position, and it relies on the object being 64 bytes long, live, initialized and readable.

Follow-up questions

Prove the range

The read covers offsets oo to o+3o + 3, and the two checks together force o≥0o \ge 0 and o+3≤63o + 3 \le 63, so every byte read lies inside the object. Here the object is a 64-byte block of memory that a device’s own software handed to a downloaded program, with bytes numbered by offset from 0 to 63, starting at an address base\mathrm{base} that is a multiple of 4. The offset oo is an unsigned number chosen by whoever sent the request. The program reads 4 bytes starting at offset oo only if o≤60o \le 60 and oo is a multiple of 4.

  1. Lower end. oo is unsigned, so o≥0o \ge 0, and the first byte read, at offset oo, is at or after offset 0.

  2. Upper end. The first check refuses every o>60o > 60, so a read happens only when o≤60o \le 60. The last byte read is at offset o+3≤60+3=63o + 3 \le 60 + 3 = 63, the object’s last offset. Because o+3o + 3 is at most 63, computing it cannot exceed the largest number the machine can hold.

  3. Boundary case. At o=60o = 60 the read covers offsets 60, 61, 62 and 63. Written as a half-open range §14.12, which includes its start and excludes its end, the read covers [60,64)[60, 64) and the object covers [0,64)[0, 64): the read’s end, 60+4=6460 + 4 = 64, equals the object’s end. At o=61o = 61 the program refuses before reading, because 61>6061 > 60. The read would have needed offset 61+3=6461 + 3 = 64, which is not part of the object.

  4. Alignment §4.22. The second check leaves only the multiples of 4 from 0 to 60, which is 60/4+1=1660 / 4 + 1 = 16 offsets. A sum of two multiples of 4 is a multiple of 4, so base+o\mathrm{base} + o is 4-byte aligned. For o=60=15×4o = 60 = 15 \times 4, the read starts at base+60\mathrm{base} + 60, an aligned address. Alignment matters because some processors refuse, or perform more slowly, a 4-byte read from an address that is not a multiple of 4.

The general rule behind the bound: a read of ww bytes at offset oo in an object of nn bytes stays inside when o≥0o \ge 0 and o+w≤no + w \le n. With n=64n = 64 and w=4w = 4 that is o≤60o \le 60. The arithmetic is only half of the proof. The other half is four facts that the host must supply and no check on oo can establish: the object really is 64 bytes long, it still exists for the whole read, all its bytes have been written, and the program may read it.

What if the region is only 32 bytes?

Then the bound must become 28, and a check that still says 60 accepts reads beyond the end of the object. The setting: a downloaded program reads 4 bytes starting at an attacker-chosen unsigned offset oo in a memory object whose bytes are numbered from 0, and it refuses any oo above a bound and any oo that is not a multiple of 4. For an object of nn bytes and a read of ww bytes, the largest safe start is n−wn - w. With n=32n = 32 and w=4w = 4, that is 32−4=2832 - 4 = 28: offset 28 reads offsets 28 to 31, the last 4 bytes of the object.

If the program keeps the old check o≤60o \le 60, it accepts the multiples of 4 from 32 to 60, which is (60−32)/4+1=8(60 - 32) / 4 + 1 = 8 offsets that start at or beyond the object’s end. At o=60o = 60 it reads offsets 60 to 63, which lie 29 to 32 bytes past the object’s last byte at offset 31, in memory that belongs to something else. With the correct bound 28, offset 32 is refused although it is a multiple of 4, because 32>2832 > 28. Alignment says nothing about size.

The repair is to derive the bound from the size of the object actually being read, not from a constant written for an earlier object. The host should pass the size nn along with the object, or the verifier, a checker that examines the program before it runs, must know it. The bound n−wn - w must itself be computed safely. If ww could exceed nn, the unsigned subtraction n−wn - w would wrap around to a huge number, so the program checks w≤nw \le n first.

What if oo is signed?

Then o=−4o = -4 passes both checks, and the program reads the 4 bytes just before the object. The setting: a downloaded program reads 4 bytes at offset oo in a 64-byte memory object that starts at address base\mathrm{base}, and it refuses the read if o>60o > 60 or if oo is not a multiple of 4. A signed number can be negative. With o=−4o = -4, the test −4>60-4 > 60 is false, so the first check passes. Since −4=−1×4-4 = -1 \times 4, it is a multiple of 4, and in C the remainder -4 % 4 is 0, so the second check passes. The read covers addresses base−4\mathrm{base} - 4 to base−1\mathrm{base} - 1, all outside the object.

The single upper check was enough before only because an unsigned oo is never below 0. A signed oo needs both ends checked: 0≤o≤600 \le o \le 60. One unsigned comparison §2.12 can test both ends at once. Converting a signed 32-bit value to unsigned keeps its bits and reads them as a non-negative number, and every negative value then reads as at least 231=2,147,483,6482^{31} = 2{,}147{,}483{,}648. The value −4-4 reads as 232−4=4,294,967,2922^{32} - 4 = 4{,}294{,}967{,}292, which is greater than 60, so the read is refused. In eBPF, the instruction chooses, not the value: a register holds a plain 64-bit pattern, JGT (jump if greater than) compares two such patterns as unsigned numbers, and JSGT (jump if signed greater than) compares them as signed numbers. The check o>60o > 60 written with JSGT lets −4-4 through. Written with JGT, it sees −4-4 as 264−42^{64} - 4 and refuses it.

Whatever form the check takes, it must test the very number that later forms the address, at the same width. A program that tests only the low 32 bits of a 64-bit register and then adds the whole register to base\mathrm{base} has proved a fact about a different number. A register holding 232+42^{32} + 4 has low 32 bits equal to 4, which passes, and the address is then computed from a number 2322^{32} larger than the one tested.

What if an intervening helper frees the region?

Then the read is a use-after-free §4.6: the arithmetic still passes, but the object it was about no longer exists. The setting: a downloaded program checks that an offset oo satisfies o≤60o \le 60 and is a multiple of 4, so that a 4-byte read at oo stays inside a 64-byte memory object that the host, the device’s own software, handed it. Between the check and the read, the program calls a helper function, a host routine the program may call, and the helper frees the object, meaning it gives the memory back to the host for reuse.

With o=4o = 4, the check 4≤604 \le 60 still holds, and the address base+4\mathrm{base} + 4 is still computed correctly, where base\mathrm{base} is the address where the object started. But the premise that the object exists for the whole read is now false. The 4 bytes at base+4\mathrm{base} + 4 may already belong to another object, perhaps one holding a key or the next request. Reading them can leak that data, and writing them would corrupt it. The range check established spatial memory safety §2.28: the access falls within the object’s extent. What fails here is temporal memory safety §2.29: the access must happen while the object exists. No range check, however correct, can detect this failure.

Three repairs exist, all in the host.

What if the output is wrong?

Then the program is memory-safe and still wrong: a read can stay inside its object and fetch the wrong field, or fetch the right bytes and combine them in the wrong order. The setting: a downloaded program reads values from a 64-byte memory object that the device’s own software handed it, at offsets it has checked, and answers ALLOW or DENY from those values. Suppose offsets 8 and 9 of the object hold the bytes 0x01 and 0x02, a 16-bit field, and the rule is to deny any value above 300.

Both reads touch only offsets 8 and 9, inside the object, and the bounds proof holds for both. Only one of them matches the format the sender used. A read at offset 10 instead of 8 would also be in bounds and would fetch a different field entirely.

Memory safety and functional correctness are separate properties. Memory safety says that no access leaves its object. Functional correctness says that the program computes what its specification requires. Establishing it needs a written specification of each field’s offset, width and byte order and of the verdict rule, and a test or proof against that specification. A bounds proof supplies none of these.

Related entries: Use-after-free (§4.6), Overflow-safe range check (§4.17), Alignment (§4.22), Endianness (§4.21), Spatial memory safety (§2.28), Temporal memory safety (§2.29), Signed versus unsigned comparison (§2.12), Half-open interval (§14.12), Security versus functional correctness (§1.35), Helper contract (§2.21), Tree 6: “Can a bounds check itself overflow?” (§20.6). Further reading: [160].