Section 21.5

RapidPatch: virtual patches and trigger contracts

Full name: RapidPatch is a coined name, not an abbreviation. In the title of this entry, a virtual patch is a patch that runs inside a virtual machine, and a trigger contract is everything a patch assumes about the place and the moment at which it runs; neither phrase is the paper’s own term.

What it is

RapidPatch is a hotpatching §14.33 framework for the firmware of microcontroller-based real-time devices: it repairs a vulnerability while the device keeps running, by executing a small patch program whenever execution reaches the vulnerable code. Patches are eBPF §2.1 (extended Berkeley Packet Filter) programs that run in a virtual machine §2.3 on the device, so that one patch source can serve devices with different processors. Yi He, Zhenhua Zou, Kun Sun, Zhuotao Liu, Ke Xu, Qian Wang, Chao Shen, Zhi Wang and Qi Li published it at the USENIX Security Symposium in 2022.

Basis of this account

This account rests on the paper as published in the proceedings of the 31st USENIX Security Symposium, August 2022, pages 2225 to 2242, in the open-access copy on the USENIX website; USENIX is the name of a professional association for computing. The parts read were the abstract, the introduction, part of the background, the threat model §1.2, the design, the implementation, the results on which vulnerabilities could be patched and on porting, the discussion, and the appendix table that lists each patch. The measurements of delay and overhead were not examined beyond the figures stated in the abstract. Nothing was run.

What the paper claims

The authors make these claims.

How it works

RapidPatch divides the work between the developers of the operating system or library, the maintainers of each device model, and the device.

  1. Write. From the official fix in C, a developer writes an eBPF patch, also in C, and a configuration file in YAML (YAML Ain’t Markup Language), a text format for structured data. The configuration names the install point, which is the entry of a function or of a basic block §3.14, a straight run of instructions with one entry. It also names the kind of trigger, the execution mode, and the native functions and global variables that the patch uses.

  2. Generate. For each firmware build, a device maintainer runs the patch generator. It looks up every named function and variable in the firmware’s symbol table §12.8, the list of names and addresses that the linker §12.4 produced. It replaces the names in the patch by these concrete addresses, compiles the result to eBPF bytecode §2.4 with the Clang §12.17 compiler, and finds the install address. The output is bytecode and a deployment configuration for that one firmware.

  3. Verify. A patch verifier, run offline, sorts patches into two kinds. A filter patch only checks inputs and returns a decision. It counts as safe when it writes no memory outside its own eBPF stack, calls no native C function and has bounded loops, and the authors let a safe patch be deployed without further tests. A code replace patch replaces vulnerable code; it may write memory and call native functions through a foreign function interface (FFI §16.15), so the verifier flags it for manual testing. The authors explain that the Linux eBPF verifier §3.3 is too strict here, because a patch must read global variables and the stack of the vulnerable function.

  4. Install. The patch travels over the device’s existing encrypted update channel. The installer can translate it to native code with a JIT §2.14 (just-in-time) compiler, implemented for Cortex-M3 and Cortex-M4 §6.3 processors. It then publishes a new patch list with one atomic switch, in the manner of read-copy update §9.20 (RCU), so that readers always see a complete list.

  5. Trigger. Execution reaches a patch in one of three ways. A fixed patch point, compiled into the firmware at function entries, works on every processor. The FPB (Flash Patch and Breakpoint §14.29) unit of Cortex-M3 and Cortex-M4 processors provides 6 points. Hardware breakpoints that raise a debug monitor exception, which hands control to a handler on the device itself instead of to an external debugger, a method the authors call KProbe, provide 8 points on Cortex-M3 to Cortex-M55 and on RISC-V.

  6. Run and return. A patch loader saves the registers of the interrupted function on the stack and hands them to a new virtual machine instance, so the patch can read the function’s arguments. The patch returns OP_PASS (continue the function), OP_REDIRECT (jump to a chosen basic block of the function, such as its error handling) or OP_DROP (skip the function and return to its caller), and the loader changes the return address to match. During the run, software fault isolation §2.41 (SFI) checks stores and jumps, and a counter of backward jumps stops loops at a threshold chosen from the processor speed and the delay the device tolerates.

Worked example

