Section 21.7

AutoPatch: deriving a hotpatch from an official repair

Full name: AutoPatch is a coined name, not an abbreviation.

What it is

AutoPatch is a method that automatically turns the official source-code fix for a vulnerability in microcontroller §6.1 firmware into a hotpatch: code that a running device executes, without a restart, to obtain the effect of the fix. Before release, it inserts calls to a patch dispatcher, called trampolines §14.28, into every function. When a fix appears, it rewrites the fix to run at a trampoline before the fix’s own position. Mohsen Salehi and Karthik Pattabiraman published it at the Conference on Computer and Communications Security (CCS) in 2024.

Basis of this account

This account rests on the authors’ preprint, arXiv:2408.15372, version 1 of 27 August 2024, posted on the public arXiv server. It states that the paper was accepted at CCS 2024 and carries the reference of the published version: CCS 2024, Salt §5.19 Lake City, 14 to 18 October 2024, published by the ACM (Association for Computing Machinery), https://doi.org/10.1145/3658644.3690255. The published text was not compared. The parts read were the abstract, Sections 1 to 5 (introduction, background, motivation with the threat model §1.2, design and implementation), the part of the evaluation on which vulnerabilities could be patched, and the discussion. The measurements of time and memory were not examined beyond the figures stated in the abstract and introduction, nor were the related work and the appendix. Nothing was run.

What the paper claims

The authors make these claims.

How it works

AutoPatch works on LLVM §12.18 IR §3.37, the intermediate representation of the LLVM compiler framework: a typed instruction language between C source and machine code. LLVM began as Low Level Virtual Machine; the project now uses the letters as a plain name. The paper assumes the IR is in static single assignment §3.38 (SSA) form, in which every variable is assigned in exactly one place.

  1. Instrument, once, before release. The developers of the operating system run an instrumentation §14.24 pass, a compiler stage that adds code, over every firmware function. It inserts a trampoline at four kinds of places: at the entrance of the function, after its local variables are set up; after every function call, whose effect would otherwise need analysis across functions; inside and after every complex loop, one whose number of iterations cannot be computed statically, for example because its condition reads memory through a pointer, or which is nested; and inside and after the body of every complex branch, such as a nested if. A trampoline returns at once when no hotpatch is active.

  2. Choose the trampoline. When a vulnerability is fixed, an analysis pass receives the patched function and the position of the fix. It picks the best trampoline: in the simplest case, the closest one before the fix that lies outside any loop or branch body; for a fix inside a complex loop, the trampoline inside that loop.

  3. Move the fix backwards. The pass walks from the fix back to the chosen trampoline. An instruction that assigns a variable used by the fix is folded into the fix: the variable is replaced by the assigned expression. An instruction that touches none of those variables is skipped. A simple loop or branch on the way that changes such a variable is copied into the hotpatch.

  4. Package. The result becomes an LLVM IR function. It receives the vulnerable function’s values in a data structure and returns PASS (continue the original function), REDIRECT (jump to a chosen point in it) or DROP (return to the caller). Each device maintainer compiles it to native code for the board.

  5. Install and run. The native hotpatch travels over an encrypted channel together with the address of its trampoline. The dispatcher, built on the runtime of RapidPatch, keys active hotpatches by the link register §4.45 (LR) value that their trampoline produces, saves the function’s context, calls the hotpatch, and changes the return address according to its result.

Worked example

The function and the fix are the paper’s (its Listings 1 and 3); every number is invented, and so are the types assumed below. CVE-2020-10021, an entry of the Common Vulnerabilities and Exposures (CVE) catalog, lies in the function infoTransfer of the USB mass-storage code of the Zephyr §6.33 operating system, which handles commands from a host computer. In the instrumented function:

The analysis starts at line 6 and walks back. Line 5 assigns no variable of the fix, so it is skipped. Line 4 assigns nn, so nn in the fix is replaced by its expression. The hotpatch, run at line 3, therefore tests whether the number formed from bytes 2 to 5, times BB, is at least MM; if so, it performs lines 7 to 10 and returns DROP with the value false.

Take B=512B = 512 bytes, M=1,048,576M = 1{,}048{,}576 bytes (1 MiB), and command bytes 2 to 5 equal to 0x00, 0x00, 0x10, 0x00. Then nn is hexadecimal 1000, which is 4096, and 4096×512=2,097,152≥1,048,5764096 \times 512 = 2{,}097{,}152 \ge 1{,}048{,}576. Both the official fix at line 6 and the hotpatch at line 3 reject the command. The largest accepted block number is 2047, because 2047×512=1,048,064<1,048,5762047 \times 512 = 1{,}048{,}064 < 1{,}048{,}576, while 2048×512=1,048,5762048 \times 512 = 1{,}048{,}576 is not below MM.

