Section 2.21

Helper contract

Definition

A helper contract is the complete statement of what a helper function §2.19 requires and what it promises. A helper function is a host function that a guest program, code running inside a virtual machine §2.3 (VM), may call. The contract states the preconditions, facts that must hold when the call starts, such as which arguments are pointers and how many bytes they cover; the postconditions, facts that hold when it returns, including the meaning of its result; what else the call may change; and which resources it may use.

The idea

Saying that a helper “takes a pointer and a length” leaves open the questions that decide safety. Take a helper that sends a guest’s message over a radio. May it write into the message? Does it send before it returns, or queue the pointer and send later, after the guest’s memory has been reused? May it wait until the radio is free, and for how long? Each answer changes what must be checked before the call and what the guest may assume after it.

The checks that make a guest safe cover the guest’s own instructions. A helper is host code with the host’s full rights, and no check on the guest’s instructions looks inside it. Safety therefore rests on two halves of one agreement: the caller establishes the preconditions, and the helper keeps its promises. The Linux documentation of the eBPF §2.1 (extended Berkeley Packet Filter) verifier, the checker that examines eBPF programs before they run, states both halves: the verifier guarantees that a helper is called with valid arguments, and each function made available to programs has to be thought through for safety.

A building contract is a fair picture. The client opens one room; the builder promises to paint that room, leave the others alone, finish by a date and hand the key back. The picture stops at enforcement. A broken building contract can go to court afterwards, but a helper that writes one byte too many has already corrupted memory, and nothing reports it.

How it works

A complete helper contract has six parts.

  1. Arguments. For each argument: whether it is a plain number, a pointer, or the size that belongs to a pointer; its allowed range; and, for a pointer with size ss, whether the ss bytes of guest memory starting at the pointer must be readable, writable or already written. In eBPF the arguments travel in registers R1 to R5.

  2. Result. The meaning of the return value, which eBPF places in R0, for success and for each kind of failure.

  3. Effects on failure. What has been written when the call fails: nothing, zeros, or a stated part. A contract that promises that nothing is written on failure promises failure atomicity §8.5, the property that a failed operation leaves no partial effect.

  4. Frame condition. Everything the contract does not list as changed stays as it was. In the eBPF calling convention §2.6, a helper may overwrite R1 to R5, while R6 to R9 and the frame pointer R10 keep their values.

  5. Lifetime and ownership. Whether the helper only borrows a guest pointer for the duration of the call, keeps it after returning, or takes over the memory it points to; and whether the result is an object that the guest must later hand back.

  6. Execution and resources. Whether the helper may wait, for example for a lock or a device; whether it may allocate memory; the longest time it may take; how much native stack, the host thread’s call stack, it uses; and whether it may be called from an interrupt handler §6.12, code that the processor runs in response to a hardware signal.

The caller’s side, the preconditions, can be checked by a machine, either before the program runs or at the start of the helper on every call. Linux eBPF writes the argument part of each contract in machine-readable form, the helper’s prototype, which gives a type to each argument and to the result, and the verifier checks every call against it before the program may run. The helper’s side, meaning its postconditions, frame condition and resource limits, cannot be checked by anything that watches the guest. It is established by reviewing, testing or proving the helper’s own code.

Three questions settle most reviews of a helper: may it wait, may it allocate memory, and may it keep a guest pointer after it returns. The answer at the end of this entry treats each one.

Worked example

A hypothetical helper get_reading has this contract.

A guest has an 8-byte buffer, positions 0 to 7, holding 10, 11, 12, 13, 14, 15, 16 and 17. It calls the helper with R1 pointing at position 2. The precondition holds: the call needs positions 2 and 3, and both lie inside the buffer. The reading is 500, which is 0x01F4 in hexadecimal: the low byte 0xF4 is 244 and the high byte 0x01 is 1, and 1×256+244=5001 \times 256 + 244 = 500. Little-endian order puts 244 at position 2 and 1 at position 3. Afterwards the buffer holds 10, 11, 244, 1, 14, 15, 16, 17, and R0 is 2. Positions 0, 1, 4, 5, 6 and 7 still hold 10, 11, 14, 15, 16 and 17, so the frame condition holds.

A faulty version of the helper stores the reading as a 32-bit number: 4 bytes, 244, 1, 0 and 0, at positions 2 to 5. R0 is still 2 and the two promised bytes are right, but positions 4 and 5 now hold 0 and 0 instead of 14 and 15. The postcondition holds and the frame condition is broken. No check on the guest’s instructions notices; the guest simply finds wrong values in its buffer later. Had the buffer ended at position 3, the same bug would have overwritten 2 bytes of whatever memory follows the buffer.

A precondition failure looks different. A call with R1 pointing at position 7 needs positions 7 and 8, but the buffer ends at position 7: the space left from position 7 is 8−7=18 - 7 = 1 byte, less than the 2 bytes required. The checker must refuse this call before the helper runs, because the helper itself, trusting its precondition, would write position 8.

Stated precisely

A contract can be written as a Hoare triple §3.31 {P}c{Q}\{P\}\ c\ \{Q\}: if the precondition PP holds when the call cc starts and the call returns, then the postcondition QQ holds when it returns. Read this way, the triple promises nothing about a call that never returns. A helper contract therefore adds a resource clause: the call returns within a stated time tmaxt_{\max} and uses at most stated amounts of memory and native stack.

Let M(a)M(a) be the guest byte at position aa before the call, M′(a)M'(a) the byte at the same position after the call, and WW the set of positions that the contract allows the helper to write. The frame condition is M′(a)=M(a)for every position a∉W,M'(a) = M(a) \quad \text{for every position } a \notin W, where ∉\notin means “is not an element of”. In the example W={2,3}W = \{2, 3\}. At a=4a = 4 the frame condition requires M′(4)=M(4)=14M'(4) = M(4) = 14; the faulty helper gives 0. At a=2a = 2 the frame condition says nothing, and the postcondition requires M′(2)=244M'(2) = 244. Permission to change a byte, which is membership of WW, and the promised new value, which is part of QQ, are separate statements.

The resource clause needs units and a worst case. Suppose a copy helper, on a stated processor and clock rate and with interrupts §6.11 unable to delay it, takes at most 3 microseconds plus 2 microseconds per byte. For nn bytes its worst-case execution time §9.33 (WCET), the longest time any call can take, is at most 3+2n3 + 2n microseconds; for n=4n = 4 that is 3+2×4=113 + 2 \times 4 = 11 microseconds. The bound holds only because the helper never waits: a helper that waits for a lock takes as long as the lock’s holder keeps it.

Every guarantee above rests on three named assumptions: the helper’s code is correct; nothing else, such as another thread or an interrupt handler, changes the guest memory during the call; and the bytes on which the precondition was checked are the bytes the helper uses, unchanged between the check and the use.

Limits and common mistakes

A contract is a promise, not a check. A checker can confirm that the guest establishes the preconditions. Nothing that watches the guest confirms that a helper keeps its postconditions, its frame condition or its time limit.

A precondition checked earlier can be false by the time it is used. If another thread frees or changes the guest memory between the check and the helper’s access, the helper acts on bytes nobody checked: a time-of-check to time-of-use §9.26 (TOCTOU) error.

Contracts often leave failure undefined. If a helper may write part of its output before failing, the contract must say so, and the guest must not use the buffer after a failure.

A machine-readable prototype covers only what its types can express. The eBPF prototype states argument types and sizes. Whether a helper may wait, and how long it may take, must be stated and checked separately.

Changing a helper’s contract can invalidate programs that were checked against the old one. The contract is part of the guest-host ABI §2.37 (application binary interface), the agreement on how guest and host exchange data and calls, and a change to it needs a new version that old programs cannot mistake for the one they were checked against.

Questions answered

Is the helper allowed to block, allocate, or retain a guest pointer?

A helper may wait (block), allocate memory or keep (retain) a guest pointer only when its contract says so explicitly and the host is built to carry the consequences; for a helper called by untrusted guest code the safe default is no to all three. A helper is a host function that a guest program, code running in a virtual machine, may call, and its contract is the statement of what it requires and what it promises.

Waiting. A guest’s run time is usually bounded by an instruction budget §2.32, a limit on the number of instructions it may execute, and a helper call counts as one instruction. A helper that waits for a lock, a device or a timer makes the run time depend on something outside that count, so the bound no longer holds. Waiting is forbidden outright when the guest runs inside an interrupt handler, which must not wait, and waiting for a resource that the caller already holds causes a deadlock §9.14. Linux, for example, lets an eBPF (extended Berkeley Packet Filter) program call a helper that may sleep, meaning give up the processor until some event occurs, only if the program was loaded as sleepable.

Allocating. An allocation can fail, takes a varying amount of time, and can cause heap fragmentation §4.9 on a small device. A helper may allocate only if the contract states a size limit, a failure result that the guest must handle, who owns the memory, and a release that happens on every way the guest’s run can end, including a fault. Otherwise each call can cause a memory leak §4.8, and repeated calls exhaust the heap. The Linux eBPF verifier §3.3 enforces the release part for helpers that hand out objects: it rejects a program that could end while still holding such an object.

Keeping a guest pointer. Guest memory, such as the guest’s stack, its scratch memory §2.24 and its input, belongs to one run and is reused when the run ends. A helper that saves a pointer and uses it later, for example from a timer, reads or writes memory that by then holds other data, exactly as with a dangling pointer §4.1. A helper that needs the data later must copy the bytes into memory it owns before returning. A contract that lets it keep the pointer must also keep that memory alive and reserved until the last use.

Related entries: Helper function (§2.19), Hoare triple (§3.31), Termination versus time bound (§2.33), Helper allowlist (§2.20), Copy-only helper (§2.22), Instruction budget (fuel) (§2.32), Object lifetime (§4.1), Time-of-check to time-of-use (TOCTOU) (§9.26), A helper can invalidate an instruction-time bound (§17.4). Further reading: [2].