The vulnerability and the patch are the paper’s (its Figure 5 §18.2.5); the second firmware build is invented. In picoTCP, a small open-source network library for embedded devices, CVE-2020-17443 lies in the function pico_icmp6_send_echoreply. It answers an ICMPv6 echo request, the ping message of ICMPv6 (Internet Control Message Protocol for Internet Protocol version 6). The function copies the request’s payload with a length of transport_len minus 8, where transport_len is a 16-bit unsigned field and 8 is the header size. Take a packet with transport_len equal to 5. Integer promotion §4.18 first turns the 16-bit value into a signed int, so the subtraction gives 5−8=−35 - 8 = -3. The cast to a 32-bit unsigned length turns −3-3 into 232−3=4,294,967,2932^{32} - 3 = 4{,}294{,}967{,}293, and the copy runs far past both buffers. The official fix returns −1-1 when transport_len is below 8.

The eBPF filter patch, attached at the entry of the function, makes the same check by hand. It reads register r0 from the saved registers, because the Arm calling convention passes the first argument there; that argument points to the packet’s frame structure. The patch adds 38 to that pointer, the byte offset of transport_len in that structure, and reads 2 bytes. If the value is at least 8, the patch returns OP_PASS. Otherwise it returns OP_DROP with the value −1-1, so the caller sees the same result as with the official fix.

Now load the same bytecode on a second device whose build of the library places one extra 4-byte field before transport_len, so that the field sits at offset 38+4=4238 + 4 = 42. The bytecode still runs, because the instruction set is the same. But it reads bytes 38 and 39, which now belong to another field, and its decision has nothing to do with the packet length. If that device has a RISC-V processor, the first argument arrives in register a0, and the loader must place it where the patch expects r0. The patch generator does not help with the offset: it fills in the addresses of named functions and global variables, while the 38 is written into the patch source. The bytecode is portable; the patch’s assumptions are not.

What this account does not establish

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

Safe means harmless to the rest of the system, not correct. The authors write that the verifier can only ensure that patches do not damage the system, without guaranteeing that they are free of bugs, and that RapidPatch itself cannot ensure that a patch works properly.

Patches are written by hand from the official fix. For example, a mistaken offset might still point to memory that the patch may read. The verifier can then accept a patch that checks the wrong value.

Fixed patch points sit only at function entries. A fix that the official patch places in the middle of a function must be restated as a check on what is known at the entry, or must use a hardware trigger, which exists only on some processors and in small numbers, 6 or 8.

Fixes that change data structures or macros, or many functions at once, are out of scope: 6 of the 62 vulnerabilities.

Some patches depend on the processor. For CVE-2020-10024 and CVE-2020-10027 in Zephyr, the appendix describes filter patches that read the carry flag §4.42, which records the outcome of an unsigned comparison §2.12, in the Arm APSR (Application Program Status Register) after a compare instruction. A RISC-V processor has no such flags, so these patches have no meaning there.

The paper assumes that patches come from trusted sources, signed and encrypted in transit, and relies on the device’s existing update channel for that.

Questions answered

Why does bytecode portability not imply patch portability?

Bytecode portability does not imply patch portability because the bytecode only fixes how instructions execute, while a patch also depends on facts about one particular firmware build at the moment the patch runs. Bytecode portability means that the same instructions, here eBPF (extended Berkeley Packet Filter) bytecode, run with the same meaning on every device that has the virtual machine. Patch portability would mean that the same patch also repairs the same vulnerability on every such device. Between the two lies everything the patch assumes about its surroundings.

RapidPatch (He and others, 2022), a hotpatching system for microcontrollers §6.1, shows both sides. Its eBPF runtime runs the same bytecode on Arm, RISC-V and Xtensa processors, yet its tools generate device-specific bytecode for each firmware from one patch source, and the authors promise one patch per vulnerability only for devices with the same version of the operating system or library. To move a patch safely, state these dependencies explicitly, resolve them for each target build, and check the final bytecode with the checker of the virtual machine that will run it.

Related entries: Guest-host ABI (§2.37), HERA: hardware-assisted hotpatching (§21.6), AutoPatch: deriving a hotpatch from an official repair (§21.7), Dynamic patching and hot update (§14.33), Helper function (§2.19), Symbol table (§12.8), Struct padding and packing (§4.27), eBPF verifier (§3.3). Further reading: [157].