Section 21.8

Kintsugi: code shadowing and a patch-application boundary

Full name: Kintsugi is not an abbreviation. The authors take the name from the Japanese art of joining the pieces of broken pottery with gold, which, in their words, embraces the history of the break rather than hiding it.

What it is

Kintsugi is a hotpatching §14.33 framework: software inside a device that installs small fixes into its running code without a restart. Mackensen, Niesler, Blanco, Davi and Moonsamy published it at the 2025 USENIX Security Symposium. It is meant for real-time devices (devices that must meet fixed deadlines) that run their firmware from a copy in RAM, a practice the paper calls code shadowing. Kintsugi applies each fix during a context switch, when the operating system stops one task and starts another, and uses memory protection hardware against tampering by other tasks.

Basis of this account

This account rests on the published paper: Philipp Mackensen, Christian Niesler, Roberto Blanco, Lucas Davi and Veelasha Moonsamy, “Kintsugi: Secure Hotpatching for Code-Shadowing Real-Time Embedded Systems”, Proceedings of the 34th USENIX Security Symposium, Seattle, August 2025, pages 1847 to 1866. USENIX is the name of the computing-systems association that runs the symposium. The open-access PDF was read from https://www.usenix.org/conference/usenixsecurity25/presentation/mackensen. Sections 1 to 9 and Section 11 were read in full and Section 10 (related work) in part. The appendices and the published source code were not read. Nothing was run or measured for this account.

What the paper claims

The authors make these claims.

How it works

Setting and threat model §1.2. The device has a memory protection unit §6.8 (MPU), a hardware unit that gives each memory region its own read, write and execute permissions; the prototype’s MPU has 8 regions. The firmware runs from RAM under an RTOS with fixed-priority preemptive scheduling §6.37: the highest-priority task that is ready always runs, and it preempts any lower-priority task. The MPU is set up so that no memory region is both writable and executable. Hotpatches arrive over a channel that already provides encryption and digital signatures §5.14. The attacker is remote, has taken over one unprivileged task through a vulnerability, and wants to subvert the patching: undo a fix, alter a stored hotpatch or redirect the write. The attacker cannot send hotpatches of their own. Whether the code of a hotpatch is itself correct is outside the paper’s scope.

The problem. A simple design runs the patcher as a low-priority task that writes the fix into the code. A higher-priority task can preempt that task halfway through the write and then run half-written code. Turning interrupts §6.11 off around the write makes it atomic, but holds back urgent tasks for an unpredictable time. The authors observe that the RTOS already contains a short critical section that no task can enter in the middle: the context switch, which must save one task’s registers and load another’s as one indivisible step. Kintsugi does its write there.

Kintsugi has three parts.

  1. The Manager is a low-priority task. It first copies an incoming hotpatch into a quarantine region that only the Manager can access and that cannot execute, so that no other task can change the hotpatch after it is checked and before it is used; such a change would be a time-of-check to time-of-use §9.26 (TOCTOU) error. It checks the hotpatch’s integrity §1.9 with a signature scheme that the paper leaves to the implementer. It then validates the hotpatch’s description: the target address must lie in a table of permitted regions, and the sizes must fit. It stores the description in a fixed slot and the code in a separate code region. To schedule the hotpatch, it fills three fields of a small record called the applicator context: a status, the target address and the bytes to write.

  2. The Applicator does the write. It runs inside the context switch, and only when the task about to run is the Manager and a hotpatch is scheduled. It copies the bytes to the target address, which takes 7 inline assembly instructions in the prototype, and marks the hotpatch as applied. It is the only part allowed to write the firmware.

  3. The Guard runs in every context switch, in privileged mode, and reprograms the MPU according to which task ran before and which runs next. By default every task sees the firmware, the hotpatch regions and the applicator context as read-only. While the Manager runs, it may write the quarantine, the slots, the code region and the applicator context, but not the firmware. The Guard opens the firmware for writing only around the Applicator and closes it again at once. Only the firmware and the hotpatch code may execute. A forbidden write raises a fault that Kintsugi lets pass without effect, so the memory stays unchanged.

