Section 2.19

Helper function

Definition

A helper function is a function of the host system that an eBPF §2.1 (extended Berkeley Packet Filter) program may call. The program cannot jump into arbitrary host code: it names a helper by a number in a call instruction, and the runtime runs the host function registered under that number, once it has checked that this program may call it. Helpers provide what the program’s own instructions cannot, such as reading a clock or looking up a map. A helper runs as native host code with the host’s full privileges.

The idea

eBPF instructions compute with registers and with memory the program was given, and nothing else. A program that filters sensor events needs the current time, a stored threshold, perhaps a way to send an alert. Raw access to the host’s memory and devices would provide all of that and confine nothing. A helper is the middle way: the host offers a short list of named operations, each with arguments, effects and results that can be stated and checked.

A counter at a records office is a fair picture. A visitor cannot walk into the archive but can hand a numbered request form to a clerk, who checks it, fetches the document and hands back a copy. The picture stops at the clerk. In software the clerk is host code with the keys to every room, so a careless helper, one that trusts a length or an address it was given, opens the whole archive. A program is no safer than the helpers it may call.

How it works

  1. Encode. The eBPF call instruction has opcode §2.8 0x85. With its source register field set to 0, it calls a helper by number: RFC 9669, the eBPF instruction-set standard, calls this a call to a helper function by static ID, and the 32-bit immediate field holds the number. In Linux, bpf_map_lookup_elem is helper 1, and bpf_ktime_get_ns, which returns the time since boot in nanoseconds, is helper 5.

  2. Pass arguments. Under the eBPF calling convention §2.6, the program puts up to five arguments in r1 to r5, and the result arrives in r0. After the call r1 to r5 hold no defined values, while r6 to r9 keep theirs.

  3. Check at load. The runtime confirms that the number names a helper that exists and that this kind of program may call; this is the job of a helper allowlist §2.20. In Linux each helper also has a prototype giving the type of every argument, such as a map, a pointer to a key of that map, a pointer to memory with its size in bytes, or any number, and the type of the result. The eBPF verifier §3.3 checks every call against the prototype; a pointer and size pair, for example, must describe memory the program may access.

  4. Dispatch. At run time the call reaches the host function either through a dispatch table §2.39, a table from helper numbers to host functions, or through a direct call that the loader or a just-in-time (JIT §2.14) compiler has written into the code.

  5. Return. The helper runs, may change host state, and returns. The verifier gives r0 the result type from the prototype; a lookup result, for instance, must be compared with zero before use.

A helper’s contract has four parts. Preconditions state which argument values and memory ranges, of which lengths and lifetimes, the helper accepts. Side effects state everything it changes: map entries, buffers, devices, locks, output. The result states the meaning of every possible return value, including how errors are reported. The cost states how much time and memory one call can take, and whether the call can wait. The helper contract §2.21 entry treats this structure in full.

Worked example

Encoding a call. An eBPF instruction is 8 bytes: an 8-bit opcode, two 4-bit register fields, a 16-bit offset and a 32-bit immediate. A call to helper 5 has opcode 0x85, register fields 0, offset 0 and immediate 5. In little-endian §4.21 byte order, which stores the least significant byte of each field first, its bytes are 85 00 00 00 05 00 00 00.

Checking a pointer and a size. A helper copies nn bytes from host memory into a program buffer whose address it takes in r1, with nn in r2. The program’s stack is 512 bytes, addressed from the frame pointer r10 at offsets −512-512 to −1-1. The program passes the address r10 minus 16 and n=16n = 16. The helper would write offsets −16-16 to −16+16−1=−1-16 + 16 - 1 = -1, all inside the stack, so the verifier accepts the call. With n=24n = 24 the last byte would land at offset −16+24−1=7-16 + 24 - 1 = 7, above the stack, so the verifier rejects the program. In a runtime without such a check, the helper must test nn itself; one that trusts nn writes 8 bytes of host memory beyond the stack.

Cost. An instruction budget §2.32 charges one unit per guest instruction. Suppose, with invented numbers, that a copying helper takes 2 microseconds plus 0.05 microseconds per byte. Four calls copying 256 bytes each take 4×(2+0.05×256)=4×14.8=59.24 \times (2 + 0.05 \times 256) = 4 \times 14.8 = 59.2 microseconds, yet the budget sees four instructions. A helper that waits for a radio reply with no timeout §9.34 has no bound at all.

Stated precisely

For a helper hh, let Preh\mathrm{Pre}_h be a condition on the argument registers and on the memory they point to, Posth\mathrm{Post}_h a relation between the state before and after the call, including the value of r0, FhF_h the set of memory locations and devices that hh may change, and ThT_h a bound on the time of one call as a function of the arguments. The contract states: if Preh\mathrm{Pre}_h holds when hh is called, then hh returns within ThT_h, the state after the call satisfies Posth\mathrm{Post}_h, and nothing outside FhF_h has changed. The verifier or runtime must establish Preh\mathrm{Pre}_h before every call; the implementation of hh must deliver Posth\mathrm{Post}_h, FhF_h and ThT_h whenever Preh\mathrm{Pre}_h holds. A helper that cannot rely on a checker must test Preh\mathrm{Pre}_h itself and return an error when it fails.

Limits and common mistakes

A checked call is not a correct helper. The verifier can establish every precondition while the helper’s own code reads past a buffer or writes the wrong device register. Every permitted helper belongs to the trusted computing base §1.4, the code that must be correct for the system to be secure.

Checking arguments on entry is not enough if the helper keeps them. A helper that stores a pointer to the program’s stack and uses it after the run has ended writes into memory that now belongs to something else.

Restricting which helpers exist reduces a program’s authority, but each permitted helper must still be safe for every argument value the checker allows.

An error code must not look like a valid result. If a temperature helper returns −1-1 for a failed reading, a program comparing the result with a threshold of 30 degrees takes the failure for a cold reading.

Linux helper numbers and prototypes belong to Linux. Another runtime may number its helpers differently and check fewer argument types or none, so the same bytecode §2.4 can be safe in one runtime and unsafe in another.

Questions answered

What preconditions, side effects, and execution costs does each helper have?

Each helper function, a host function that an eBPF program may call by number, has its own preconditions, side effects and costs, and all three must be written in its contract. Preconditions are the conditions under which it may be called: the valid range of each argument and, for every pointer argument, the memory it must point to, how many bytes, whether readable or writable, and for how long. Side effects are everything the call changes besides its result: map entries, buffers, device registers, locks, log output, or a packet sent. Execution cost is the time and memory one call can take as a function of its arguments, and whether the call can wait, for example for a lock or a device.

In Linux, for example, bpf_map_lookup_elem requires a map and a pointer to a key of that map’s key size, changes nothing, and returns the address of the stored value or zero; bpf_map_update_elem also requires a pointer to a value of the map’s value size, inserts or replaces an entry, and can fail when the map is full; bpf_ktime_get_ns takes no argument, changes nothing, and returns the time since boot in nanoseconds. A checker can establish preconditions, but the helper’s implementation must keep its side-effect and cost promises. An instruction budget counts a helper call as one instruction, so every helper’s cost must be bounded separately.

Related entries: Helper contract (§2.21), Helper allowlist (§2.20), Dispatch table (§2.39), eBPF register convention (§2.6), Copy-only helper (§2.22), BPF map (§2.18), Instruction budget (fuel) (§2.32), Least privilege (§1.15), Trusted computing base (TCB) (§1.4). Further reading: [2, 160].