Section 3.3

eBPF verifier

Full name: Verifier for the extended Berkeley Packet Filter (eBPF).

Definition

The eBPF verifier is the checker that a host runs on an eBPF §2.1 (extended Berkeley Packet Filter) program before agreeing to load it. An eBPF program is bytecode §2.4, instructions for a small virtual machine §2.3, that a host such as the Linux kernel runs when an event occurs. The verifier analyzes the instructions without running them, and accepts the program only if it can show that every execution follows fixed rules: each memory access stays inside a permitted object, only permitted kernel functions are called, and the program always finishes.

The idea

The Linux kernel lets programs supplied from user space run inside the kernel, for example to filter network packets or to trace kernel events. Such code shares the kernel’s memory and privileges: one stray write can corrupt any kernel data, and one endless loop can hang a processor. The code could run in a separate, hardware-isolated process, which is slow, or have every memory access checked while it runs, which costs time on every instruction. The verifier takes a third route. It proves, once and before loading, that no execution breaks the rules, so the code can then run at full speed, often after just-in-time compilation §2.14 to machine code.

The verifier is a static analysis §3.1 with a strict policy, conservative refusal §3.36: when it cannot prove a rule, it rejects. Rejecting a safe program costs its author a rewrite. Accepting an unsafe program could cost the whole system.

How it works

Linux runs its verifier inside the bpf system call that loads a program. The loader states a program type, such as a packet filter or a tracing program. The type fixes two things: the context, which is the data the program receives as input, and the helper functions §2.19, the kernel functions that the program may call. The verifier then proceeds as follows.

  1. Control-flow check. It builds the control-flow graph §3.13, the graph of possible next instructions, and rejects jumps outside the program and instructions that no path reaches. Kernels before Linux 5.3 also reject every loop.

  2. Path exploration. The eBPF machine has 11 registers, R0 to R10. At the start, R1 points to the context, R10 is a read-only pointer to the stack, and the other registers are uninitialized. Starting at the first instruction, the verifier simulates the program on facts about registers instead of real values. At a conditional jump whose outcome it cannot determine, it follows both successors, each with the facts that the condition adds. Every path must end at an exit instruction.

  3. Facts per register. Each register is uninitialized, a scalar (a plain number), or a pointer of a known kind: to the context, to the program’s 512-byte stack, to a value stored in an eBPF map §2.18 (a key-value table kept by the kernel), to packet data, and others. A pointer carries an offset into its object. A scalar carries its lowest and highest possible value, as an unsigned and as a signed number, and a tnum §3.10, short for tristate number, which records which bits are known to be 0 or 1.

  4. Rules per instruction. Reading an uninitialized register or stack byte is rejected. A memory access must use a pointer of a kind that allows it, and every possible offset plus the access size must fall inside the object. Arithmetic on pointers is restricted. A helper call must be allowed for the program type, with each argument of the kind the helper declares. A helper that can return a null pointer §4.26, such as the map lookup, forces a comparison with 0 before the result is used; on the branch where the result is not 0, the register becomes a valid pointer.

  5. Limits and pruning. Current kernels reject a program once the verifier has processed 1 million instructions, counted over all explored paths. To stay within that limit, the verifier prunes: when a path reaches an instruction with facts that lie within facts already proved safe from that instruction, it stops following that path. Since Linux 5.3 a loop is accepted if the verifier, simulating its iterations one by one, sees it end within the limit.

  6. After acceptance. The verifier rewrites some instructions, for example turning reads of context fields into reads of the kernel’s real data structures. It also inserts protection against speculative execution, in which the processor runs instructions past a bounds check before the check’s outcome is known. The kernel then interprets the program or compiles it to machine code.

Worked example

