[PATCH 5/6] x86/fpu: Fix memory leak with dynamic fpstate and exec()
From: Chang S. Bae
Date: Mon Nov 30 2026 - 16:19:26 EST
Using an XFD-controlled xfeature can allocate a larger fpstate. On
exec(), fpstate_reset() replaces fpu->fpstate with the embedded buffer
without freeing the dynamic allocation. Once the pointer is overwritten,
the allocation cannot be freed on task exit.
Free vmalloc'ed area from fpu_reset_fpstate().
Fixes: 500afbf645a0 ("x86/fpu/xstate: Add fpstate_realloc()/free()")
Closes: https://lore.kernel.org/20260929151013.81562-2-weiguixiong@xxxxxxxxxxxxx
Reported-by: Guixiong Wei <weiguixiong@xxxxxxxxxxxxx>
Originally-by: Guixiong Wei <weiguixiong@xxxxxxxxxxxxx>
Signed-off-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
arch/x86/kernel/fpu/core.c | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index de746eedfe397..f088b1f8518ea 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -680,16 +680,28 @@ static int update_fpu_shstk(struct task_struct *dst, unsigned long ssp)
* Reset fpstate metadata and register storage to the init values from:
* - fpu_clone() for new task's FPU state
* - fpu_flush_thread() for current
+ *
+ * Also, free up the expanded storage.
*/
static void fpu_reset_fpstate(struct task_struct *tsk, struct fpu *fpu, bool reset_regs)
{
+ struct fpstate *oldfps = NULL;
+
/* Current must get its register storage reset. */
WARN_ON_ONCE(tsk == current && !reset_regs);
- if (tsk == current)
+ /* New tasks must have a null pointer from arch_dup_task_struct() */
+ WARN_ON_ONCE(tsk != current && fpu->fpstate != NULL);
+
+ if (tsk == current) {
+ oldfps = fpu->fpstate;
fpregs_lock();
+ }
- /* Reset the fpstate metadata: */
+ /*
+ * Reset the fpstate metadata, including fpu->fpstate being
+ * reset to __fpstate.
+ */
fpstate_reset(fpu);
/*
@@ -719,8 +731,10 @@ static void fpu_reset_fpstate(struct task_struct *tsk, struct fpu *fpu, bool res
*/
set_tsk_thread_flag(tsk, TIF_NEED_FPU_LOAD);
- if (tsk == current)
+ if (tsk == current) {
fpregs_unlock();
+ fpstate_free(oldfps);
+ }
}
/* Clone current's FPU state on fork */
--
2.53.0