fix(core): attribute user MLSPERR to unprivileged task
What changed, and why it matters
This commit fixes a bug in the Trezor firmware's task scheduler where a specific hardware memory fault (related to lazy floating-point register saving) was being blamed on the wrong task. Previously, the kernel/privileged code could be incorrectly blamed, potentially causing the wrong task to be killed or the fault to be mishandled. The fix correctly attributes the fault to the unprivileged task whose stack caused it, and handles the floating-point state safely. This is a security-relevant bug fix in the operating system core.
Treat as a security-relevant fix and include in release notes/changelog. Review whether the bug could be triggered by a malicious unprivileged app/task to cause denial of service or escape isolation. Backport to supported firmware branches. Consider adding regression tests for FP lazy stacking fault attribution.
Security signals we found
Incorrect fault attribution between privileged kernel and unprivileged task
Potential wrong task termination/killing due to misattributed memory fault
Lazy floating-point state preservation (FP lazy stacking) edge case
Handler-mode vs thread-mode fault handling distinction
Memory protection / task isolation boundary issue
No changelog entry despite security-relevant fix
Evidence from the diff
The change is in core/embed/sys/task/stm32/systask.c. It addresses the case where a MemManage fault with MMFSR.MLSPRIV (actually MLSPERR — lazy floating-point state preservation error) occurs in privileged handler mode but is actually caused by the unprivileged task’s Process Stack Pointer (PSP). The fix detects this condition, switches fault attribution to the unprivileged task, reads the PSP, and clears FPCCR.LSPEND/LSPACT if a lazy save to the user frame is pending. It also adjusts systask_switch_to_kernel logic to avoid switching to the kernel task when the fault originated in handler mode, and refactors systask_switch_to_kernel signature. This prevents incorrect task termination and avoids repeated faults on FP instruction restart.
Changed components
core/embed/sys/task/stm32/systask.cTrezor Core OS task schedulerMemManage fault handlerFloating-point lazy state preservation handlingUnprivileged task isolation mechanismInspect captured patch +50 / −5
### core/embed/sys/task/stm32/systask.c
@@ -653,6 +653,43 @@ static void systask_exit_fault_ex(uint32_t msp, uint32_t exc_return,
systask_scheduler_t* scheduler = &g_systask_scheduler;
uint32_t cfsr = SCB->CFSR;
+ int32_t irqn = (int32_t)(__get_IPSR() & IPSR_ISR_Msk) - 16;
+
+#ifdef USE_APPLETS
+ if (irqn == MemoryManagement_IRQn && privileged &&
+ (cfsr & SCB_CFSR_MLSPERR_Msk) != 0 &&
+ scheduler->active_task != &scheduler->kernel_task) {
+ // Lazy FP state preservation failed. The FP registers are saved to
+ // the exception frame of the unprivileged task when the first FP
+ // instruction is executed in handler mode, so the fault is taken
+ // from a privileged context although it's caused by the invalid
+ // stack of the unprivileged task.
+ //
+ // FPCCR can't be used to identify the owner of the frame. If the
+ // fault was raised with exceptions masked (e.g. inside irq_lock()),
+ // it's just pended, the FP instruction is completed and the fault is
+ // taken later. FPCCR then already describes the frame of this
+ // handler.
+ //
+ // As a consequence, the task is blamed even if the failed frame
+ // belongs to the kernel (nested handlers using FP). This is
+ // acceptable: kernel frames live on the main stack right above the
+ // already pushed basic frame, so they can fail only if the kernel
+ // itself is broken (main stack underflow or inaccessible). Then the
+ // task is killed by mistake, but the kernel faults again right after
+ // that anyway.
+ privileged = false;
+ sp = __get_PSP();
+
+ uint32_t user_lsp = FPU_FPCCR_LSPACT_Msk | FPU_FPCCR_USER_Msk;
+ if ((FPU->FPCCR & user_lsp) == user_lsp) {
+ // The lazy save to the task frame is still pending. Drop it,
+ // otherwise it would be retried (and fault again) when the
+ // interrupted FP instruction is restarted.
+ FPU->FPCCR &= ~FPU_FPCCR_LSPACT_Msk;
+ }
+ }
+#endif
// Task that was running when the exception occurred
systask_t* origin =
@@ -674,7 +711,7 @@ static void systask_exit_fault_ex(uint32_t msp, uint32_t exc_return,
#if !(defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__))
pminfo->fault.sp_lim = origin->sp_lim;
#endif
- pminfo->fault.irqn = (__get_IPSR() & IPSR_ISR_Msk) - 16;
+ pminfo->fault.irqn = irqn;
pminfo->fault.cfsr = cfsr;
pminfo->fault.mmfar = SCB->MMFAR;
pminfo->fault.bfar = SCB->BFAR;
@@ -714,7 +751,14 @@ __attribute((used)) static void systask_exit_fault(uint32_t msp,
#ifdef USE_APPLETS
systask_scheduler_t* scheduler = &g_systask_scheduler;
- if (scheduler->active_task->killed) {
+ bool thread_mode = (exc_return & 0x8) != 0;
+
+ // If the fault was taken from handler mode (lazy FP state preservation
+ // fault), we just return to the interrupted handler. The exception return
+ // to the kernel task is not possible while another handler is active and
+ // the killed task is switched out by the pending PendSV.
+
+ if (scheduler->active_task->killed && thread_mode) {
// The task was killed, so we have to switch to the kernel task.
//
// We can't just rely on the PendSV pended by `systask_kill()` and
@@ -1020,13 +1064,14 @@ __attribute__((naked, no_stack_protector)) void PendSV_Handler(void) {
strlen(filename), __LINE__); \
} while (0)
-__attribute__((no_stack_protector, used)) static uint32_t svc_handler(
- uint32_t* stack, uint32_t* msp, uint32_t exc_return, uint32_t r4,
- uint32_t r5, uint32_t r6) {
+__attribute__((no_stack_protector,
+ used)) static void svc_handler(uint32_t* stack, uint32_t r4,
+ uint32_t r5, uint32_t r6) {
IRQ_LOG_ENTER();
systask_scheduler_t* scheduler = &g_systask_scheduler;
systask_t* task = systask_active();
+
uint8_t svc_number = ((uint8_t*)stack[6])[-2];
mpu_mode_t mpu_mode = mpu_reconfig(MPU_MODE_DEFAULT);
Why this scored 59/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.