Section 2.16

uBPF

Full name: Userspace eBPF VM.

Definition

uBPF is an open-source C library that runs eBPF §2.1 (extended Berkeley Packet Filter) programs inside an ordinary application instead of inside the Linux kernel. The project describes itself as a “Userspace eBPF VM”: user space is where ordinary applications run, outside the kernel, and VM §2.3 means virtual machine. An application that embeds uBPF registers the host functions that programs may call, loads a program’s bytecode §2.4, and runs it with an interpreter §2.13 or with a just-in-time (JIT §2.14) compiler for x86-64 or 64-bit Arm.

The idea

Linux runs eBPF programs inside its kernel, after the eBPF verifier §3.3, a load-time checker, has proved each program memory-safe. That implementation cannot be lifted into other software: it depends on the kernel, and its license requires derived works to use the same license, which many projects cannot accept. The uBPF project gives that license as its reason for a separate library under the Apache License 2.0, which a network appliance, a database or another operating system can embed.

A library is not a sandbox §2.30 by itself. It runs inside the host application with all of the application’s privileges, so a program it runs is confined only as far as the library’s checks and the application’s choices confine it. In its default mode uBPF has no verifier like that of Linux: it checks a program’s structure when loading it and, in the interpreter only, checks memory accesses while the program runs. Which protection comes from the library and which from the application is therefore the central question about any use of uBPF.

How it works

The facts below describe the upstream source and documentation as consulted in November 2026; other revisions differ. An application uses five calls.

  1. ubpf_create makes a VM object, or returns a null pointer §4.26 if memory runs out.

  2. ubpf_register associates a helper number with a host function; a call instruction in the program names a helper by that number.

  3. ubpf_load copies the bytecode into the VM and checks it, returning an error message if a check fails. Helpers are registered first, because the checks include helper numbers.

  4. ubpf_exec runs the program in the interpreter. Register r1 receives the address of a memory region that the application passes in. The call returns 0 on success and −1-1 on failure, and only on success does it store the program’s result, the final value of r0. Alternatively, ubpf_compile returns a native function that runs the program as JIT-compiled code.

  5. ubpf_destroy frees the VM.

Checks at load. The program must be a multiple of 8 bytes long, with at most 65,536 instructions. Every opcode §2.8 must be known and every register number valid. Every jump must land inside the program and not in the middle of a 16-byte wide instruction, and a jump to itself is rejected as an endless loop. Every helper call must name a registered helper. These checks are structural: the loader does not track whether a register holds a number or an address, and it does not prove that loops end.

Checks in the interpreter. Before every load and store, the interpreter checks that the whole accessed range lies inside the region passed to ubpf_exec, inside the VM stack §2.25, or inside a range that a host-registered callback §14.22 approves. This bounds check is on by default, and ubpf_toggle_bounds_check turns it off. ubpf_set_instruction_limit sets an instruction budget §2.32; the limit is 0, meaning none, until the application sets it. A failed check ends the run with status −1-1.

The JIT path. The JIT compilers emit no bounds checks, and the documentation states that the instruction limit has no effect on JIT-compiled programs. Constant blinding, a defense against JIT spraying, in which constants that an attacker chose are misused as machine instructions, is available but off by default.

The safe profile. Newer revisions add an optional, interpreter-only safe profile. It records for every register which memory region an address came from, checks each access against that region’s bounds and permissions, and requires every helper that returns an address to be registered with a description of what it returns.

Worked example

An application passes a 16-byte region to ubpf_exec, so the valid offsets are 0 to 15. The program loads 4 bytes from offset 14. The access needs offsets 14 to 17, and 14+4=18>1614 + 4 = 18 > 16, so 2 of the 4 bytes lie outside the region. The interpreter compares the end of the access, region start plus 18, with the end of the region, region start plus 16, refuses the load, stops the program and returns −1-1. The same program compiled with ubpf_compile performs the load and reads 2 bytes of whatever the application keeps after the region.

Now suppose the application handles the result carelessly. It sets a verdict variable to 1, meaning allow, passes it to ubpf_exec, ignores the status, and allows the event if the verdict is 1. The run above fails, the library returns −1-1 and leaves the variable untouched, and the application allows the event. The library reported the fault correctly; the application turned the fault into permission.

Stated precisely

In the default profile, with the bounds check on and execution in the interpreter, the library guarantees the following. Let bb and nn be the start address and length of the region passed to ubpf_exec. Every load or store of ww bytes at address aa that the program performs satisfies b≤ab \le a and a+w≤b+na + w \le b + n, or lies entirely inside the VM stack, or was approved by the host’s callback. If the application set an instruction limit L>0L > 0, a run executes at most LL instructions. Every violation ends the run with status −1-1 and no result.

The guarantee rests on four assumptions: bb and nn describe memory that the program may use and that stays valid for the whole run; every registered helper is safe for every argument value, because the library passes r1 to r5 to it as raw 64-bit numbers and cannot tell an address from a number; the callback approves only memory the program may use; and the interpreter’s own code is correct. Nothing is guaranteed about JIT-compiled runs, about running time when no limit is set, or about what a returned value means.

Limits and common mistakes

uBPF is not the Linux eBPF verifier. Its default mode never proves a program safe in advance; it catches bad memory accesses one at a time, while interpreting. Applications that need guarantees before running, for example to use the JIT compiler, pair uBPF with a separate verifier such as PREVAIL §16.12 (Polynomial-Runtime eBPF Verifier using an Abstract Interpretation Layer), which checks programs before they are loaded.

The host can weaken every library feature. The bounds check can be turned off, a callback can approve too much, and without an instruction limit a program that loops for ever is never stopped.

Helpers hold most of the authority. A helper that copies four approved bytes and a helper that reads any address it is given have the same calling form, and the library treats them alike; the second hands the program the application’s whole memory.

The library loads whatever bytes it is given. Deciding which programs may be loaded at all, for instance by checking a signature, is the application’s job.

Questions answered

Which safety features are supplied by the library and which by its embedding host?

The uBPF library supplies load-time structural checks and, in its interpreter only, run-time checks; the embedding application supplies everything else. In the upstream revision consulted in November 2026, the library checks at load that opcodes and register numbers are valid, that jumps land inside the program and do not target themselves, and that helper calls name registered helpers. Its interpreter checks every load and store against the input region, the VM stack and an optional host callback, and enforces an instruction limit if the application sets one; an optional interpreter-only safe profile also checks that each address stays within the region it came from. Its just-in-time compiler adds no bounds checks, ignores the instruction limit, and blinds constants only if asked to.

The embedding application decides which programs may be loaded, which helpers exist and whether each is safe for every argument, which memory the program receives and how long it stays valid, whether the bounds check stays on, what instruction limit applies, whether to interpret or compile, and how to treat a failed run. It must check the status of every call: a failed run returns −1-1 and stores no result, and an application that reads a stale result variable can turn a correctly reported fault into an allow decision.

Related entries: Helper contract (§2.21), eBPF verifier (§3.3), Just-in-time compilation (JIT) (§2.14), Runtime bounds check (§2.27), Instruction budget (fuel) (§2.32), Helper function (§2.19), PREVAIL (§16.12). Further reading: [3, 163].