fix(core): show RSOD after a fault in an IRQ handler
What changed, and why it matters
This update changes how Trezor hardware wallets recover when something goes wrong inside an interrupt handler. Previously, the device could freeze silently instead of showing the red error screen (RSOD). Now it unwinds nested interrupt handlers and shows the error screen. The change is a robustness improvement, not a direct fix for an exploitable vulnerability, but the freeze behavior could have been used to make a device hang or confuse a user.
Treat as a reliability and minor security-hardening fix. Review the new `unwind_exception` and `ensure_thread_mode` paths for correctness, especially the use of `0xFFFFFFF1` handler return and the `irq_lock`/`irq_unlock` pairing during exception unwinding. No immediate user action is required beyond normal firmware update.
Security signals we found
Crash inside interrupt handler previously caused freeze instead of controlled error screen
New exception-unwinding assembly manipulates LR/xPSR/CONTROL and active exception bits
Guard macro changed from KERNEL_MODE to SECURE_MODE, expanding scope of affected code
Pending exception clearing now includes SysTick, reducing chance of spurious handler re-entry
Vendor changelog explicitly describes user-visible failure mode (freeze vs error screen)
Evidence from the diff
The commit reworks ensure_thread_mode() in sysutils.c so that when the firmware, bootloader, or prodtest crashes inside an IRQ handler, it unwinds all active exceptions and returns to privileged thread mode before showing the RSOD. It also broadens KERNEL_MODE guards to SECURE_MODE in sysutils.h, bootutils.c, and sysutils.c, and fixes reset_peripherals_and_interrupts() to clear pending SysTick as well as PendSV. The changelog frames this as ‘show the error screen instead of freezing’.
Changed components
core/embed/sys/startup/stm32/sysutils.ccore/embed/sys/startup/stm32/bootutils.ccore/embed/sys/startup/inc/sys/sysutils.hTrezor Model T / Safe 3 / Safe 5 / Safe 7 firmwareTrezor bootloaderTrezor prodtestInspect captured patch +88 / −38
### core/.changelog.d/noissue.fixed
@@ -0,0 +1 @@
+[T2T1,T2B1,T3B1,T3T1] Show the error screen instead of freezing when the firmware crashes inside an interrupt handler.
### core/embed/projects/bootloader/.changelog.d/noissue.fixed
@@ -0,0 +1 @@
+Show the error screen instead of freezing when the bootloader crashes inside an interrupt handler.
### core/embed/projects/prodtest/.changelog.d/noissue.fixed
@@ -0,0 +1 @@
+Show the error screen instead of freezing when prodtest crashes inside an interrupt handler.
### core/embed/sys/startup/inc/sys/sysutils.h
@@ -19,7 +19,7 @@
#pragma once
-#ifdef KERNEL_MODE
+#ifdef SECURE_MODE
#include <trezor_types.h>
@@ -87,4 +87,4 @@ __attribute((noreturn)) void jump_to_vectbl(uint32_t vectbl_addr, uint32_t r11,
__attribute((noreturn)) void jump_to_vectbl_ns(uint32_t vectbl_addr);
#endif
-#endif // KERNEL_MODE
+#endif // SECURE_MODE
### core/embed/sys/startup/stm32/bootutils.c
@@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifdef KERNEL_MODE
+#ifdef SECURE_MODE
#include <trezor_bsp.h>
#include <trezor_model.h>
@@ -40,8 +40,6 @@
#endif
#endif
-#ifdef SECURE_MODE
-
#ifdef STM32U5
// Persistent variable that holds the 'command' for the next reboot.
boot_command_t __attribute__((section(".boot_command"))) g_boot_command;
@@ -237,8 +235,6 @@ __attribute__((noreturn)) void reboot_or_halt_after_rsod(void) {
#endif
}
-#endif // SECURE_MODE
-
// args1 - vector table address of the next stage
// args2 - pointer to the startup_args_t struct
static void jump_to_next_stage_phase_2(uint32_t arg1, uint32_t arg2) {
@@ -284,4 +280,4 @@ void __attribute__((noreturn)) jump_to_next_stage(uint32_t vectbl_address,
jump_to_next_stage_phase_2);
}
-#endif // KERNEL_MODE
+#endif // SECURE_MODE
### core/embed/sys/startup/stm32/sysutils.c
@@ -18,11 +18,13 @@
*/
#include <trezor_bsp.h>
+#include <trezor_rtl.h>
+#include <sys/irq.h>
#include <sys/linker_utils.h>
#include <sys/sysutils.h>
-#ifdef KERNEL_MODE
+#ifdef SECURE_MODE
#ifdef TREZOR_MODEL_T2T1
#include "../stm32f4/startup_init.h"
@@ -136,24 +138,11 @@ __attribute((naked, noreturn, no_stack_protector)) void call_with_new_stack(
);
}
-// Ensure that we are running in privileged thread mode.
-//
-// This function is used only on STM32F4, where a direct jump to the
-// bootloader is performed. It checks if we are in handler mode, and
-// if so, it switches to privileged thread mode.
-__attribute((naked, no_stack_protector)) void ensure_thread_mode(void) {
+__attribute((naked, no_stack_protector)) static void unwind_exception(
+ uint32_t exc_return_code, uint32_t xpsr) {
__asm__ volatile(
- // --------------------------------------------------------------
- // Check if we are in handler mode
- // --------------------------------------------------------------
-
- "LDR R1, =0x1FF \n" // Get lower 9 bits of IPSR
- "MRS R0, IPSR \n"
- "ANDS R0, R0, R1 \n"
- "CMP R0, #0 \n" // == 0 if in thread mode
- "IT EQ \n"
- "BXEQ LR \n" // return if in thread mode
-
+ "MOV R2, R0 \n"
+ "MOV R3, R1 \n"
// --------------------------------------------------------------
// Disable FP registers lazy stacking
// --------------------------------------------------------------
@@ -165,7 +154,7 @@ __attribute((naked, no_stack_protector)) void ensure_thread_mode(void) {
"STR R0, [R1] \n"
// --------------------------------------------------------------
- // Exit handler mode, enter thread mode
+ // Exit handler mode
// --------------------------------------------------------------
"MOV R0, SP \n" // Align stack pointer to 8 bytes
@@ -182,18 +171,80 @@ __attribute((naked, no_stack_protector)) void ensure_thread_mode(void) {
"STR LR, [SP, #20] \n" // future LR = LR
"BIC LR, LR, #1 \n"
"STR LR, [SP, #24] \n" // return address = LR
- "LDR R0, = 0x01000000 \n" // THUMB bit set
- "STR R0, [SP, #28] \n" // future xPSR
+ "STR R3, [SP, #28] \n" // future xPSR
"MRS R0, CONTROL \n" // Clear SPSEL to use MSP for thread
"BIC R0, R0, #3 \n" // Clear nPRIV to run in privileged mode
"MSR CONTROL, R0 \n"
-#if !defined(__ARM_FEATURE_CMSE) || (__ARM_FEATURE_CMSE == 3U)
- "LDR LR, = 0xFFFFFFF9 \n" // Return to Secure Thread mode, use MSP
-#else
- "LDR LR, = 0xFFFFFFB8 \n" // Return to Thread mode, use MSP
-#endif
- "BX LR \n");
+ "BX R2 \n");
+}
+
+static uint32_t find_other_active_exception(uint32_t current) {
+ // External interrupts
+ for (size_t i = 0; i < ARRAY_LENGTH(NVIC->IABR); i++) {
+ uint32_t active = NVIC->IABR[i];
+
+ while (active != 0) {
+ uint32_t bit = __builtin_ctz(active);
+ active &= active - 1;
+
+ uint32_t exception = 16u + 32u * i + bit;
+ if (exception != current) {
+ return exception;
+ }
+ }
+ }
+
+ // System exceptions
+ static const struct {
+ uint32_t active_mask;
+ uint32_t exception;
+ } system_exceptions[] = {
+ {SCB_SHCSR_MEMFAULTACT_Msk, MemoryManagement_IRQn + 16},
+ {SCB_SHCSR_BUSFAULTACT_Msk, BusFault_IRQn + 16},
+ {SCB_SHCSR_USGFAULTACT_Msk, UsageFault_IRQn + 16},
+ {SCB_SHCSR_SVCALLACT_Msk, SVCall_IRQn + 16},
+ {SCB_SHCSR_MONITORACT_Msk, DebugMonitor_IRQn + 16},
+ {SCB_SHCSR_PENDSVACT_Msk, PendSV_IRQn + 16},
+ {SCB_SHCSR_SYSTICKACT_Msk, SysTick_IRQn + 16},
+ };
+
+ uint32_t shcsr = SCB->SHCSR;
+ for (size_t i = 0; i < ARRAY_LENGTH(system_exceptions); i++) {
+ if ((shcsr & system_exceptions[i].active_mask) != 0 &&
+ system_exceptions[i].exception != current) {
+ return system_exceptions[i].exception;
+ }
+ }
+
+ return 0;
+}
+
+void ensure_thread_mode(void) {
+ if ((__get_IPSR() & IPSR_ISR_Msk) == 0) {
+ // We are already in thread mode, no need to switch
+ return;
+ }
+
+ irq_key_t key = irq_lock();
+
+ // Unwind all active exceptions
+ for (;;) {
+ uint32_t current = __get_IPSR() & IPSR_ISR_Msk;
+ uint32_t next = find_other_active_exception(current);
+
+ if (next == 0) {
+ break;
+ }
+
+ // Switch to the handler mode
+ unwind_exception(0xFFFFFFF1, 0x01000000 | next);
+ }
+
+ // Switch to thread mode, use MSP
+ unwind_exception(0xFFFFFFF9, 0x01000000);
+
+ irq_unlock(key);
}
// Clears USB FIFO memory to prevent data leakage of sensitive information
@@ -399,8 +450,8 @@ void reset_peripherals_and_interrupts(void) {
// Disable SysTick
SysTick->CTRL = 0;
- // Clear PENDSV flag to prevent the PendSV_Handler call
- SCB->ICSR &= ~SCB_ICSR_PENDSVSET_Msk;
+ // Clear pending PendSV and SysTick exceptions.
+ SCB->ICSR = SCB_ICSR_PENDSVCLR_Msk | SCB_ICSR_PENDSTCLR_Msk;
// Clear SCB->SHCSR exception flags so we can return back
// to thread mode without any exception active
@@ -440,4 +491,4 @@ void reset_peripherals_and_interrupts(void) {
SCB->SHCSR &= ~(cleared_flags & ~preserved_flag);
}
-#endif // KERNEL_MODE
+#endif // SECURE_MODEWhy this scored 34/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.