Section 2.26

Structural bytecode admission

Definition

Structural bytecode admission is the set of checks that a loader runs once on a bytecode §2.4 program, before letting it run, to confirm that its bytes form a well-formed program. Bytecode is a program written as numeric instructions for a virtual machine §2.3 (VM), a computer defined in software. The checks confirm that every instruction is an operation the VM defines, with legal fields, and that every jump lands on an instruction inside the program. They examine the program’s form, not the values it computes while it runs.

The idea

The interpreter §2.13 follows a program counter §2.9, which records where to read the next instruction. An unrecognized operation or an invalid register number can make a program impossible to interpret. A loader can detect these errors before any event calls the program. A jump must also land at the start of an instruction within the program.

These faults are visible in the bytes, because an instruction’s operation, register numbers and jump distance are constants written into it; one pass before the first run settles them for every run. The pass cannot see run-time values such as register contents, so a load from a register plus a constant can be perfectly formed and still read anywhere. A clerk who checks that a bank form is complete and legible is a fair picture: the clerk does not check that the account is the customer’s. The picture stops being accurate in one respect: a form is carried out once, while a program runs many times with different register contents.

How it works

RFC 9669, the specification of the extended Berkeley Packet Filter §2.1 (eBPF) instruction set, gives its encoding [160]: a program is a sequence of 8-byte slots. A basic instruction fills one slot; a wide immediate instruction §2.10, which loads a 64-bit constant, fills two, and its second slot is called here the continuation. Each slot holds a 1-byte opcode §2.8 (operation code), two 4-bit register numbers, a signed 16-bit offset and a signed 32-bit constant, the immediate. The loader, the host code that receives a program and prepares it to run, performs these steps and rejects the program at the first one that fails.

  1. Length. The program is not empty, its length is a multiple of 8 bytes, and it has no more slots than the VM’s limit. Let NN be the number of slots, numbered 0 to N−1N - 1.

  2. Starts. Walk the slots from slot 0; each slot the walk reaches is an instruction start. A wide instruction must start at slot N−2N - 2 or earlier, so that its continuation exists, and the walk steps over the continuation.

  3. Fields. At each start the opcode is one the VM defines, each register number is at most 10, and the instruction does not write r10, which holds the read-only address of the VM stack §2.25, the program’s own stack area. Unused fields are zero. A continuation is zero except for its half of the constant.

  4. Jump targets. A jump in slot ii with offset oo continues at slot t=i+1+ot = i + 1 + o. The target must satisfy 0≤t≤N−10 \le t \le N - 1 and be a start; a call to a function inside the program gets the same test. A call to a helper function §2.19, a host function the program may request, names it by a constant number, which can be checked against the helper allowlist §2.20 here too.

  5. Ending. The last instruction must never continue to a next slot: it is an exit, which ends the program, or an unconditional jump.

The work grows in proportion to NN. The interpreter must decode exactly as the checker does and must run the checked bytes: the loader copies the program into memory that nothing else writes, checks the copy and runs it, which avoids a time-of-check to time-of-use §9.26 (TOCTOU) error. Facts that depend on run-time values are left to a static verifier, such as the eBPF verifier §3.3, or to a runtime bounds check §2.27.

Worked example

Well formed but unsafe. These 16 bytes, in hexadecimal with one slot per line, form two instructions.

71 10 e8 03 00 00 00 00
95 00 00 00 00 00 00 00

Length: 16=2×816 = 2 \times 8. Slot 0: opcode 0x71 loads 1 byte from the address in the source register plus the offset into the destination register. On a little-endian §4.21 host, one that stores a number’s lowest byte first, the register byte 0x10 names destination r0 in its low 4 bits and source r1 in its high 4 bits, and the offset bytes e8 03 are 0x03e8 =1000= 1000. The unused immediate is zero. Slot 1: opcode 0x95, exit, with all other fields zero. With no jumps and an exit at the end, the program is accepted. It loads the byte at r1 +1000+ 1000 into r0, and by the eBPF calling convention §2.6 r1 holds at entry the address of the program’s input, its guest context §2.23. If the context is 16 bytes, with valid offsets 0 to 15, offset 1000 lies 1000−15=9851000 - 15 = 985 bytes past the last of them. The bytes do not state the context’s size, so no structural rule can object.

