Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v4 0/2] x86/fpu: Fix dynamic fpstate leak on exec()
@ 2026-11-02 10:08 Guixiong Wei
  2026-11-02 10:10 ` [PATCH v4 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec() Guixiong Wei
  0 siblings, 1 reply; 4+ messages in thread
From: Guixiong Wei @ 2026-11-02 10:08 UTC (permalink / raw)
  To: x86
  Cc: Guixiong Wei, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H . Peter Anvin, Chang S . Bae, Shuah Khan,
	linux-kernel, linux-kselftest, stable

An exec() after using an XFD-controlled xfeature leaks the dynamically
allocated fpstate. Fix the cleanup in fpstate_reset() and add an AMX
regression selftest.

The selftest reports FAIL on an unfixed kernel (10/10 FAILs on 7.1.3),
SKIP when vmallocinfo addresses are hidden, and OK with the fix applied.

Changes since v3:
- Close the use-after-free that freeing in fpstate_reset() exposed.
  fpu_flush_thread() called fpstate_reset() before
  fpu_reset_fpstate_regs() set TIF_NEED_FPU_LOAD, so a context switch
  or a kernel FPU user in that window could still see live register
  state and save it into the freed buffer. Set TIF_NEED_FPU_LOAD before
  the reset and document on fpstate_reset() that callers operating on
  the current task must prevent such a save. The race was caught by
  the Sashiko review and the fix was suggested by Chang S. Bae.

v3: https://lore.kernel.org/r/cover.1793072271.git.weiguixiong@bytedance.com
v2: https://lore.kernel.org/r/cover.1791428412.git.weiguixiong@bytedance.com
v1: https://lore.kernel.org/r/20260929151013.81562-2-weiguixiong@bytedance.com

Guixiong Wei (2):
  x86/fpu: Fix memory leak with dynamic fpstate and exec()
  selftests/x86/amx: Test dynamic fpstate cleanup across exec()

 arch/x86/kernel/fpu/core.c        |  12 ++
 tools/testing/selftests/x86/amx.c | 251 +++++++++++++++++++++++++++++-
 2 files changed, 261 insertions(+), 2 deletions(-)


base-commit: 72d3fcf802c45d00b300f25b848a93c3a2bd7c7e
-- 
2.55.0

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v4 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec()
  2026-11-02 10:08 [PATCH v4 0/2] x86/fpu: Fix dynamic fpstate leak on exec() Guixiong Wei
@ 2026-11-02 10:10 ` Guixiong Wei
  2026-11-18 20:16   ` Chang S. Bae
  0 siblings, 1 reply; 4+ messages in thread
From: Guixiong Wei @ 2026-11-02 10:10 UTC (permalink / raw)
  To: x86; +Cc: Guixiong Wei, Chang S. Bae, stable

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 dynamic state at the start of fpstate_reset(). In fpu_clone(),
clear the new task's fpstate pointer before resetting its state, so the
parent's allocation is not freed.

fpstate_reset() may now free the current task's buffer. fpu_flush_thread()
calls it before fpu_reset_fpstate_regs() sets TIF_NEED_FPU_LOAD. If a
context switch or a kernel FPU user happens in that window, the register
state still looks live and gets saved into the freed buffer. Set
TIF_NEED_FPU_LOAD before the reset so that nothing saves the hardware
state until it is reloaded from the reset in-memory state.

Fixes: 500afbf645a0 ("x86/fpu/xstate: Add fpstate_realloc()/free()")
Suggested-by: Chang S. Bae <chang.seok.bae@intel.com>
Cc: stable@vger.kernel.org
Signed-off-by: Guixiong Wei <weiguixiong@bytedance.com>
---
 arch/x86/kernel/fpu/core.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index d1aeecd57f5ed..af1c1688d69bf 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -592,8 +592,15 @@ static void __fpstate_reset(struct fpstate *fpstate)
 	fpstate->user_xfeatures	= fpu_user_cfg.default_features;
 }
 
+/*
+ * Callers resetting the current task must ensure that its FPU register
+ * state isn't saved on context switch as the buffer may be freed here.
+ */
 void fpstate_reset(struct fpu *fpu)
 {
+	/* Free dynamic fpstate (if any) before resetting pointer: */
+	fpstate_free(fpu);
+
 	/* Set the fpstate pointer to the default fpstate */
 	fpu->fpstate = &fpu->__fpstate;
 	__fpstate_reset(fpu->fpstate);
@@ -670,6 +677,8 @@ int fpu_clone(struct task_struct *dst, u64 clone_flags, bool minimal,
 	/* The new task's FPU state cannot be valid in the hardware. */
 	dst_fpu->last_cpu = -1;
 
+	/* Break the link to the other FPU's fpstate: */
+	dst_fpu->fpstate = NULL;
 	fpstate_reset(dst_fpu);
 
 	if (!cpu_feature_enabled(X86_FEATURE_FPU))
@@ -866,6 +875,9 @@ void fpu__clear_user_states(struct fpu *fpu)
 
 void fpu_flush_thread(void)
 {
+	/* Prevent the hardware state from being saved on context switch */
+	set_thread_flag(TIF_NEED_FPU_LOAD);
+
 	fpstate_reset(x86_task_fpu(current));
 	fpu_reset_fpstate_regs();
 }

base-commit: 72d3fcf802c45d00b300f25b848a93c3a2bd7c7e
-- 
2.55.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec()
  2026-11-02 10:10 ` [PATCH v4 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec() Guixiong Wei
@ 2026-11-18 20:16   ` Chang S. Bae
  2026-11-21 22:11     ` Chang S. Bae
  0 siblings, 1 reply; 4+ messages in thread
From: Chang S. Bae @ 2026-11-18 20:16 UTC (permalink / raw)
  To: Guixiong Wei, x86; +Cc: stable

On 11/2/2026 2:10 AM, Guixiong Wei wrote:
> 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 dynamic state at the start of fpstate_reset(). In fpu_clone(),
> clear the new task's fpstate pointer before resetting its state, so the
> parent's allocation is not freed.
> 
> fpstate_reset() may now free the current task's buffer. fpu_flush_thread()
> calls it before fpu_reset_fpstate_regs() sets TIF_NEED_FPU_LOAD. If a
> context switch or a kernel FPU user happens in that window, the register
> state still looks live and gets saved into the freed buffer. Set
> TIF_NEED_FPU_LOAD before the reset so that nothing saves the hardware
> state until it is reloaded from the reset in-memory state.

Nit: this paragraph reads wordy, could be trimmed a bit.

> 
> Fixes: 500afbf645a0 ("x86/fpu/xstate: Add fpstate_realloc()/free()")
> Suggested-by: Chang S. Bae <chang.seok.bae@intel.com>
> Cc: stable@vger.kernel.org
> Signed-off-by: Guixiong Wei <weiguixiong@bytedance.com>

Looks like here you missed maintainers the whole and oh, even LKML... 
Please include them all.

Other than that,
Reviewed-by: Chang S. Bae <chang.seok.bae@intel.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec()
  2026-11-18 20:16   ` Chang S. Bae
@ 2026-11-21 22:11     ` Chang S. Bae
  0 siblings, 0 replies; 4+ messages in thread
From: Chang S. Bae @ 2026-11-21 22:11 UTC (permalink / raw)
  To: Guixiong Wei, x86; +Cc: stable, LKML

On 11/18/2026 12:16 PM, Chang S. Bae wrote:
> On 11/2/2026 2:10 AM, Guixiong Wei wrote:
> 
>> Suggested-by: Chang S. Bae <chang.seok.bae@intel.com>
...
> Reviewed-by: Chang S. Bae <chang.seok.bae@intel.com>

Include LKML back.

Sorry, I just realized there is another issue:
https://lore.kernel.org/all/88456eb3-9c58-47b5-8dee-fc6ad79334e6@intel.com/

Let me retrace them and get back with more changes. Thanks for bringing 
this up. I'll include you in the follow-up.

Chang

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-21 22:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-02 10:08 [PATCH v4 0/2] x86/fpu: Fix dynamic fpstate leak on exec() Guixiong Wei
2026-11-02 10:10 ` [PATCH v4 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec() Guixiong Wei
2026-11-18 20:16   ` Chang S. Bae
2026-11-21 22:11     ` Chang S. Bae

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox