Section 21.9

StackPatch: reconstructing execution state at a patch boundary

Full name: StackPatch is a name, not an abbreviation. It joins the words stack, as in stack frame, and patch.

What it is

StackPatch is a hotpatching §14.33 framework for microcontroller §6.1 firmware: it repairs a known vulnerability in running code without a restart. Ming Zhou and six co-authors described it in a paper accepted at the 2025 ACM (Association for Computing Machinery) Conference on Computer and Communications Security. At the start of the vulnerable code, a breakpoint §14.29 makes the processor run an exception handler §6.15. The handler saves the registers on the stack as one block, the stack frame; patch code edits the saved frame; the handler then resumes the program from the edited frame.

Basis of this account

This account rests on the authors’ preprint on the arXiv server, arXiv:2509.10213, version 1 of 12 September 2025: Ming Zhou, Xupu Hu, Zhihao Wang, Haining Wang, Hui Wen, Limin Sun and Peng Zhang, “Dynamic Vulnerability Patching for Heterogeneous Embedded Systems Using Stack Frame Reconstruction”. The arXiv record states that the paper was accepted at ACM CCS 2025 (the Conference on Computer and Communications Security) and links the publisher’s digital object identifier (DOI) 10.1145/3719027.3765200. The published text was not read or compared. Sections 1 to 3, Section 4.1 and the first part of Section 4.2, Sections 6 to 8, and Tables 4 and 5 were read; the rest of Section 4, Section 5 (related work) and the appendix were not. Nothing was run or measured for this account.

What the paper claims

The authors make these claims.

How it works

On the patching host, before deployment.

  1. An analyst locates the vulnerable code by hand. The analyst builds the vulnerable and the officially fixed firmware, disassembles both, and compares them with the binary-comparison tool BinDiff.

  2. The update point is the first instruction at which the two versions differ in meaning. It must not be a stack operation (push or pop), it must lie where a trigger can be set, and it must not lie in StackPatch’s own update service. Every variable that the fix reads must already hold its value there; the authors check this with data flow analysis §3.15 in the reverse-engineering tool Ghidra.

  3. Two maps are built. From Ghidra’s intermediate representation §3.37, a machine-independent form of the code, StackPatch derives which register holds each local variable at the update point. From the order in which the modified exception handler saves registers, it derives the offset from the stack pointer at which each register is saved. Composing the two maps gives each variable’s position in the saved frame.

  4. The official C fix is rewritten so that every variable becomes a read or a write at its position in the frame. The patch may change the saved return value and the saved return address, but it may not push or pop.

  5. A filter rejects a patch that contains an unbounded loop, a call to a C function or a dangerous instruction, and a patch that is too slow. The delay is Tstackpatch=Texception+Tdispatch+TpatchT_{\mathrm{stackpatch}} = T_{\mathrm{exception}} + T_{\mathrm{dispatch}} + T_{\mathrm{patch}}, the time of the exception handler, of finding the patch and of running it. With WW the timeout §9.34 of the watchdog §6.16, the timer that resets a device that stops responding, and CC the worst-case execution time §9.33 of a critical task, the delay must stay below T=W−CT = W - C.

On the device.

  1. An update service of the lowest priority (in an RTOS, the idle task; on bare metal, the end of a control cycle) receives a list of patches over Bluetooth, Wi-Fi or a wired debug or serial connection, stores each patch in RAM and sets its trigger.

  2. The trigger is a hardware breakpoint (an address comparator in the processor, of which 2 to 8 are typically available), a software breakpoint (the instruction at the update point is replaced by a breakpoint instruction such as bkpt on Arm, ebreak on RISC-V or break on Xtensa; this works only for code in RAM) or a hook (a jump placed in the code).

  3. When the program counter §2.9 reaches the update point, the processor raises an exception. The handler selects the stack in use (Arm Cortex-M processors have two stack pointers, one for the operating system and interrupts §6.11 and one for tasks), reserves space, and saves the return address first and then the general registers. This saved block is the frame ff. The handler sets the processor status so that the patch runs at the priority of the interrupted task; more urgent tasks can still preempt it.

  4. A dispatcher finds the patch for this address by binary search in a table, and the patch runs on the frame, turning it into the edited frame f′f'.

  5. The handler restores the general registers from f′f', restores the return address last, releases the space and returns. Execution continues at the return address stored in f′f'.

The edited return address decides where execution continues. Pass sets it just past the update-point instruction. That instruction never ran: the trigger fires before it executes, and a software breakpoint has even overwritten it. The patch must therefore perform the instruction’s work itself. Redirect sets it either to the vulnerable function’s own return instruction, so that the function returns to its caller at once, or to the first instruction after the vulnerable code.

Worked example

This is the preprint’s own example; only the packet values are invented. The vulnerability CVE-2018-16601 (CVE stands for Common Vulnerabilities and Exposures, a public catalog of vulnerabilities) affects the network code of FreeRTOS, a widely used open-source RTOS, up to version 10.0.1. The function prvProcessIPPacket, which handles a received IP (Internet Protocol) packet, does not check the header length that the packet declares. The official fix adds a check: the header length must not exceed the received data length minus the 14-byte Ethernet header, and must be at least 20 bytes, the minimum IPv4 (Internet Protocol version 4) header.