Instructions are written here in a C-like notation. R6 points to a 4-byte map value, byte positions 0 to 3, already checked against null. R2 holds a byte taken from the event’s data, so the verifier knows only R2 ∈[0,255]\in [0, 255], where [a,b][a, b] means the integers from aa to bb inclusive.

  1. Accepted. The program runs if R2 >= 4 goto out, an unsigned comparison §2.12. On the path that continues, R2 ∈[0,3]\in [0, 3]. It sets R3 = R6 + R2 and reads 1 byte at R3. The highest position read is 3+1−1=33 + 1 - 1 = 3, inside 0 to 3, so the verifier accepts.

  2. Rejected, and really unsafe. With a 2-byte read instead, R2 =3= 3 reads positions 3 and 3+2−1=43 + 2 - 1 = 4. Position 4 is outside the value, and the verifier correctly rejects.

  3. Rejected, though safe. Another program sets R2 = R2 & 3, which keeps only the two lowest bits, so R2 ∈[0,3]\in [0, 3]. Call that value rr. It then sets R4 = 3 - R2 and R2 = R2 + R4, and reads 1 byte at R6 + R2. In every execution R2 ends as r+(3−r)=3r + (3 - r) = 3, so the read is always at position 3, inside the value. The verifier keeps a range for each register, not relations between registers. It computes R4 ∈[3−3,3−0]=[0,3]\in [3 - 3, 3 - 0] = [0, 3] and then R2 ∈[0+0,3+3]=[0,6]\in [0 + 0, 3 + 3] = [0, 6]. Positions 4 to 6 lie outside the value, so the verifier rejects a program that never reads out of bounds.

The usual repair is a check that is redundant for the program but informative for the verifier: if R2 > 3 goto out just before the read narrows R2 back to [0,3][0, 3], and the program is accepted.

Stated precisely

For Linux the guarantee is conditional. If the verifier accepts program pp for program type tt, then every execution of pp that starts from a context supplied by type tt satisfies five rules:

  1. every register and stack byte is written before it is read;

  2. every memory access lies inside an object that type tt makes accessible, and is a read or write that the object permits;

  3. every helper call is one that type tt allows, with arguments of the kinds the helper declares;

  4. the program reaches an exit instruction, so it terminates;

  5. for a program loaded without privileges, no kernel address is stored where user space can read it.

The guarantee rests on four assumptions: the verifier’s own code is correct; the instructions that run, after rewriting and compilation to machine code, mean what the verifier analyzed; the processor executes them as the verifier models, which speculative execution does not respect unless mitigated; and every helper, which is ordinary kernel code that the verifier does not analyze, behaves as declared. Two have failed in practice. A range-tracking bug in the verifier, CVE-2021-3490 in the Common Vulnerabilities and Exposures (CVE) catalog, let a crafted program read and write kernel memory out of bounds. And Google Project Zero’s 2018 demonstrations of Spectre, an attack through speculative execution [252], used eBPF programs to read kernel memory [253].

Limits and common mistakes

Acceptance is not correctness. An accepted packet filter can still drop every packet. The verifier proves that the program finishes, not that it finishes within a deadline, and a helper can take much longer than one instruction.

The accepted set of programs depends on the kernel version, the program type and the loader’s privileges. A program accepted by a new kernel can be rejected by an older one. A compiler update can turn an accepted program into a rejected one by moving a bounds check onto a different register.

The rules described here are those of Linux. Other eBPF hosts use other checkers: eBPF for Windows uses PREVAIL §16.12, a separate verifier based on abstract interpretation §3.4, and small eBPF runtimes for microcontrollers §6.1 often perform only simple structural checks and rely on checks during execution. Running eBPF instructions does not bring the Linux verifier’s guarantees along.

A rejection after 1 million processed instructions means that the verifier ran out of budget, not that the program is unsafe.

Questions answered

Why can a safe program still be rejected?

A safe program can be rejected because the eBPF (extended Berkeley Packet Filter) verifier accepts only what its own approximate reasoning can prove within a fixed budget, and that reasoning cannot prove every true fact. Five causes are common:

No checker can avoid every such rejection. Deciding safety exactly is impossible for programs in general, and far too costly even for eBPF programs, whose length and loops are bounded. A checker that must always finish and must never accept an unsafe program therefore rejects some safe ones. The verifier accepts that trade because a rejection costs the program’s author a rewrite, while a wrong acceptance can compromise the kernel.

Related entries: Abstract interpretation (§3.4), Completeness (§3.8), Bytecode authenticity versus safety (§2.43), eBPF (§2.1), Interval analysis (§3.9), Bitmask abstract value (tnum) (§3.10), Conservative refusal (§3.36), Verifier soundness and safe-program rejection (§17.5). Further reading: [2, 61, 252, 253].