Setting
A sensor gateway is a small device that collects readings from sensors and passes them on. The gateway in this invented example also runs a small decision program from an authorized publisher, delivered through a phone that only carries the bytes. The program is eBPF §2.1 bytecode §2.4: instructions for a processor that exists only as a definition, which the gateway’s software carries out one at a time. The program keeps its working values in registers, the numbered storage locations of that processor. The name eBPF began as an abbreviation of extended Berkeley Packet Filter and is no longer treated as an acronym.
Before the gateway runs a new program, a verifier examines it. A verifier is a checker that reads a program without running it and accepts the program only if it can show that the program obeys stated safety rules on every possible run. The rule here is memory safety: every byte the program reads or writes lies inside a memory object, meaning a block of memory with a known start and length, that the program is allowed to use and that still exists at the moment of the access.
Each request reaches the program as a 16-byte record with bytes at offsets 0 to 15, an offset being a distance in bytes from the start of the record. One byte of the record is an index chosen by whoever sent the request, possibly an attacker. Read as an unsigned number, that is, one that is never negative, can be any value from 0 to 255. The program performs one 4-byte load, meaning that it copies 4 bytes of memory into a register: the bytes at offsets to of the record. The verifier runs before any request exists, so it cannot know which will arrive.
Short answer
A verifier establishes memory safety by reasoning about every possible run of a program before the program runs. At each instruction it keeps a summary of the values each register could hold there, such as “this offset is between 0 and 12”. It accepts a memory access only if every value in the summary keeps the whole access inside a valid object, and it accepts the program only if every access passes. The resulting proof is only as good as the verifier itself and the software that executes the program.
Follow-up questions
First answer
A verifier, a checker that reads a program before it runs, establishes memory safety by checking the program against every possible input at once, not against one input at a time. Memory safety means that every byte the program reads or writes lies in memory it is allowed to use. At each instruction the verifier holds a summary of every value that could be present there, and it accepts a memory access only when every value in the summary is safe.
Take a program that reads 4 bytes from a 16-byte record at offset , a distance in bytes from the start of the record. The sender of a request chooses as one byte, so can be anything from 0 to 255. The record has offsets 0 to 15 and the read touches offsets to , so the read fits exactly when , that is, when .
Version A of the program rejects the request when . On the path that continues, the verifier narrows its summary of from “0 to 255” to “0 to 12”. The worst case, , reads offsets 12 to 15. All 13 remaining values fit, so the verifier accepts the read.
Version B has no comparison, so the summary stays “0 to 255”. The value would read offsets 13 to 16, and offset 16 does not exist. The verifier rejects the program, although 13 of the 256 values would have been harmless.
A test that sends passes on both versions. It shows that one value is safe; the summary shows whether all 256 are. Where two paths through a program meet, a verifier either follows each path separately or merges their summaries, and a merged summary must contain every value from both paths. A loop brings a path back to an earlier instruction. A verifier then forbids loops, follows each pass of the loop up to a limit, or finds a summary that still holds after every pass. Acceptance is a proof about the bytes that were checked. It protects the device only if those same bytes run and the software that executes them gives each instruction the meaning the verifier assumed.
What facts matter?
Seven facts about each memory access matter, and a number that lies in range is only one of them. A verifier, a checker that examines a program before it runs, must establish all seven before it accepts the access. In the list, the offset of an access is the distance in bytes from the start of the object to the first byte accessed.
A real object. The address is built from a pointer, a value that holds the address of a known memory object, and not from an arbitrary number.
A live object. The object has not been freed, that is, given back for reuse.
Permission. The program may perform this kind of access. A region can be readable without being writable.
The offset and how it is read. The verifier needs the range of the offset, and it needs to know whether the bits of the offset are treated as a signed or an unsigned number. The byte with all eight bits set, written
0xFFin hexadecimal, is 255 as an unsigned number and as a signed one in two’s complement, the encoding of signed numbers that processors normally use. A signed comparison §2.12 accepts as “at most 12”, although an offset of lies before the start of the object.The full width. Every byte of the access, from the first to the last, lies inside the object. In a 16-byte record, with offsets 0 to 15, a 4-byte read at offset 12 covers offsets 12 to 15 and fits. The same read at offset 13 covers offsets 13 to 16. Its first byte is inside the record and its last byte is not, so it is an out-of-bounds read §4.5.
Initialization. The bytes that are read were written earlier, so the program cannot see what an earlier user of the memory left behind.
Alignment §4.22. On a system that demands it, the address of the access is a multiple of a required number, typically the size of the value: a multiple of 4 for a 4-byte value.
The first three facts can fail while every number stays right. Suppose the program calls a helper function §2.19, a routine of the device’s own software that is offered to the program, and the helper frees the record. An offset of 12 is still in range, but the read now reaches memory that no longer belongs to the record, an error called a use-after-free §4.6. The bound is a fact about numbers. Whether the record still exists is a fact about object lifetime §4.1, and it must be established separately.
What is the soundness §3.7 obligation?
At every point of a program, the summary of possible machine states kept by a verifier, a checker that examines the program before it runs, must include every state that the real machine can be in at that point. A machine state is the content of the memory and of every register, a storage location of the processor, at one moment. The summary may contain extra states that never occur, but it may never leave out a real one. A verifier that meets this obligation at every point, and accepts a memory access only if every state in its summary passes the rule, is sound: it never accepts a program that can break the rule being checked.
Stated precisely: let be the summary before an instruction, and let be the set of real machine states that stands for. Let be the set of states the machine reaches by executing the instruction once from each state in a set , and let be the summary that the verifier computes for the point after the instruction. The obligation is where means that every member of the set on its left is also a member of the set on its right. If the summary at the first instruction covers every state in which the program can start, the inclusion holds for every instruction, and no case is dropped where paths meet, then the summaries cover every state the machine reaches, one step after another. The inclusion comes from abstract interpretation §3.4, the theory of such summaries, which Patrick Cousot and Radhia Cousot introduced in a paper of 1977. The paper states the inclusion for each basic instruction and calls it local consistency.
Two mistakes break the obligation.
Dropping a case where paths meet. On one path through the program a register holds a pointer, that is, the address of a block of memory. On another path it holds an ordinary number. After the paths meet, the summary must say “pointer or number”. A verifier that keeps only “pointer” accepts a read through that register, and on the second path the program reads from an arbitrary address.
Forgetting that arithmetic wraps. Suppose a register is 8 bits wide: it holds 0 to 255, and a sum above 255 wraps around, so is stored as 0. A program takes an input in such a register, computes , continues only if , and then reads 4 bytes starting bytes into a 16-byte record. A verifier that reasons with ordinary integers concludes that and accepts. The real machine also lets , 253, 254 and 255 through, because their sums are stored as 0, 1, 2 and 3, and the read then starts far outside the record. Real eBPF arithmetic is 64-bit or 32-bit, and RFC 9669, the IETF specification of the eBPF instruction set, says that on overflow “the 64-bit or 32-bit value will wrap”, so the same mistake is possible at full width.
Soundness is a statement about all cases, judged against the true meaning of each instruction. No number of passing tests establishes it. A sound verifier also proves only what it models: if the software that executes the program treats an instruction differently from the model, the proof does not apply to what actually runs.
Can a safe program be rejected?
Yes: a verifier, a checker that examines a program before it runs, rejects every program it cannot prove safe, and some safe programs lie beyond its method of proof.
For example, a program takes a value chosen by the sender of a request, checks that it lies between 0 and 10, and copies it into two registers and , which are storage locations of the processor. It then reads 4 bytes of a 16-byte record, starting bytes from the start of the record. The registers are always equal, so the difference is always 0 and the program reads the first 4 bytes of the record: the program is safe. A verifier that tracks each register only as a separate range, a method called interval analysis §3.9, knows that and each lie between 0 and 10, and nothing else. The smallest difference it must allow for is . The pair , never occurs, but two separate ranges cannot say so. A read that starts 10 bytes before the record is outside it, so the verifier rejects the program. A verifier that also records the relation proves that the difference is 0 and accepts.
A second cause is a limit on effort: a verifier that stops after a fixed amount of work rejects the program it was examining, although it found no fault. A rejection therefore means only “not proved”, which is different from “shown unsafe”. Rejecting whatever is not proved is conservative refusal §3.36: when in doubt, the verifier says no. It preserves soundness, meaning that the verifier never accepts an unsafe program. It gives up completeness, which would mean that the verifier accepts every safe program.
For a general-purpose language, in which a program may loop and use memory without limit, the price cannot be avoided. A checker that always finished and accepted exactly the safe programs could be used to decide whether an arbitrary program ever stops, and that question, the halting problem §19.2.2, cannot be decided by any algorithm §14.1. A language whose programs always stop within a fixed number of steps escapes that theorem, but its verifier still rejects safe programs in practice, because following every exact value costs too much: a single 64-bit input has possible values, about .
What is a Linux reference example?
The reference example is the eBPF verifier §3.3 inside the Linux kernel. The kernel is the privileged core of the operating system, and Linux can run eBPF programs inside it. An eBPF program is bytecode, meaning instructions for a processor that exists only as a definition. Before such a program runs, the kernel’s verifier, a checker that examines the program without running it, decides whether it is safe. RFC 9669, the IETF specification of the eBPF instruction set, points to the kernel documentation page “eBPF verifier”, which describes the following.
Two steps. The first step checks the program’s control-flow graph §3.13, the map of which instruction can follow which, and detects, among other things, instructions that can never be reached. The second step starts at the first instruction and follows the possible paths, simulating each instruction. It records how each instruction changes the registers, which are the processor’s numbered storage locations, and the stack, which is the program’s own working memory. It does not follow every path to its end: it stops following a path when it reaches an instruction with registers and stack that are covered by a situation it has already accepted at that instruction.
A type for every register. A register is
NOT_INITif it has never been written, and it may then not be read. It isSCALAR_VALUEif it holds a number that may not be used as a pointer, meaning a memory address. Otherwise it has a pointer type that names what it points to, such asPTR_TO_CTXfor the program’s input andPTR_TO_STACKfor the stack.A range for every number. The verifier tracks the minimum and maximum of a value both as an unsigned number and as a signed number, that is, without and with negative values, and what is known about individual bits. A comparison narrows the range on each branch: after a test whether a value, read as an unsigned number, is greater than 8, the unsigned minimum is 9 where the test succeeded and the unsigned maximum is 8 where it failed.
Checked memory access. Reads and writes of memory are allowed only through registers that hold a valid pointer type. They are checked for bounds, meaning that the access stays inside the memory the pointer refers to, and for alignment, meaning that the address is a multiple of the size of the access. Stack bytes may be read only after the program has written them.
Checked calls. When the program calls a helper function, a kernel routine offered to eBPF programs, the verifier checks that the argument registers meet the function’s declared constraints. After the call, register R0 has the function’s return type and registers R1 to R5 are unreadable.
Two cautions apply. First, these details differ between kinds of program and are revised as the kernel develops, and the page can lag the code. The page still says that the first step disallows loops, although a change made to the kernel in 2019 lets the verifier accept a loop in a program loaded by a privileged user, provided that it can simulate every pass of the loop within its limit on work. The reference is therefore the documentation of the kernel version in use and, where the two differ, its source code. Second, the details belong to Linux, not to the eBPF specification: RFC 9669 defines only the instructions and states that the details of verification are out of its scope. Other software that executes eBPF instructions, for example on a microcontroller §6.1, a small single-chip computer, contains the Linux verifier, its helper functions and its rules only if someone put them there. For any such software, what its checker assumes must be shown to match the behavior of the part that executes the instructions.
Related entries: eBPF verifier (§3.3), Abstract interpretation (§3.4), Soundness (§3.7), Completeness (§3.8), Relational abstract domain (§15.12), Verifier soundness and safe-program rejection (§17.5), Tree 5: “Can one prove that a read stays inside its object?” (§20.5), Tree 6: “Can a bounds check itself overflow?” (§20.6). Further reading: [2, 68, 160].