Section 21.3

CertrBPF: connecting a VM specification to generated C

Full name: Certified rBPF. The paper does not spell the name out; it introduces the name under the heading “A Certified rBPF Interpreter”. rBPF §2.17 is the name of the eBPF §2.1 (extended Berkeley Packet Filter) virtual machine §2.3 (VM), the program that executes eBPF instructions, in RIOT, an open-source operating system for microcontrollers §6.1.

What it is

CertrBPF is an interpreter §2.13 for eBPF programs on microcontrollers (single-chip computers), delivered as C code with a machine-checked proof that it confines the programs it runs. An interpreter executes another program’s instructions one at a time; eBPF is an instruction set for small programs loaded into a running system. Shenghao Yuan, Frédéric Besson, Jean-Pierre Talpin, Samuel Hym, Koen Zandberg and Emmanuel Baccelli published it at the Computer Aided Verification (CAV) conference in 2022. It addresses the gap between a proof about a model and the code that runs.

Basis of this account

This account rests on the published, open-access chapter “End-to-End Mechanized Proof of an eBPF Virtual Machine for Micro-controllers” in the second part of the proceedings of the 34th CAV conference, Springer Lecture Notes in Computer Science volume 13372, pages 293 to 316 (https://doi.org/10.1007/978-3-031-13188-2_15). All 24 pages were read. The statements of Theorems 1 to 4 and of the two theorems in Section 5.1 were checked on the typeset pages. Of the source code that the paper links to, only the function check_mem_aux2 in the file clight/interpreter.c was read, in the first author’s copy of the artifact on GitHub (https://github.com/shenghaoyuan/CertrBPF, branch CAV22-AE), to confirm the comparison used in the worked example. The proof scripts were not read, checked or run.

What the paper claims

The authors make these claims.

How it works

The work connects four objects in a chain, and Coq checks every proof named below.

  1. Proof model. This is the specification: an interpreter for rBPF written in Gallina. Its state holds the program, the position of the current instruction, eleven 64-bit registers, a status flag, the memory, and the list of memory regions the program may use. Before each division, remainder, shift, memory read or memory write the interpreter runs a check. If the check fails, it sets the flag to an error value and stops. It also stops with an error value when a preset budget of steps, which the paper calls fuel §2.32, is used up. The authors prove that a well-formed state stays well-formed, and that from a well-formed state the interpreter never reaches its crash result, which stands for undefined behavior §4.12.

  2. Synthesis model. A second Gallina interpreter, optimized and written in the shape of the intended C code. The authors prove that it computes exactly the same function as the proof model, so both are called the model below.

  3. C code. A tool named ∂x\partial x translates the synthesis model into C, construct by construct. This tool is not trusted: step 4 checks its output.

  4. Clight program. The tool clightgen reads the C text back into Clight, a simplified C that has a formal meaning inside Coq. The authors prove that whenever the synthesis model goes from a state ss to a state s′s', the Clight function, started in a memory that represents ss, ends in a memory that represents s′s'. The paper calls this proof translation validation §3.33.

A state is well-formed when three conditions hold. Every register holds a 64-bit integer. Every memory region is a valid, readable block of bytes, and distinct regions do not overlap. Every instruction names registers 0 to 10, and every jump lands inside the program. A small checker, which the authors verify by the same method, establishes the third condition before a program runs.

The last link is a forward simulation §19.2.9: every step of the model is matched by the Clight program. The paper notes that the opposite direction, from the Clight program back to the model, follows because the model is a function and Clight is determinate: once its inputs are fixed, a Clight program can behave in only one way, so the matched behavior is its only behavior.

Worked example

The memory region and the addresses are invented for teaching; the check and the code are the paper’s. A program may use one region that starts at address 0x20001000, is 32 bytes long and is writable. The program executes an instruction that stores a 4-byte word at address 0x20001020.

The memory check of the proof model computes the offset 𝟶𝚡𝟸𝟶𝟶𝟶𝟷𝟶𝟸𝟶−𝟶𝚡𝟸𝟶𝟶𝟶𝟷𝟶𝟶𝟶=32\texttt{0x20001020} - \texttt{0x20001000} = 32 and the end of the access 32+4=3632 + 4 = 36. The check has four conditions. The permission is sufficient. The offset is a multiple of 4, because 32mod4=032 \bmod 4 = 0. The offset is far below the bound that keeps the end of the access within the range of a 32-bit number. The range condition requires the end of the access to be below the region size, and 36<3236 < 32 is false. The check therefore returns the null pointer §4.26, a value that stands for no valid address. The interpreter then sets the flag to the error value for an illegal memory access, and nothing is written. A store to 0x20001008 passes: the offset is 8, the end is 8+4=128 + 4 = 12, and 12<3212 < 32. The comparison is strict, in the paper’s formula and in the code of the artifact. A store to 0x2000101C has the offset 28 and the end 28+4=3228 + 4 = 32. Since 32<3232 < 32 is false, the check rejects this store too, although all 4 of its bytes lie inside the region.

The generated C code contains the same check. For this instruction the paper lists the call check_mem(st, 2U, 4U, addr), followed by upd_flag(st, -2) when the returned pointer is null. Here 2U stands for writable, 4U for 4 bytes and -2 for the illegal-memory flag. The theorem says more than that the two texts look alike: the C function changes the C state exactly as the model changes its state. For the rejected store, the 4-byte flag field at offset 4 of the C state block becomes −2-2, and the memory regions keep their content.

What this account does not establish

The theorem ends at the Clight program, that is, at the C source code with the meaning that CompCert gives to C. The paper does not claim a proof of the step in which clightgen reads the C text into Clight. For its measurements the paper compiled the code with GCC §12.17 (GNU Compiler Collection), the C compiler of the GNU free-software project, which the proof does not cover. The authors pass compiler options §12.3 that make signed and pointer arithmetic §4.25 wrap around, switch off an assumption about which pointers can refer to the same memory, and remove tail-recursive calls to bound the stack size. In those builds the step from C to machine code rests on GCC and these options, not on the theorem.

The proof assumes that no stack overflow §4.10 occurs. The paper calls this an implicit assumption of CompCert.

The specification is trusted. The proof shows that the C code behaves like the Gallina interpreter and that this interpreter is isolated. It does not compare the interpreter with an independent definition of eBPF, so an instruction that is specified wrongly is implemented wrongly.

Functions of the host system that an eBPF program calls are trusted and lie outside the proof. The measured CertrBPF does not yet let a program call functions of RIOT, and the authors name this as one reason why it needs less memory than the interpreter it replaced.

The property holds from well-formed states only: the registers and memory regions must be set up correctly, and the program must have passed the checker, before the interpreter starts.

The proved property is isolation: no undefined operation and no memory access outside the granted regions. The theorem gives no bound on execution time §9.32 and does not say that the result a program computes is the right one.

Questions answered

What does an end-to-end theorem need to connect?

An end-to-end theorem needs to connect the specification on which a property is proved to the program that really runs, with a proved link across every step in between. To state such a theorem, five things must be named.

  1. The specification: the precise description of the intended behavior, on which the property is proved.

  2. The implementation and its semantics, meaning the formal definition of what each statement of the implementation language does.

  3. A relation that says when a state of the implementation represents a state of the specification.

  4. The starting states that are admitted, for example only states in which every memory region is valid.

  5. The property that is carried across, for example “no memory access outside the granted regions”.

The theorem then says: from every admitted starting state, each step of the specification is matched by the implementation, and the two resulting states are again related. The property proved on the specification then holds for the implementation, provided that related states agree on the property and that the implementation can behave in only one way, so that the matched behavior is its only behavior.

Whatever lies beyond the last link is assumed, not proved. CertrBPF (Yuan and others, 2022) is a verified interpreter for eBPF (extended Berkeley Packet Filter) programs, small programs loaded into a running system. Its chain runs from a specification written in Coq, a program that checks proofs, to C code. The compiler that turns the C code into machine code, the processor, and the functions of the surrounding system that a program calls stay outside the chain. The theorem carries exactly the stated property. It does not show that the specification is the intended one, that a program finishes within a deadline, or that the decisions a program computes are correct.

Related entries: Forward simulation and observable refinement (§19.2.9), Translation validation (§3.33), Refinement (§3.32), Trusted computing base (TCB) (§1.4), rBPF (§2.17), Compiler correctness (§3.40), Software fault isolation (SFI) (§2.41), Femto-Containers: small functions rather than whole process containers (§21.4). Further reading: [205].