x86/fpu for v5.14-rc1
Subsystems (7)
Linus,
please pull the latest x86/fpu branch from:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git x86-fpu-2021-07-07
up to: 93c2cdc975aa: x86/fpu/xstate: Clear xstate header in copy_xstate_to_uabi_buf() again
Fixes and improvements for FPU handling on x86:
- Prevent sigaltstack out of bounds writes. The kernel unconditionally writes the FPU state to the alternate stack without checking whether the stack is large enough to accomodate it.
Check the alternate stack size before doing so and in case it's too small force a SIGSEGV instead of silently corrupting user space data.
- MINSIGSTKZ and SIGSTKSZ are constants in signal.h and have never been updated despite the fact that the FPU state which is stored on the signal stack has grown over time which causes trouble in the field when AVX512 is available on a CPU. The kernel does not expose the minimum requirements for the alternate stack size depending on the available and enabled CPU features.
ARM already added an aux vector AT_MINSIGSTKSZ for the same reason. Add it to x86 as well
- A major cleanup of the x86 FPU code. The recent discoveries of XSTATE related issues unearthed quite some inconsistencies, duplicated code and other issues like lack of robustness.
The fine granular overhaul addresses this, makes the code more robust and maintainable, which allows to integrate upcoming XSTATE related features in sane ways.
This PR comes late so the changes could soak for a while in -next. The changes have been extensively tested by various teams at Intel and the marginal fallout has been addressed. Some test cases have been added already, but there is a larger set of Intel internal tests coming up soon which will allow to catch similar issues in the future.
Thanks,
tglx
------------------>
Andy Lutomirski (7):
x86/fpu: Add address range checks to copy_user_to_xstate() selftests/x86: Test signal frame XSTATE header corruption handling x86/fpu: Rewrite xfpregs_set()
x86/fpu: Fail ptrace() requests that try to set invalid MXCSR values x86/fpu: Clean up fpregs_set()
x86/fpu: Rename "dynamic" XSTATEs to "independent" x86/fpu: Clean up the fpu__clear() variants
Chang S. Bae (6):
uapi/auxvec: Define the aux vector AT_MINSIGSTKSZ x86/signal: Introduce helpers to get the maximum signal frame size x86/elf: Support a new ELF aux vector AT_MINSIGSTKSZ selftest/sigaltstack: Use the AT_MINSIGSTKSZ aux vector if available x86/signal: Detect and prevent an alternate signal stack overflow selftest/x86/signal: Include test cases for validating sigaltstack
Dave Hansen (5):
x86/fpu: Simplify PTRACE_GETREGS code x86/kvm: Avoid looking up PKRU in XSAVE buffer x86/pkeys: Move read_pkru() and write_pkru() x86/fpu: Add PKRU storage outside of task XSAVE buffer x86/fpu: Hook up PKRU into ptrace()
Thomas Gleixner (56):
x86/fpu: Fix copy_xstate_to_kernel() gap handling
x86/pkeys: Revert a5eff7259790 ("x86/pkeys: Add PKRU value to init_fpstate") x86/fpu: Mark various FPU state variables __ro_after_init x86/fpu: Make xfeatures_mask_all __ro_after_init
x86/fpu: Get rid of fpu__get_supported_xfeatures_mask() x86/fpu: Remove unused get_xsave_field_ptr()
x86/fpu: Move inlines where they belong
x86/fpu: Limit xstate copy size in xstateregs_set()
x86/fpu: Sanitize xstateregs_set()
x86/fpu: Reject invalid MXCSR values in copy_kernel_to_xstate() x86/fpu: Make copy_xstate_to_kernel() usable for [x]fpregs_get() x86/fpu: Use copy_xstate_to_uabi_buf() in xfpregs_get() x86/fpu: Use copy_xstate_to_uabi_buf() in fpregs_get()
x86/fpu: Remove fpstate_sanitize_xstate()
x86/fpu/regset: Move fpu__read_begin() into regset
x86/fpu: Move fpu__write_begin() to regset
x86/fpu: Get rid of using_compacted_format()
x86/fpu: Cleanup arch_set_user_pkey_access()
x86/fpu: Get rid of copy_supervisor_to_kernel()
x86/fpu: Rename copy_xregs_to_kernel() and copy_kernel_to_xregs() x86/fpu: Rename copy_user_to_xregs() and copy_xregs_to_user() x86/fpu: Rename fxregs-related copy functions
x86/math-emu: Rename frstor()
x86/fpu: Rename fregs-related copy functions
x86/fpu: Rename xstate copy functions which are related to UABI x86/fpu: Deduplicate copy_uabi_from_user/kernel_to_xstate() x86/fpu: Rename copy_fpregs_to_fpstate() to save_fpregs_to_fpstate() x86/fpu: Get rid of the FNSAVE optimization
x86/fpu: Rename copy_kernel_to_fpregs() to restore_fpregs_from_fpstate() x86/fpu: Rename initstate copy functions
x86/fpu/xstate: Sanitize handling of independent features x86/fpu: Rename and sanitize fpu__save/copy()
x86/cpu: Sanitize X86_FEATURE_OSPKE
x86/pkru: Provide pkru_get_init_value()
x86/pkru: Provide pkru_write_default()
x86/cpu: Write the default PKRU value when enabling PKE x86/fpu: Use pkru_write_default() in copy_init_fpstate_to_fpregs() x86/fpu: Rename fpu__clear_all() to fpu_flush_thread()
x86/fpu: Rename __fpregs_load_activate() to fpregs_restore_userregs() x86/fpu: Move FXSAVE_LEAK quirk info __copy_kernel_to_fpregs() x86/fpu: Rename xfeatures_mask_user() to xfeatures_mask_uabi() x86/fpu: Dont restore PKRU in fpregs_restore_userspace() x86/fpu: Mask PKRU from kernel XRSTOR[S] operations
x86/fpu: Remove PKRU handling from switch_fpu_finish()
x86/fpu: Don't store PKRU in xstate in fpu_reset_fpstate() x86/pkru: Remove xstate fiddling from write_pkru()
x86/fpu: Mark init_fpstate __ro_after_init
x86/fpu/signal: Move initial checks into fpu__restore_sig() x86/fpu/signal: Remove the legacy alignment check x86/fpu/signal: Sanitize the xstate check on sigframe x86/fpu/signal: Sanitize copy_user_to_fpregs_zeroing() x86/fpu/signal: Split out the direct restore code
x86/fpu: Return proper error codes from user access functions x86/fpu/signal: Handle #PF in the direct restore path x86/fpu/signal: Let xrstor handle the features to init x86/fpu/xstate: Clear xstate header in copy_xstate_to_uabi_buf() again
Documentation/x86/elf_auxvec.rst | 53 ++ Documentation/x86/index.rst | 1 + arch/x86/events/intel/lbr.c | 6 +- arch/x86/include/asm/elf.h | 4 + arch/x86/include/asm/fpu/internal.h | 202 +++---- arch/x86/include/asm/fpu/signal.h | 2 + arch/x86/include/asm/fpu/xstate.h | 78 ++- arch/x86/include/asm/pgtable.h | 57 +- arch/x86/include/asm/pkeys.h | 9 +- arch/x86/include/asm/pkru.h | 62 ++ arch/x86/include/asm/processor.h | 9 + arch/x86/include/asm/sigframe.h | 2 + arch/x86/include/asm/special_insns.h | 14 +- arch/x86/include/uapi/asm/auxvec.h | 4 +- arch/x86/kernel/cpu/common.c | 37 +- arch/x86/kernel/fpu/core.c | 282 ++++----- arch/x86/kernel/fpu/init.c | 15 +- arch/x86/kernel/fpu/regset.c | 223 ++++--- arch/x86/kernel/fpu/signal.c | 438 +++++++------- arch/x86/kernel/fpu/xstate.c | 644 ++++++++------------- arch/x86/kernel/process.c | 22 +- arch/x86/kernel/process_64.c | 28 +- arch/x86/kernel/signal.c | 88 ++- arch/x86/kernel/traps.c | 5 +- arch/x86/kvm/svm/sev.c | 1 + arch/x86/kvm/x86.c | 56 +- arch/x86/math-emu/fpu_proto.h | 2 +- arch/x86/math-emu/load_store.c | 2 +- arch/x86/math-emu/reg_ld_str.c | 2 +- arch/x86/mm/extable.c | 2 +- arch/x86/mm/fault.c | 2 +- arch/x86/mm/pkeys.c | 22 - include/linux/pkeys.h | 4 - include/linux/sched/signal.h | 19 +- include/uapi/linux/auxvec.h | 3 + tools/testing/selftests/sigaltstack/sas.c | 20 +- tools/testing/selftests/x86/Makefile | 5 +- .../testing/selftests/x86/corrupt_xstate_header.c | 114 ++++ tools/testing/selftests/x86/sigaltstack.c | 128 ++++ 39 files changed, 1480 insertions(+), 1187 deletions(-) create mode 100644 Documentation/x86/elf_auxvec.rst create mode 100644 arch/x86/include/asm/pkru.h create mode 100644 tools/testing/selftests/x86/corrupt_xstate_header.c create mode 100644 tools/testing/selftests/x86/sigaltstack.c
Documentation/x86/elf_auxvec.rst new file +53 -0
@@ -0,0 +1,53 @@ +.. SPDX-License-Identifier: GPL-2.0 + +================================== +x86-specific ELF Auxiliary Vectors +================================== + +This document describes the semantics of the x86 auxiliary vectors. + +Introduction +============ + +ELF Auxiliary vectors enable the kernel to efficiently provide +configuration-specific parameters to userspace. In this example, a program +allocates an alternate stack based on the kernel-provided size:: + + #include <sys/auxv.h> + #include <elf.h> + #include <signal.h> + #include <stdlib.h> + #include <assert.h> + #include <err.h> + + #ifndef AT_MINSIGSTKSZ + #define AT_MINSIGSTKSZ 51 + #endif + + .... + stack_t ss; + + ss.ss_sp = malloc(ss.ss_size); + assert(ss.ss_sp); + + ss.ss_size = getauxval(AT_MINSIGSTKSZ) + SIGSTKSZ; + ss.ss_flags = 0; + + if (sigaltstack(&ss, NULL)) + err(1, "sigaltstack"); + + +The exposed auxiliary vectors +============================= + +AT_SYSINFO is used for locating the vsyscall entry point. It is not +exported on 64-bit mode. + +AT_SYSINFO_EHDR is the start address of the page containing the vDSO. + +AT_MINSIGSTKSZ denotes the minimum stack size required by the kernel to +deliver a signal to user-space. AT_MINSIGSTKSZ comprehends the space +consumed by the kernel to accommodate the user context for the current +hardware configuration. It does not comprehend subsequent user-space stack +consumption, which must be added by the user. (e.g. Above, user-space adds +SIGSTKSZ to AT_MINSIGSTKSZ.)
Documentation/x86/index.rst +1 -0
@@ -35,3 +35,4 @@ x86-specific Documentation sva sgx features + elf_auxvec
arch/x86/events/intel/lbr.c +3 -3
@@ -491,7 +491,7 @@ static void intel_pmu_arch_lbr_xrstors(void *ctx) {struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx; -copy_kernel_to_dynamic_supervisor (&task_ctx->xsave,XFEATURE_MASK_LBR ); +xrstors (&task_ctx->xsave,XFEATURE_MASK_LBR ); } static __always_inline bool lbr_is_reset_in_cstate(void *ctx)
@@ -576,7 +576,7 @@ static void intel_pmu_arch_lbr_xsaves(void *ctx) {struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx; -copy_dynamic_supervisor_to_kernel (&task_ctx->xsave,XFEATURE_MASK_LBR ); +xsaves (&task_ctx->xsave,XFEATURE_MASK_LBR ); }static void __intel_pmu_lbr_save (void *ctx)
@@ -992,7 +992,7 @@ static void intel_pmu_arch_lbr_read_xsave(struct cpu_hw_events *cpuc)intel_pmu_store_lbr (cpuc,NULL );return ; } -copy_dynamic_supervisor_to_kernel (&xsave->xsave,XFEATURE_MASK_LBR ); +xsaves (&xsave->xsave,XFEATURE_MASK_LBR );intel_pmu_store_lbr (cpuc, xsave->lbr.entries); }
arch/x86/include/asm/elf.h +4 -0
@@ -312,6 +312,7 @@ do { \NEW_AUX_ENT (AT_SYSINFO ,VDSO_ENTRY ); \NEW_AUX_ENT (AT_SYSINFO_EHDR ,VDSO_CURRENT_BASE ); \ } \ +NEW_AUX_ENT (AT_MINSIGSTKSZ ,get_sigframe_size ()); \ }while (0 )/*
@@ -328,6 +329,7 @@ extern unsigned long task_size_32bit(void);extern unsigned long task_size_64bit (int full_addr_space);extern unsigned long get_mmap_base (int is_legacy);extern bool mmap_address_hint_valid (unsigned long addr,unsigned long len); +extern unsigned long get_sigframe_size (void );#ifdef CONFIG_X86_32
@@ -349,6 +351,7 @@ do { \if (vdso64_enabled) \NEW_AUX_ENT (AT_SYSINFO_EHDR , \ (unsigned long __force )current->mm->context.vdso); \ +NEW_AUX_ENT (AT_MINSIGSTKSZ ,get_sigframe_size ()); \ }while (0 )/* As a historical oddity, the x32 and x86_64 vDSOs are controlled together. */
@@ -357,6 +360,7 @@ do { \if (vdso64_enabled) \NEW_AUX_ENT (AT_SYSINFO_EHDR , \ (unsigned long __force )current->mm->context.vdso); \ +NEW_AUX_ENT (AT_MINSIGSTKSZ ,get_sigframe_size ()); \ } while (0)#define AT_SYSINFO 32
arch/x86/include/asm/fpu/internal.h +80 -122
376 lines not on this page · raw message
arch/x86/include/asm/fpu/signal.h +2 -0
@@ -29,6 +29,8 @@ unsigned long fpu__alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx, unsigned long *size); +unsigned long fpu__get_fpstate_size (void ); +extern void fpu__init_prepare_fx_sw_frame (void );#endif /* _ASM_X86_FPU_SIGNAL_H */
arch/x86/include/asm/fpu/xstate.h +57 -21
133 lines not on this page · raw message
arch/x86/include/asm/pgtable.h +1 -56
75 lines not on this page · raw message
arch/x86/include/asm/pkeys.h +4 -5
@@ -9,14 +9,14 @@* will be necessary to ensure that the types that store key * numbers and masks have sufficient capacity. */ -#define arch_max_pkey () (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1) +#define arch_max_pkey () (cpu_feature_enabled(X86_FEATURE_OSPKE) ? 16 : 1)extern int arch_set_user_pkey_access (struct task_struct *tsk,int pkey,unsigned long init_val);static inline bool arch_pkeys_enabled (void ) { -return boot_cpu_has (X86_FEATURE_OSPKE ); +return cpu_feature_enabled (X86_FEATURE_OSPKE ); }/*
@@ -26,7 +26,7 @@ static inline bool arch_pkeys_enabled(void)extern int __execute_only_pkey (struct mm_struct *mm);static inline int execute_only_pkey (struct mm_struct *mm) { -if (!boot_cpu_has (X86_FEATURE_OSPKE )) +if (!cpu_feature_enabled (X86_FEATURE_OSPKE ))return ARCH_DEFAULT_PKEY ;return __execute_only_pkey (mm);
@@ -37,7 +37,7 @@ extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma,static inline int arch_override_mprotect_pkey (struct vm_area_struct *vma,int prot,int pkey) { -if (!boot_cpu_has (X86_FEATURE_OSPKE )) +if (!cpu_feature_enabled (X86_FEATURE_OSPKE ))return 0 ;return __arch_override_mprotect_pkey (vma, prot, pkey);
@@ -124,7 +124,6 @@ extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, unsigned long init_val);extern int __arch_set_user_pkey_access (struct task_struct *tsk,int pkey,unsigned long init_val); -extern void copy_init_pkru_to_fpregs (void );static inline int vma_pkey (struct vm_area_struct *vma) {
arch/x86/include/asm/pkru.h new file +62 -0
62 lines not on this page · raw message
arch/x86/include/asm/processor.h +9 -0
@@ -518,6 +518,15 @@ struct thread_struct { unsigned int sig_on_uaccess_err:1; +/* +* Protection Keys Register for Userspace. Loaded immediately on +* context switch. Store it in thread_struct to avoid a lookup in +* the tasks's FPU xstate buffer. This value is only valid when a +* task is scheduled out. For 'current' the authoritative source of +* PKRU is the hardware itself. +*/ +u32 pkru; +/* Floating point and extended processor state */ struct fpu fpu;/*
arch/x86/include/asm/sigframe.h +2 -0
@@ -85,4 +85,6 @@ struct rt_sigframe_x32 {#endif /* CONFIG_X86_64 */ +void __init init_sigframe_size (void ); +#endif /* _ASM_X86_SIGFRAME_H */
arch/x86/include/asm/special_insns.h +1 -13
@@ -104,25 +104,13 @@ static inline void wrpkru(u32 pkru) : : "a" (pkru), "c"(ecx), "d"(edx)); } -static inline void __write_pkru (u32 pkru) -{ -/* -* WRPKRU is relatively expensive compared to RDPKRU. -* Avoid WRPKRU when it would not change the value. -*/ -if (pkru ==rdpkru ()) -return ; - -wrpkru (pkru); -} -#else static inline u32 rdpkru (void ) {return 0 ; } -static inline void __write_pkru (u32 pkru) +static inline void wrpkru (u32 pkru) { }#endif
arch/x86/include/uapi/asm/auxvec.h +2 -2
@@ -12,9 +12,9 @@/* entries in ARCH_DLINFO: */ #if defined(CONFIG_IA32_EMULATION ) || !defined(CONFIG_X86_64 ) -# define AT_VECTOR_SIZE_ARCH 2 +# define AT_VECTOR_SIZE_ARCH 3#else /* else it's non-compat x86-64 */ -# define AT_VECTOR_SIZE_ARCH 1 +# define AT_VECTOR_SIZE_ARCH 2#endif #endif /* _ASM_X86_AUXVEC_H */
arch/x86/kernel/cpu/common.c +17 -20
@@ -58,6 +58,7 @@#include <asm/intel-family.h> #include <asm/cpu_device_id.h> #include <asm/uv/uv.h> +#include <asm/sigframe.h> #include "cpu.h"
@@ -465,27 +466,22 @@ static bool pku_disabled; static __always_inline void setup_pku(struct cpuinfo_x86 *c) { -struct pkru_state *pk; +if (c == &boot_cpu_data) { +if (pku_disabled || !cpu_feature_enabled (X86_FEATURE_PKU )) +return ; +/* +* Setting CR4.PKE will cause the X86_FEATURE_OSPKE cpuid +* bit to be set. Enforce it. +*/ +setup_force_cpu_cap (X86_FEATURE_OSPKE ); -/* check the boot processor, plus compile options for PKU: */ -if (!cpu_feature_enabled (X86_FEATURE_PKU )) -return ; -/* checks the actual processor's cpuid bits: */ -if (!cpu_has (c,X86_FEATURE_PKU )) -return ; -if (pku_disabled) + }else if (!cpu_feature_enabled (X86_FEATURE_OSPKE )) {return ; + }cr4_set_bits (X86_CR4_PKE ); - pk =get_xsave_addr (&init_fpstate.xsave,XFEATURE_PKRU ); -if (pk) - pk->pkru = init_pkru_value; -/* -* Setting X86_CR4_PKE will cause the X86_FEATURE_OSPKE -* cpuid bit to be set. We need to ensure that we -* update that bit in this CPU's "cpu_info". -*/ -set_cpu_cap (c,X86_FEATURE_OSPKE ); +/* Load the default PKRU value */ +pkru_write_default (); }#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
@@ -1332,6 +1328,8 @@ static void __init early_identify_cpu(struct cpuinfo_x86 *c)fpu__init_system (c); +init_sigframe_size (); +#ifdef CONFIG_X86_32 /* * Regardless of whether PCID is enumerated, the SDM says
@@ -1717,9 +1715,8 @@ void print_cpu_info(struct cpuinfo_x86 *c) }/* -* clearcpuid= was already parsed in fpu__init_parse_early_param. -* But we need to keep a dummy __setup around otherwise it would -* show up as an environment variable for init. +* clearcpuid= was already parsed in cpu_parse_early_param(). This dummy +* function prevents it from becoming an environment variable for init. */ static __init int setup_clearcpuid (char *arg) {
arch/x86/kernel/fpu/core.c +147 -135
403 lines not on this page · raw message
arch/x86/kernel/fpu/init.c +2 -13
@@ -89,7 +89,7 @@ static void fpu__init_system_early_generic(struct cpuinfo_x86 *c)/* * Boot time FPU feature detection code: */ -unsigned int mxcsr_feature_mask__read_mostly =0xffffffffu ; +unsigned int mxcsr_feature_mask__ro_after_init =0xffffffffu ;EXPORT_SYMBOL_GPL (mxcsr_feature_mask);static void __init fpu__init_system_mxcsr (void )
@@ -135,7 +135,7 @@ static void __init fpu__init_system_generic(void)* This is inherent to the XSAVE architecture which puts all state * components into a single, continuous memory block: */ -unsigned int fpu_kernel_xstate_size; +unsigned int fpu_kernel_xstate_size__ro_after_init ;EXPORT_SYMBOL_GPL (fpu_kernel_xstate_size);/* Get alignment of the TYPE. */
@@ -216,17 +216,6 @@ static void __init fpu__init_system_xstate_size_legacy(void) fpu_user_xstate_size = fpu_kernel_xstate_size; } -/* -* Find supported xfeatures based on cpu features and command-line input. -* This must be called after fpu__init_parse_early_param() is called and -* xfeatures_mask is enumerated. -*/ -u64 __init fpu__get_supported_xfeatures_mask (void ) -{ -return XFEATURE_MASK_USER_SUPPORTED | -XFEATURE_MASK_SUPERVISOR_SUPPORTED ; -} -/* Legacy code to initialize eager fpu mode. */ static void __init fpu__init_system_ctx_switch (void ) {
arch/x86/kernel/fpu/regset.c +128 -95
335 lines not on this page · raw message
arch/x86/kernel/fpu/signal.c +215 -223
576 lines not on this page · raw message
arch/x86/kernel/fpu/xstate.c +248 -396
728 lines not on this page · raw message
arch/x86/kernel/signal.c +82 -6
88 lines not on this page · raw message
arch/x86/kvm/x86.c +30 -26
56 lines not on this page · raw message
tools/testing/selftests/sigaltstack/sas.c +15 -5
Lines of this file are not shown. View the raw message.
tools/testing/selftests/x86/corrupt_xstate_header.c new file +114 -0
114 lines not on this page · raw message
tools/testing/selftests/x86/sigaltstack.c new file +128 -0
128 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.
The pull request you sent on Wed, 07 Jul 2021 09:28:02 -0000:
The pull request you sent on Wed, 07 Jul 2021 09:28:02 -0000:
Quoted 1 line
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.gitx86-fpu-2021-07-07
has been merged into torvalds/linux.git:
https://git.kernel.org/torvalds/c/1423e2660cf134a8f21f2451865a04792013e49e
Thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/prtracker.html