fix(core): validate unprivileged task psp
What changed, and why it matters
This commit adds a safety check inside the Trezor hardware wallet's core task scheduler. Before saving a task's state during a context switch, the operating system now verifies that the task's stack pointer points to a valid location within its assigned stack. Without this check, a malicious or buggy unprivileged task could set its stack pointer outside its stack, causing the kernel to overwrite sensitive memory (including other tasks' data or kernel structures) when saving registers. The fix detects this and kills the offending task instead.
Treat as a security fix. Review whether similar validation is needed for other context-switch paths or architectures, and consider whether the static 'Invalid PSP' message and __LINE__ reporting provide adequate logging. Because the commit has no changelog entry, verify it is included in release notes and backported to affected firmware branches.
Security signals we found
Kernel writes to user-controlled pointer without bounds validation (pre-patch)
Privilege escalation / memory corruption via forged PSP in unprivileged task
Addition of explicit PSP range validation before context save
Task termination on validation failure
Kernel task exempted from check (MSP path)
No changelog entry despite security-relevant fix
Evidence from the diff
The patch modifies the ARM PendSV_Handler in core/embed/sys/task/stm32/systask.c. The PendSV handler saves R4-R11 and, if floating-point context is active, S16-S31 below the Process Stack Pointer (PSP) of the currently active unprivileged task. Because the handler runs with kernel privileges, it trusts the PSP value supplied by the task. A task could set PSP outside its stack region, causing the kernel to write context-save data to arbitrary memory. The new pendsv_check_psp() function validates that the entire exception frame (0x20 or 0x68 bytes above PSP depending on exc_return.LR[4]) and the handler-saved area (0x60 bytes below PSP) fit within [task->stack_base, task->stack_end]. If not, the task is terminated via systask_exit_fatal(). The check is skipped for kernel tasks (MSP).
Changed components
core/embed/sys/task/stm32/systask.cPendSV_Handlerpendsv_check_pspsystask_exit_fatalUnprivileged task stack managementInspect captured patch +42 / −2
### core/embed/sys/task/stm32/systask.c
@@ -915,19 +915,59 @@ systask_switch_to_kernel(void) {
);
}
+// Checks that the context of the active unprivileged task can be saved
+// on its stack.
+//
+// `psp` is fully controlled by the task. The PendSV handler stores
+// R4-R11 and S16-S31 below it with kernel privileges, so the whole area
+// (including the exception frame above `psp`) must lie inside the task
+// stack. If it does not, the task is killed and `false` is returned.
+__attribute__((no_stack_protector, used)) static bool pendsv_check_psp(
+ uint32_t psp, uint32_t exc_return) {
+ systask_t* task = g_systask_scheduler.active_task;
+
+ // Exception frame pushed by the CPU (with/without FP context)
+ uint32_t frame_size = (exc_return & 0x10) == 0 ? 0x68 : 0x20;
+ // R4-R11 and S16-S31 saved by the PendSV handler
+ uint32_t save_size = 0x60;
+
+ if (psp >= task->stack_base + save_size &&
+ psp <= task->stack_end - frame_size) {
+ return true;
+ }
+
+ mpu_mode_t mpu_mode = mpu_reconfig(MPU_MODE_DEFAULT);
+ static const char msg[] = "Invalid PSP";
+ systask_exit_fatal(task, msg, sizeof(msg) - 1, NULL, 0, __LINE__);
+ mpu_restore(mpu_mode);
+
+ return false;
+}
+
__attribute__((naked, no_stack_protector)) void PendSV_Handler(void) {
__asm__ volatile(
"LDR R0, =%[active_task] \n"
"LDR R0, [R0] \n" // R0 = active_task
"LDR R1, =%[waiting_task]\n"
"LDR R1, [R1] \n" // R1 = waiting_task
"CMP R0, R1 \n"
- "BEQ 1f \n" // No task switch needed
+ "BEQ 2f \n" // No task switch needed
"LDR R0, [R0, #12] \n" // R0 = active_task->killed
"CMP R0, #0 \n"
"BNE systask_switch_to_kernel\n" // Killed => don't save context
+ "TST LR, #0x4 \n" // Return stack (1=>PSP, 0=>MSP)
+ "BEQ 1f \n" // Kernel task => no check needed
+ "MRS R0, PSP \n" // `psp` argument
+ "MOV R1, LR \n" // `exc_return` argument
+ "PUSH {R4, LR} \n"
+ "BL pendsv_check_psp \n" // Is there a space for the context?
+ "POP {R4, LR} \n"
+ "CMP R0, #0 \n"
+ "BEQ systask_switch_to_kernel\n" // Killed => don't save context
+ "1: \n"
+
// Save the current task context on its stack before switching
"TST LR, #0x4 \n" // Return stack (1=>PSP, 0=>MSP)
@@ -958,7 +998,7 @@ __attribute__((naked, no_stack_protector)) void PendSV_Handler(void) {
// fp context preservation of S0-S15
"B systask_switch_context\n"
- "1: \n"
+ "2: \n"
"BX LR \n"
: // No output
: [active_task] "i"(&g_systask_scheduler.active_task), // InputWhy this scored 65/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.