Two cases show what a statement of equivalence must settle. First, on a rejected command the official version writes the debug line of line 5 before rejecting; the hotpatched version rejects at line 3 and never writes it. The two versions agree only if debug output does not count as behavior. Second, suppose nn, BB and MM are 32-bit unsigned values, and take bytes 0x00, 0x80, 0x00, 0x00. Then n=8,388,608n = 8{,}388{,}608 and n×512=4,294,967,296=232n \times 512 = 4{,}294{,}967{,}296 = 2^{32}. In 32-bit unsigned arithmetic this wraps around to 0, so the official check accepts the command, while a translation that computed in 64 bits would reject it. A faithful hotpatch keeps the source’s 32-bit arithmetic, even where that arithmetic weakens the check. Whether the real code behaves like this depends on its actual types and constants, which this account did not check.

What this account does not establish

The results are the authors’ own, and this account did not reproduce them. The measurements of time and memory were not examined.

Equivalence is claimed, not proved. In the parts read, the paper gives no formal definition of functional equivalence and no proof. Its evidence is manual inspection and the judgment of two developers. The authors mention that a checking tool such as LLBMC (Low-Level Bounded Model Checker §3.29) could be applied to a hotpatch, but they did not test this.

The authors assume that the official fixes are free of bugs. A faithful translation of a wrong fix is still wrong.

The skipped instructions are judged only by whether they change a variable of the fix. The parts read do not discuss skipped instructions with effects outside the function, such as the debug output in the example, or other tasks and interrupt handlers §6.12 that might change the fix’s variables between the trampoline and the fix’s original position.

Fixes that change data structures or macros, and fixes spread over many functions, are out of scope.

Trampolines must be compiled into the firmware before release, so a device shipped without them cannot be patched this way. They also cost time when no hotpatch is active; the authors report at most 3.5% for real applications.

The position of the fix inside the function is an input that the developers of the operating system supply.

Questions answered

How would one state a translation-correctness obligation?

One states a translation-correctness obligation as a theorem that compares two systems on the same inputs: for every admitted starting state and every admitted input, the observable behavior of the translated system equals, or is permitted by, the observable behavior of the reference system. A translation-correctness obligation is what must be proved, or at least checked, to show that a translation kept the meaning of what it translated. Here the translation turns an official source-code fix into a hotpatch, code that a running device executes to obtain the effect of the fix. Stating the obligation means naming six things.

  1. The reference. The firmware built with the official fix, with the meaning of its source language fixed, including integer widths and overflow rules.

  2. The translated system. The unfixed firmware with the hotpatch installed, together with the trampoline, the dispatcher and the return codes that run it.

  3. Admitted starting states and inputs. For example: every state the firmware can be in, a hotpatch activated only while no call of the patched function is in progress, and any bytes an outside party can send.

  4. Observations. What counts as behavior: return values, writes to memory that outlives the call, messages sent, calls to other functions with their arguments, and whether the call ends. Say explicitly whether debug output and timing count.

  5. The environment. Which other tasks and interrupt handlers may run. Either none of them changes a variable that the moved check reads between the trampoline and the fix’s original position, or their steps are part of the model.

  6. The relation. Equality of observations, ∀s∈S0,∀i∈I:obs(H,s,i)=obs(R,s,i),\forall s \in S_0,\ \forall i \in I:\quad \mathrm{obs}(H, s, i) = \mathrm{obs}(R, s, i), where RR is the reference, HH the hotpatched system, S0S_0 the set of admitted starting states, II the set of admitted input sequences, and obs(P,s,i)\mathrm{obs}(P, s, i) the sequence of observations that system PP produces from state ss on inputs ii. The weaker one-way form is refinement: every observation sequence of HH is one that RR can also produce.

When a check is moved backwards, as AutoPatch (Salehi and Pattabiraman, 2024) does, the local step is the assignment rule [224] of Hoare logic §3.31. Suppose the only steps between the trampoline and the fix are assignments x:=ex := e with no observable effect. Then the condition cc evaluated at the fix has the same truth value as c[e/x]c[e/x], the condition with ee written in place of xx, evaluated at the trampoline. The rule needs ee to be evaluated with the source’s own arithmetic and nothing else to change xx or the variables of ee in between. Cases outside the supported meaning, such as fixes that change data structures, must be refused, not approximated.

Related entries: Refinement (§3.32), Forward simulation and observable refinement (§19.2.9), Hoare triple (§3.31), Intermediate representation (IR) (§3.37), Static single assignment (SSA) (§3.38), Dynamic patching and hot update (§14.33), RapidPatch: virtual patches and trigger contracts (§21.5), HERA: hardware-assisted hotpatching (§21.6). Further reading: [208, 224].