mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] x86/fpu: Put non-enabled dynamic features into init state on sigreturn
@ 2026-11-27  9:00 Daniel Wade
  2026-12-02 18:12 ` Chang S. Bae
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Wade @ 2026-11-27  9:00 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86
  Cc: H . Peter Anvin, Chang S . Bae, Andrei Vagin, linux-kernel

A dynamic feature not enabled by a task is expected to be in init state
while that task owns the FPU registers. The direct sigreturn path limits
both restore and init masks to fpstate::user_xfeatures. If the task loses
registers inside rt_sigreturn(), another task's tiles can remain there:

  A: rt_sigreturn() with AMX permitted, XTILE_DATA not enabled, faults or
     is preempted; TIF_NEED_FPU_LOAD is set.
  B: runs on the same CPU and leaves nonzero XTILE_DATA in the registers.
  A: resumes with XFD set. XRSTOR leaves XTILE_DATA untouched, then
     fpregs_mark_activate() marks the registers as A's.
  A: first AMX instruction traps. fpstate_realloc() clears XFD without
     reloading, so TILESTORED reads B's tile data.

A supplies TILECFG in its signal frame, avoiding LDTILECFG, which would
zero the tile data before TILESTORED.

If the task no longer owns the FPU registers, reset any state its signal
frame did not restore, including XTILE_DATA. Otherwise keep the old mask.

Fixes: 5509cc78080d ("x86/fpu/signal: Use fpstate for size and features")
Suggested-by: Chang S. Bae <chang.seok.bae@intel.com>
Signed-off-by: Daniel Wade <danjwade95@gmail.com>
Tested-by: Andrei Vagin <avagin@gmail.com>
Reviewed-by: Andrei Vagin <avagin@gmail.com>
Cc: stable@vger.kernel.org
---
v2: Use Chang's suggested code and comment. Add Andrei's explanation
of how the race happens.

Andrei's selftest for this is at
https://lore.kernel.org/all/CANaxB-zWf3BkCM8hsd0pXTULHi2ZmpRnLgpp2BFzT=_XwMdRrw@mail.gmail.com/

 arch/x86/kernel/fpu/signal.c |   11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c
index 164255d..7d0642a 100644
--- a/arch/x86/kernel/fpu/signal.c
+++ b/arch/x86/kernel/fpu/signal.c
@@ -266,7 +266,16 @@ static int __restore_fpregs_from_user(void __user *buf, u64 task_xfeatures,
 
 		/* Restore enabled features only. */
 		xrestore_mask &= task_xfeatures;
-		init_bv = task_xfeatures & ~xrestore_mask;
+		/*
+		 * If the FPU registers do not belong to current, they may still
+		 * contain state from a previous task with features beyond
+		 * current's enabled features. Clear up to max_features to
+		 * prevent state leakage.
+		 */
+		if (test_thread_flag(TIF_NEED_FPU_LOAD))
+			init_bv = fpu_user_cfg.max_features & ~xrestore_mask;
+		else
+			init_bv = task_xfeatures & ~xrestore_mask;
 		if (likely(!fx_only))
 			ret = xrstor_from_user_sigframe(buf, xrestore_mask);
 		else

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

* Re: [PATCH v2] x86/fpu: Put non-enabled dynamic features into init state on sigreturn
  2026-11-27  9:00 [PATCH v2] x86/fpu: Put non-enabled dynamic features into init state on sigreturn Daniel Wade
@ 2026-12-02 18:12 ` Chang S. Bae
  0 siblings, 0 replies; 2+ messages in thread
From: Chang S. Bae @ 2026-12-02 18:12 UTC (permalink / raw)
  To: Daniel Wade, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86
  Cc: H . Peter Anvin, Andrei Vagin, linux-kernel

On 11/27/2026 1:00 AM, Daniel Wade wrote:
> A dynamic feature not enabled by a task is expected to be in init state
> while that task owns the FPU registers. The direct sigreturn path limits
> both restore and init masks to fpstate::user_xfeatures. If the task loses
> registers inside rt_sigreturn(), another task's tiles can remain there:
> 
>    A: rt_sigreturn() with AMX permitted, XTILE_DATA not enabled, faults or
>       is preempted; TIF_NEED_FPU_LOAD is set.
>    B: runs on the same CPU and leaves nonzero XTILE_DATA in the registers.
>    A: resumes with XFD set. XRSTOR leaves XTILE_DATA untouched, then
>       fpregs_mark_activate() marks the registers as A's.
>    A: first AMX instruction traps. fpstate_realloc() clears XFD without
>       reloading, so TILESTORED reads B's tile data.

Nit: Vagin's note [*] looks a bit more illustrative to me.

[*]: 
https://lore.kernel.org/CANaxB-zWf3BkCM8hsd0pXTULHi2ZmpRnLgpp2BFzT=_XwMdRrw@mail.gmail.com

> A supplies TILECFG in its signal frame, avoiding LDTILECFG, which would
> zero the tile data before TILESTORED.
> 
> If the task no longer owns the FPU registers, reset any state its signal
> frame did not restore, including XTILE_DATA. Otherwise keep the old mask.

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

Thanks,
Chang

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

end of thread, other threads:[~2026-12-02 18:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27  9:00 [PATCH v2] x86/fpu: Put non-enabled dynamic features into init state on sigreturn Daniel Wade
2026-12-02 18:12 ` 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

all inboxes | Powered by JetHome®