Not well formed. These 24 bytes form three slots, so N=3N = 3.

18 01 00 00 44 33 22 11
00 00 00 00 88 77 66 55
05 00 fe ff 00 00 00 00

Slot 0, opcode 0x18, is a wide immediate instruction that loads a 64-bit constant into r1, and slot 1 is its continuation, so the starts are slots 0 and 2. Slot 2, opcode 0x05, is an unconditional jump; its offset bytes fe ff are 0xfffe, which is −2-2 as a signed 16-bit number. The target t=2+1+(−2)=1t = 2 + 1 + (-2) = 1 lies inside the program but is a continuation, so the program is rejected; if it ran, the interpreter would decode half of a constant as an instruction. Offset +5+5 would give 2+1+5=8>N−1=22 + 1 + 5 = 8 > N - 1 = 2, also rejected. A file of 10 bytes, 10=1×8+210 = 1 \times 8 + 2, fails at step 1.

First the loader checks the instruction layout. A verifier can then ask a further question. Suppose two branches reach a comparison that reads a temporary register. One branch has written a value into it, and the other can reach the comparison without writing one. The instructions can all be well formed even though the verifier must reject the program under a rule requiring initialized registers. The time spent checking these paths belongs to admission. Later, when an admitted program handles one event, the interpreter follows the branch selected by that event. A timer around that invocation measures execution and its invocation overhead. A timer around the entire callback §14.22 can also include preparation and the firmware’s response before it returns.

Stated precisely

Let NN be the number of slots and SS the set of starts that step 2 finds. The checker accepts exactly when steps 1 to 5 all pass. Guarantee: in every run of an accepted program, the program counter holds only members of SS, so the interpreter decodes only complete, defined instructions inside the program. The proof is an induction over a run. Slot 0 is in SS. An instruction that continues reaches the next start, which exists by step 5. A jump or call reaches a member of SS by step 4, and a return reaches the start after its call, which exists by step 5. An exit ends the run.

Three assumptions carry the guarantee: the bytes that run are the bytes checked; the interpreter decodes with the checker’s opcode table, wide-instruction rule and jump arithmetic; and it carries out each defined instruction as the specification says. Nothing is guaranteed about the addresses that loads and stores form, or about how many steps a run takes.

Limits and common mistakes

Well formed does not mean memory safe: the first example is accepted and reads a byte 985 bytes past the end of its input.

Well formed does not mean the program stops. eBPF allows backward jumps: the single slot 05 00 ff ff 00 00 00 00, an unconditional jump with offset −1-1 and target 0+1−1=00 + 1 - 1 = 0, loops forever yet passes every step. Classic BPF §2.2, the original Berkeley Packet Filter that eBPF extends, allowed only forward jumps, so a structural rule alone ensured that every run ended within NN steps; eBPF needs an instruction budget §2.32 or a verifier’s proof instead.

A checker that accepts an opcode the interpreter does not implement breaks the guarantee. A second engine, such as a just-in-time (JIT §2.14) compiler that translates bytecode to native code, must agree with the checker too.

Origin does not matter: a trusted compiler’s output is checked like hand-written bytes, because bytes can be altered after compilation. A valid signature shows who supplied a program, not that it is well formed; the entry on bytecode authenticity versus safety §2.43 separates the two.

Questions answered

Can well-formed bytecode still compute an invalid memory address?

Yes, well-formed bytecode can still compute an invalid memory address, because structural admission checks only facts written into the bytes, such as defined operations, legal fields and jump targets, while an address is computed at run time from a register’s contents plus a constant offset. The eBPF (extended Berkeley Packet Filter) instruction that loads one byte from the address in register r1 plus 1000 is well formed, yet if r1 holds the address of a 16-byte input, it reads 985 bytes past the input’s last byte. Stopping it needs a second mechanism: a static verifier that tracks every register’s possible values and rejects the program, or a runtime bounds check that tests each address when the access happens.

Related entries: Instruction encoding (§2.7), eBPF verifier (§3.3), Runtime bounds check (§2.27), Opcode (§2.8), Wide immediate instruction (§2.10), Instruction budget (fuel) (§2.32), Bytecode authenticity versus safety (§2.43). Further reading: [160].