Skip to contentSkip to conversation

x86/fpu: hold fpregs_lock() across fpstate_reset() in fpu_flush_thread()

PATCH In review Ömer Mete Kaya 2 participants 3 messages Nov 23 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/fpu: hold fpregs_lock() across fpstate_reset() in fpu_flush_thread()

fpstate_reset() updates the fpstate pointer before resetting its size and feature metadata without holding fpregs_lock(). If the task is preempted during this transition, switch_fpu_prepare() can save the FPU registers using the stale feature configuration into the newly selected, smaller buffer, potentially causing an out-of-bounds write into the embedded fpstate buffer.

Hold fpregs_lock() across fpstate_reset() to prevent a context switch or softirq from accessing the partially updated FPU state.

Fixes e61d6310a0f8 ("x86/fpu: Reset permission and fpstate on exec()")
---
 arch/x86/kernel/fpu/core.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kernel/fpu/core.c +2 -0
index d1aeecd57f5e..939f800373a9 100644
@@ -866,7 +866,9 @@ void fpu__clear_user_states(struct fpu *fpu)
 
 void fpu_flush_thread(void)
 {
+	fpregs_lock();
 	fpstate_reset(x86_task_fpu(current));
+	fpregs_unlock();
 	fpu_reset_fpstate_regs();
 }
 /*
Signature · 1 line
2.56.0
#1 lore raw
Quoted 7 lines · Ömer Mete Kaya

fpstate_reset() updates the fpstate pointer before resetting its size and feature metadata without holding fpregs_lock(). If the task is preempted during this transition, switch_fpu_prepare() can save the FPU registers using the stale feature configuration into the newly selected, smaller buffer, potentially causing an out-of-bounds write into the embedded fpstate buffer.

Quoted 6 lines

Hold fpregs_lock() across fpstate_reset() to prevent a context switch or softirq from accessing the partially updated FPU state.

Fixes e61d6310a0f8 ("x86/fpu: Reset permission and fpstate on exec()")

Hi Omer,

I'm genuinely curious how you came across this issue and decided to send a patch for it.

Did you happen to read the other (brief) conversation on it?

https://lore.kernel.org/lkml/[email protected]/

#2 lore raw
Quoted 8 lines · Dave Hansen

Hi Omer,

I'm genuinely curious how you came across this issue and decided to send a patch for it.

Did you happen to read the other (brief) conversation on it?

https://lore.kernel.org/lkml/[email protected]/

Hi Dave,

I saw that on
https://syzkaller.appspot.com/bug?extid=58cbd7974a103b8b8916 and in the "discussions" section there, the discussion you had with Chang wasn't visible, so I hadn't read it. Looks like you were already working on it.

Thanks,
Ömer