Section 2.23

Guest context

Definition

The guest context is the input a host gives a guest program for one run: a block of memory describing the event the run is about, such as a received packet or a sensor reading. A guest program is code that a host runs inside a virtual machine §2.3 (VM). In eBPF §2.1 (extended Berkeley Packet Filter), the host passes the context’s address in register R1. The context’s contract states which fields exist, their offsets and units, which bytes the guest may read or write, and how long they stay valid.

The idea

A guest program decides about one event: accept or drop a packet, raise an alarm or stay quiet. It can decide only from what the host gives it. Suppose the host receives a 20-byte packet but copies only its first 12 bytes into the context, to save memory. If the context reports “length 20” and nothing else, a guest that correctly checks its reads against that length can read up to 8 bytes past the copy, into host memory. The guest’s check was correct; the number it checked against was wrong.

The context is the guest’s whole view of the event, and every check that the guest or the virtual machine makes rests on numbers taken from it. Building the context is therefore trusted host work, as much a part of safety as the checks themselves. A judge who rules from a case file is a fair picture: even a perfect judge rules wrongly from a file with a missing page, or with a page from another case. The picture stops at change. A paper file does not change while the judge reads it, but a context in shared memory can, unless the host prevents it.

How it works

The life of a context for one run follows these steps.

  1. Build. Before the run, the host writes a value for this event into every byte the guest may read. Padding, the unused bytes a compiler inserts between fields, is either set to zero or made unreadable, so that it cannot carry data from an earlier event.

  2. Pass. The host starts the guest with the context’s address in R1, the register that holds the first argument in the eBPF calling convention §2.6.

  3. Check each access. Every guest read or write through the context’s address is checked: its offset and width must fall inside one field the guest may access, with the right permission. The Linux eBPF verifier §3.3, which checks programs before they run, asks the program’s type, through a function named is_valid_access, whether a given offset and size may be read or written, and rejects the program otherwise.

  4. Reach variable-length data through bounds. Packet bytes vary too much in number to sit in fixed fields, so the context holds two addresses, where the data starts and where it ends, and the guest must compare against the end before every read. In the Linux eXpress Data Path (XDP), a hook that runs programs on arriving packets, these fields are data and data_end, and the verifier rejects any packet read that no such comparison has shown to be in bounds.

  5. Keep it stable. During the run, nothing else writes the bytes the guest may read: the host owns the context for that run, or the guest gets a private copy. A helper function §2.19 that may change the data, for example by resizing a packet, can make every earlier bounds check on the data wrong, so the Linux verifier forgets all checked packet pointers after a call to such a helper.

  6. Release. After the guest returns, the host may reuse the context’s memory for the next event. Neither the guest nor any helper may keep a pointer into it.

The context the guest sees need not be the host’s internal structure. Linux gives networking programs a public structure, __sk_buff, whose layout stays fixed across kernel versions. The verifier rewrites each access to one of its fields into an access to the matching field of the kernel’s internal packet structure, whose layout may change between versions. The context’s layout, units and byte order §4.21 are part of the guest-host ABI §2.37 (application binary interface), the agreement on how guest and host exchange data.

Worked example

A host defines a 24-byte context.

offset size field
0 4 event code
4 2 original packet length, here 20
6 2 copied length, here 12
8 4 arrival time in milliseconds
12 12 the first 12 bytes of the packet

Offsets and sizes are in bytes, and 12+12=2412 + 12 = 24. The packet bytes occupy context offsets 12 to 23. The guest wants a 2-byte field at packet offset 10, then one at packet offset 13.

Checked against the copied length 12: offset 10 needs packet bytes 10 and 11, and 10≤12−2=1010 \le 12 - 2 = 10 holds, so the read is allowed. Offset 13 fails, since 13≤1013 \le 10 is false. Checked wrongly against the original length 20: offset 13 passes, since 13≤20−2=1813 \le 20 - 2 = 18. The read then fetches packet bytes 13 and 14, which sit at context offsets 12+13=2512 + 13 = 25 and 12+14=2612 + 14 = 26, outside the 24-byte context. The arithmetic was right and the field was wrong, and the result is an out-of-bounds read §4.5 of host memory.

