Re: [PATCH] x86/fpu: Disable shadow stack when xstate support is missing

From: Eric Biggers

Date: Sun Dec 06 2026 - 00:20:30 EST


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@xxxxxxxxxxxxxx/
> Cc: stable@xxxxxxxxxxxxxxx
> Assisted-by: Antigravity:Gemini
> Signed-off-by: Jiaming Zhang <r772577952@xxxxxxxxx>

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@xxxxxxxxxx/
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