Section 20.3

Tree 3: “Can handwritten assembly bypass checks on C code?”

Setting

A sensor gateway is a small device that collects readings from sensors and passes them on. The gateway in this invented example also accepts a small decision program, delivered through a phone that only carries the bytes. The program comes from a publisher, the one party authorized to change the gateway’s policy, meaning its rules for deciding requests. 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. A part of the gateway’s software called a virtual machine §2.3 (VM) carries out the instructions, while the gateway keeps serving its other work.

The publisher can produce the bytecode in two ways. At first the publisher writes the program in the C programming language, and a compiler, a program that translates source code into instructions, turns it into eBPF bytecode. The Clang §12.17 compiler, for example, can do this. It packs the bytecode into an ELF §12.6 (Executable and Linking Format) file, a standard container that holds instructions together with information about them, such as which instructions still refer to named data. Later the publisher writes the program in eBPF assembly language instead: a readable text form in which each line names one instruction and its operands. An assembler, a program that translates such text line by line, turns it into the same kind of bytecode.

On the gateway, a loader, the part that receives a submitted program and prepares it for execution, takes the instructions out of the file and completes every instruction that still refers to something by name. A verifier, a checker that examines a program before it runs, then decides whether the program obeys the gateway’s safety rules, such as reading and writing only the memory it was given. The gateway lets the VM run a program only after the verifier has accepted the program’s final instructions. The question is whether writing the program by hand in assembly, instead of in C, can get around those checks.

Short answer

Only if the checks examine the C source and nothing after it. The gateway receives bytecode, not C, so its verifier examines the final instructions that the VM will run. Then the source language makes no difference: hand-written assembly becomes the same kind of bytecode and meets the same verifier. A bypass needs a gap in that path: a check made only on the source, a loading route that skips the verifier, instructions changed after the check, or a verifier bug. None of these gaps is specific to assembly.

Follow-up questions

First answer

No: when the check is made on the final instructions, hand-written assembly passes through exactly the same check as compiled C. The gateway never sees C. It receives eBPF bytecode, a sequence of instructions for a defined, not physical, processor. A compiler turns C into such bytecode, and an assembler turns eBPF assembly, a readable text form of the same instructions, into such bytecode. The two routes may produce different instructions for the same task, but both produce bytecode of one format, and the gateway handles both in the same four steps.

  1. The loader, the part of the gateway that receives and prepares a program, takes the instructions out of the submitted file.

  2. The loader completes the instructions that still refer to something by name. In an ELF (Executable and Linking Format) file, the container a C compiler typically produces, a list of relocation records §12.50 tells the loader which instruction fields to fill in, for example with a reference to a data table. This step changes instruction bytes.

  3. The verifier, a checker that examines the program before it runs, examines the completed instructions, the ones that will actually run, and accepts or rejects the program.

  4. Only an accepted program goes to the virtual machine (VM) that executes it.

Nothing in these steps asks which language the program was written in, so assembly gets no shortcut. The answer changes in one design: if the only safety check runs on the C source, for example a code review or an analysis tool on the publisher’s computer, then any route to bytecode that avoids that check bypasses it, and hand-written assembly is the easiest such route. The check belongs on the gateway, after the last step that changes the bytes.

What if I supply native Arm assembly instead?

Then the gateway has nothing that can run it: a gateway whose only way to execute downloaded code is its eBPF virtual machine (VM) reads every submitted byte as eBPF bytecode, whatever the bytes were meant to be. Native Arm assembly is the readable form of machine code for Arm processors, the instructions that the gateway’s processor executes directly, and an Arm assembler turns it into Arm machine code. The gateway’s loader, the part that receives a program, and its VM decode the submitted bytes by the eBPF rules instead. A basic eBPF instruction is 8 bytes long, and its first byte, the opcode §2.8, says which operation it is.

Two outcomes are possible. Usually the bytes do not form a valid eBPF program, for example because an opcode byte names no eBPF operation or a register field names a register other than the eleven registers numbered 0 to 10, and the gateway rejects the submission. If the bytes happen to form a valid eBPF program, that program is checked and run as eBPF, under every eBPF rule. Under RFC 9669, the IETF specification of eBPF, a byte 0xb7 in the opcode position, for example, means: copy the signed 32-bit constant stored in this instruction into the 64-bit destination register. What the same byte would mean to an Arm processor plays no role, because the VM never hands the bytes to the processor.

The question becomes real only if the gateway also has a second route that runs native code, for example a facility for installing native functions. That route is a separate trust boundary §1.3, with checks of its own to justify. Native code runs with the full authority of the processor mode it runs in, and the eBPF verifier §3.3 says nothing about it. Such a route needs its own argument, for example hardware confinement with a memory protection unit §6.8 (MPU), a circuit that restricts which addresses code may use, or software fault isolation §2.41, a rewriting of machine code that keeps its memory accesses and jumps inside an assigned region. Limiting the route to the authorized publisher establishes who sent the code, not that the code is safe.

