[PATCH 3/6] x86/fpu: Reset the fpstate on exec() under fpregs_lock()
From: Chang S. Bae
Date: Mon Nov 30 2026 - 16:19:43 EST
fpregs_lock() should be held when either FPU registers or storage is
updated. The fpstate metadata is also relevant so fpstate_reset() has to
be under the lock. Currently fpu_flush_thread() has fpstate_reset()
without the lock.
A couple of sysbot reports [1,2] both indicated a race condition to a
task that previously loaded dynamic states.
To address this issue, first prepare the helper by renaming to
fpu_reset_fpstate() and providing the storage reset option to accommodate
the fpu_clone() path. Then, move fpstate_reset() under the lock.
Fixes: e61d6310a0f8 ("x86/fpu: Reset permission and fpstate on exec()")
Closes: https://syzkaller.appspot.com/bug?extid=58cbd7974a103b8b8916 [1]
Closes: https://syzkaller.appspot.com/bug?extid=b07ec23d35be305855be [2]
Reported-by: syzbot+58cbd7974a103b8b8916@xxxxxxxxxxxxxxxxxxxxxxxxx
Reported-by: syzbot+b07ec23d35be305855be@xxxxxxxxxxxxxxxxxxxxxxxxx
Suggested-by: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Signed-off-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
Cc: Ömer Mete Kaya <omermetekaya0@xxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
arch/x86/kernel/fpu/core.c | 36 +++++++++++++++++++++++++++---------
1 file changed, 27 insertions(+), 9 deletions(-)
diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index 80847a08ff3bb..de746eedfe397 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -613,6 +613,9 @@ static void __fpstate_reset(struct fpstate *fpstate)
fpstate->user_xfeatures = fpu_user_cfg.default_features;
}
+/*
+ * fpregs_lock() must be held when fpstate belongs to the current task
+ */
void fpstate_reset(struct fpu *fpu)
{
/* Set the fpstate pointer to the default fpstate */
@@ -674,15 +677,21 @@ static int update_fpu_shstk(struct task_struct *dst, unsigned long ssp)
}
/*
- * Reset FPU register storage to the init values from:
+ * Reset fpstate metadata and register storage to the init values from:
* - fpu_clone() for new task's FPU state
* - fpu_flush_thread() for current
*/
-static void fpu_reset_fpstate_regs(struct task_struct *tsk, struct fpu *fpu)
+static void fpu_reset_fpstate(struct task_struct *tsk, struct fpu *fpu, bool reset_regs)
{
+ /* Current must get its register storage reset. */
+ WARN_ON_ONCE(tsk == current && !reset_regs);
+
if (tsk == current)
fpregs_lock();
+ /* Reset the fpstate metadata: */
+ fpstate_reset(fpu);
+
/*
* This does not change the actual hardware registers. It just
* resets the memory image. Do not use fpstate_init() here. Just copy
@@ -691,8 +700,17 @@ static void fpu_reset_fpstate_regs(struct task_struct *tsk, struct fpu *fpu)
* PKRU handling does not rely on the xstate when restoring for
* user space as PKRU is eagerly written in switch_to() and
* flush_thread().
+ *
+ * When skipping the reset, from fpu_clone(), it means the new task
+ * inherits the current register context instead.
+ */
+ if (reset_regs)
+ memcpy(&fpu->fpstate->regs, &init_fpstate.regs, init_fpstate_copy_size());
+
+ /*
+ * Even if !reset_regs, new task's FPU state cannot be valid in the
+ * hardware
*/
- memcpy(&fpu->fpstate->regs, &init_fpstate.regs, init_fpstate_copy_size());
__fpu_invalidate_fpregs_state(fpu);
/*
@@ -720,10 +738,11 @@ int fpu_clone(struct task_struct *dst, u64 clone_flags, bool minimal,
BUILD_BUG_ON(sizeof(*dst) % SMP_CACHE_BYTES != 0);
- fpstate_reset(dst_fpu);
-
- /* Fill the new task FPU storage with init-state */
- fpu_reset_fpstate_regs(dst, dst_fpu);
+ /*
+ * Reset the fpstate metadata and fill the new task FPU storage
+ * with init-state when needed.
+ */
+ fpu_reset_fpstate(dst, dst_fpu, minimal);
/*
* No FPU state inheritance for kernel threads and IO
@@ -874,8 +893,7 @@ void fpu__clear_user_states(struct fpu *fpu)
void fpu_flush_thread(void)
{
- fpstate_reset(x86_task_fpu(current));
- fpu_reset_fpstate_regs(current, x86_task_fpu(current));
+ fpu_reset_fpstate(current, x86_task_fpu(current), true);
}
/*
* Load FPU context before returning to userspace.
--
2.53.0