* [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 [flat|nested] 4+ messages in thread* Re: [PATCH] x86/fpu: Disable shadow stack when xstate support is missing
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
1 sibling, 1 reply; 4+ messages in thread
From: Eric Biggers @ 2026-12-06 5:20 UTC (permalink / raw)
To: Jiaming Zhang
Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Rick Edgecombe, Chang S. Bae, Andrei Vagin,
linux-kernel, stable
On Sun, Dec 06, 2026 at 12:55:05PM +0800, Jiaming Zhang wrote:
> 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>
I already had an almost identical diff in a stack of patches that I
hadn't had a chance to send out yet. I've gone ahead and done so:
https://lore.kernel.org/all/20261206051225.298193-1-ebiggers@kernel.org/
I updated the commit message of the first one to include your
Reported-by and the scenario you mentioned, though I had written the
diff earlier before seeing your report since it was needed anyway.
Fixes and Cc stable may be debatable, as this would be a hypervisor bug.
- Eric
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] x86/fpu: Disable shadow stack when xstate support is missing
2026-12-06 5:20 ` Eric Biggers
@ 2026-12-06 5:30 ` Eric Biggers
0 siblings, 0 replies; 4+ messages in thread
From: Eric Biggers @ 2026-12-06 5:30 UTC (permalink / raw)
To: Jiaming Zhang
Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Rick Edgecombe, Chang S. Bae, Andrei Vagin,
linux-kernel, stable
On Sat, Dec 05, 2026 at 09:20:26PM -0800, Eric Biggers wrote:
> On Sun, Dec 06, 2026 at 12:55:05PM +0800, Jiaming Zhang wrote:
> > 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>
>
> I already had an almost identical diff in a stack of patches that I
> hadn't had a chance to send out yet. I've gone ahead and done so:
> https://lore.kernel.org/all/20261206051225.298193-1-ebiggers@kernel.org/
> I updated the commit message of the first one to include your
> Reported-by and the scenario you mentioned, though I had written the
> diff earlier before seeing your report since it was needed anyway.
>
> Fixes and Cc stable may be debatable, as this would be a hypervisor bug.
Note: I didn't check for the X86_FEATURE_XSAVES dependency not being met
in mine, since it's already listed as a dependency in cpuid_deps[].
Yes, cpuid_deps[] is non-enforcing, but that's the x86 kernel policy.
- Eric
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] x86/fpu: Disable shadow stack when xstate support is missing
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:22 ` Borislav Petkov
1 sibling, 0 replies; 4+ messages in thread
From: Borislav Petkov @ 2026-12-06 5:22 UTC (permalink / raw)
To: Jiaming Zhang
Cc: Thomas Gleixner, Ingo Molnar, Dave Hansen, x86, H. Peter Anvin,
Rick Edgecombe, Eric Biggers, Chang S. Bae, Andrei Vagin,
linux-kernel, stable
On Sun, Dec 06, 2026 at 12:55:05PM +0800, Jiaming Zhang wrote:
> On a CPU or VM guest advertising SHSTK without XFEATURE_MASK_CET_USER
> or XSAVES
If this is a contrived qemu CPU model, we don't care.
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette
^ permalink raw reply [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
all inboxes | Powered by JetHome®