Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH] x86/fpu: Disable shadow stack when xstate support is missing
@ 2026-12-06  4:55 Jiaming Zhang
  2026-12-06  5:20 ` Eric Biggers
  2026-12-06  5:22 ` Borislav Petkov
  0 siblings, 2 replies; 4+ messages in thread
From: Jiaming Zhang @ 2026-12-06  4:55 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86
  Cc: H. Peter Anvin, Rick Edgecombe, Eric Biggers, Chang S. Bae,
	Andrei Vagin, linux-kernel, Jiaming Zhang, stable

On x86-64, user shadow stacks keep their settings and stack pointer in
supervisor registers saved across context switches via XSAVES under the
XFEATURE_CET_USER xstate component.  Callers reading that state, such as
ssp_get() on core dump or ptrace(), expect XFEATURE_CET_USER to be
enabled whenever a task has shadow stacks on, and call get_xsave_addr():

	if (WARN_ON_ONCE(!xfeature_enabled(xfeature_nr)))
		return NULL;

During boot, however, identify_boot_cpu() runs before
fpu__init_system().  Since commit 0dc2a76092d9 ("x86/cpufeatures: Enable
CET CR4 bit for shadow stack"), setup_cet() sets X86_FEATURE_USER_SHSTK
from X86_FEATURE_SHSTK before fpu__init_system_xstate() reads the CPU's
XSAVE state mask from CPUID leaf 0xD.

When fpu__init_system_xstate() checks that mask, it disables AVX and
AVX-512 if their xstate bits are missing, but leaves X86_FEATURE_SHSTK
set when XSAVES or XFEATURE_MASK_CET_USER is absent.  Also, cpuid_deps[]
lacks a dependency from X86_FEATURE_USER_SHSTK to X86_FEATURE_SHSTK, so
clearing SHSTK or XSAVE later leaves X86_FEATURE_USER_SHSTK enabled.

On a CPU or VM guest advertising SHSTK without XFEATURE_MASK_CET_USER
or XSAVES, any unprivileged 64-bit task can turn on shadow stacks via
arch_prctl().  Context switches then neither save nor restore the
shadow-stack MSRs, leaking them to other tasks, while core dumps, thread
clones, or ptrace() hit the warning in get_xsave_addr().

Record X86_FEATURE_USER_SHSTK as depending on X86_FEATURE_SHSTK in
cpuid_deps[], and clear X86_FEATURE_SHSTK in fpu__init_system_xstate()
when XSAVES or XFEATURE_MASK_CET_USER is missing, matching the AVX and
AVX-512 checks.  Placing this check before the xsave_cpuid_features[]
loop also masks out XFEATURE_CET_KERNEL if present.

Fixes: 0dc2a76092d9 ("x86/cpufeatures: Enable CET CR4 bit for shadow stack")
Closes: https://lore.kernel.org/lkml/CANypQFbh+s+dORvfGnJ=Wp-2VDQPCJ7_wFWiVkVacdJA_U=6Ww@mail.gmail.com/
Cc: stable@vger.kernel.org
Assisted-by: Antigravity:Gemini
Signed-off-by: Jiaming Zhang <r772577952@gmail.com>
---
 arch/x86/kernel/cpu/cpuid-deps.c | 1 +
 arch/x86/kernel/fpu/xstate.c     | 9 +++++++++
 2 files changed, 10 insertions(+)

diff --git a/arch/x86/kernel/cpu/cpuid-deps.c b/arch/x86/kernel/cpu/cpuid-deps.c
index acbcec394ef1..a1a7b2c1b2f0 100644
--- a/arch/x86/kernel/cpu/cpuid-deps.c
+++ b/arch/x86/kernel/cpu/cpuid-deps.c
@@ -89,6 +89,7 @@ static const struct cpuid_dep cpuid_deps[] = {
 	{ X86_FEATURE_AMX_BF16,			X86_FEATURE_AMX_TILE  },
 	{ X86_FEATURE_AMX_INT8,			X86_FEATURE_AMX_TILE  },
 	{ X86_FEATURE_SHSTK,			X86_FEATURE_XSAVES    },
+	{ X86_FEATURE_USER_SHSTK,		X86_FEATURE_SHSTK     },
 	{ X86_FEATURE_FRED,			X86_FEATURE_LKGS      },
 	{ X86_FEATURE_SPEC_CTRL_SSBD,		X86_FEATURE_SPEC_CTRL },
 	{ X86_FEATURE_LASS,			X86_FEATURE_SMAP      },
diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index da303714379c..6982913f2f00 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -861,6 +861,15 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)
 		}
 	}
 
+	if (boot_cpu_has(X86_FEATURE_SHSTK)) {
+		if (!boot_cpu_has(X86_FEATURE_XSAVES) ||
+		    !(fpu_kernel_cfg.max_features & XFEATURE_MASK_CET_USER)) {
+			pr_err(FW_BUG
+			       "x86/fpu: Disabling shadow stack support due to missing xstate features\n");
+			setup_clear_cpu_cap(X86_FEATURE_SHSTK);
+		}
+	}
+
 	if (fpu_kernel_cfg.max_features & XFEATURE_MASK_APX &&
 	    fpu_kernel_cfg.max_features & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR)) {
 		/*

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.43.0


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

end of thread, other threads:[~2026-12-06  5:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  4:55 [PATCH] x86/fpu: Disable shadow stack when xstate support is missing Jiaming Zhang
2026-12-06  5:20 ` Eric Biggers
2026-12-06  5:30   ` Eric Biggers
2026-12-06  5:22 ` Borislav Petkov

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