Hotpatches come in two types. A replacement hotpatch overwrites instructions in place, for example to turn a signed comparison §2.12 into an unsigned one. A redirect hotpatch overwrites 8 bytes at the start of the vulnerable code with a trampoline §14.28, a short piece of code that jumps to new code stored elsewhere. The 8 bytes are the instruction ldr pc, [pc, #0] followed by the 32-bit address of the hotpatch code. The instruction loads the 32-bit word that follows it into the program counter §2.9 (PC), the register that holds the address of the next instruction, so execution continues at the hotpatch code wherever it lies in memory. To connect Kintsugi to an RTOS, a call to the Guard is added to the context-switch code: to the function vTaskSwitchContext in FreeRTOS and to z_arm_pendsv_c in Zephyr.

Worked example

This example is invented for teaching. Only the two cycle counts and the clock rate come from the paper; the code layout and the tasks do not.

The cost of the boundary. At 64 MHz one cycle lasts 1/64,000,000 of a second, which is 15.625 nanoseconds. The ordinary 38-cycle check therefore takes 38×15.625=593.7538 \times 15.625 = 593.75 nanoseconds, about 0.59 microseconds, and the 216-cycle check-and-write takes 216×15.625=3375216 \times 15.625 = 3375 nanoseconds, about 3.38 microseconds. At an assumed 1000 context switches per second, the ordinary check costs 1000×38=38,0001000 \times 38 = 38{,}000 cycles per second, which is 38,000/64,000,000≈0.059%38{,}000 / 64{,}000{,}000 \approx 0.059\% of the processor’s time.

An atomic write that is still unsafe. A function parse in RAM consists, at its start, of four 16-bit Thumb instructions (the Thumb instruction set §4.40 is the compact instruction encoding §2.7 of Arm Cortex-M processors) at the addresses 0x20001000, 0x20001002, 0x20001004 and 0x20001006. The instruction at 0x20001002 is blx r3, a 16-bit call through a register to a function that waits for the next message, so the call returns to 0x20001004. A redirect hotpatch replaces the 8 bytes from 0x20001000 to 0x20001007. Its first 4 bytes are the 32-bit instruction ldr pc, [pc, #0], and its last 4 bytes are the address of the new code, 0x20003001: the new code starts at 0x20003000, and Arm Cortex-M processors require bit 0 of an address loaded into the PC to be 1, which marks Thumb code. In memory the address word is stored least significant byte first, so the halfword at 0x20001004 becomes 0x3001 and the halfword at 0x20001006 becomes 0x2000.

Task A, the network task, has the highest priority. It called the waiting function from parse and is blocked, so its stack holds a return address that points to 0x20001004. Task B, whose priority is below the Manager’s, also runs parse; it was preempted after executing the instruction at 0x20001000, so its saved PC is 0x20001002. The Manager becomes ready, and during the context switch into the Manager the Applicator writes the 8 bytes. No task runs during the write, so the write is atomic.

When task B resumes, the processor fetches from 0x20001002, the second half of the 32-bit ldr instruction, and decodes it together with the following halfword as an instruction that neither the old nor the new code contains. When task A’s message arrives, the waiting function returns to 0x20001004, where the processor decodes 0x3001 as adds r0, r0, #1 and 0x2000 as movs r0, #0. Task A thereby destroys the result of the call and then continues at 0x20001008 in the old code, running neither the old function correctly nor the fix. Both tasks go wrong although the write itself was atomic.

What this account does not establish

The numbers are the authors’ own, and this account reproduced none of them. The cycle counts are micro-benchmarks. The authors state that they did not analyze schedulability §9.37, that is, whether every task still meets its deadline, and that such an analysis is needed for each real system.

Kintsugi’s atomicity §8.3 is atomicity with respect to tasks: no task runs between the first and the last byte of a write, so the write is the update’s linearization point §8.6, the single instant at which the change takes effect for all tasks. That alone does not make the moment a safe update point. A suspended task whose saved PC lies inside the replaced bytes, or whose stack holds a return address that points inside them, resumes in the middle of the new bytes, as the worked example shows. Code that is still running in the old version of a function may also disagree with patched code about the meaning of shared data. The parts of the paper read for this account do not discuss these cases.

The context switch stops tasks, not necessarily every interrupt handler §6.12. In FreeRTOS on Arm Cortex-M processors, for example, the context-switch code masks only the interrupts whose urgency is at or below a configured level (configMAX_SYSCALL_INTERRUPT_PRIORITY); a more urgent interrupt handler can still run in the middle of it. An interrupt handler that calls the code being patched could see a partly written trampoline. On a processor with several cores, the other cores keep running while one core switches context.

Newly written instructions take effect only when the processor actually fetches them. On processors with instruction caches or prefetch buffers this needs barrier or cache-maintenance instructions. The parts of the paper read for this account do not discuss this point.

The paper does not fix a signature scheme, and the Manager’s measured times leave out the signature check. The authors also note that an attacker who corrupts a hotpatch before the Manager copies it causes the hotpatch to be rejected, which blocks the fix: a denial of service §1.28 that Kintsugi does not prevent.

Hotpatches stay in RAM until the next reboot. After a reboot they must be applied again, or the fixes must arrive in a firmware update.

The prototype needs four MPU regions. The authors point out that an RTOS that already uses many MPU regions may not have four free.

Questions answered

Is any context switch a universally safe update point?

No. A context switch is a good place to make the write of an update, because no task runs while it executes. A short write there is therefore atomic with respect to every task: each task sees either all of the old bytes or all of the new ones. Kintsugi, a hotpatching framework for real-time devices (Mackensen and others, 2025), uses the context switch into its own low-priority task for exactly this purpose. A safe update point needs more than that. It also needs these conditions, which a context switch does not establish:

  1. no suspended task’s saved program counter lies inside the bytes being replaced;

  2. no return address on any task’s stack points into those bytes, and every stack frame that will return into changed code still matches what that code expects;

  3. data that the old and the new code share has a form that both read the same way, or is converted at the same instant;

  4. nothing else runs during the write, neither an interrupt handler that the context switch leaves unmasked nor another processor core;

  5. the processor fetches the new bytes, not stale copies held in a prefetch buffer or an instruction cache.

A context switch settles only the question of interleaving, meaning whether another task can run in the middle of the write. The other conditions must be checked for the specific patch and the specific tasks, or met by choosing a quiescent point §14.34: a moment at which no task is executing, or will return into, the code being changed.

Related entries: Quiescent state for a live update (§19.5.1), Linearization point (§8.6), Dynamic patching and hot update (§14.33), Quiescent point (§14.34), Trampoline (§14.28), Memory protection unit (MPU) (§6.8), Time-of-check to time-of-use (TOCTOU) (§9.26), Instruction-cache coherence and executable-code publication (§19.5.3), RapidPatch: virtual patches and trigger contracts (§21.5), HERA: hardware-assisted hotpatching (§21.6), AutoPatch: deriving a hotpatch from an official repair (§21.7), StackPatch: reconstructing execution state at a patch boundary (§21.9). Further reading: [209].