Setting
A sensor gateway is a small device that collects readings from sensors and passes them on. In this invented example the gateway also accepts a small decision program, delivered through a phone from a publisher, the one party authorized to change the gateway’s rules. The program is eBPF §2.1 bytecode §2.4: instructions for a processor that exists only as a specification. The name eBPF began as an abbreviation of extended Berkeley Packet Filter; RFC 9669, the IETF specification of the instruction set, says that the name is no longer an acronym for anything. The decision program is the guest; the gateway’s own software is the host.
Before the guest may run, a verifier, a part of the host that examines the whole program in advance, must accept it. The verifier reasons about each instruction as RFC 9669 defines it, and about each helper function §2.19, a host routine that the guest may call, from that helper’s written contract. The host then runs the accepted guest in one of two ways: with an interpreter §2.13, a host routine that carries out the instructions one at a time, or with a just-in-time (JIT §2.14) compiler, which translates the bytecode into the processor’s own machine instructions just before the first run. The guest calls a helper read_sensor, which copies a sensor reading into guest memory. The question is whether, after the verifier has accepted the guest, a helper or the JIT can still make it unsafe.
Short answer
Yes. The verifier’s acceptance is a conclusion about a model in which every instruction does what the specification says and every helper keeps its written contract. A helper is native host code that the verifier never examines, so a bug inside it can corrupt memory however carefully the call was checked. A JIT compiler produces new native code; if that code gives any instruction a meaning other than the one the verifier assumed, the program can do what the verifier ruled out.
Follow-up questions
Who is responsible for a helper’s native safety?
The host is responsible: the authors of the helper and whoever builds it into the device, not the downloaded program and not the verifier. A helper function is a routine of the device’s own software, the host, that a downloaded program, the guest, may call. It is compiled native code and runs with all of the host’s rights. A verifier, a checker that examines the guest before it runs, checks only the call: that the helper number is one the guest may use, and that each argument has the type and range the helper’s contract demands, for example a pointer to guest memory with at least 8 writable bytes. These checks establish the helper’s preconditions. They say nothing about what the helper’s body then does.
A helper whose every call was checked correctly can still:
copy 9 bytes into the 8-byte buffer it was given, a buffer overflow §4.4;
read a host object that was freed earlier, a use-after-free §4.6;
change a setting that the guest had no right to change;
wait for a radio reply that never comes, so that the guest never finishes.
Each helper is therefore part of the trusted computing base §1.4, the part of a system whose correctness every safety conclusion depends on. The helper’s own safety must be established for the helper itself, by review, by testing against its contract or by proof, and a safety claim for the whole system must list the helpers among its assumptions.
What must a helper contract §2.21 specify?
A helper contract must specify everything a caller may rely on and everything the helper may do: its arguments, the memory it touches and for how long, its writes on failure, its results, its effects, its resource use and the context it may run in. A helper contract is the written agreement between a helper function, a routine of the device’s own software, and the downloaded programs that call it. It must be exact enough for a verifier, a checker that examines a program before it runs, to check every call, and for a reviewer to check the helper’s body.
Arguments. For each argument: the register that carries it, its width, whether it is signed, and its meaning. In the eBPF calling convention §2.6 (eBPF began as an abbreviation of extended Berkeley Packet Filter), arguments travel in registers
R1toR5and the result returns inR0.Memory. For each pointer: which kind of memory it may point to, how many bytes, whether the helper reads or writes them, and how long the helper may keep the pointer. A pointer lent only until the helper returns must not be stored for a later callback §14.22.
Writes on failure. Which bytes may have changed when the helper reports an error. A helper that writes bytes and fails partway may leave some of them changed; an error result does not mean that memory is unchanged unless the contract says so.
Results. The range of the return value, the meaning of each error code, and the state of the other registers afterwards. In the eBPF calling convention,
R1toR5hold no usable value after a call, andR6toR9keep their values.Effects. Which host state the helper may change, and whether later runs see the change.
Resources. An upper bound on running time, whether the helper may wait, and how much memory it may reserve.
Context. Whether the helper may run while interrupts §6.11, the hardware signals that make the processor suspend the current code, are disabled, and which locks it takes.
Example: read_sensor(dst, len). The pointer dst must point into guest memory with at least len writable bytes, and len must be between 1 and 16. On success the helper writes exactly len bytes and returns 0; on failure it writes nothing and returns a negative error code. It keeps no pointer after returning, never waits, and finishes within 50 microseconds.
What is the JIT obligation?
A just-in-time (JIT) compiler must produce native code that does exactly what the specification says the accepted bytecode does, for every input. The JIT compiler translates the downloaded program’s bytecode, here eBPF instructions (eBPF began as an abbreviation of extended Berkeley Packet Filter), into the processor’s own machine instructions shortly before they run. The verifier, the checker that accepted the program, reasoned about the bytecode; the processor runs the translation. The verifier’s conclusion carries over to the translation only if the translation preserves:
every instruction’s result, with the widths, signs and wraparound rules that RFC 9669, the specification of the instruction set, defines;
every memory access, at the same address and with the same size, and every check made while the program runs that the safety argument relies on, such as a bounds check;
every helper call, with the arguments placed where the processor’s native calling convention requires and with the host’s own registers and stack left intact;
the accounting the host relies on, such as an instruction budget §2.32 that stops the program after a set number of steps.
There are two ways to establish this: prove the translator correct once for all programs, or check each translation against its bytecode after it is produced, which is called translation validation §3.33. Running the verifier again on the same bytecode establishes neither, because the bytecode was never in doubt; the native code was. The native code must also stay as produced. The memory that holds it should be writable while the JIT compiler fills it and executable but not writable afterwards, so that a separate bug cannot rewrite code that was checked.
What is a semantics-mismatch counterexample?
A semantics-mismatch counterexample is one concrete program on which two components disagree about what an instruction does, so that the checker’s conclusion is false for the code that actually runs. Neither component reports an error, because the program is well formed; they simply apply different rules.
The simplest source is extension, widening a value to more bits. The 8-bit value 0xFF is 255 read as unsigned and read as signed in two’s complement, the usual encoding in which the top bit counts negatively. Zero extension §4.19 to 64 bits fills the new upper bits with 0 and gives 255. Sign extension copies the top bit, here 1, into the new bits and gives 0xFFFFFFFFFFFFFFFF, which is . The signed test “greater than 0” is true for 255 and false for , so the two rules send the program down opposite branches.
eBPF (the name began as an abbreviation of extended Berkeley Packet Filter) has rules of exactly this kind. RFC 9669, its specification, says that an instruction of the 32-bit arithmetic class, which it calls the ALU class (ALU is the usual abbreviation of arithmetic logic unit, the part of a processor that does arithmetic), zeroes the upper 32 bits of its destination register. The immediate, a constant stored inside an instruction, is a signed 32-bit number, and a 64-bit operation uses it sign-extended to 64 bits. In the program below, w9 names the lower 32 bits of register r9.
w9 = 0xFFFFFFFF, a 32-bit move. By the specification,r9becomes0x00000000FFFFFFFF.if r9 != 0xFFFFFFFF goto L, a 64-bit comparison. The immediate is sign-extended to0xFFFFFFFFFFFFFFFF, the two values differ, and the jump toLis taken.
A verifier that wrongly sign-extends in step 1 believes that r9 equals 0xFFFFFFFFFFFFFFFF, concludes that the jump is never taken, and never checks the code at L. At run time that unchecked code runs. The Linux kernel’s eBPF verifier §3.3 had a flaw of this kind, which the Common Vulnerabilities and Exposures (CVE) list records as CVE-2017-16995: the verifier modeled a 32-bit move of an immediate value with the wrong sign extension. The same mismatch can sit in a JIT compiler instead of a verifier.
Does testing two engines settle the issue?
No. Running the same programs through two engines, for example an interpreter and a just-in-time (JIT) compiler, and comparing the results is differential testing §10.6. Agreement on every tested case shows only that the two engines agree on those cases.
The sample is tiny. The tested programs and inputs are a vanishing fraction of all possible ones. A mismatch that only one rare pattern triggers, such as one particular constant in one particular instruction, is easily missed.
Both engines can share a mistake. If both were written from the same misreading of the specification, or one reuses the other’s decoding code, they agree and are both wrong. A fault shared in this way is called a common-mode error §19.2.13.
Only compared outputs count. Two engines that return the same verdict but access memory differently look identical to a test that compares verdicts only.
A disagreement, by contrast, proves that at least one engine is wrong. Reduce the failing program to the smallest one that still disagrees, then compare each engine with the specification, here RFC 9669, to find which one is wrong. The specification acts as the test oracle §10.5, the source of the correct answer, and an oracle is useful only if it is independent of the engines it judges. Agreement is bounded empirical evidence. Equivalence for all programs needs a proof about the translator, a check of each translation, or an independently built reference model of the instruction set used as the oracle.
Related entries: Refinement (§3.32), Differential semantics (§3.34), Helper contract (§2.21). Further reading: [160].