Section 2.39

Dispatch table

Definition

A dispatch table is an array §14.5 that maps a small integer, called the selector, to the code that handles it. The selector is typically an opcode §2.8, the number that names an instruction, or the number of a helper function §2.19. Dispatch means reading the entry at the selector and transferring control to it, by calling a function pointer or jumping to a code address. When the selector comes from untrusted input, the lookup must refuse out-of-range selectors before reading the table, and must define what happens for slots without a handler.

The idea

An interpreter §2.13 runs a loop: fetch the next instruction, find the code for its opcode, run that code. Finding the code with a chain of comparisons, first against opcode 0, then 1, and so on, costs up to one comparison per defined opcode for every instruction executed. A table indexed by the opcode costs one memory read and one indirect jump, a jump to an address held in data, whatever the opcode is. A C compiler turns a dense switch statement into such a table on its own. The same pattern selects a helper function from the number in a call instruction.

The table turns a number into a code address, and that is its risk. If an attacker chooses the number and nothing checks it, the attacker chooses which word of memory the processor treats as a code address. The result can be an immediate crash or a call into code the attacker picked. A dispatch table is safe only when every possible selector value has a defined outcome: either a call to a valid handler that this program may use, or a refusal.

How it works

  1. Read the selector at its declared width and sign. In eBPF §2.1 (extended Berkeley Packet Filter) the opcode is an 8-bit unsigned field, so it takes the values 0 to 255. A helper number sits in the 32-bit immediate field of the call instruction, which the instruction format declares as a signed integer.

  2. Check the range before reading the table. A table of NN slots has the valid indices 0 to N−1N-1, so the condition for selector ss is 0≤s<N0 \le s < N. Check the same value, at the same width, that will be used as the index. A table with one slot for every value the selector can take, such as 256 slots for an 8-bit unsigned opcode, needs no range check.

  3. Handle holes. A hole is a slot inside the range that has no handler. One design stores a null pointer §4.26, a pointer value that refers to nothing, and tests for it before every call. The other fills every hole with a refusal handler that only reports an invalid instruction, so every slot is safe to call and the fast path needs no test. The Linux kernel’s eBPF interpreter uses the second design: its 256-slot table starts with every slot pointing at code that reports an unknown opcode and stops, and only the defined opcodes are then filled in.

  4. Check permission. A populated slot means that the handler exists, not that this program may call it. A helper allowlist §2.20 decides that separately.

  5. Call through one function type. All handlers share one function type: the same parameter types and the same return type. In C, calling a function through a pointer of a different type is undefined behavior §4.12, so a cast that makes the table compile does not make the call valid.

  6. Keep the table constant. A table declared const can be placed in read-only memory, such as flash memory §6.6 on a microcontroller §6.1, so a memory-corruption bug elsewhere cannot overwrite a handler address. A table that changes at run time, for example when handlers are registered, needs a rule that keeps a handler in place while a call to it runs.

A loader can refuse a program that names a hole or an out-of-range helper once, before the program runs, which removes the check from every call. That is sound only if the table cannot change after loading.

Worked example

A 32-bit microcontroller has 4-byte pointers. A helper table T of N=5N = 5 slots starts at address 0x20000100 and occupies 5×4=205 \times 4 = 20 bytes, up to 0x20000113. This example reserves helper number 0 to mean no helper, so slot 0 is empty; slots 1, 2 and 4 hold handlers; slot 3 is a hole. Slot ss lies at address 𝟶𝚡𝟸𝟶𝟶𝟶𝟶𝟷𝟶𝟶+4s\texttt{0x20000100} + 4s.

Converting −1-1 to an unsigned 32-bit value gives 232−1=4,294,967,2952^{32} - 1 = 4{,}294{,}967{,}295, written 0xFFFFFFFF. Without a check the conversion does not help: the byte offset 4,294,967,295×44{,}294{,}967{,}295 \times 4 is 0x3FFFFFFFC, a 32-bit address calculation keeps only 0xFFFFFFFC, and 0x20000100 plus 0xFFFFFFFC wraps around to 0x200000FC, the same word as before. With the check, the single unsigned comparison §2.12 4,294,967,295<54{,}294{,}967{,}295 < 5 fails and the call is refused.

Stated precisely

Let the table have NN slots, let D⊆{0,…,N−1}D \subseteq \{0, \dots, N-1\} be the set of slots that hold a handler, and let A⊆DA \subseteq D be the set of handlers the current program may call. Read the selector ss as an ordinary integer, using the width and sign that its field declares. The dispatch contract is call the handler in slot s⇔0≤s<N∧s∈D∧s∈A,\text{call the handler in slot } s \iff 0 \le s < N \ \wedge\ s \in D \ \wedge\ s \in A, and in every other case take the defined refusal path, which reports a fault and ends the call. The slot T[s]T[s] is read only after 0≤s<N0 \le s < N has been established.

One unsigned comparison checks both bounds. In two’s complement, the usual encoding of signed integers, a negative ww-bit value ss has the same bits as the unsigned number s+2ws + 2^{w}. Let u(s)u(s) be that unsigned reading: u(s)=su(s) = s if s≥0s \ge 0 and u(s)=s+2wu(s) = s + 2^{w} if s<0s < 0. If 0<N≤2w−10 < N \le 2^{w-1}, then u(s)<Nu(s) < N holds exactly when 0≤s<N0 \le s < N. For s≥0s \ge 0 the two tests are the same test. For s<0s < 0, the smallest possible value is −2w−1-2^{w-1}, so u(s)≥2w−2w−1=2w−1≥Nu(s) \ge 2^{w} - 2^{w-1} = 2^{w-1} \ge N, and the comparison fails as it should. For w=32w = 32 this covers every table of up to 2312^{31} slots.

Limits and common mistakes

Range is not presence. The check 0≤s<N0 \le s < N alone lets every hole through.

A count constant such as HELPER_MAX is the first invalid index, not the last valid one; comparing with ≤\le instead of << reads one slot past the end.

A refusal handler must refuse. A default slot that sends unknown numbers to a general-purpose or privileged handler turns a wrong number into a granted power.

On a processor that executes speculatively, running instructions ahead before an earlier comparison is resolved, a correct check can still be bypassed for a moment: the processor reads T[s]T[s] for an out-of-range ss and leaves a trace in its cache. This is Spectre variant 1, bounds check bypass. Where it matters, the index is also clamped with branch-free arithmetic; Linux provides array_index_nospec for this.

A writable table of function pointers is a classic attack target: overwriting one entry redirects every later call through it.

If a handler can be unloaded between reading its slot and calling it, the call runs code that no longer exists, a use-after-free §4.6 of code.

A switch statement handles range and holes through its default branch, which must refuse. A computed jump written by hand, through a compiler extension, needs the explicit checks above.

Questions answered

What happens for holes, negative values, and the maximum identifier?

In a correct dispatch table all three take the same defined refusal path: the call is refused and a fault is reported, without reading memory outside the table and without calling anything. A dispatch table maps a number, such as an opcode or a helper function number, to the code that handles it, and a table of NN slots has the valid indices 0 to N−1N-1.

A test suite exercises each of these values, together with a populated slot that the program is not allowed to call.

Related entries: Helper allowlist (§2.20), Opcode (§2.8), Interpreter (§2.13), Null pointer (§4.26), Native application binary interface (ABI) (§12.47).