A stability failure needs no bad offset. Suppose the context holds a message number 5 and an amount 100, and another thread rewrites it to message number 6 and amount 900 while the guest runs. The guest reads the number first and gets 5, then reads the amount and gets 900. Every read was in bounds, yet the guest decides on the pair (5, 900), which no message ever contained. The host must give the guest a coherent snapshot §9.23: values taken at one instant that no writer changes while the guest runs.

Stated precisely

Let RR be the set of context offsets the guest may read and W⊆RW \subseteq R the set it may also write, where ⊆\subseteq means that every element of the left set is in the right set. A context meets its contract for one run when four conditions hold.

  1. Initialized: before the guest starts, every offset in RR holds a value the host wrote for this event.

  2. Bounded: a guest access of width ww at offset oo is allowed only if all of the offsets o,o+1,…,o+w−1o, o + 1, \dots, o + w - 1 are in RR for a read, or in WW for a write, and lie inside one field.

  3. Stable: every offset in RR that is not in WW holds the same value at every guest read during the run, unless the contract names a helper that may change it; after a call to such a helper, earlier checks on those bytes no longer count.

  4. Live: the context’s memory stays allocated to this run from the guest’s start until the guest and every helper it called have finished with it, and no pointer into it is used afterwards.

For data reached through a start and an end, the bound for a ww-byte read at data offset oo is o≤L−wo \le L - w, where LL is the number of bytes actually present, the copied length in the example, and w≤Lw \le L has been checked first. A fifth property, that each field’s value is correct for the event in its stated units and byte order, is the host’s responsibility alone; no check on the guest can establish it.

Limits and common mistakes

Initialized is not the same as correct. A timestamp of 0 is initialized and still wrong. Zeroing every field prevents leaks of old data but does not supply a missing sensor value.

Bounds checks do not hide secrets. If the host puts a key in the context, a guest may read it within bounds. Put in the context only what the guest needs; this is the principle of least privilege §1.15.

Padding is the usual source of old data. A 1-byte field followed by a 4-byte integer field usually leaves 3 bytes of padding between them, holding whatever the previous event left there, unless the host zeroes the context before filling it or the access check refuses those offsets.

Writable context fields carry data back to the host. A value the guest wrote is untrusted input for the host, which must check it before relying on it.

Units and byte order fail silently. A host that writes a time in microseconds for a guest that expects milliseconds causes no fault, only wrong decisions.

Questions answered

Which context bytes are initialized and stable during execution?

Every context byte the guest may read must be initialized by the host for the current event before the guest starts, and every one of those bytes that the guest may not write must stay unchanged until the guest returns; any exception must be named in the context’s contract. The guest context is the block of memory that a host hands a guest program, code running in a virtual machine, to describe the event a run is about.

Initialization covers padding too: the host zeroes the whole context before filling it, or the access check refuses the offsets the host does not fill. Stability comes from ownership: the host builds the context before the run and lets nothing else write it during the run, or it gives the guest a private copy. The exceptions are fields the guest itself may write; data that a helper function is allowed to change, after which earlier bounds checks no longer hold, which is why the Linux eBPF (extended Berkeley Packet Filter) verifier forgets its checked packet pointers after such a helper; and fields that another writer deliberately updates. The contract must list each exception. All context bytes stay valid until the run ends, and must not be used through a kept pointer afterwards.

Related entries: Guest-host ABI (§2.37), Deterministic invocation state (§2.35), Copy-only helper (§2.22), Runtime bounds check (§2.27), Struct padding and packing (§4.27), Endianness (§4.21), Coherent snapshot (§9.23). Further reading: [2].