Section 2.1

eBPF

Full name: extended Berkeley Packet Filter.

Definition

eBPF is a technology for running small programs inside a larger piece of software, such as the kernel (the central program) of an operating system, without rebuilding or restarting that software. Its core is an instruction set: a short list of simple machine instructions and the bits that encode each one, defined in the Internet specification RFC 9669. The component of that software which loads and executes eBPF programs is called a runtime. The name began as extended Berkeley Packet Filter; RFC 9669 says it no longer stands for anything.

The idea

The kernel of an operating system is its central program: it controls the hardware and can read and write all memory. Its owner often wants to add one small rule to it, for example “raise an alert when a sensor reading is above 30”. Rebuilding the kernel for each rule is slow. Loading machine code written by someone else is dangerous, because machine code inside the kernel can do everything the kernel can do.

eBPF takes a middle path. The rule is written as a program for a small invented computer, called a virtual machine §2.3, not for the real processor. That computer has eleven 64-bit registers, which are numbered storage locations written R0 to R10, and a short list of instructions: arithmetic, comparisons that jump, loads and stores of memory, calls and exit. Because the instruction set is small and regular, software can read a whole program before running it and decide whether to accept it. The larger software, inside which the program runs, is called the host. The program is called the guest.

The word eBPF covers two layers, and a security question must say which layer it is about. The first layer is the ISA §4.38 (instruction set architecture): the agreement on what each instruction does and how it is written in bits. It is the same everywhere. The second layer is a runtime: one implementation inside one host, with its own rules about which programs it accepts and what a program may touch. The Linux kernel contains the original runtime. eBPF began there as an extension of classic BPF §2.2 (the original Berkeley Packet Filter), a 1993 design for selecting network packets [73], and current documents often write BPF for eBPF.

How it works

An eBPF program passes through four stages. The instruction set fixes the form of the program. The runtime decides everything else.

  1. Compile. A compiler translates the author’s source text, usually written in the programming language C, into eBPF instructions. Each instruction occupies 8 bytes, except one kind that carries a 64-bit constant and occupies 16. A program in this form is called bytecode §2.4.

  2. Load and check. The host receives the bytes and can refuse them before they ever run. The Linux kernel does so with the eBPF verifier §3.3: a checker that follows every path through the program, tracks what each register can hold, and rejects the program if any load, store or call could break the kernel’s rules. uBPF §2.16 (userspace eBPF), a runtime that applications embed, takes another route: its interpreter §2.13, the component that carries out the instructions, checks each memory access while the program runs, and the application can switch that check off.

  3. Execute. An interpreter carries out the instructions one at a time. Alternatively, a JIT §2.14 (just-in-time) compiler first translates them into instructions of the real processor.

  4. Exchange. The host puts the program’s input in a register, lets the program call a chosen list of host functions, called helper functions §2.19, and reads the result from a register when the program exits.

Worked example

A hypothetical host passes a sensor reading as a number in register R1 and reads the answer from register R0: 1 means alert, 0 means no alert. The rule is “alert when the reading is greater than 30”. The program has four instructions of 8 bytes each, 32 bytes in all. The bytes are shown in hexadecimal for a little-endian §4.21 machine, which stores the lowest byte of a number first.

slot bytes meaning
0 b7 00 00 00 00 00 00 00 r0 = 0
1 d5 01 01 00 1e 00 00 00 if r1 s<= 30 goto +1
2 b7 00 00 00 01 00 00 00 r0 = 1
3 95 00 00 00 00 00 00 00 exit

In slot 1, s<= is a less-than-or-equal comparison of signed numbers, that is, of numbers that may be negative, 1e is 30 in hexadecimal, and goto +1 means “skip one instruction”.

Reading 28: slot 0 sets R0 to 0. Slot 1 asks whether 28≤3028 \le 30. It is, so slot 2 is skipped, and slot 3 exits with R0 equal to 0: no alert. Reading 34: slot 0 sets R0 to 0. Slot 1 asks whether 34≤3034 \le 30. It is not, so slot 2 sets R0 to 1, and slot 3 exits with R0 equal to 1: alert. Reading 30: 30≤3030 \le 30 holds, so the result is 0. The rule is “greater than 30”, not “30 or more”. The instruction set fixes all of this: on every correct runtime that supports these instructions, the program exits with 0 in R0 when it starts with 28 in R1, and with 1 in R0 when it starts with 34. That the host puts the reading in R1 and takes the answer from R0, that 1 means alert, and who was allowed to install the program are rules of this host.

Now replace slot 0 by 71 10 00 00 00 00 00 00, the instruction r0 = *(u8 *)(r1 + 0). Its meaning is exact: read one byte from the memory address held in R1 and put it in R0. With a reading of 28, that address is 28. Whether the program may read address 28 is not decided by the instruction set. A runtime with a verifier rejects the program before it runs, because R1 holds a plain number and not a pointer, meaning an address, into memory that the program was given. A runtime that checks each access during the run stops the program at this instruction. A runtime with neither check performs the read.

Stated precisely

RFC 9669 fixes four things for every runtime.

RFC 9669 leaves six things to the runtime. Its section 6 says that a program can use the instructions to misuse memory, processor time and other resources, and that execution environments should be designed to run only programs that are trusted and verified.

Limits and common mistakes

The name eBPF does not include a verifier. The verifier belongs to the Linux runtime. Another runtime may check differently or not at all.

Verified does not mean authorized, and neither means correct. A verifier decides whether a program respects memory and time limits. A check of a digital signature §5.14, a value that only the holder of a secret key can compute for the bytes of the program, or a permission check decides whether the right party supplied the program. Neither decides whether 30 was the right threshold.

A helper function can undo the confinement of a program. A program that cannot touch memory outside its own area can still ask a helper function to do so, if the host offers such a helper function.

Questions answered

Which security properties come from the ISA and which from a particular runtime?

In eBPF, a technology for running small programs inside a larger piece of software, the ISA (instruction set architecture) supplies predictability, and the runtime supplies every protection. RFC 9669, the specification of the instruction set, fixes the ISA, so three facts hold in every conforming implementation. Each instruction has one defined meaning. Arithmetic has a defined result for every input, including overflow and division by zero. Every instruction is 8 or 16 bytes long and every jump in RFC 9669 names its target with a constant, so a program can be decoded completely, and its possible paths listed, before it runs. These facts make a program checkable. They do not make it safe: the ISA lets a program address any memory, loop for ever and call any helper function number.

A particular runtime, the software that executes the program, supplies the rest: memory confinement, by a verifier that checks the program before it runs or by a runtime bounds check §2.27, a test made at each memory access while the program runs; a bound on running time, by a verifier or by an instruction budget §2.32 (a limit on the number of instructions executed); the list of helper functions (host functions the program may call) and what each may do; the decision who may load a program; a correct interpreter or JIT (just-in-time) compiler; and a clean starting state for each run. To judge an eBPF system, ask for each of these properties which part of that runtime enforces it. The word eBPF answers none of them.

Related entries: Virtual machine (VM) (§2.3), eBPF verifier (§3.3), Bytecode authenticity versus safety (§2.43), Classic BPF (cBPF) (§2.2), Bytecode (§2.4), eBPF register convention (§2.6), uBPF (§2.16), Instruction set architecture (ISA) (§4.38). Further reading: [1, 2, 3, 73, 160].