Section 21.6

HERA: hardware-assisted hotpatching

Full name: Hotpatching of Embedded Real-time Applications.

What it is

HERA is a hotpatching §14.33 method for Arm Cortex-M microcontrollers §6.1: it repairs vulnerable firmware, the program stored in the device, while the device keeps running, without rewriting its flash memory §6.6 or restarting. It uses the Flash Patch and Breakpoint §14.29 (FPB) unit, a debugging circuit that can substitute the instruction fetched from a chosen address, to divert execution into patch code held in RAM. Christian Niesler, Sebastian Surminski and Lucas Davi published it at the Network and Distributed System Security (NDSS) Symposium in 2021. It targets devices with hard deadlines.

Basis of this account

This account rests on the paper in the proceedings of NDSS 2021, in the copy linked from the paper’s page on the NDSS Symposium website. The parts read were the abstract, the introduction, the background on the FPB unit, the design, the implementation, the example of patching FreeRTOS, a real-time operating system, the case study and the timing measurements. The measurements with several patches at once, the related work and the conclusion were not examined, and no Arm manual was consulted for this account. Nothing was run.

What the paper claims

The authors make these claims.

How it works

HERA works in four steps.

  1. Build the patch. The developer compiles the firmware with and without the fix, compares the two binaries instruction by instruction, and takes the inserted or changed instructions as the patch. The first changed instruction gives the insertion point. Because of a limitation of the FPB unit, the insertion point moves back to the nearest earlier instruction whose address is a multiple of 4. The developer adds small changes, such as restoring stack values and the frame pointer, the register that marks the current function’s area on the stack. A patch file holds the patch code, the insertion point, the trampoline §14.28, which is one branch instruction that leads toward the patch, and the choice of hardware breakpoint.

  2. Prepare. An updater task that runs at low priority, or in idle time, receives the patch and checks its integrity §1.9 and origin. It copies the code into a patch slot in RAM, adds an entry to a small dispatcher, and writes the insertion point and the trampoline into the tables of the FPB unit. Tasks of higher priority preempt this work, so it delays no deadline.

  3. Switch over. One register write enables the breakpoint. From then on, the FPB unit compares every instruction fetch with the insertion point.

  4. Redirect. When the processor fetches the instruction at the insertion point, the FPB unit supplies the trampoline instead: bl jump_section, a branch with link, which stores the return address in the link register §4.45 (LR). The jump section sits in flash memory. It saves registers and loads the program counter §2.9 with the address of the dispatcher in RAM, because on the authors’ board RAM lies beyond the reach of a branch instruction placed in flash. The dispatcher compares the value in LR with its entries to find the patch, the patch runs, and execution jumps back into the firmware.

The patch is ordinary code, and the real-time operating system can interrupt it like any other code. It lives in RAM, so a restart removes it; the paper notes that the updater would have to apply it again at every start. The number of active patches is limited by the number of comparators, and the paper states that a full FPB unit has up to six. One patch can still repair several faults if it replaces a larger stretch of code.

Worked example

The paper’s FreeRTOS case is real: FreeRTOS 9.0.0 contains CVE-2018-16601 in the function prvProcessIPPacket, and the official fix adds a bounds check on the length of an IP (Internet Protocol) header, which the binary comparison shows as 11 inserted instructions. The flash addresses below are invented to show what must be checked; the RAM address 0x20000074 is the patch location in the paper’s dispatcher listing.

  1. Alignment §4.22. Say the first inserted instruction would go at flash address 0x08016196. Only its lowest byte, 0x96, decides divisibility by 4, because 256 is a multiple of 4. That byte is 150, and 150=4×37+2150 = 4 \times 37 + 2, so the address is not a multiple of 4. The insertion point moves back to 0x08016194. The FPB unit replaces the 4 bytes from 0x08016194 to 0x08016197 with the 4-byte trampoline. If those bytes held two 2-byte instructions of the Thumb instruction set §4.40, the patch path must execute both, because neither runs any more at its old place.

  2. Reach. A bl instruction reaches about 16 MiB, that is 224=16,777,2162^{24} = 16{,}777{,}216 bytes, forward or backward from its own address plus 4. From 0x08016198 to 0x20000074 the distance is 0x17FE9EDC bytes, which is 402,562,780 bytes or about 384 MiB, roughly 24 times the reach. A direct branch is impossible, which is why the trampoline leads to a jump section in flash that loads the full 32-bit address into the program counter.

  3. Registers. The bl overwrites LR with 0x08016199: the address after the branch, 0x08016198, with its lowest bit set to mark Thumb code. The dispatcher uses this value to pick the patch, so each insertion point gives a distinct key. If the firmware still needed its old LR value, for example in a function that keeps its return address only in LR, that value is now lost. The insertion point must therefore lie where the old LR value is no longer needed, for example after the function has pushed it onto the stack.

  4. Time. At 42 MHz one clock cycle lasts 1/(42×106)1 / (42 \times 10^{6}) seconds, about 23.8 nanoseconds. The 196 nanoseconds that the paper measured for the redirection are 196/23.8≈8.2196 / 23.8 \approx 8.2 cycles. The patch’s own instructions come on top, and every task that runs this code path pays the total on every pass.

