Chapter 2

eBPF, virtual machines, and execution confinement

This chapter explains how a system can run a small program supplied by someone else without letting that program harm it. Its running example is eBPF §2.1, an instruction set for small programs that a host, such as an operating-system kernel or the firmware of a device, loads and runs while it is already running. The first entries describe the program format: what a virtual machine §2.3 and its bytecode §2.4 are, how eBPF’s registers and instructions are encoded, and the ways to execute bytecode, by an interpreter §2.13 or by translating it into machine code before or while it runs.

The rest of the chapter is about confinement. A guest program can reach the host only through helper functions §2.19 and the data that the host hands it, so the chapter defines the contracts of these interfaces, the memory a guest may use, and the checks that keep it inside: checks before a program is admitted, bounds checks while it runs, and a limit on the number of instructions it may execute. It closes with what these checks do and do not establish, such as spatial and temporal memory safety §2.29, termination, determinism and the difference between knowing who wrote a program and knowing that it is safe to run, and with alternatives such as software fault isolation §2.41 and proof-carrying code §2.42. The chapters on memory and on program analysis supply useful background.

In this chapter

  1. 2.1 eBPF
  2. 2.2 Classic BPF (cBPF)
  3. 2.3 Virtual machine (VM)
  4. 2.4 Bytecode
  5. 2.5 Register machine
  6. 2.6 eBPF register convention
  7. 2.7 Instruction encoding
  8. 2.8 Opcode
  9. 2.9 Program counter (PC)
  10. 2.10 Wide immediate instruction
  11. 2.11 ALU32 versus ALU64
  12. 2.12 Signed versus unsigned comparison
  13. 2.13 Interpreter
  14. 2.14 Just-in-time compilation (JIT)
  15. 2.15 Ahead-of-time compilation (AOT)
  16. 2.16 uBPF
  17. 2.17 rBPF
  18. 2.18 BPF map
  19. 2.19 Helper function
  20. 2.20 Helper allowlist
  21. 2.21 Helper contract
  22. 2.22 Copy-only helper
  23. 2.23 Guest context
  24. 2.24 Scratch memory
  25. 2.25 VM stack
  26. 2.26 Structural bytecode admission
  27. 2.27 Runtime bounds check
  28. 2.28 Spatial memory safety
  29. 2.29 Temporal memory safety
  30. 2.30 Sandbox
  31. 2.31 Sandbox escape
  32. 2.32 Instruction budget (fuel)
  33. 2.33 Termination versus time bound
  34. 2.34 Fault-to-verdict mapping
  35. 2.35 Deterministic invocation state
  36. 2.36 Guest persistent state
  37. 2.37 Guest-host ABI
  38. 2.38 Interpreter semantic fidelity
  39. 2.39 Dispatch table
  40. 2.40 Resource confinement
  41. 2.41 Software fault isolation (SFI)
  42. 2.42 Proof-carrying code
  43. 2.43 Bytecode authenticity versus safety