The firmware is compiled for an Arm Cortex-M4 into Thumb-2 §4.40 code, which mixes 16-bit instructions of the compact Thumb instruction set with 32-bit instructions. In this code the header length is already in register R6 at the update point 0x8004CCA. That instruction, ldrb.w r8, [r9, #0x17], is 4 bytes long and loads one byte, the protocol field of the packet, into register R8. It is the first instruction whose meaning differs between the versions, and a software breakpoint replaces it.

The frame is a sequence of 32-bit words counted from the stack pointer, which the patch, a C function, receives as sp, a pointer to 32-bit words. Word 0 holds the value that tells the exception return which stack and mode to resume. Words 1 to 8 hold R4 to R11, which the handler saved. Words 9 to 16 hold R0, R1, R2, R3, R12, the link register §4.45 (LR), the return address and the combined program status register (xPSR), which the processor saved automatically on entry. So *(sp + 3u) (the suffix u marks an unsigned constant) is word 3, at byte offset 3×4=123 \times 4 = 12: the saved R6, the header length. The descriptor of the received buffer is addressed by R4 (word 1), the packet by R9 (word 6), the return value R0 is word 9 (byte offset 9×4=369 \times 4 = 36) and the return address is word 15 (byte offset 15×4=6015 \times 4 = 60).

The patch does the official fix’s work on the frame.

Take a received frame of 60 bytes, so the data length is 60 and the bound is 60−14=4660 - 14 = 46. The low 4 bits of the first header byte give the header length in 4-byte words. An honest packet whose first header byte is 0x45 declares 5×4=205 \times 4 = 20 bytes. Since 20≤4620 \le 46 and 20≥2020 \ge 20, the patch passes: R8 receives the protocol byte and the function continues as before. A crafted packet whose first header byte is 0x4F declares 15×4=6015 \times 4 = 60 bytes. Since 60>4660 > 46, the patch makes the function return eReleaseBuffer before it reads past the received data.

On the nRF52840 §6.4 board, whose Cortex-M4 runs at 64 MHz (megahertz, millions of cycles per second), the largest average total the authors report for that board, 259 cycles, is 259/64≈4.05259 / 64 \approx 4.05 microseconds.

What this account does not establish

The results are the authors’ own, and this account reproduced none of them.

Locating the vulnerable code and choosing the update point are manual; the authors list this as a limitation. The frame positions are correct only if the register map that the analysis derived matches the compiled code at the update point.

The filter checks time, loops, calls and kinds of instruction. It does not show that the patched function behaves like the officially fixed one, nor that the frame the patch leaves behind is valid for the code at the new return address. Both rest on the analyst’s choice of update point and return target.

Under Pass, the patch must redo the replaced instruction completely. If that instruction also set the condition flags §4.42, the bits in the status register that record the outcome of a comparison, the patch would also have to update word 16. This is an observation of this account, not of the paper; the paper’s example instruction is a load, which sets no flags.

The authors state that StackPatch targets single-core processors. With several cores, per-core caches could disagree during a hot patch, and they leave the handling of this to future work.

Patches live in RAM and are lost when the device loses power, as the authors note.

The preprint does not describe how a device checks that a received patch comes from an authorized sender.

The authors state that StackPatch does not robustly support fixes that change several multi-line macros or complex expressions at once, or fixes that change build or configuration files.

Questions answered

What must a reconstructed frame preserve?

A reconstructed frame must preserve everything that the code at the resume address will read, in exactly the place and form that this code expects. The frame is the block of registers that an exception handler saved on the stack; a patch edits it, and the handler reloads it, so the processor resumes in whatever state the edited frame describes. In detail, the frame must hold:

Memory-safety checks, which show only that the patch writes inside the frame, establish none of these properties. StackPatch (Zhou and others, 2025), a hotpatching framework for microcontrollers, protects several of them through design rules: the update point avoids stack operations, a patch may not push or pop, it redirects only to the function’s own return instruction or to the instruction after the vulnerable code, and the handler restores the return address last. The others, above all the live values and the full effects of a replaced instruction, still rest on the analyst’s choice of update point and return target and on the patch code itself.

Related entries: Live state transfer and representation invariants (§19.5.2), Native application binary interface (ABI) (§12.47), Dynamic patching and hot update (§14.33), Breakpoint and watchpoint (§14.29), Exception and fault handler (§6.15), Link register (LR) (§4.45), Live-in register (§4.44), Condition flags (APSR) (§4.42), RapidPatch: virtual patches and trigger contracts (§21.5), HERA: hardware-assisted hotpatching (§21.6), AutoPatch: deriving a hotpatch from an official repair (§21.7), Kintsugi: code shadowing and a patch-application boundary (§21.8). Further reading: [210].