What this account does not establish

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

The method depends on the remapping feature of the FPB unit. The authors of RapidPatch §21.5 and of AutoPatch §21.7 note that newer Arm processors such as the Cortex-M7 lack it.

Comparators are few: up to six patches at a time, and a debugger that sets hardware breakpoints competes for the same comparators.

In the parts read, the paper describes no tool that checks a patch or its insertion point. Whether the displaced instructions are re-executed, the registers preserved and the patch itself correct rests on the developer.

Fixes that change data structures or program state would need state transfer, the conversion of existing data to the new layout, which HERA does not provide. The authors limit it to small security fixes.

A patch held in RAM disappears at a restart.

The timing figures cover the switch-over and the redirection, not the patch’s own running time. A deadline argument also needs the worst-case execution time §9.33 of the patch.

Questions answered

What must be checked before using a hardware event as a patch trigger?

Before a hardware event is used as a patch trigger, one must check that it fires exactly where and when the repair must happen, that the patch path preserves everything the interrupted code relies on, that execution resumes correctly, and that time and hardware resources suffice. A hardware event here is a signal from the processor’s own circuits, such as a breakpoint comparator that matches the address of an instruction; a patch trigger is the mechanism that diverts execution into repair code. HERA (Niesler and others, 2021), which uses the Flash Patch and Breakpoint (FPB) unit of Arm Cortex-M processors, illustrates each check.

  1. The event. An address comparator fires every time execution reaches the instruction at that address, in any task or interrupt handler §6.12, whatever the data. The patch must test the data itself.

  2. The instruction boundary. The replaced bytes must start where an instruction starts and must not split an instruction. They must not fall inside an IT block §4.41 (If-Then block), a group of instructions that execute under a shared condition. Every instruction they displace must be executed by the patch path or skipped on purpose. On the FPB unit the replaced unit is an aligned 4-byte word.

  3. The processor state. Know the mode, the stack and the interrupt priority in which the patch runs. A replaced instruction runs in the interrupted code’s own context; a breakpoint that raises a debug exception runs the patch inside an exception handler §6.15.

  4. Register preservation. Every register and condition flag that later code reads must hold the same value after the patch, apart from values the repair changes on purpose. A branch with link, for example, overwrites the link register (LR).

  5. Reentrancy §9.27. Another task or an interrupt handler can reach the same address while the patch runs, so the patch and its dispatcher must tolerate two runs at once.

  6. Continuation. State where execution goes after the patch, with which stack frame, and with which return value for the caller.

  7. Time. The redirection, the dispatch and the worst-case execution time of the patch must fit every deadline on every path that reaches the address.

  8. Capacity. The processor model must provide the feature, comparators are few (up to six on a full FPB unit, according to the paper), and a debugger competes for them.

  9. Activation and lifetime. All tables must be complete before the single write that enables the trigger, and a patch held in RAM disappears at a restart.

A trigger that fires is therefore not yet a repair. These checks show that the patch runs at the right place without disturbing the code around it; whether the patch itself fixes the fault is a separate argument.

Related entries: Probe and hook (§14.26), Exception and fault handler (§6.15), Trampoline (§14.28), Breakpoint and watchpoint (§14.29), Dynamic patching and hot update (§14.33), Link register (LR) (§4.45), Arm Thumb and Thumb-2 (§4.40), Reentrancy (§9.27), Worst-case execution time (WCET) (§9.33), RapidPatch: virtual patches and trigger contracts (§21.5). Further reading: [207].