refactor(core): harden task switching against fault injections
What changed, and why it matters
This commit hardens the Trezor firmware's task scheduler against fault-injection attacks. These attacks use physical techniques like voltage glitches or laser pulses to trick the device into executing code with higher privileges than intended. The change ensures the processor starts in a lower-privilege state before deciding whether to elevate the kernel task, and adds a safety check that halts the device if a non-kernel task somehow ends up privileged. It is a defensive hardening patch rather than a fix for a known remotely exploitable bug.
Treat as a legitimate defensive hardening commit. No immediate user action is required, but ensure the change is included in the next firmware release. Security teams may want to review related scheduler paths for similar single-point-of-failure patterns and validate that error_shutdown behaves correctly under fault-injection test conditions.
Security signals we found
Fault-injection hardening (FIH) explicitly mentioned in commit title and code comments
Privilege-level transition split into multiple guarded steps
Added runtime invariant check that only the kernel task may run privileged
Use of error_shutdown on invariant violation (fail-secure)
No changelog entry, consistent with internal hardening change
Evidence from the diff
In core/embed/sys/task/stm32/systask.c, the scheduler’s PendSV handler is modified to reduce the risk of privilege escalation via fault injection. Previously, the CONTROL register was read once, the nPRIV bit was set or cleared based on whether the next task was the kernel task, and then written. The new code first sets nPRIV (unprivileged) unconditionally, then only clears it if the scheduled task is both the kernel_task pointer and has id == 0, and finally re-reads CONTROL and calls error_shutdown if any non-kernel task is observed running privileged. This adds redundant checks and narrows the fault window where a glitch could leave a user task in privileged mode.
Changed components
core/embed/sys/task/stm32/systask.cPendSV scheduler context switchARM CONTROL register / nPRIV privilege handlingTrezor Model T / Safe 3 STM32-based devices using this tasking layerInspect captured patch +16 / −5
### core/embed/sys/task/stm32/systask.c
@@ -730,14 +730,25 @@ __attribute((no_stack_protector, used)) static uint32_t scheduler_pendsv(
// Load the scheduled task context
systask_t* next_task = scheduler->active_task;
- // Set task privilege level
+ // Lower the privilege level initially (FIH)
uint32_t control = __get_CONTROL();
+ control |= CONTROL_nPRIV_Msk;
+ __set_CONTROL(control);
+
+ // Set task privilege level
if (next_task == &scheduler->kernel_task) {
- control &= ~CONTROL_nPRIV_Msk;
- } else {
- control |= CONTROL_nPRIV_Msk;
+ if (next_task->id == 0) {
+ control &= ~CONTROL_nPRIV_Msk;
+ __set_CONTROL(control);
+ }
+ }
+
+ // Check if the privilege level is correct for the next task (FIH)
+ control = __get_CONTROL();
+ if ((control & CONTROL_nPRIV_Msk) == 0 &&
+ next_task != &scheduler->kernel_task) {
+ error_shutdown("Privileged task is not the kernel task");
}
- __set_CONTROL(control);
// Setup the MPU for the new task
mpu_reconfig(next_task->mpu_mode);Why this scored 70/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.