Section 20.4

Tree 4: “Why use a different instruction set at all?”

Setting

A sensor gateway is a small device that collects readings from sensors and passes them on. The gateway in this invented example is built around a microcontroller §6.1 (MCU): a small computer on one chip, here with an Arm processor, that runs a single program, its firmware, with no operating system that separates programs from each other. The gateway accepts a small decision program, delivered through a phone that only carries the bytes, from a publisher, the one party authorized to change the gateway’s policy. Each time someone asks to change one of the gateway’s settings, the gateway runs the decision program, which answers ALLOW or DENY, while the gateway keeps serving its other work.

The publisher could have compiled the decision program into native code, the Arm machine code that the processor executes directly. Instead the program is eBPF §2.1 bytecode §2.4: instructions for a processor that exists only as a definition, not as hardware. The name eBPF began as an abbreviation of extended Berkeley Packet Filter and is no longer treated as an acronym. The instruction set is also commonly called BPF, after the Berkeley Packet Filter (BPF), the older and smaller instruction set from which it grew. The firmware runs the bytecode in a virtual machine §2.3 (VM). Here the VM is an interpreter §2.13: native code that repeatedly reads the next eBPF instruction, decodes it, meaning that it works out from the instruction’s bits which operation is meant and on which values, and then performs that operation. The decision program is called the guest. The firmware, which contains the VM, is called the host. The question is why the gateway uses a second instruction set at all.

Short answer

Because a virtual instruction set lets the host stand between the guest and every effect the guest can have. Each memory access, jump and call of the guest is an operation that the host defines and can check, so the host can confine the guest to the memory and routines it grants, on any processor and without hardware support. The different encoding isolates nothing by itself: the isolation comes from the host’s checks and a correct interpreter. The price is slower execution and more trusted host code.

Follow-up questions

First answer

A virtual instruction set exists so that the host decides what each guest instruction may do. The host is the gateway’s own firmware, and the guest is the downloaded decision program, written in eBPF bytecode: instructions for a processor that exists only as a definition. A guest in native code, the processor’s own machine code, would act on the hardware directly. Its loads and stores would reach any address the processor can reach, and its jumps could go anywhere. A guest in bytecode acts only through the host’s virtual machine (VM), the part of the host that carries out bytecode.

On this gateway the VM is an interpreter, which handles each instruction in three steps.

  1. It reads the next instruction of the guest.

  2. It decodes the instruction: from the instruction’s bits it works out the operation, such as a 4-byte load, and its inputs, such as a register and an offset.

  3. It performs the operation. Before any memory access it can compare the address with the memory regions granted to the guest, which is called a runtime bounds check §2.27.

Some checks can instead run once, before the program starts: a verifier, a checker that examines the whole program, can prove that every access stays inside the granted regions. Either way, the instruction set makes every effect explicit. The guest reaches memory only through load and store instructions, the host’s routines only through a call instruction that names a helper function §2.19, a host routine offered to the guest, and other places in its own program only through jumps whose targets the host can check.

The new instruction set is a means, not the protection. The bits of an eBPF instruction are no harder to misuse than the bits of an Arm instruction. Confinement comes from the checks that the host implements and from the correctness of the interpreter, which itself runs as native code with the full authority of the firmware.

Where does isolation actually come from?

Isolation comes from checks that the host implements on every path by which the guest can have an effect, and from the correctness of the code that makes those checks. The host is the gateway’s firmware, and the guest is a downloaded eBPF program that runs in the host’s virtual machine (VM). Four conditions together confine the guest.

  1. Memory. Every load and store of the guest is shown to fall inside a region the host granted for this run, either by a check during the run or by a proof before it. A guest value used as an address is checked before use and never becomes an unchecked address in the host.

  2. Control flow. Every jump lands on the start of an instruction of the guest’s own program.

  3. Calls. The guest can call only helper functions, host routines that the host has chosen to offer. Each helper checks its arguments and grants no more authority than its purpose needs. A helper that writes into a buffer named by the guest must first check that the whole buffer lies inside the guest’s regions.

  4. Trusted code. The interpreter, the verifier if there is one, and every helper are correct. They run as native code with the full authority of the firmware, so a bug in any of them is a hole in the isolation.