Does a C compiler guarantee safety?

No: a compiler translates a program, and translating a program does not make it safe or correct. A compiler is a program that turns source code, here C, into instructions, here eBPF bytecode for a gateway. Three gaps remain after compilation.

  1. Undefined behavior §4.12. The C standard gives some operations no defined meaning, for example reading past the end of an array §14.5, or a signed integer overflow §4.15, where the sum of two signed integers does not fit in their type. A program that performs such an operation has undefined behavior: the standard places no requirement on what happens. A compiler need not reject such a program, and in general it cannot tell, because whether the operation happens can depend on input that arrives only when the program runs. A C function that reads element ii of a 16-element array, with ii taken from a request that can carry any value from 0 to 255, compiles without complaint.

  2. Wrong policy. A program that answers ALLOW to every request is valid C. It compiles and it is wrong. The compiler knows the language, not what the owner of the gateway intended.

  3. Compiler bugs. A compiler is a large program and can itself translate wrongly. The property it should have is compiler correctness §3.40: for every source program whose behavior the language defines, the produced code behaves as the source does. Even CompCert, a C compiler with a machine-checked proof of this property, promises nothing for a source program with undefined behavior.

On the gateway, safety is not decided by the compiler. The gateway’s verifier, a checker that examines the final eBPF instructions before they run, rejects the out-of-bounds read §4.5 of item 1, because it cannot show that every ii from 0 to 255 stays inside 16 elements, whatever compiler produced the instructions. No verifier catches item 2: a program that stays inside its memory and returns the wrong verdict is safe in the verifier’s sense and still harmful.

Does arbitrary assembly defeat every verifier?

No: a verifier judges the final instructions, so the way they were written gives no power that a compiler’s output lacks. A verifier is a checker that examines a program before it runs and accepts it only if it can show that every possible run obeys stated rules, here that every memory access stays inside the memory the program was given. A verifier is called sound if it never accepts a program that breaks those rules. A sound verifier rejects every unsafe program, whether a person wrote the program in assembly or a compiler produced it.

Assembly matters in one way: it can produce instruction sequences that no compiler would emit, and so reach corners of the verifier that ordinary programs never exercise. When such a program is accepted and then breaks the rules, the fault lies in a specific gap of the verifier, not in assembly. Four kinds of gap, each a real way for a verifier for eBPF bytecode to fail:

A sound verifier also rejects some safe programs. It decides about every possible run without running the program, so it must approximate, and its approximations err on the side of rejection. Rejecting a safe program costs convenience; it is not a defeat.

What if verification and execution use different bytes?

Then the verification says nothing about the run: a verifier’s acceptance covers the bytes it examined, and only those bytes. Here a verifier is the gateway’s checker that examines a downloaded program before the gateway’s virtual machine (VM) runs it. Suppose the loader, the part that receives the program, has the verifier check the instructions in a buffer bb, a region of memory, and the VM later runs a buffer b′b' whose bytes differ. That can happen because another task wrote into bb after the check, because the phone’s next message reused bb, or because the VM reads a different copy, such as the place where the program was first stored. Any instruction in b′b' may be unchecked. This is a time-of-check to time-of-use §9.26 (TOCTOU) error.

The repair binds the run to the checked bytes.

  1. Make every change that loading requires, such as filling in references named in the file, before verification.

  2. Keep the program in memory that nothing else can write from the check until the program is discarded, such as a buffer owned only by the loader and the VM, or memory made read-only by hardware.

  3. Verify that copy, and run that same copy.

A digest, a short value computed from the bytes by a cryptographic hash function §5.1, identifies bytes but does not protect them. Comparing a digest computed at the check with one computed just before the run shows that the bytes were the same at those two moments; a write after the second moment is still possible unless the memory itself is protected. Transformations after verification create new bytes. The common case is just-in-time (JIT §2.14) compilation, the translation of eBPF instructions into the processor’s own machine code after loading. Either the translator must be trusted to be correct, or its output must be checked against its input, which is called translation validation §3.33. A check of the instructions before translation does not by itself cover the machine code that runs.

Related entries: Translation validation (§3.33), Compiler correctness (§3.40), Bytecode (§2.4), Structural bytecode admission (§2.26), Machine-code proof boundary (§4.48), eBPF verifier (§3.3), Time-of-check to time-of-use (TOCTOU) (§9.26), Relocation: turning a symbolic reference into exact encoded bits (§12.50), Wide immediate instruction (§2.10), Undefined behavior (UB) (§4.12), Bytecode authenticity versus safety (§2.43), Software fault isolation (SFI) (§2.41). Further reading: [72, 78, 160].