The demand that every access be checked, with no route around the check, is called complete mediation §1.16.

Hardware can add a second layer, but only where it is configured. A memory protection unit §6.8 (MPU), a circuit in many microcontrollers that restricts which address ranges code may read, write or execute, protects the regions it is set up for, against code running at the privilege level §6.10 it restricts; a privilege level is a processor mode that decides which operations code may perform. Running the VM in its own thread, a separate flow of execution, isolates nothing by itself. Without a configured MPU, all threads on a microcontroller share one address space, and any of them can write anywhere. A claim of hardware isolation must name the regions, the code they restrict and the privilege level of that code.

Why not run native C directly?

Native code is faster, but it runs with the authority of whatever runs it, and on a small microcontroller (MCU) that is usually the whole device. Native code here means C compiled into machine code for the gateway’s own processor. It needs no interpreter, the host routine that carries out each bytecode instruction in software, so it avoids a cost paid on every instruction. On an MCU whose firmware runs without memory protection, though, a native decision program can read the gateway’s keys, overwrite any variable and, running at the privileged level, switch off interrupts §6.11. Writing the program in C confines nothing, and neither does the fact that its job is only to return a verdict.

Three methods can confine native code.

A cryptographic tag from the publisher, such as a digital signature §5.14 or a message authentication code §5.7, does not confine native code either: it shows who produced the code, not what the code does. To choose between the options, fix one guarantee, such as “the decision program reads and writes only its own regions”. Then compare, for each option, the time it adds to every run and the amount of code the guarantee depends on: an interpreter and its checks, an MPU configuration and its entry points, or an SFI rewriter’s checker.

Does portability mean every BPF runtime accepts the same program?

No: portability means that the instruction encoding §2.7 is the same for every host, not that every host offers the same environment, and a program depends on much more than its instructions. A host here is the software that loads, checks and runs downloaded programs. A BPF runtime is the part of a host that does so for eBPF, an instruction set for a processor that exists only as a definition, also commonly called BPF after the Berkeley Packet Filter from which it grew. One eBPF program can be run by hosts on Arm or x86 processors, but a given host accepts it only if all of the following match.

Together these agreements form the guest-host application binary interface §2.37 (guest-host ABI): the contract, at the level of bytes and numbers, between a downloaded program and the host that runs it, covering calls, data layouts and their meanings. A program runs unchanged on two hosts only when both honor the same guest-host ABI and both accept the program under their own checks.

Can interpretation add bugs?

Yes: the interpreter is native code with the full authority of the host, so a mistake in it can break every guarantee the instruction set was meant to give. An interpreter is the host routine that reads each bytecode instruction, decodes it and performs it. Here the host is the gateway’s firmware, written in C, and the bytecode is eBPF, whose meaning RFC 9669, the IETF specification, defines. The interpreter must give every instruction exactly that meaning, a requirement called semantic fidelity §2.38. Typical mistakes break it.

A smaller instruction set makes the interpreter easier to check; it does not make the interpreter correct. The interpreter belongs to the trusted computing base §1.4, the part of the system whose correctness the guarantee depends on, together with the verifier, if there is one, and the helpers.

Related entries: Virtual machine (VM) (§2.3), Guest-host ABI (§2.37), Software fault isolation (SFI) (§2.41), Interpreter (§2.13), Interpreter semantic fidelity (§2.38), Memory protection unit (MPU) (§6.8), Microcontroller (MCU) (§6.1), Complete mediation (§1.16), Trusted computing base (TCB) (§1.4), Helper function (§2.19), Wide immediate instruction (§2.10). Further reading: [70, 160].