Control-flow Enforcement: Shadow Stack
Subsystems (19)
v7 cover Control-flow Enforcement: Shadow Stack
Intel has published Control-flow Enforcement (CET) in the Architecture Instruction Set Extensions Programming Reference:
https://software.intel.com/en-us/download/intel-architecture-instruction-set-
extensions-programming-reference
The previous version (v6) of CET Shadow Stack patches is here:
https://lkml.org/lkml/2018/11/20/225
Summary of changes from v6:
Rebase to v5.2-rc3.
Adapt XSAVES system state changes to the recent FPU changes:
- Add modify_fpu_regs_begin(), modify_fpu_regs_end() to protect CET MSR changes.
- Update signal handling.
Move ELF header-parsing out of x86.
Other small fixes in response to comments.
This version passed GLIBC built-in tests.
Hi Maintainers,
What else is needed before this series can be merged?
Thanks,
Yu-cheng
Yu-cheng Yu (27):
Documentation/x86: Add CET description
x86/cpufeatures: Add CET CPU feature flags for Control-flow
Enforcement Technology (CET)
x86/fpu/xstate: Change names to separate XSAVES system and user states x86/fpu/xstate: Introduce XSAVES system states x86/fpu/xstate: Add XSAVES system states for shadow stack x86/cet: Add control protection exception handler x86/cet/shstk: Add Kconfig option for user-mode shadow stack
mm: Introduce VM_SHSTK for shadow stack memory
mm/mmap: Prevent Shadow Stack VMA merges
x86/mm: Change _PAGE_DIRTY to _PAGE_DIRTY_HW
x86/mm: Introduce _PAGE_DIRTY_SW
drm/i915/gvt: Update _PAGE_DIRTY to _PAGE_DIRTY_BITS x86/mm: Modify ptep_set_wrprotect and pmdp_set_wrprotect for
_PAGE_DIRTY_SW
x86/mm: Shadow stack page fault error checking
mm: Handle shadow stack page fault
mm: Handle THP/HugeTLB shadow stack page fault
mm: Update can_follow_write_pte/pmd for shadow stack
mm: Introduce do_mmap_locked()
x86/cet/shstk: User-mode shadow stack support x86/cet/shstk: Introduce WRUSS instruction
x86/cet/shstk: Handle signals for shadow stack
binfmt_elf: Extract .note.gnu.property from an ELF file x86/cet/shstk: ELF header parsing of Shadow Stack x86/cet/shstk: Handle thread shadow stack
mm/mmap: Add Shadow stack pages to memory accounting x86/cet/shstk: Add arch_prctl functions for Shadow Stack x86/cet/shstk: Add Shadow Stack instructions to opcode map
.../admin-guide/kernel-parameters.txt | 6 + Documentation/x86/index.rst | 1 + Documentation/x86/intel_cet.rst | 268 +++++++++++++ arch/x86/Kconfig | 26 ++ arch/x86/Makefile | 7 + arch/x86/entry/entry_64.S | 2 +- arch/x86/ia32/ia32_signal.c | 8 + arch/x86/include/asm/cet.h | 48 +++ arch/x86/include/asm/cpufeatures.h | 2 + arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/fpu/internal.h | 25 +- arch/x86/include/asm/fpu/signal.h | 2 + arch/x86/include/asm/fpu/types.h | 22 ++ arch/x86/include/asm/fpu/xstate.h | 26 +- arch/x86/include/asm/mmu_context.h | 3 + arch/x86/include/asm/msr-index.h | 18 + arch/x86/include/asm/pgtable.h | 191 ++++++++-- arch/x86/include/asm/pgtable_types.h | 38 +- arch/x86/include/asm/processor.h | 5 + arch/x86/include/asm/special_insns.h | 32 ++ arch/x86/include/asm/traps.h | 5 + arch/x86/include/uapi/asm/prctl.h | 5 + arch/x86/include/uapi/asm/processor-flags.h | 2 + arch/x86/include/uapi/asm/sigcontext.h | 15 + arch/x86/kernel/Makefile | 2 + arch/x86/kernel/cet.c | 327 ++++++++++++++++ arch/x86/kernel/cet_prctl.c | 85 +++++ arch/x86/kernel/cpu/common.c | 25 ++ arch/x86/kernel/cpu/cpuid-deps.c | 2 + arch/x86/kernel/fpu/core.c | 26 +- arch/x86/kernel/fpu/init.c | 10 - arch/x86/kernel/fpu/signal.c | 81 +++- arch/x86/kernel/fpu/xstate.c | 156 +++++--- arch/x86/kernel/idt.c | 4 + arch/x86/kernel/process.c | 8 +- arch/x86/kernel/process_64.c | 31 ++ arch/x86/kernel/relocate_kernel_64.S | 2 +- arch/x86/kernel/signal.c | 10 +- arch/x86/kernel/signal_compat.c | 2 +- arch/x86/kernel/traps.c | 57 +++ arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/lib/x86-opcode-map.txt | 26 +- arch/x86/mm/fault.c | 18 + arch/x86/mm/pgtable.c | 41 ++ drivers/gpu/drm/i915/gvt/gtt.c | 2 +- fs/Kconfig.binfmt | 3 + fs/Makefile | 1 + fs/binfmt_elf.c | 13 + fs/gnu_property.c | 351 ++++++++++++++++++ fs/proc/task_mmu.c | 3 + include/asm-generic/pgtable.h | 14 + include/linux/elf.h | 12 + include/linux/mm.h | 26 ++ include/uapi/asm-generic/siginfo.h | 3 +- include/uapi/linux/elf.h | 14 + mm/gup.c | 8 +- mm/huge_memory.c | 12 +- mm/memory.c | 7 +- mm/mmap.c | 11 + .../arch/x86/include/asm/disabled-features.h | 8 +- tools/objtool/arch/x86/lib/x86-opcode-map.txt | 26 +- 61 files changed, 2029 insertions(+), 165 deletions(-) create mode 100644 Documentation/x86/intel_cet.rst create mode 100644 arch/x86/include/asm/cet.h create mode 100644 arch/x86/kernel/cet.c create mode 100644 arch/x86/kernel/cet_prctl.c create mode 100644 fs/gnu_property.c
Signature · 1 line
2.17.1
PATCH v7 1/27 Documentation/x86: Add CET description
Explain how CET works and the no_cet_shstk/no_cet_ibt kernel parameters.
--- .../admin-guide/kernel-parameters.txt | 6 + Documentation/x86/index.rst | 1 + Documentation/x86/intel_cet.rst | 268 ++++++++++++++++++ 3 files changed, 275 insertions(+) create mode 100644 Documentation/x86/intel_cet.rst
Documentation/admin-guide/kernel-parameters.txt +6 -0
@@ -2906,6 +2906,12 @@ noexec=on: enable non-executable mappings (default) noexec=off: disable non-executable mappings + no_cet_ibt [X86-64] Disable indirect branch tracking for user-mode + applications + + no_cet_shstk [X86-64] Disable shadow stack support for user-mode + applications + nosmap [X86,PPC] Disable SMAP (Supervisor Mode Access Prevention) even if it is supported by processor.
Documentation/x86/index.rst +1 -0
@@ -21,6 +21,7 @@ x86-specific Documentation pat protection-keys intel_mpx + intel_cet amd-memory-encryption pti mds
Documentation/x86/intel_cet.rst new file +268 -0
268 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v7 2/27 x86/cpufeatures: Add CET CPU feature flags for Control-flow Enforcement Technology (CET)
In reply to Yu-cheng YuAdd CPU feature flags for Control-flow Enforcement Technology (CET).
CPUID.(EAX=7,ECX=0):ECX[bit 7] Shadow stack CPUID.(EAX=7,ECX=0):EDX[bit 20] Indirect branch tracking
--- arch/x86/include/asm/cpufeatures.h | 2 ++ arch/x86/kernel/cpu/cpuid-deps.c | 2 ++ 2 files changed, 4 insertions(+)
arch/x86/include/asm/cpufeatures.h +2 -0
@@ -323,6 +323,7 @@#define X86_FEATURE_PKU (16*32+ 3)/* Protection Keys for Userspace */ #define X86_FEATURE_OSPKE (16*32+ 4)/* OS Protection Keys Enable */ #define X86_FEATURE_AVX512_VBMI2 (16*32+ 6)/* Additional AVX512 Vector Bit Manipulation Instructions */ +#define X86_FEATURE_SHSTK (16*32+ 7)/* Shadow Stack */ #define X86_FEATURE_GFNI (16*32+ 8)/* Galois Field New Instructions */ #define X86_FEATURE_VAES (16*32+ 9)/* Vector AES */ #define X86_FEATURE_VPCLMULQDQ (16*32+10)/* Carry-Less Multiplication Double Quadword */
@@ -347,6 +348,7 @@#define X86_FEATURE_MD_CLEAR (18*32+10)/* VERW clears CPU buffers */ #define X86_FEATURE_TSX_FORCE_ABORT (18*32+13)/* "" TSX_FORCE_ABORT */ #define X86_FEATURE_PCONFIG (18*32+18)/* Intel PCONFIG */ +#define X86_FEATURE_IBT (18*32+20)/* Indirect Branch Tracking */ #define X86_FEATURE_SPEC_CTRL (18*32+26)/* "" Speculation Control (IBRS + IBPB) */ #define X86_FEATURE_INTEL_STIBP (18*32+27)/* "" Single Thread Indirect Branch Predictors */ #define X86_FEATURE_FLUSH_L1D (18*32+28)/* Flush L1D cache */
arch/x86/kernel/cpu/cpuid-deps.c +2 -0
@@ -59,6 +59,8 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_AVX512_4VNNIW, X86_FEATURE_AVX512F }, { X86_FEATURE_AVX512_4FMAPS, X86_FEATURE_AVX512F }, { X86_FEATURE_AVX512_VPOPCNTDQ, X86_FEATURE_AVX512F }, + { X86_FEATURE_SHSTK, X86_FEATURE_XSAVES }, + { X86_FEATURE_IBT, X86_FEATURE_XSAVES }, {} };
Signature · 1 line
2.17.1
PATCH v7 3/27 x86/fpu/xstate: Change names to separate XSAVES system and user states
In reply to Yu-cheng YuControl-flow Enforcement (CET) MSR contents are XSAVES system states. To support CET, introduce XSAVES system states first.
XSAVES is a "supervisor" instruction and, comparing to XSAVE, saves additional "supervisor" states that can be modified only from CPL 0. However, these states are per-task and not kernel's own. Rename "supervisor" states to "system" states to clearly separate them from "user" states.
--- arch/x86/include/asm/fpu/internal.h | 4 +- arch/x86/include/asm/fpu/xstate.h | 20 +++---- arch/x86/kernel/fpu/init.c | 2 +- arch/x86/kernel/fpu/signal.c | 10 ++-- arch/x86/kernel/fpu/xstate.c | 86 ++++++++++++++--------------- 5 files changed, 60 insertions(+), 62 deletions(-)
arch/x86/include/asm/fpu/internal.h +2 -2
@@ -92,7 +92,7 @@ static inline void fpstate_init_xstate(struct xregs_state *xsave)* XRSTORS requires these bits set in xcomp_bv, or it will * trigger #GP: */ - xsave->header.xcomp_bv =XCOMP_BV_COMPACTED_FORMAT | xfeatures_mask; + xsave->header.xcomp_bv =XCOMP_BV_COMPACTED_FORMAT | xfeatures_mask_user; }static inline void fpstate_init_fxstate (struct fxregs_state *fx)
@@ -225,7 +225,7 @@ static inline void copy_fxregs_to_kernel(struct fpu *fpu)/* * If XSAVES is enabled, it replaces XSAVEOPT because it supports a compact -* format and supervisor states in addition to modified optimization in +* format and system states in addition to modified optimization in * XSAVEOPT. * * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
arch/x86/include/asm/fpu/xstate.h +10 -10
@@ -25,15 +25,15 @@#define XFEATURE_MASK_SUPERVISOR (XFEATURE_MASK_PT)/* All currently supported features */ -#define XCNTXT_MASK (XFEATURE_MASK_FP | \ - XFEATURE_MASK_SSE | \ - XFEATURE_MASK_YMM | \ - XFEATURE_MASK_OPMASK | \ - XFEATURE_MASK_ZMM_Hi256 | \ - XFEATURE_MASK_Hi16_ZMM | \ - XFEATURE_MASK_PKRU | \ - XFEATURE_MASK_BNDREGS | \ - XFEATURE_MASK_BNDCSR) +#define SUPPORTED_XFEATURES_MASK (XFEATURE_MASK_FP | \ + XFEATURE_MASK_SSE | \ + XFEATURE_MASK_YMM | \ + XFEATURE_MASK_OPMASK | \ + XFEATURE_MASK_ZMM_Hi256 | \ + XFEATURE_MASK_Hi16_ZMM | \ + XFEATURE_MASK_PKRU | \ + XFEATURE_MASK_BNDREGS | \ + XFEATURE_MASK_BNDCSR)#ifdef CONFIG_X86_64 #define REX_PREFIX "0x48, "
@@ -41,7 +41,7 @@#define REX_PREFIX #endif -extern u64 xfeatures_mask; +extern u64 xfeatures_mask_user;extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS ]; extern void __init update_regset_xstate_info(unsigned int size,
arch/x86/kernel/fpu/init.c +1 -1
@@ -230,7 +230,7 @@ static void __init fpu__init_system_xstate_size_legacy(void)*/ u64 __init fpu__get_supported_xfeatures_mask (void ) { -return XCNTXT_MASK ; +return SUPPORTED_XFEATURES_MASK ; }/* Legacy code to initialize eager fpu mode. */
arch/x86/kernel/fpu/signal.c +5 -5
@@ -256,11 +256,11 @@ static int copy_user_to_fpregs_zeroing(void __user *buf, u64 xbv, int fx_only) {if (use_xsave ()) {if (fx_only) { -u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE ; +u64 init_bv = xfeatures_mask_user & ~XFEATURE_MASK_FPSSE ;copy_kernel_to_xregs (&init_fpstate.xsave, init_bv);return copy_user_to_fxregs (buf); }else { -u64 init_bv = xfeatures_mask & ~xbv; +u64 init_bv = xfeatures_mask_user & ~xbv;if (unlikely (init_bv))copy_kernel_to_xregs (&init_fpstate.xsave, init_bv);return copy_user_to_xregs (buf, xbv);
@@ -359,7 +359,7 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)if (use_xsave () && !fx_only) { -u64 init_bv = xfeatures_mask & ~xfeatures; +u64 init_bv = xfeatures_mask_user & ~xfeatures;if (using_compacted_format ()) { ret =copy_user_to_xstate (&fpu->state.xsave, buf_fx);
@@ -390,7 +390,7 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)fpregs_lock ();if (use_xsave ()) { -u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE ; +u64 init_bv = xfeatures_mask_user & ~XFEATURE_MASK_FPSSE ;copy_kernel_to_xregs (&init_fpstate.xsave, init_bv); }
@@ -464,7 +464,7 @@ void fpu__init_prepare_fx_sw_frame(void) fx_sw_reserved.magic1 =FP_XSTATE_MAGIC1 ; fx_sw_reserved.extended_size = size; - fx_sw_reserved.xfeatures = xfeatures_mask; + fx_sw_reserved.xfeatures = xfeatures_mask_user; fx_sw_reserved.xstate_size = fpu_user_xstate_size; if (IS_ENABLED(CONFIG_IA32_EMULATION) ||
arch/x86/kernel/fpu/xstate.c +42 -44
249 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v7 4/27 x86/fpu/xstate: Introduce XSAVES system states
In reply to Yu-cheng YuControl-flow Enforcement (CET) MSR contents are XSAVES system states. To support CET, introduce XSAVES system states first.
--- arch/x86/include/asm/fpu/internal.h | 21 ++++++- arch/x86/include/asm/fpu/xstate.h | 4 +- arch/x86/kernel/fpu/core.c | 26 +++++++-- arch/x86/kernel/fpu/init.c | 10 ---- arch/x86/kernel/fpu/signal.c | 4 +- arch/x86/kernel/fpu/xstate.c | 90 +++++++++++++++++++---------- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/signal.c | 2 +- 8 files changed, 104 insertions(+), 55 deletions(-)
arch/x86/include/asm/fpu/internal.h +18 -3
@@ -31,7 +31,8 @@ extern void fpu__save(struct fpu *fpu);extern int fpu__restore_sig (void __user *buf,int ia32_frame);extern void fpu__drop (struct fpu *fpu);extern int fpu__copy (struct task_struct *dst,struct task_struct *src); -extern void fpu__clear (struct fpu *fpu); +extern void fpu__clear_user_states (struct fpu *fpu); +extern void fpu__clear_all (struct fpu *fpu);extern int fpu__exception_code (struct fpu *fpu,int trap_nr);extern int dump_fpu (struct pt_regs *ptregs,struct user_i387_struct *fpstate);
@@ -44,7 +45,6 @@ extern void fpu__init_cpu_xstate(void);extern void fpu__init_system (struct cpuinfo_x86 *c);extern void fpu__init_check_bugs (void );extern void fpu__resume_cpu (void ); -extern u64 fpu__get_supported_xfeatures_mask (void );/* * Debugging facility:
@@ -92,7 +92,7 @@ static inline void fpstate_init_xstate(struct xregs_state *xsave)* XRSTORS requires these bits set in xcomp_bv, or it will * trigger #GP: */ - xsave->header.xcomp_bv =XCOMP_BV_COMPACTED_FORMAT | xfeatures_mask_user; + xsave->header.xcomp_bv =XCOMP_BV_COMPACTED_FORMAT | xfeatures_mask_all; }static inline void fpstate_init_fxstate (struct fxregs_state *fx)
@@ -615,6 +615,21 @@ static inline void switch_fpu_finish(struct fpu *new_fpu)__write_pkru (pkru_val); } +/* +* Helpers for changing XSAVES system states. +*/ +static inline void modify_fpu_regs_begin (void ) +{ +fpregs_lock (); +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +__fpregs_load_activate (); +} + +static inline void modify_fpu_regs_end (void ) +{ +fpregs_unlock (); +} +/* * MXCSR and XCR definitions: */
arch/x86/include/asm/fpu/xstate.h +1 -3
@@ -21,9 +21,6 @@#define XSAVE_YMM_SIZE 256#define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET) -/* Supervisor features */ -#define XFEATURE_MASK_SUPERVISOR (XFEATURE_MASK_PT) -/* All currently supported features */ #define SUPPORTED_XFEATURES_MASK (XFEATURE_MASK_FP | \ XFEATURE_MASK_SSE | \
@@ -42,6 +39,7 @@#endif extern u64 xfeatures_mask_user; +extern u64 xfeatures_mask_all;extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS ]; extern void __init update_regset_xstate_info(unsigned int size,
arch/x86/kernel/fpu/core.c +22 -4
@@ -316,12 +316,16 @@ void fpu__drop(struct fpu *fpu)* Clear FPU registers by setting them up from * the init fpstate: */ -static inline void copy_init_fpstate_to_fpregs (void ) +static inline void copy_init_fpstate_to_fpregs (u64 features_mask) {fpregs_lock (); +/* +* Only XSAVES user states are copied. +* System states are preserved. +*/ if (use_xsave ()) -copy_kernel_to_xregs (&init_fpstate.xsave,-1 ); +copy_kernel_to_xregs (&init_fpstate.xsave, features_mask);else if (static_cpu_has (X86_FEATURE_FXSR ))copy_kernel_to_fxregs (&init_fpstate.fxsave);else
@@ -340,7 +344,21 @@ static inline void copy_init_fpstate_to_fpregs(void)* Called by sys_execve(), by the signal handler code and by various * error paths. */ -void fpu__clear (struct fpu *fpu) +void fpu__clear_user_states (struct fpu *fpu) +{ +WARN_ON_FPU (fpu != ¤t->thread.fpu);/* Almost certainly an anomaly */ + +fpu__drop (fpu); + +/* +* Make sure fpstate is cleared and initialized. +*/ +fpu__initialize (fpu); +if (static_cpu_has (X86_FEATURE_FPU )) +copy_init_fpstate_to_fpregs (xfeatures_mask_user); +} + +void fpu__clear_all (struct fpu *fpu) {WARN_ON_FPU (fpu != ¤t->thread.fpu);/* Almost certainly an anomaly */
@@ -351,7 +369,7 @@ void fpu__clear(struct fpu *fpu)*/ fpu__initialize (fpu);if (static_cpu_has (X86_FEATURE_FPU )) -copy_init_fpstate_to_fpregs (); +copy_init_fpstate_to_fpregs (xfeatures_mask_all); }/*
arch/x86/kernel/fpu/init.c +0 -10
@@ -223,16 +223,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 SUPPORTED_XFEATURES_MASK ; -} -/* Legacy code to initialize eager fpu mode. */ static void __init fpu__init_system_ctx_switch (void ) {
arch/x86/kernel/fpu/signal.c +2 -2
@@ -287,7 +287,7 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size) IS_ENABLED(CONFIG_IA32_EMULATION));if (!buf) { -fpu__clear (fpu); +fpu__clear_user_states (fpu);return 0 ; }
@@ -409,7 +409,7 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size) err_out:if (ret) -fpu__clear (fpu); +fpu__clear_user_states (fpu);return ret; }
arch/x86/kernel/fpu/xstate.c +59 -31
229 lines not on this page · raw message
arch/x86/kernel/process.c +1 -1
@@ -139,7 +139,7 @@ void flush_thread(void)flush_ptrace_hw_breakpoint (tsk);memset (tsk->thread.tls_array,0 ,sizeof (tsk->thread.tls_array)); -fpu__clear (&tsk->thread.fpu); +fpu__clear_all (&tsk->thread.fpu); }void disable_TSC (void )
arch/x86/kernel/signal.c +1 -1
@@ -763,7 +763,7 @@ handle_signal(struct ksignal *ksig, struct pt_regs *regs)/* * Ensure the signal handler starts with the new fpu state. */ -fpu__clear (fpu); +fpu__clear_user_states (fpu); }signal_setup_done (failed, ksig, stepping); }
Signature · 1 line
2.17.1
Re: [PATCH v7 04/27] x86/fpu/xstate: Introduce XSAVES system states
Quoted 4 lines
arch/x86/include/asm/fpu/internal.h +4
-0+/* +* Helpers for changing XSAVES system states. +*/ +static inline void modify_fpu_regs_begin (void )
arch/x86/include/asm/fpu/internal.h static inline void switch_fpu_finish(struct fpu *new_fpu) · line 631
Quoted 10 lines
arch/x86/include/asm/fpu/internal.h +10
-0+{ +fpregs_lock (); +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +__fpregs_load_activate (); +} + +static inline void modify_fpu_regs_end (void ) +{ +fpregs_unlock (); +}
These are massively under-commented and under-changelogged. This looks like it's intended to ensure that we have supervisor FPU state for this task loaded before we go and run the MSRs that might be modifying it.
But, that seems broken. If we have supervisor state, we can't always defer the load until return to userspace, so we'll never?? have TIF_NEED_FPU_LOAD. That would certainly be true for cet_kernel_state.
It seems like we actually need three classes of XSAVE states:
1. User state
2. Supervisor state that affects user mode
3. Supervisor state that affects kernel mode
We can delay the load of 1 and 2, but not 3.
But I don't see any infrastructure for this.
Re: [PATCH v7 04/27] x86/fpu/xstate: Introduce XSAVES system states
Quoted 19 lines · Dave Hansen
Quoted 4 lines
arch/x86/include/asm/fpu/internal.h +4
-0+/* +* Helpers for changing XSAVES system states. +*/ +static inline void modify_fpu_regs_begin (void )arch/x86/include/asm/fpu/internal.h static inline void switch_fpu_finish(struct fpu *new_fpu) · line 631
Quoted 10 lines
arch/x86/include/asm/fpu/internal.h +10
-0+{ +fpregs_lock (); +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +__fpregs_load_activate (); +} + +static inline void modify_fpu_regs_end (void ) +{ +fpregs_unlock (); +}These are massively under-commented and under-changelogged. This looks like it's intended to ensure that we have supervisor FPU state for this task loaded before we go and run the MSRs that might be modifying it.
Quoted 3 lines
But, that seems broken. If we have supervisor state, we can't always defer the load until return to userspace, so we'll never?? have
TIF_NEED_FPU_LOAD. That would certainly be true forcet_kernel_state.
Ugh. I was sort of imagining that we would treat supervisor state completely separately from user state. But can you maybe give examples of exactly what you mean?
Quoted 3 lines
It seems like we actually need three classes of XSAVE states:
1. User state
This is FPU, XMM, etc, right?
Quoted 1 line
2. Supervisor state that affects user mode
User CET?
Quoted 1 line
3. Supervisor state that affects kernel mode
Like supervisor CET? If we start doing supervisor shadow stack, the context switches will be real fun. We may need to handle this in asm.
Where does PKRU fit in? Maybe we can treat it as #3?
—Andy
Re: [PATCH v7 04/27] x86/fpu/xstate: Introduce XSAVES system states
Quoted 6 lines · Andy Lutomirski
Quoted 4 lines
But, that seems broken. If we have supervisor state, we can't always defer the load until return to userspace, so we'll never?? have
TIF_NEED_FPU_LOAD. That would certainly be true forcet_kernel_state.Ugh. I was sort of imagining that we would treat supervisor state
completely separately from user state. But can you maybe give
examples of exactly what you mean?
Quoted 5 lines
Quoted 3 lines
It seems like we actually need three classes of XSAVE states: 1. User state
This is FPU, XMM, etc, right?
Yep.
Quoted 3 lines
Quoted 1 line
2. Supervisor state that affects user mode
User CET?
Yep.
Quoted 5 lines
Quoted 1 line
3. Supervisor state that affects kernel mode
Like supervisor CET? If we start doing supervisor shadow stack, the context switches will be real fun. We may need to handle this in asm.
Yeah, that's what I was thinking.
I have the feeling Yu-cheng's patches don't comprehend this since Sebastian's patches went in after he started working on shadow stacks.
Quoted 1 line
Where does PKRU fit in? Maybe we can treat it as #3?
I thought Sebastian added specific PKRU handling to make it always eager. It's actually user state that affect kernel mode. :)
Re: [PATCH v7 04/27] x86/fpu/xstate: Introduce XSAVES system states
Quoted 33 lines · Dave Hansen
Quoted 6 lines · Andy Lutomirski
Quoted 4 lines
But, that seems broken. If we have supervisor state, we can't always defer the load until return to userspace, so we'll never?? have
TIF_NEED_FPU_LOAD. That would certainly be true forcet_kernel_state.Ugh. I was sort of imagining that we would treat supervisor state
completely separately from user state. But can you maybe give
examples of exactly what you mean?Quoted 5 lines
Quoted 3 lines
It seems like we actually need three classes of XSAVE states: 1. User state
This is FPU, XMM, etc, right?
Yep.
Quoted 3 lines
Quoted 1 line
2. Supervisor state that affects user mode
User CET?
Yep.
Quoted 5 lines
Quoted 1 line
3. Supervisor state that affects kernel mode
Like supervisor CET? If we start doing supervisor shadow stack, the context switches will be real fun. We may need to handle this in asm.
Yeah, that's what I was thinking.
Quoted 7 lines
I have the feeling Yu-cheng's patches don't comprehend this since Sebastian's patches went in after he started working on shadow stacks.
Quoted 1 line
Where does PKRU fit in? Maybe we can treat it as #3?
I thought Sebastian added specific PKRU handling to make it always eager. It's actually user state that affect kernel mode. :)
For CET user states, we need to restore before making changes. If they are not being changed (i.e. signal handling and syscalls), then they are restored only before going back to user-mode.
For CET kernel states, we only need to make small changes in the way similar to the PKRU handling, right? We'll address it when sending CET kernel-mode patches.
I will put in more comments as suggested by Dave in earlier emails.
Yu-cheng
Re: [PATCH v7 04/27] x86/fpu/xstate: Introduce XSAVES system states
In reply to Dave HansenQuoted 7 lines · Dave Hansen
On 6/6/19 3:04 PM, Andy Lutomirski wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
But, that seems broken. If we have supervisor state, we can't always defer the load until return to userspace, so we'll never?? have
TIF_NEED_FPU_LOAD. That would certainly be true forcet_kernel_state.Ugh. I was sort of imagining that we would treat supervisor state
completely separately from user state. But can you maybe give examples of exactly what you mean?
I was imagining a completely separate area in memory for supervisor states. I guess this might defeat the modified optimization and is probably a bad idea.
Quoted 16 lines
Quoted 5 lines
Quoted 3 lines
It seems like we actually need three classes of XSAVE states: 1. User state
This is FPU, XMM, etc, right?
Yep.
Quoted 3 lines
Quoted 1 line
2. Supervisor state that affects user mode
User CET?
Yep.
Quoted 2 lines
Quoted 1 line
3. Supervisor state that affects kernel mode
Quoted 8 lines
Quoted 3 lines
Like supervisor CET? If we start doing supervisor shadow stack, the context switches will be real fun. We may need to handle this in asm.
Yeah, that's what I was thinking.
I have the feeling Yu-cheng's patches don't comprehend this since Sebastian's patches went in after he started working on shadow stacks.
Do we need to have TIF_LOAD_FPU mean “we need to load *some* of the xsave state”? If so, maybe a bunch of the accessors should have their interfaces reviewed to make sure they’re sill sensible.
Quoted 5 lines
Quoted 2 lines
Where does PKRU fit in? Maybe we can treat it as #3?
I thought Sebastian added specific PKRU handling to make it always eager. It's actually user state that affect kernel mode. :)
Indeed. But, if we document a taxonomy of states, we should make sure it fits in. I guess it’s like supervisor CET except that user code can can also read and write it.
We should probably have self tests that make sure that the correct states, and nothing else, show up in ptrace and signal states, and that trying to write supervisor CET via ptrace and sigreturn is properly rejected.
Just to double check my mental model: it’s okay to XSAVES twice to the same buffer with disjoint RFBM as long as we do something intelligent with XSTATE_BV afterwards, right? Because, unless we split up the buffers, I think we will have to do this when we context switch while TIF_LOAD_FPU is set.
Are there performance numbers for how the time needed to XRSTORS everything versus the time to XRSTORS supervisor CET and then separately XRSTORS the FPU? This may affect whether we want context switches to have the new task eagerly or lazily restored.
Hmm. I wonder if we need some way for a selftest to reliably trigger TIF_LOAD_FPU.
—Andy
PATCH v7 5/27 x86/fpu/xstate: Add XSAVES system states for shadow stack
In reply to Yu-cheng YuIntel Control-flow Enforcement Technology (CET) introduces the following MSRs.
MSR_IA32_U_CET (user-mode CET settings), MSR_IA32_PL3_SSP (user-mode shadow stack), MSR_IA32_PL0_SSP (kernel-mode shadow stack), MSR_IA32_PL1_SSP (Privilege Level 1 shadow stack), MSR_IA32_PL2_SSP (Privilege Level 2 shadow stack).
Introduce them into XSAVES system states.
--- arch/x86/include/asm/fpu/types.h | 22 +++++++++++++++++++++ arch/x86/include/asm/fpu/xstate.h | 4 +++- arch/x86/include/uapi/asm/processor-flags.h | 2 ++ arch/x86/kernel/fpu/xstate.c | 10 ++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-)
arch/x86/include/asm/fpu/types.h +22 -0
@@ -114,6 +114,9 @@ enum xfeature { XFEATURE_Hi16_ZMM,XFEATURE_PT_UNIMPLEMENTED_SO_FAR ,XFEATURE_PKRU , +XFEATURE_RESERVED , +XFEATURE_CET_USER , +XFEATURE_CET_KERNEL , XFEATURE_MAX, };
@@ -128,6 +131,8 @@ enum xfeature {#define XFEATURE_MASK_Hi16_ZMM (1 << XFEATURE_Hi16_ZMM)#define XFEATURE_MASK_PT (1 << XFEATURE_PT_UNIMPLEMENTED_SO_FAR)#define XFEATURE_MASK_PKRU (1 << XFEATURE_PKRU) +#define XFEATURE_MASK_CET_USER (1 << XFEATURE_CET_USER) +#define XFEATURE_MASK_CET_KERNEL (1 << XFEATURE_CET_KERNEL)#define XFEATURE_MASK_FPSSE (XFEATURE_MASK_FP | XFEATURE_MASK_SSE)#define XFEATURE_MASK_AVX512 (XFEATURE_MASK_OPMASK \
@@ -229,6 +234,23 @@ struct pkru_state {u32 pad; } __packed; +/* +* State component 11 is Control-flow Enforcement user states +*/ +struct cet_user_state { +u64 user_cet;/* user control-flow settings */ +u64 user_ssp;/* user shadow stack pointer */ +}; + +/* +* State component 12 is Control-flow Enforcement kernel states +*/ +struct cet_kernel_state { +u64 kernel_ssp;/* kernel shadow stack */ +u64 pl1_ssp;/* privilege level 1 shadow stack */ +u64 pl2_ssp;/* privilege level 2 shadow stack */ +}; +struct xstate_header {u64 xfeatures;u64 xcomp_bv;
arch/x86/include/asm/fpu/xstate.h +3 -1
@@ -30,7 +30,9 @@ XFEATURE_MASK_Hi16_ZMM | \ XFEATURE_MASK_PKRU | \ XFEATURE_MASK_BNDREGS | \ - XFEATURE_MASK_BNDCSR) + XFEATURE_MASK_BNDCSR | \ + XFEATURE_MASK_CET_USER | \ + XFEATURE_MASK_CET_KERNEL)#ifdef CONFIG_X86_64 #define REX_PREFIX "0x48, "
arch/x86/include/uapi/asm/processor-flags.h +2 -0
@@ -130,6 +130,8 @@#define X86_CR4_SMAP _BITUL(X86_CR4_SMAP_BIT)#define X86_CR4_PKE_BIT 22/* enable Protection Keys support */ #define X86_CR4_PKE _BITUL(X86_CR4_PKE_BIT) +#define X86_CR4_CET_BIT 23/* enable Control-flow Enforcement */ +#define X86_CR4_CET _BITUL(X86_CR4_CET_BIT)/* * x86-64 Task Priority Register, CR8
arch/x86/kernel/fpu/xstate.c +10 -0
@@ -36,6 +36,9 @@ static const char *xfeature_names[] ="Processor Trace (unused)" ,"Protection Keys User registers" ,"unknown xstate feature" , +"Control-flow User registers" , +"Control-flow Kernel registers" , + "unknown xstate feature" , }; static short xsave_cpuid_features[] __initdata = {
@@ -49,6 +52,9 @@ static short xsave_cpuid_features[] __initdata = {X86_FEATURE_AVX512F ,X86_FEATURE_INTEL_PT ,X86_FEATURE_PKU , +0 ,/* Unused */ +X86_FEATURE_SHSTK ,/* XFEATURE_CET_USER */ + X86_FEATURE_SHSTK, /* XFEATURE_CET_KERNEL */ };/*
@@ -320,6 +326,8 @@ static void __init print_xstate_features(void)print_xstate_feature (XFEATURE_MASK_ZMM_Hi256);print_xstate_feature (XFEATURE_MASK_Hi16_ZMM);print_xstate_feature (XFEATURE_MASK_PKRU ); +print_xstate_feature (XFEATURE_MASK_CET_USER ); +print_xstate_feature (XFEATURE_MASK_CET_KERNEL ); }/*
@@ -566,6 +574,8 @@ static void check_xstate_against_struct(int nr) XCHECK_SZ(sz, nr, XFEATURE_ZMM_Hi256, struct avx_512_zmm_uppers_state); XCHECK_SZ(sz, nr, XFEATURE_Hi16_ZMM, struct avx_512_hi16_state); XCHECK_SZ(sz, nr, XFEATURE_PKRU, struct pkru_state); + XCHECK_SZ(sz, nr, XFEATURE_CET_USER, struct cet_user_state); + XCHECK_SZ(sz, nr, XFEATURE_CET_KERNEL, struct cet_kernel_state);/* * Make *SURE* to add any feature numbers in below if
Signature · 1 line
2.17.1
Peter Zijlstra
maintainerRe: [PATCH v7 05/27] x86/fpu/xstate: Add XSAVES system states for shadow stack
Quoted 11 lines · Yu-cheng Yu
Intel Control-flow Enforcement Technology (CET) introduces the following MSRs.
MSR_IA32_U_CET(user-mode CET settings),MSR_IA32_PL3_SSP(user-mode shadow stack),MSR_IA32_PL0_SSP(kernel-mode shadow stack),MSR_IA32_PL1_SSP(Privilege Level 1 shadow stack),MSR_IA32_PL2_SSP(Privilege Level 2 shadow stack).Introduce them into XSAVES system states.
Quoted 7 lines
Signed-off-by Yu-cheng Yu <[email protected]>--- arch/x86/include/asm/fpu/types.h | 22 +++++++++++++++++++++ arch/x86/include/asm/fpu/xstate.h | 4 +++- arch/x86/include/uapi/asm/processor-flags.h | 2 ++ arch/x86/kernel/fpu/xstate.c | 10 ++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-)
And yet, no changes to msr-index.h !?
Re: [PATCH v7 05/27] x86/fpu/xstate: Add XSAVES system states for shadow stack
Quoted 20 lines · Peter Zijlstra
Quoted 11 lines · Yu-cheng Yu
Intel Control-flow Enforcement Technology (CET) introduces the following MSRs.
MSR_IA32_U_CET(user-mode CET settings),MSR_IA32_PL3_SSP(user-mode shadow stack),MSR_IA32_PL0_SSP(kernel-mode shadow stack),MSR_IA32_PL1_SSP(Privilege Level 1 shadow stack),MSR_IA32_PL2_SSP(Privilege Level 2 shadow stack).Introduce them into XSAVES system states.
Quoted 7 lines
Signed-off-by Yu-cheng Yu <[email protected]>--- arch/x86/include/asm/fpu/types.h | 22 +++++++++++++++++++++ arch/x86/include/asm/fpu/xstate.h | 4 +++- arch/x86/include/uapi/asm/processor-flags.h | 2 ++ arch/x86/kernel/fpu/xstate.c | 10 ++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-)
Quoted 1 line
And yet, no changes to
msr-index.h!?
You are right. I will move msr-index.h changes to here.
Yu-cheng
PATCH v7 6/27 x86/cet: Add control protection exception handler
In reply to Yu-cheng YuA control protection exception is triggered when a control flow transfer attempt violated shadow stack or indirect branch tracking constraints. For example, the return address for a RET instruction differs from the safe copy on the shadow stack; or a JMP instruction arrives at a non- ENDBR instruction.
The control protection exception handler works in a similar way as the
general protection fault handler.
--- arch/x86/entry/entry_64.S | 2 +- arch/x86/include/asm/traps.h | 3 ++ arch/x86/kernel/idt.c | 4 +++ arch/x86/kernel/signal_compat.c | 2 +- arch/x86/kernel/traps.c | 57 ++++++++++++++++++++++++++++++ include/uapi/asm-generic/siginfo.h | 3 +- 6 files changed, 68 insertions(+), 3 deletions(-)
arch/x86/entry/entry_64.S +1 -1
@@ -993,7 +993,7 @@ idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0 idtentry coprocessor_error do_coprocessor_error has_error_code=0 idtentry alignment_check do_alignment_check has_error_code=1 idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0 - +idtentry control_protection do_control_protection has_error_code=1 /* * Reload gs selector with exception handling
arch/x86/include/asm/traps.h +3 -0
@@ -26,6 +26,7 @@ asmlinkage void invalid_TSS(void); asmlinkage void segment_not_present(void); asmlinkage void stack_segment(void); asmlinkage void general_protection(void); +asmlinkage void control_protection(void); asmlinkage void page_fault(void); asmlinkage void async_page_fault(void); asmlinkage void spurious_interrupt_bug(void);
@@ -81,6 +82,7 @@ struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s);void __init trap_init (void );#endif dotraplinkage void do_general_protection(struct pt_regs *regs, long error_code); +dotraplinkage void do_control_protection(struct pt_regs *regs, long error_code); dotraplinkage void do_page_fault(struct pt_regs *regs, unsigned long error_code); dotraplinkage void do_spurious_interrupt_bug(struct pt_regs *regs, long error_code); dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code);
@@ -151,6 +153,7 @@ enum {X86_TRAP_AC ,/* 17, Alignment Check */ X86_TRAP_MC ,/* 18, Machine Check */ X86_TRAP_XF ,/* 19, SIMD Floating-Point Exception */ +X86_TRAP_CP =21 ,/* 21 Control Protection Fault */ X86_TRAP_IRET = 32, /* 32, IRET Exception */ };
arch/x86/kernel/idt.c +4 -0
@@ -103,6 +103,10 @@ static const __initconst struct idt_data def_idts[] = {#elif defined(CONFIG_X86_32 ) SYSG(IA32_SYSCALL_VECTOR, entry_INT80_32),#endif + +#ifdef CONFIG_X86_64 + INTG(X86_TRAP_CP, control_protection), +#endif };/*
arch/x86/kernel/signal_compat.c +1 -1
@@ -27,7 +27,7 @@ static inline void signal_compat_build_tests(void)*/ BUILD_BUG_ON (NSIGILL !=11 );BUILD_BUG_ON (NSIGFPE !=15 ); -BUILD_BUG_ON (NSIGSEGV !=7 ); +BUILD_BUG_ON (NSIGSEGV !=8 );BUILD_BUG_ON (NSIGBUS !=5 );BUILD_BUG_ON (NSIGTRAP !=5 );BUILD_BUG_ON (NSIGCHLD !=6 );
arch/x86/kernel/traps.c +57 -0
63 lines not on this page · raw message
include/uapi/asm-generic/siginfo.h +2 -1
@@ -229,7 +229,8 @@ typedef struct siginfo {#define SEGV_ACCADI 5/* ADI not enabled for mapped object */ #define SEGV_ADIDERR 6/* Disrupting MCD error */ #define SEGV_ADIPERR 7/* Precise MCD exception */ -#define NSIGSEGV 7 +#define SEGV_CPERR 8 +#define NSIGSEGV 8/* * SIGBUS si_codes
Signature · 1 line
2.17.1
PATCH v7 7/27 x86/cet/shstk: Add Kconfig option for user-mode shadow stack
In reply to Yu-cheng YuIntroduce Kconfig option X86_INTEL_SHADOW_STACK_USER.
An application has shadow stack protection when all the following are true:
(1) The kernel has X86_INTEL_SHADOW_STACK_USER enabled,
(2) The running processor supports the shadow stack,
(3) The application is built with shadow stack enabled tools & libs and, and at runtime, all dependent shared libs can support shadow stack.
If this kernel config option is enabled, but (2) or (3) above is not true, the application runs without the shadow stack protection. Existing legacy applications will continue to work without the shadow stack protection.
The user-mode shadow stack protection is only implemented for the 64-bit kernel. Thirty-two bit applications are supported under the compatibility mode.
--- arch/x86/Kconfig | 25 +++++++++++++++++++++++++ arch/x86/Makefile | 7 +++++++ 2 files changed, 32 insertions(+)
arch/x86/Kconfig +25 -0
@@ -1915,6 +1915,31 @@ config X86_INTEL_MEMORY_PROTECTION_KEYS If unsure, say y. +config X86_INTEL_CET + def_bool n + +config ARCH_HAS_SHSTK + def_bool n + +config X86_INTEL_SHADOW_STACK_USER + prompt "Intel Shadow Stack for user-mode" + def_bool n + depends on CPU_SUP_INTEL && X86_64 + select ARCH_USES_HIGH_VMA_FLAGS + select X86_INTEL_CET + select ARCH_HAS_SHSTK + ---help--- + Shadow stack provides hardware protection against program stack + corruption. Only when all the following are true will an application + have the shadow stack protection: the kernel supports it (i.e. this + feature is enabled), the application is compiled and linked with + shadow stack enabled, and the processor supports this feature. + When the kernel has this configuration enabled, existing non shadow + stack applications will continue to work, but without shadow stack + protection. + + If unsure, say y. + config EFI bool "EFI runtime service support" depends on ACPI
arch/x86/Makefile +7 -0
@@ -148,6 +148,13 @@ ifdef CONFIG_X86_X32 endif export CONFIG_X86_X32_ABI +# Check assembler shadow stack suppot +ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER + ifeq ($(call as-instr, saveprevssp, y),) + $(error CONFIG_X86_INTEL_SHADOW_STACK_USER not supported by the assembler) + endif +endif + # # If the function graph tracer is used with mcount instead of fentry, # '-maccumulate-outgoing-args' is needed to prevent a GCC bug
Signature · 1 line
2.17.1
PATCH v7 8/27 mm: Introduce VM_SHSTK for shadow stack memory
In reply to Yu-cheng YuVM_SHSTK indicates a shadow stack memory area.
The shadow stack is implemented only for the 64-bit kernel.
--- fs/proc/task_mmu.c | 3 +++ include/linux/mm.h | 8 ++++++++ 2 files changed, 11 insertions(+)
fs/proc/task_mmu.c +3 -0
@@ -661,6 +661,9 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) [ilog2(VM_PKEY_BIT4)] = "",#endif #endif /* CONFIG_ARCH_HAS_PKEYS */ +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER + [ilog2(VM_SHSTK)] = "ss" +#endif };size_t i;
include/linux/mm.h +8 -0
@@ -287,11 +287,13 @@ extern unsigned int kobjsize(const void *objp);#define VM_HIGH_ARCH_BIT_2 34/* bit only usable on 64-bit architectures */ #define VM_HIGH_ARCH_BIT_3 35/* bit only usable on 64-bit architectures */ #define VM_HIGH_ARCH_BIT_4 36/* bit only usable on 64-bit architectures */ +#define VM_HIGH_ARCH_BIT_5 37/* bit only usable on 64-bit architectures */ #define VM_HIGH_ARCH_0 BIT(VM_HIGH_ARCH_BIT_0)#define VM_HIGH_ARCH_1 BIT(VM_HIGH_ARCH_BIT_1)#define VM_HIGH_ARCH_2 BIT(VM_HIGH_ARCH_BIT_2)#define VM_HIGH_ARCH_3 BIT(VM_HIGH_ARCH_BIT_3)#define VM_HIGH_ARCH_4 BIT(VM_HIGH_ARCH_BIT_4) +#define VM_HIGH_ARCH_5 BIT(VM_HIGH_ARCH_BIT_5)#endif /* CONFIG_ARCH_USES_HIGH_VMA_FLAGS */ #ifdef CONFIG_ARCH_HAS_PKEYS
@@ -329,6 +331,12 @@ extern unsigned int kobjsize(const void *objp);# define VM_MPX VM_NONE#endif +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +# define VM_SHSTK VM_HIGH_ARCH_5 +#else +# define VM_SHSTK VM_NONE +#endif +#ifndef VM_GROWSUP # define VM_GROWSUP VM_NONE#endif
Signature · 1 line
2.17.1
PATCH v7 9/27 mm/mmap: Prevent Shadow Stack VMA merges
In reply to Yu-cheng YuTo prevent function call/return spills into the next shadow stack area, do not merge shadow stack areas.
--- mm/mmap.c | 6 ++++++ 1 file changed, 6 insertions(+)
mm/mmap.c +6 -0
@@ -1149,6 +1149,12 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,if (vm_flags &VM_SPECIAL )return NULL ; +/* +* Do not merge shadow stack areas. +*/ +if (vm_flags &VM_SHSTK ) +return NULL ; +if (prev) next = prev->vm_next;else
Signature · 1 line
2.17.1
PATCH v7 10/27 x86/mm: Change _PAGE_DIRTY to _PAGE_DIRTY_HW
In reply to Yu-cheng YuBefore introducing _PAGE_DIRTY_SW for non-hardware, memory management purposes in the next patch, rename _PAGE_DIRTY to _PAGE_DIRTY_HW and _PAGE_BIT_DIRTY to _PAGE_BIT_DIRTY_HW to make these PTE dirty bits more clear. There are no functional changes in this patch.
--- arch/x86/include/asm/pgtable.h | 6 +++--- arch/x86/include/asm/pgtable_types.h | 17 +++++++++-------- arch/x86/kernel/relocate_kernel_64.S | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-)
arch/x86/include/asm/pgtable.h +3 -3
@@ -332,7 +332,7 @@ static inline pte_t pte_mkexec(pte_t pte)static inline pte_t pte_mkdirty (pte_t pte) { -return pte_set_flags (pte, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); +return pte_set_flags (pte, _PAGE_DIRTY_HW | _PAGE_SOFT_DIRTY); }static inline pte_t pte_mkyoung (pte_t pte)
@@ -406,7 +406,7 @@ static inline pmd_t pmd_wrprotect(pmd_t pmd)static inline pmd_t pmd_mkdirty (pmd_t pmd) { -return pmd_set_flags (pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); +return pmd_set_flags (pmd, _PAGE_DIRTY_HW | _PAGE_SOFT_DIRTY); }static inline pmd_t pmd_mkdevmap (pmd_t pmd)
@@ -460,7 +460,7 @@ static inline pud_t pud_wrprotect(pud_t pud)static inline pud_t pud_mkdirty (pud_t pud) { -return pud_set_flags (pud, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); +return pud_set_flags (pud, _PAGE_DIRTY_HW | _PAGE_SOFT_DIRTY); }static inline pud_t pud_mkdevmap (pud_t pud)
arch/x86/include/asm/pgtable_types.h +9 -8
@@ -15,7 +15,7 @@#define _PAGE_BIT_PWT 3/* page write through */ #define _PAGE_BIT_PCD 4/* page cache disabled */ #define _PAGE_BIT_ACCESSED 5/* was accessed (raised by CPU) */ -#define _PAGE_BIT_DIRTY 6/* was written to (raised by CPU) */ +#define _PAGE_BIT_DIRTY_HW 6/* was written to (raised by CPU) */ #define _PAGE_BIT_PSE 7/* 4 MB (or 2MB) page */ #define _PAGE_BIT_PAT 7/* on 4KB pages */ #define _PAGE_BIT_GLOBAL 8/* Global TLB entry PPro+ */
@@ -45,7 +45,7 @@#define _PAGE_PWT (_AT(pteval_t, 1) << _PAGE_BIT_PWT)#define _PAGE_PCD (_AT(pteval_t, 1) << _PAGE_BIT_PCD)#define _PAGE_ACCESSED (_AT(pteval_t, 1) << _PAGE_BIT_ACCESSED) -#define _PAGE_DIRTY (_AT(pteval_t, 1) << _PAGE_BIT_DIRTY) +#define _PAGE_DIRTY_HW (_AT(pteval_t, 1) << _PAGE_BIT_DIRTY_HW)#define _PAGE_PSE (_AT(pteval_t, 1) << _PAGE_BIT_PSE)#define _PAGE_GLOBAL (_AT(pteval_t, 1) << _PAGE_BIT_GLOBAL)#define _PAGE_SOFTW1 (_AT(pteval_t, 1) << _PAGE_BIT_SOFTW1)
@@ -73,7 +73,7 @@ _PAGE_PKEY_BIT3)#if defined(CONFIG_X86_64 ) || defined(CONFIG_X86_PAE ) -#define _PAGE_KNL_ERRATUM_MASK (_PAGE_DIRTY | _PAGE_ACCESSED) +#define _PAGE_KNL_ERRATUM_MASK (_PAGE_DIRTY_HW | _PAGE_ACCESSED)#else #define _PAGE_KNL_ERRATUM_MASK 0#endif
@@ -112,9 +112,9 @@#define _PAGE_PROTNONE (_AT(pteval_t, 1) << _PAGE_BIT_PROTNONE)#define _PAGE_TABLE_NOENC (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |\ - _PAGE_ACCESSED | _PAGE_DIRTY) + _PAGE_ACCESSED | _PAGE_DIRTY_HW)#define _KERNPG_TABLE_NOENC (_PAGE_PRESENT | _PAGE_RW | \ - _PAGE_ACCESSED | _PAGE_DIRTY) + _PAGE_ACCESSED | _PAGE_DIRTY_HW)/* * Set of bits not changed in pte_modify. The pte's
@@ -123,7 +123,7 @@* pte_modify() does modify it. */ #define _PAGE_CHG_MASK (PTE_PFN_MASK | _PAGE_PCD | _PAGE_PWT | \ - _PAGE_SPECIAL | _PAGE_ACCESSED | _PAGE_DIRTY | \ + _PAGE_SPECIAL | _PAGE_ACCESSED | _PAGE_DIRTY_HW | \ _PAGE_SOFT_DIRTY | _PAGE_DEVMAP)#define _HPAGE_CHG_MASK (_PAGE_CHG_MASK | _PAGE_PSE)
@@ -168,7 +168,8 @@ enum page_cache_mode { _PAGE_ACCESSED)#define __PAGE_KERNEL_EXEC \ - (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_GLOBAL) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY_HW | _PAGE_ACCESSED | \ + _PAGE_GLOBAL)#define __PAGE_KERNEL (__PAGE_KERNEL_EXEC | _PAGE_NX)#define __PAGE_KERNEL_RO (__PAGE_KERNEL & ~_PAGE_RW)
@@ -187,7 +188,7 @@ enum page_cache_mode {#define _PAGE_ENC (_AT(pteval_t, sme_me_mask))#define _KERNPG_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | \ - _PAGE_DIRTY | _PAGE_ENC) + _PAGE_DIRTY_HW | _PAGE_ENC)#define _PAGE_TABLE (_KERNPG_TABLE | _PAGE_USER)#define __PAGE_KERNEL_ENC (__PAGE_KERNEL | _PAGE_ENC)
arch/x86/kernel/relocate_kernel_64.S +1 -1
@@ -17,7 +17,7 @@ */ #define PTR(x) (x << 3) -#define PAGE_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY) +#define PAGE_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY_HW) /* * control_page + KEXEC_CONTROL_CODE_MAX_SIZE
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -3364,7 +3364,7 @@ static int init_rmode_identity_map(struct kvm *kvm)/* Set up identity-mapping pagetable for EPT in real mode */ for (i =0 ; i <PT32_ENT_PER_PAGE ; i++) { tmp = (i <<22 ) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | - _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); + _PAGE_ACCESSED | _PAGE_DIRTY_HW | _PAGE_PSE); r =kvm_write_guest_page (kvm, identity_map_pfn, &tmp, i *sizeof (tmp),sizeof (tmp));if (r <0 )
Signature · 1 line
2.17.1
PATCH v7 11/27 x86/mm: Introduce _PAGE_DIRTY_SW
In reply to Yu-cheng YuA RO and dirty PTE exists in the following cases:
(a) A page is modified and then shared with a fork()'ed child;
(b) A R/O page that has been COW'ed;
(c) A SHSTK page.
The processor does not read the dirty bit for (a) and (b), but checks the dirty bit for (c). To prevent the use of non-SHSTK memory as SHSTK, we introduce a spare bit of the 64-bit PTE as _PAGE_BIT_DIRTY_SW and use that for (a) and (b). This results to the following possible PTE settings:
Modified PTE: (R/W + DIRTY_HW) Modified and shared PTE: (R/O + DIRTY_SW) R/O PTE COW'ed: (R/O + DIRTY_SW) SHSTK PTE: (R/O + DIRTY_HW) SHSTK PTE COW'ed: (R/O + DIRTY_HW) SHSTK PTE shared: (R/O + DIRTY_SW)
Note that _PAGE_BIT_DRITY_SW is only used in R/O PTEs but not R/W PTEs.
When this patch is applied, there are six free bits left in the 64-bit PTE. There is no more free bit in the 32-bit PTE (except for PAE) and shadow stack is not implemented for the 32-bit kernel.
--- arch/x86/include/asm/pgtable.h | 129 ++++++++++++++++++++++----- arch/x86/include/asm/pgtable_types.h | 21 ++++- 2 files changed, 128 insertions(+), 22 deletions(-)
arch/x86/include/asm/pgtable.h +109 -20
@@ -120,9 +120,9 @@ extern pmdval_t early_pmd_flags;* The following only work if pte_present() is true. * Undefined behaviour if not.. */ -static inline int pte_dirty (pte_t pte) +static inline bool pte_dirty (pte_t pte) { -return pte_flags (pte) & _PAGE_DIRTY; +return pte_flags (pte) & _PAGE_DIRTY_BITS; }
@@ -159,9 +159,9 @@ static inline int pte_young(pte_t pte)return pte_flags (pte) & _PAGE_ACCESSED; } -static inline int pmd_dirty (pmd_t pmd) +static inline bool pmd_dirty (pmd_t pmd) { -return pmd_flags (pmd) & _PAGE_DIRTY; +return pmd_flags (pmd) & _PAGE_DIRTY_BITS; }static inline int pmd_young (pmd_t pmd)
@@ -169,9 +169,9 @@ static inline int pmd_young(pmd_t pmd)return pmd_flags (pmd) & _PAGE_ACCESSED; } -static inline int pud_dirty (pud_t pud) +static inline bool pud_dirty (pud_t pud) { -return pud_flags (pud) & _PAGE_DIRTY; +return pud_flags (pud) & _PAGE_DIRTY_BITS; }static inline int pud_young (pud_t pud)
@@ -310,9 +310,23 @@ static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear)return native_make_pte (v & ~clear); } +#if defined(CONFIG_X86_INTEL_SHADOW_STACK_USER ) +static inline pte_t pte_move_flags (pte_t pte,pteval_t from,pteval_t to) +{ +if (pte_flags (pte) & from) + pte =pte_set_flags (pte_clear_flags (pte, from), to); +return pte; +} +#else +static inline pte_t pte_move_flags (pte_t pte,pteval_t from,pteval_t to) +{ +return pte; +} +#endif +static inline pte_t pte_mkclean (pte_t pte) { -return pte_clear_flags (pte, _PAGE_DIRTY); +return pte_clear_flags (pte, _PAGE_DIRTY_BITS); }static inline pte_t pte_mkold (pte_t pte)
@@ -322,6 +336,7 @@ static inline pte_t pte_mkold(pte_t pte)static inline pte_t pte_wrprotect (pte_t pte) { + pte =pte_move_flags (pte, _PAGE_DIRTY_HW, _PAGE_DIRTY_SW);return pte_clear_flags (pte, _PAGE_RW); }
@@ -332,9 +347,24 @@ static inline pte_t pte_mkexec(pte_t pte)static inline pte_t pte_mkdirty (pte_t pte) { +pteval_t dirty = (!IS_ENABLED (CONFIG_X86_INTEL_SHADOW_STACK_USER ) || +pte_write (pte)) ? _PAGE_DIRTY_HW:_PAGE_DIRTY_SW; +return pte_set_flags (pte, dirty | _PAGE_SOFT_DIRTY); +} + +#ifdef CONFIG_ARCH_HAS_SHSTK +static inline pte_t pte_mkdirty_shstk (pte_t pte) +{ + pte =pte_clear_flags (pte, _PAGE_DIRTY_SW);return pte_set_flags (pte, _PAGE_DIRTY_HW | _PAGE_SOFT_DIRTY); } +static inline bool pte_dirty_hw (pte_t pte) +{ +return pte_flags (pte) & _PAGE_DIRTY_HW; +} +#endif +static inline pte_t pte_mkyoung (pte_t pte) {return pte_set_flags (pte, _PAGE_ACCESSED);
@@ -342,6 +372,7 @@ static inline pte_t pte_mkyoung(pte_t pte)static inline pte_t pte_mkwrite (pte_t pte) { + pte =pte_move_flags (pte, _PAGE_DIRTY_SW, _PAGE_DIRTY_HW);return pte_set_flags (pte, _PAGE_RW); }
@@ -389,6 +420,20 @@ static inline pmd_t pmd_clear_flags(pmd_t pmd, pmdval_t clear)return native_make_pmd (v & ~clear); } +#if defined(CONFIG_X86_INTEL_SHADOW_STACK_USER ) +static inline pmd_t pmd_move_flags (pmd_t pmd,pmdval_t from,pmdval_t to) +{ +if (pmd_flags (pmd) & from) + pmd =pmd_set_flags (pmd_clear_flags (pmd, from), to); +return pmd; +} +#else +static inline pmd_t pmd_move_flags (pmd_t pmd,pmdval_t from,pmdval_t to) +{ +return pmd; +} +#endif +static inline pmd_t pmd_mkold (pmd_t pmd) {return pmd_clear_flags (pmd, _PAGE_ACCESSED);
@@ -396,19 +441,36 @@ static inline pmd_t pmd_mkold(pmd_t pmd)static inline pmd_t pmd_mkclean (pmd_t pmd) { -return pmd_clear_flags (pmd, _PAGE_DIRTY); +return pmd_clear_flags (pmd, _PAGE_DIRTY_BITS); }static inline pmd_t pmd_wrprotect (pmd_t pmd) { + pmd =pmd_move_flags (pmd, _PAGE_DIRTY_HW, _PAGE_DIRTY_SW);return pmd_clear_flags (pmd, _PAGE_RW); }static inline pmd_t pmd_mkdirty (pmd_t pmd) { +pmdval_t dirty = (!IS_ENABLED (CONFIG_X86_INTEL_SHADOW_STACK_USER ) || + (pmd_flags (pmd) & _PAGE_RW)) ? + _PAGE_DIRTY_HW:_PAGE_DIRTY_SW; +return pmd_set_flags (pmd, dirty | _PAGE_SOFT_DIRTY); +} + +#ifdef CONFIG_ARCH_HAS_SHSTK +static inline pmd_t pmd_mkdirty_shstk (pmd_t pmd) +{ + pmd =pmd_clear_flags (pmd, _PAGE_DIRTY_SW);return pmd_set_flags (pmd, _PAGE_DIRTY_HW | _PAGE_SOFT_DIRTY); } +static inline bool pmd_dirty_hw (pmd_t pmd) +{ +return pmd_flags (pmd) & _PAGE_DIRTY_HW; +} +#endif +static inline pmd_t pmd_mkdevmap (pmd_t pmd) {return pmd_set_flags (pmd, _PAGE_DEVMAP);
@@ -426,6 +488,7 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd)static inline pmd_t pmd_mkwrite (pmd_t pmd) { + pmd =pmd_move_flags (pmd, _PAGE_DIRTY_SW, _PAGE_DIRTY_HW);return pmd_set_flags (pmd, _PAGE_RW); }
@@ -443,6 +506,20 @@ static inline pud_t pud_clear_flags(pud_t pud, pudval_t clear)return native_make_pud (v & ~clear); } +#if defined(CONFIG_X86_INTEL_SHADOW_STACK_USER ) +static inline pud_t pud_move_flags (pud_t pud,pudval_t from,pudval_t to) +{ +if (pud_flags (pud) & from) + pud =pud_set_flags (pud_clear_flags (pud, from), to); +return pud; +} +#else +static inline pud_t pud_move_flags (pud_t pud,pudval_t from,pudval_t to) +{ +return pud; +} +#endif +static inline pud_t pud_mkold (pud_t pud) {return pud_clear_flags (pud, _PAGE_ACCESSED);
@@ -450,17 +527,22 @@ static inline pud_t pud_mkold(pud_t pud)static inline pud_t pud_mkclean (pud_t pud) { -return pud_clear_flags (pud, _PAGE_DIRTY); +return pud_clear_flags (pud, _PAGE_DIRTY_BITS); }static inline pud_t pud_wrprotect (pud_t pud) { + pud =pud_move_flags (pud, _PAGE_DIRTY_HW, _PAGE_DIRTY_SW);return pud_clear_flags (pud, _PAGE_RW); }static inline pud_t pud_mkdirty (pud_t pud) { -return pud_set_flags (pud, _PAGE_DIRTY_HW | _PAGE_SOFT_DIRTY); +pudval_t dirty = (!IS_ENABLED (CONFIG_X86_INTEL_SHADOW_STACK_USER ) || + (pud_flags (pud) & _PAGE_RW)) ? + _PAGE_DIRTY_HW:_PAGE_DIRTY_SW; + +return pud_set_flags (pud, dirty | _PAGE_SOFT_DIRTY); }static inline pud_t pud_mkdevmap (pud_t pud)
@@ -480,6 +562,7 @@ static inline pud_t pud_mkyoung(pud_t pud)static inline pud_t pud_mkwrite (pud_t pud) { + pud =pud_move_flags (pud, _PAGE_DIRTY_SW, _PAGE_DIRTY_HW);return pud_set_flags (pud, _PAGE_RW); }
@@ -611,19 +694,12 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) val &= _PAGE_CHG_MASK; val |=check_pgprot (newprot) & ~_PAGE_CHG_MASK; val =flip_protnone_guard (oldval, val,PTE_PFN_MASK ); +if ((pte_write (pte) && !(pgprot_val (newprot) & _PAGE_RW))) +return pte_move_flags (__pte (val), _PAGE_DIRTY_HW, + _PAGE_DIRTY_SW);return __pte (val); } -static inline pmd_t pmd_modify (pmd_t pmd,pgprot_t newprot) -{ -pmdval_t val =pmd_val (pmd), oldval = val; - - val &= _HPAGE_CHG_MASK; - val |=check_pgprot (newprot) & ~_HPAGE_CHG_MASK; - val =flip_protnone_guard (oldval, val,PHYSICAL_PMD_PAGE_MASK ); -return __pmd (val); -} -/* mprotect needs to preserve PAT bits when updating vm_page_prot */ #define pgprot_modify pgprot_modifystatic inline pgprot_t pgprot_modify (pgprot_t oldprot,pgprot_t newprot)
@@ -1178,6 +1254,19 @@ static inline int pmd_write(pmd_t pmd)return pmd_flags (pmd) & _PAGE_RW; } +static inline pmd_t pmd_modify (pmd_t pmd,pgprot_t newprot) +{ +pmdval_t val =pmd_val (pmd), oldval = val; + + val &= _HPAGE_CHG_MASK; + val |=check_pgprot (newprot) & ~_HPAGE_CHG_MASK; + val =flip_protnone_guard (oldval, val,PHYSICAL_PMD_PAGE_MASK ); +if ((pmd_write (pmd) && !(pgprot_val (newprot) & _PAGE_RW))) +return pmd_move_flags (__pmd (val), _PAGE_DIRTY_HW, + _PAGE_DIRTY_SW); +return __pmd (val); +} +#define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEARstatic inline pmd_t pmdp_huge_get_and_clear (struct mm_struct *mm,unsigned long addr,pmd_t *pmdp)
arch/x86/include/asm/pgtable_types.h +19 -2
@@ -23,6 +23,7 @@#define _PAGE_BIT_SOFTW2 10/* " */ #define _PAGE_BIT_SOFTW3 11/* " */ #define _PAGE_BIT_PAT_LARGE 12/* On 2MB or 1GB pages */ +#define _PAGE_BIT_SOFTW5 57/* available for programmer */ #define _PAGE_BIT_SOFTW4 58/* available for programmer */ #define _PAGE_BIT_PKEY_BIT0 59/* Protection Keys, bit 1/4 */ #define _PAGE_BIT_PKEY_BIT1 60/* Protection Keys, bit 2/4 */
@@ -34,6 +35,7 @@#define _PAGE_BIT_CPA_TEST _PAGE_BIT_SOFTW1#define _PAGE_BIT_SOFT_DIRTY _PAGE_BIT_SOFTW3/* software dirty tracking */ #define _PAGE_BIT_DEVMAP _PAGE_BIT_SOFTW4 +#define _PAGE_BIT_DIRTY_SW _PAGE_BIT_SOFTW5/* was written to */ /* If _PAGE_BIT_PRESENT is clear, we use these: */ /* - if the user mapped it with PROT_NONE; pte_present gives true */
@@ -109,6 +111,21 @@#define _PAGE_DEVMAP (_AT(pteval_t, 0))#endif +/* +* _PAGE_DIRTY_HW: set by the processor when a page is written. +* _PAGE_DIRTY_SW: a spare bit tracking a written, but now R/O page. +* [R/W + _PAGE_DIRTY_HW] <-> [R/O + _PAGE_DIRTY_SW]. +* _PAGE_SOFT_DIRTY: a spare bit used to track written pages since a time point +* set by the system admin; see Documentation/admin-guide/mm/soft-dirty.rst. +*/ +#if defined(CONFIG_X86_INTEL_SHADOW_STACK_USER ) +#define _PAGE_DIRTY_SW (_AT(pteval_t, 1) << _PAGE_BIT_DIRTY_SW) +#else +#define _PAGE_DIRTY_SW (_AT(pteval_t, 0)) +#endif + +#define _PAGE_DIRTY_BITS (_PAGE_DIRTY_HW | _PAGE_DIRTY_SW) +#define _PAGE_PROTNONE (_AT(pteval_t, 1) << _PAGE_BIT_PROTNONE)#define _PAGE_TABLE_NOENC (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |\
@@ -122,9 +139,9 @@* instance, and is *not* included in this mask since * pte_modify() does modify it. */ -#define _PAGE_CHG_MASK (PTE_PFN_MASK | _PAGE_PCD | _PAGE_PWT | \ +#define _PAGE_CHG_MASK (PTE_PFN_MASK | _PAGE_PCD | _PAGE_PWT | \ _PAGE_SPECIAL | _PAGE_ACCESSED | _PAGE_DIRTY_HW | \ - _PAGE_SOFT_DIRTY | _PAGE_DEVMAP) + _PAGE_DIRTY_SW | _PAGE_SOFT_DIRTY | _PAGE_DEVMAP)#define _HPAGE_CHG_MASK (_PAGE_CHG_MASK | _PAGE_PSE)/*
Signature · 1 line
2.17.1
PATCH v7 12/27 drm/i915/gvt: Update _PAGE_DIRTY to _PAGE_DIRTY_BITS
In reply to Yu-cheng YuUpdate _PAGE_DIRTY to _PAGE_DIRTY_BITS in split_2MB_gtt_entry().
In order to support Control-flow Enforcement (CET), _PAGE_DIRTY is now _PAGE_DIRTY_HW or _PAGE_DIRTY_SW.
--- drivers/gpu/drm/i915/gvt/gtt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/i915/gvt/gtt.c +1 -1
@@ -1185,7 +1185,7 @@ static int split_2MB_gtt_entry(struct intel_vgpu *vgpu, }/* Clear dirty field. */ - se->val64 &= ~_PAGE_DIRTY; + se->val64 &= ~_PAGE_DIRTY_BITS; ops->clear_pse (se); ops->clear_ips (se);
Signature · 1 line
2.17.1
PATCH v7 13/27 x86/mm: Modify ptep_set_wrprotect and pmdp_set_wrprotect for _PAGE_DIRTY_SW
In reply to Yu-cheng YuWhen Shadow Stack is enabled, the [R/O + PAGE_DIRTY_HW] setting is reserved only for the Shadow Stack. Non-Shadow Stack R/O PTEs use [R/O + PAGE_DIRTY_SW].
When a PTE goes from [R/W + PAGE_DIRTY_HW] to [R/O + PAGE_DIRTY_SW], it could become a transient Shadow Stack PTE in two cases.
The first case is that some processors can start a write but end up seeing a read-only PTE by the time they get to the Dirty bit, creating a transient Shadow Stack PTE. However, this will not occur on processors supporting Shadow Stack therefore we don't need a TLB flush here.
The second case is that when the software, without atomic, tests & replaces PAGE_DIRTY_HW with PAGE_DIRTY_SW, a transient Shadow Stack PTE can exist. This is prevented with cmpxchg.
Dave Hansen, Jann Horn, Andy Lutomirski, and Peter Zijlstra provided many insights to the issue. Jann Horn provided the cmpxchg solution.
--- arch/x86/include/asm/pgtable.h | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+)
arch/x86/include/asm/pgtable.h +58 -0
@@ -1222,7 +1222,36 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,static inline void ptep_set_wrprotect (struct mm_struct *mm,unsigned long addr,pte_t *ptep) { +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +pte_t new_pte, pte =READ_ONCE (*ptep); + +/* +* Some processors can start a write, but end up +* seeing a read-only PTE by the time they get +* to the Dirty bit. In this case, they will +* set the Dirty bit, leaving a read-only, Dirty +* PTE which looks like a Shadow Stack PTE. +* +* However, this behavior has been improved and +* will not occur on processors supporting +* Shadow Stacks. Without this guarantee, a +* transition to a non-present PTE and flush the +* TLB would be needed. +* +* When changing a writable PTE to read-only and +* if the PTE has _PAGE_DIRTY_HW set, we move +* that bit to _PAGE_DIRTY_SW so that the PTE is +* not a valid Shadow Stack PTE. +*/ +do { + new_pte =pte_wrprotect (pte); + new_pte.pte |= (new_pte.pte & _PAGE_DIRTY_HW) >> + _PAGE_BIT_DIRTY_HW << _PAGE_BIT_DIRTY_SW; + new_pte.pte &= ~_PAGE_DIRTY_HW; + }while (!try_cmpxchg (ptep, &pte, new_pte)); +#else clear_bit (_PAGE_BIT_RW, (unsigned long *)&ptep->pte); +#endif }#define flush_tlb_fix_spurious_fault (vma, address) do { } while (0)
@@ -1285,7 +1314,36 @@ static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,static inline void pmdp_set_wrprotect (struct mm_struct *mm,unsigned long addr,pmd_t *pmdp) { +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +pmd_t new_pmd, pmd =READ_ONCE (*pmdp); + +/* +* Some processors can start a write, but end up +* seeing a read-only PMD by the time they get +* to the Dirty bit. In this case, they will +* set the Dirty bit, leaving a read-only, Dirty +* PMD which looks like a Shadow Stack PMD. +* +* However, this behavior has been improved and +* will not occur on processors supporting +* Shadow Stacks. Without this guarantee, a +* transition to a non-present PMD and flush the +* TLB would be needed. +* +* When changing a writable PMD to read-only and +* if the PMD has _PAGE_DIRTY_HW set, we move +* that bit to _PAGE_DIRTY_SW so that the PMD is +* not a valid Shadow Stack PMD. +*/ +do { + new_pmd =pmd_wrprotect (pmd); + new_pmd.pmd |= (new_pmd.pmd & _PAGE_DIRTY_HW) >> + _PAGE_BIT_DIRTY_HW << _PAGE_BIT_DIRTY_SW; + new_pmd.pmd &= ~_PAGE_DIRTY_HW; + }while (!try_cmpxchg (pmdp, &pmd, new_pmd)); +#else clear_bit (_PAGE_BIT_RW, (unsigned long *)pmdp); +#endif }#define pud_write pud_write
Signature · 1 line
2.17.1
PATCH v7 14/27 x86/mm: Shadow stack page fault error checking
In reply to Yu-cheng YuIf a page fault is triggered by a shadow stack access (e.g. call/ret) or shadow stack management instructions (e.g. wrussq), then bit[6] of the page fault error code is set.
In access_error(), verify a shadow stack page fault is within a shadow stack memory area. It is always an error otherwise.
For a valid shadow stack access, set FAULT_FLAG_WRITE to effect copy-on-write.
--- arch/x86/include/asm/traps.h | 2 ++ arch/x86/mm/fault.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+)
arch/x86/include/asm/traps.h +2 -0
@@ -166,6 +166,7 @@ enum {* bit 3 == 1: use of reserved bit detected * bit 4 == 1: fault was an instruction fetch * bit 5 == 1: protection keys block access +* bit 6 == 1: shadow stack access fault */ enum x86_pf_error_code {X86_PF_PROT =1 <<0 ,
@@ -174,5 +175,6 @@ enum x86_pf_error_code {X86_PF_RSVD =1 <<3 ,X86_PF_INSTR =1 <<4 ,X86_PF_PK =1 <<5 , + X86_PF_SHSTK = 1 << 6, };#endif /* _ASM_X86_TRAPS_H */
arch/x86/mm/fault.c +18 -0
@@ -1205,6 +1205,17 @@ access_error(unsigned long error_code, struct vm_area_struct *vma) (error_code & X86_PF_INSTR), foreign))return 1 ; +/* +* Verify X86_PF_SHSTK is within a shadow stack VMA. +* It is always an error if there is a shadow stack +* fault outside a shadow stack VMA. +*/ +if (error_code &X86_PF_SHSTK ) { +if (!(vma->vm_flags &VM_SHSTK )) +return 1 ; +return 0 ; + } +if (error_code &X86_PF_WRITE ) {/* write, present and write, not present: */ if (unlikely (!(vma->vm_flags &VM_WRITE )))
@@ -1362,6 +1373,13 @@ void do_user_addr_fault(struct pt_regs *regs,perf_sw_event (PERF_COUNT_SW_PAGE_FAULTS ,1 , regs, address); +/* +* If the fault is caused by a shadow stack access, +* i.e. CALL/RET/SAVEPREVSSP/RSTORSSP, then set +* FAULT_FLAG_WRITE to effect copy-on-write. +*/ +if (hw_error_code &X86_PF_SHSTK ) + flags |=FAULT_FLAG_WRITE ;if (hw_error_code &X86_PF_WRITE ) flags |=FAULT_FLAG_WRITE ;if (hw_error_code &X86_PF_INSTR )
Signature · 1 line
2.17.1
PATCH v7 15/27 mm: Handle shadow stack page fault
In reply to Yu-cheng YuWhen a task does fork(), its shadow stack (SHSTK) must be duplicated for the child. This patch implements a flow similar to copy-on-write of an anonymous page, but for SHSTK.
A SHSTK PTE must be RO and dirty. This dirty bit requirement is used to effect the copying. In copy_one_pte(), clear the dirty bit from a SHSTK PTE to cause a page fault upon the next SHSTK access. At that time, fix the PTE and copy/re-use the page.
--- arch/x86/mm/pgtable.c | 15 +++++++++++++++ include/asm-generic/pgtable.h | 8 ++++++++ mm/memory.c | 7 ++++++- 3 files changed, 29 insertions(+), 1 deletion(-)
arch/x86/mm/pgtable.c +15 -0
@@ -891,3 +891,18 @@ int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)#endif /* CONFIG_X86_64 */ #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ + +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +inline pte_t pte_set_vma_features (pte_t pte,struct vm_area_struct *vma) +{ +if (vma->vm_flags &VM_SHSTK ) +return pte_mkdirty_shstk (pte); +else +return pte; +} + +inline bool arch_copy_pte_mapping (vm_flags_t vm_flags) +{ +return (vm_flags &VM_SHSTK ); +} +#endif /* CONFIG_X86_INTEL_SHADOW_STACK_USER */
include/asm-generic/pgtable.h +8 -0
@@ -1188,4 +1188,12 @@ static inline bool arch_has_pfn_modify_check(void)#define mm_pmd_folded (mm) __is_defined(__PAGETABLE_PMD_FOLDED)#endif +#ifndef CONFIG_ARCH_HAS_SHSTK +#define pte_set_vma_features (pte, vma) pte +#define arch_copy_pte_mapping (vma_flags) false +#else +pte_t pte_set_vma_features (pte_t pte,struct vm_area_struct *vma); +bool arch_copy_pte_mapping (vm_flags_t vm_flags); +#endif +#endif /* _ASM_GENERIC_PGTABLE_H */
mm/memory.c +6 -1
@@ -777,7 +777,8 @@ copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,* If it's a COW mapping, write protect it both * in the parent and the child */ -if (is_cow_mapping (vm_flags) &&pte_write (pte)) { +if ((is_cow_mapping (vm_flags) &&pte_write (pte)) || +arch_copy_pte_mapping (vm_flags)) {ptep_set_wrprotect (src_mm, addr, src_pte); pte =pte_wrprotect (pte); }
@@ -2312,6 +2313,7 @@ static inline void wp_page_reuse(struct vm_fault *vmf)flush_cache_page (vma, vmf->address,pte_pfn (vmf->orig_pte)); entry =pte_mkyoung (vmf->orig_pte); entry =maybe_mkwrite (pte_mkdirty (entry), vma); + entry =pte_set_vma_features (entry, vma);if (ptep_set_access_flags (vma, vmf->address, vmf->pte, entry,1 ))update_mmu_cache (vma, vmf->address, vmf->pte);pte_unmap_unlock (vmf->pte, vmf->ptl);
@@ -2387,6 +2389,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)flush_cache_page (vma, vmf->address,pte_pfn (vmf->orig_pte)); entry =mk_pte (new_page, vma->vm_page_prot); entry =maybe_mkwrite (pte_mkdirty (entry), vma); + entry =pte_set_vma_features (entry, vma);/* * Clear the pte entry and flush it first, before updating the * pte with the new entry. This will avoid a race condition
@@ -2910,6 +2913,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) pte =mk_pte (page, vma->vm_page_prot);if ((vmf->flags &FAULT_FLAG_WRITE ) &&reuse_swap_page (page,NULL )) { pte =maybe_mkwrite (pte_mkdirty (pte), vma); + pte =pte_set_vma_features (pte, vma); vmf->flags &= ~FAULT_FLAG_WRITE ; ret |=VM_FAULT_WRITE ; exclusive =RMAP_EXCLUSIVE ;
@@ -3052,6 +3056,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) entry =mk_pte (page, vma->vm_page_prot);if (vma->vm_flags &VM_WRITE ) entry =pte_mkwrite (pte_mkdirty (entry)); + entry =pte_set_vma_features (entry, vma); vmf->pte =pte_offset_map_lock (vma->vm_mm, vmf->pmd, vmf->address, &vmf->ptl);
Signature · 1 line
2.17.1
Peter Zijlstra
maintainerRe: [PATCH v7 15/27] mm: Handle shadow stack page fault
include/asm-generic/pgtable.h static inline bool arch_has_pfn_modify_check(void) · line 1193
Quoted 11 lines · Yu-cheng Yu
include/asm-generic/pgtable.h +3
-0@@ -1188,4 +1188,12 @@ static inline bool arch_has_pfn_modify_check(void)#define mm_pmd_folded (mm) __is_defined(__PAGETABLE_PMD_FOLDED)#endif +#ifndef CONFIG_ARCH_HAS_SHSTK +#define pte_set_vma_features (pte, vma) pte +#define arch_copy_pte_mapping (vma_flags) false
static inline pte_t pte_set_vma_features(pte_t pte, struct vm_area_struct *vma)
{
return pte;
}static inline bool arch_copy_pte_mapping(unsigned long vm_flags)
{
return false;
}Please, this way we retain function prototype checking.
Quoted 4 lines
include/asm-generic/pgtable.h +4
-0+#else +pte_t pte_set_vma_features (pte_t pte,struct vm_area_struct *vma); +bool arch_copy_pte_mapping (vm_flags_t vm_flags); +#endif
PATCH v7 16/27 mm: Handle THP/HugeTLB shadow stack page fault
In reply to Yu-cheng YuThis patch implements THP shadow stack (SHSTK) copying in the same way as in the previous patch for regular PTE.
In copy_huge_pmd(), clear the dirty bit from the PMD to cause a page fault upon the next SHSTK access to the PMD. At that time, fix the PMD and copy/re-use the page.
--- arch/x86/mm/pgtable.c | 8 ++++++++ include/asm-generic/pgtable.h | 2 ++ mm/huge_memory.c | 4 ++++ 3 files changed, 14 insertions(+)
arch/x86/mm/pgtable.c +8 -0
@@ -901,6 +901,14 @@ inline pte_t pte_set_vma_features(pte_t pte, struct vm_area_struct *vma)return pte; } +inline pmd_t pmd_set_vma_features (pmd_t pmd,struct vm_area_struct *vma) +{ +if (vma->vm_flags &VM_SHSTK ) +return pmd_mkdirty_shstk (pmd); +else +return pmd; +} +inline bool arch_copy_pte_mapping (vm_flags_t vm_flags) {return (vm_flags &VM_SHSTK );
include/asm-generic/pgtable.h +2 -0
@@ -1190,9 +1190,11 @@ static inline bool arch_has_pfn_modify_check(void)#ifndef CONFIG_ARCH_HAS_SHSTK #define pte_set_vma_features (pte, vma) pte +#define pmd_set_vma_features (pmd, vma) pmd#define arch_copy_pte_mapping (vma_flags) false#else pte_t pte_set_vma_features (pte_t pte,struct vm_area_struct *vma); +pmd_t pmd_set_vma_features (pmd_t pmd,struct vm_area_struct *vma);bool arch_copy_pte_mapping (vm_flags_t vm_flags);#endif
mm/huge_memory.c +4 -0
@@ -608,6 +608,7 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf, entry =mk_huge_pmd (page, vma->vm_page_prot); entry =maybe_pmd_mkwrite (pmd_mkdirty (entry), vma); + entry =pmd_set_vma_features (entry, vma);page_add_new_anon_rmap (page, vma, haddr, true);mem_cgroup_commit_charge (page, memcg, false, true);lru_cache_add_active_or_unevictable (page, vma);
@@ -1250,6 +1251,7 @@ static vm_fault_t do_huge_pmd_wp_page_fallback(struct vm_fault *vmf,pte_t entry; entry =mk_pte (pages[i], vma->vm_page_prot); entry =maybe_mkwrite (pte_mkdirty (entry), vma); + entry =pte_set_vma_features (entry, vma); memcg = (void *)page_private (pages[i]);set_page_private (pages[i],0 );page_add_new_anon_rmap (pages[i], vmf->vma, haddr, false);
@@ -1332,6 +1334,7 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd)pmd_t entry; entry =pmd_mkyoung (orig_pmd); entry =maybe_pmd_mkwrite (pmd_mkdirty (entry), vma); + entry =pmd_set_vma_features (entry, vma);if (pmdp_set_access_flags (vma, haddr, vmf->pmd, entry,1 ))update_mmu_cache_pmd (vma, vmf->address, vmf->pmd); ret |=VM_FAULT_WRITE ;
@@ -1404,6 +1407,7 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd)pmd_t entry; entry =mk_huge_pmd (new_page, vma->vm_page_prot); entry =maybe_pmd_mkwrite (pmd_mkdirty (entry), vma); + entry =pmd_set_vma_features (entry, vma);pmdp_huge_clear_flush_notify (vma, haddr, vmf->pmd);page_add_new_anon_rmap (new_page, vma, haddr, true);mem_cgroup_commit_charge (new_page, memcg, false, true);
Signature · 1 line
2.17.1
PATCH v7 17/27 mm: Update can_follow_write_pte/pmd for shadow stack
In reply to Yu-cheng Yucan_follow_write_pte/pmd look for the (RO & DIRTY) PTE/PMD to verify an exclusive RO page still exists after a broken COW.
A shadow stack PTE is RO & PAGE_DIRTY_SW when it is shared, otherwise RO & PAGE_DIRTY_HW.
Introduce pte_exclusive() and pmd_exclusive() to also verify a shadow stack PTE is exclusive.
Also rename can_follow_write_pte/pmd() to can_follow_write() to make their meaning clear; i.e. "Can we write to the page?", not "Is the PTE writable?"
--- arch/x86/mm/pgtable.c | 18 ++++++++++++++++++ include/asm-generic/pgtable.h | 4 ++++ mm/gup.c | 8 +++++--- mm/huge_memory.c | 8 +++++--- 4 files changed, 32 insertions(+), 6 deletions(-)
arch/x86/mm/pgtable.c +18 -0
@@ -913,4 +913,22 @@ inline bool arch_copy_pte_mapping(vm_flags_t vm_flags) {return (vm_flags &VM_SHSTK ); } + +inline bool pte_exclusive (pte_t pte,struct vm_area_struct *vma) +{ +if (vma->vm_flags &VM_SHSTK ) +return pte_dirty_hw (pte); +else +return pte_dirty (pte); +} + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +inline bool pmd_exclusive (pmd_t pmd,struct vm_area_struct *vma) +{ +if (vma->vm_flags &VM_SHSTK ) +return pmd_dirty_hw (pmd); +else +return pmd_dirty (pmd); +} +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ #endif /* CONFIG_X86_INTEL_SHADOW_STACK_USER */
include/asm-generic/pgtable.h +4 -0
@@ -1192,10 +1192,14 @@ static inline bool arch_has_pfn_modify_check(void)#define pte_set_vma_features (pte, vma) pte#define pmd_set_vma_features (pmd, vma) pmd#define arch_copy_pte_mapping (vma_flags) false +#define pte_exclusive (pte, vma) pte_dirty(pte) +#define pmd_exclusive (pmd, vma) pmd_dirty(pmd)#else pte_t pte_set_vma_features (pte_t pte,struct vm_area_struct *vma);pmd_t pmd_set_vma_features (pmd_t pmd,struct vm_area_struct *vma);bool arch_copy_pte_mapping (vm_flags_t vm_flags); +bool pte_exclusive (pte_t pte,struct vm_area_struct *vma); +bool pmd_exclusive (pmd_t pmd,struct vm_area_struct *vma);#endif #endif /* _ASM_GENERIC_PGTABLE_H */
mm/gup.c +5 -3
@@ -178,10 +178,12 @@ static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,* FOLL_FORCE can write to even unwritable pte's, but only * after we've gone through a COW cycle and they are dirty. */ -static inline bool can_follow_write_pte (pte_t pte,unsigned int flags) +static inline bool can_follow_write (pte_t pte,unsigned int flags, +struct vm_area_struct *vma) {return pte_write (pte) || - ((flags &FOLL_FORCE ) && (flags &FOLL_COW ) &&pte_dirty (pte)); + ((flags &FOLL_FORCE ) && (flags &FOLL_COW ) && +pte_exclusive (pte, vma)); } static struct page *follow_page_pte(struct vm_area_struct *vma,
@@ -219,7 +221,7 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, }if ((flags &FOLL_NUMA ) &&pte_protnone (pte)) goto no_page; -if ((flags &FOLL_WRITE ) && !can_follow_write_pte (pte, flags)) { +if ((flags &FOLL_WRITE ) && !can_follow_write (pte, flags, vma)) {pte_unmap_unlock (ptep, ptl);return NULL ; }
mm/huge_memory.c +5 -3
@@ -1441,10 +1441,12 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd)* FOLL_FORCE can write to even unwritable pmd's, but only * after we've gone through a COW cycle and they are dirty. */ -static inline bool can_follow_write_pmd (pmd_t pmd,unsigned int flags) +static inline bool can_follow_write (pmd_t pmd,unsigned int flags, +struct vm_area_struct *vma) {return pmd_write (pmd) || - ((flags &FOLL_FORCE ) && (flags &FOLL_COW ) &&pmd_dirty (pmd)); + ((flags &FOLL_FORCE ) && (flags &FOLL_COW ) && +pmd_exclusive (pmd, vma)); } struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
@@ -1457,7 +1459,7 @@ struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,assert_spin_locked (pmd_lockptr (mm, pmd)); -if (flags &FOLL_WRITE && !can_follow_write_pmd (*pmd, flags)) +if (flags &FOLL_WRITE && !can_follow_write (*pmd, flags, vma)) goto out;/* Avoid dumping huge zero page */
Signature · 1 line
2.17.1
PATCH v7 18/27 mm: Introduce do_mmap_locked()
In reply to Yu-cheng YuThere are a few places that need do_mmap() with mm->mmap_sem held. Create an in-line function for that.
--- include/linux/mm.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
include/linux/mm.h +18 -0
@@ -2411,6 +2411,24 @@ static inline void mm_populate(unsigned long addr, unsigned long len)static inline void mm_populate (unsigned long addr,unsigned long len) {}#endif +static inline unsigned long do_mmap_locked (unsigned long addr, +unsigned long len,unsigned long prot,unsigned long flags, +vm_flags_t vm_flags) +{ +struct mm_struct *mm = current->mm; +unsigned long populate; + +down_write (&mm->mmap_sem); + addr =do_mmap (NULL , addr, len, prot, flags, vm_flags,0 , + &populate,NULL ); +up_write (&mm->mmap_sem); + +if (populate) +mm_populate (addr, populate); + +return addr; +} +/* These take the mm semaphore themselves */ extern int __must_check vm_brk (unsigned long ,unsigned long );extern int __must_check vm_brk_flags (unsigned long ,unsigned long ,unsigned long );
Signature · 1 line
2.17.1
Peter Zijlstra
maintainerRe: [PATCH v7 18/27] mm: Introduce do_mmap_locked()
Quoted 16 lines · Yu-cheng Yu
There are a few places that need
do_mmap()withmm->mmap_semheld. Create an in-line function for that.Signed-off-by Yu-cheng Yu <[email protected]>--- include/linux/mm.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)include/linux/mm.h +0
-0@@ -2411,6 +2411,24 @@ static inline void mm_populate(unsigned long addr, unsigned long len)static inline void mm_populate (unsigned long addr,unsigned long len) {}#endif
include/linux/mm.h static inline void mm_populate(unsigned long addr, unsigned long len) · line 2423
Quoted 10 lines
include/linux/mm.h +10
-0+static inline unsigned long do_mmap_locked (unsigned long addr, +unsigned long len,unsigned long prot,unsigned long flags, +vm_flags_t vm_flags) +{ +struct mm_struct *mm = current->mm; +unsigned long populate; + +down_write (&mm->mmap_sem); + addr =do_mmap (NULL , addr, len, prot, flags, vm_flags,0 , + &populate,NULL );
Funny thing how do_mmap() takes a file pointer as first argument and this thing explicitly NULLs that. That more or less invalidates the name do_mmap_locked().
Quoted 7 lines
include/linux/mm.h +7
-0+up_write (&mm->mmap_sem); + +if (populate) +mm_populate (addr, populate); + +return addr; +}
Peter Zijlstra
maintainerRe: [PATCH v7 18/27] mm: Introduce do_mmap_locked()
Quoted 32 lines · Peter Zijlstra
Quoted 16 lines · Yu-cheng Yu
There are a few places that need
do_mmap()withmm->mmap_semheld. Create an in-line function for that.Signed-off-by Yu-cheng Yu <[email protected]>--- include/linux/mm.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)include/linux/mm.h +0
-0@@ -2411,6 +2411,24 @@ static inline void mm_populate(unsigned long addr, unsigned long len)static inline void mm_populate (unsigned long addr,unsigned long len) {}#endif include/linux/mm.h static inline void mm_populate(unsigned long addr, unsigned long len) · line 2423
Quoted 10 lines
include/linux/mm.h +10
-0+static inline unsigned long do_mmap_locked (unsigned long addr, +unsigned long len,unsigned long prot,unsigned long flags, +vm_flags_t vm_flags) +{ +struct mm_struct *mm = current->mm; +unsigned long populate; + +down_write (&mm->mmap_sem); + addr =do_mmap (NULL , addr, len, prot, flags, vm_flags,0 , + &populate,NULL );Funny thing how
do_mmap()takes a file pointer as first argument and this thing explicitly NULLs that. That more or less invalidates the namedo_mmap_locked().
Quoted 7 lines
include/linux/mm.h static inline void mm_populate(unsigned long addr, unsigned long len) · line 2430
Quoted 7 lines
include/linux/mm.h +7
-0+up_write (&mm->mmap_sem); + +if (populate) +mm_populate (addr, populate); + +return addr; +}
You also don't retain that last @uf argument.
I'm thikning you're better off adding a helper to the cet.c file; call it cet_mmap() or whatever.
Re: [PATCH v7 18/27] mm: Introduce do_mmap_locked()
Quoted 44 lines · Peter Zijlstra
Quoted 33 lines · Peter Zijlstra
Quoted 17 lines · Yu-cheng Yu
There are a few places that need
do_mmap()withmm->mmap_semheld. Create an in-line function for that.Signed-off-by Yu-cheng Yu <[email protected]>--- include/linux/mm.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)include/linux/mm.h +0
-0@@ -2411,6 +2411,24 @@ static inline void mm_populate(unsigned long addr, unsigned long len)static inline void mm_populate (unsigned long addr,unsigned long len) {}#endif include/linux/mm.h static inline void mm_populate(unsigned long addr, · line 2424
Quoted 10 lines
include/linux/mm.h +10
-0+static inline unsigned long do_mmap_locked (unsigned long addr, +unsigned long len,unsigned long prot,unsigned long flags, +vm_flags_t vm_flags) +{ +struct mm_struct *mm = current->mm; +unsigned long populate; + +down_write (&mm->mmap_sem); + addr =do_mmap (NULL , addr, len, prot, flags, vm_flags,0 , + &populate,NULL );Funny thing how
do_mmap()takes a file pointer as first argument and this thing explicitly NULLs that. That more or less invalidates the namedo_mmap_locked().Quoted 7 lines
include/linux/mm.h static inline void mm_populate(unsigned long addr, unsigned long len) · line 2430
Quoted 7 lines
include/linux/mm.h +7
-0+up_write (&mm->mmap_sem); + +if (populate) +mm_populate (addr, populate); + +return addr; +}You also don't retain that last @uf argument.
Quoted 2 lines
I'm thikning you're better off adding a helper to the
cet.cfile; call itcet_mmap()or whatever.
Ok, I will fix that.
Yu-cheng
PATCH v7 19/27 x86/cet/shstk: User-mode shadow stack support
In reply to Yu-cheng YuThis patch adds basic shadow stack enabling/disabling routines. A task's shadow stack is allocated from memory with VM_SHSTK flag set and read-only protection. It has a fixed size of RLIMIT_STACK.
--- arch/x86/include/asm/cet.h | 34 +++++ arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 18 +++ arch/x86/include/asm/processor.h | 5 + arch/x86/kernel/Makefile | 2 + arch/x86/kernel/cet.c | 116 ++++++++++++++++++ arch/x86/kernel/cpu/common.c | 25 ++++ arch/x86/kernel/process.c | 1 + .../arch/x86/include/asm/disabled-features.h | 8 +- 9 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 arch/x86/include/asm/cet.h create mode 100644 arch/x86/kernel/cet.c
arch/x86/include/asm/cet.h new file +34 -0
@@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_CET_H +#define _ASM_X86_CET_H + +#ifndef __ASSEMBLY__ +#include <linux/types.h> + +struct task_struct ; +/* +* Per-thread CET status +*/ +struct cet_status { +unsigned long shstk_base; +unsigned long shstk_size; +unsigned int shstk_enabled:1 ; +}; + +#ifdef CONFIG_X86_INTEL_CET +int cet_setup_shstk (void ); +void cet_disable_shstk (void ); +void cet_disable_free_shstk (struct task_struct *p); +#else +static inline int cet_setup_shstk (void ) {return -EINVAL ; } +static inline void cet_disable_shstk (void ) {} +static inline void cet_disable_free_shstk (struct task_struct *p) {} +#endif + +#define cpu_x86_cet_enabled () \ + (cpu_feature_enabled(X86_FEATURE_SHSTK) || \ + cpu_feature_enabled(X86_FEATURE_IBT)) + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_X86_CET_H */
arch/x86/include/asm/disabled-features.h +7 -1
@@ -62,6 +62,12 @@# define DISABLE_PTI (1 << (X86_FEATURE_PTI & 31))#endif +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +#define DISABLE_SHSTK 0 +#else +#define DISABLE_SHSTK (1<<(X86_FEATURE_SHSTK & 31)) +#endif +/* * Make sure to add features to the correct mask */
@@ -81,7 +87,7 @@#define DISABLED_MASK13 0#define DISABLED_MASK14 0#define DISABLED_MASK15 0 -#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP) +#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP|DISABLE_SHSTK)#define DISABLED_MASK17 0#define DISABLED_MASK18 0#define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 19)
arch/x86/include/asm/msr-index.h +18 -0
@@ -839,4 +839,22 @@#define MSR_VM_IGNNE 0xc0010115#define MSR_VM_HSAVE_PA 0xc0010117 +/* Control-flow Enforcement Technology MSRs */ +#define MSR_IA32_U_CET 0x6a0/* user mode cet setting */ +#define MSR_IA32_S_CET 0x6a2/* kernel mode cet setting */ +#define MSR_IA32_PL0_SSP 0x6a4/* kernel shstk pointer */ +#define MSR_IA32_PL1_SSP 0x6a5/* ring-1 shstk pointer */ +#define MSR_IA32_PL2_SSP 0x6a6/* ring-2 shstk pointer */ +#define MSR_IA32_PL3_SSP 0x6a7/* user shstk pointer */ +#define MSR_IA32_INT_SSP_TAB 0x6a8/* exception shstk table */ + +/* MSR_IA32_U_CET and MSR_IA32_S_CET bits */ +#define MSR_IA32_CET_SHSTK_EN 0x0000000000000001ULL +#define MSR_IA32_CET_WRSS_EN 0x0000000000000002ULL +#define MSR_IA32_CET_ENDBR_EN 0x0000000000000004ULL +#define MSR_IA32_CET_LEG_IW_EN 0x0000000000000008ULL +#define MSR_IA32_CET_NO_TRACK_EN 0x0000000000000010ULL +#define MSR_IA32_CET_WAIT_ENDBR 0x00000000000000800UL +#define MSR_IA32_CET_BITMAP_MASK 0xfffffffffffff000ULL +#endif /* _ASM_X86_MSR_INDEX_H */
arch/x86/include/asm/processor.h +5 -0
@@ -24,6 +24,7 @@ struct vm86;#include <asm/special_insns.h> #include <asm/fpu/types.h> #include <asm/unwind_hints.h> +#include <asm/cet.h> #include <linux/personality.h> #include <linux/cache.h>
@@ -487,6 +488,10 @@ struct thread_struct { unsigned int sig_on_uaccess_err:1; unsigned int uaccess_err:1; /* uaccess failed */ +#ifdef CONFIG_X86_INTEL_CET +struct cet_status cet; +#endif +/* Floating point and extended processor state */ struct fpu fpu;/*
arch/x86/kernel/Makefile +2 -0
@@ -140,6 +140,8 @@ obj-$(CONFIG_UNWINDER_ORC) += unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.o obj-$(CONFIG_UNWINDER_GUESS) += unwind_guess.o +obj-$(CONFIG_X86_INTEL_CET) += cet.o + ### # 64 bit specific files ifeq ($(CONFIG_X86_64),y)
arch/x86/kernel/cet.c new file +116 -0
116 lines not on this page · raw message
arch/x86/kernel/cpu/common.c +25 -0
@@ -53,6 +53,7 @@#include <asm/microcode_intel.h> #include <asm/intel-family.h> #include <asm/cpu_device_id.h> +#include <asm/cet.h> #ifdef CONFIG_X86_LOCAL_APIC #include <asm/uv/uv.h>
@@ -417,6 +418,29 @@ static __init int setup_disable_pku(char *arg)__setup ("nopku" , setup_disable_pku);#endif /* CONFIG_X86_64 */ +static __always_inline void setup_cet(struct cpuinfo_x86 *c) +{ +if (cpu_x86_cet_enabled ()) +cr4_set_bits (X86_CR4_CET ); +} + +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +static __init int setup_disable_shstk (char *s) +{ +/* require an exact match without trailing characters */ +if (s[0 ] !='\0' ) +return 0 ; + +if (!boot_cpu_has (X86_FEATURE_SHSTK )) +return 1 ; + +setup_clear_cpu_cap (X86_FEATURE_SHSTK ); +pr_info ("x86: 'no_cet_shstk' specified, disabling Shadow Stack\n" ); +return 1 ; +} +__setup ("no_cet_shstk" , setup_disable_shstk); +#endif +/* * Some CPU features depend on higher CPUID levels, which may not always * be available due to CPUID level capping or broken virtualization
@@ -1393,6 +1417,7 @@ static void identify_cpu(struct cpuinfo_x86 *c)x86_init_rdrand (c);x86_init_cache_qos (c);setup_pku (c); +setup_cet (c);/* * Clear/Set all flags overridden by options, need do it
arch/x86/kernel/process.c +1 -0
@@ -42,6 +42,7 @@#include <asm/prctl.h> #include <asm/spec-ctrl.h> #include <asm/proto.h> +#include <asm/cet.h> #include "process.h"
tools/arch/x86/include/asm/disabled-features.h +7 -1
@@ -62,6 +62,12 @@# define DISABLE_PTI (1 << (X86_FEATURE_PTI & 31))#endif +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +#define DISABLE_SHSTK 0 +#else +#define DISABLE_SHSTK (1<<(X86_FEATURE_SHSTK & 31)) +#endif +/* * Make sure to add features to the correct mask */
@@ -81,7 +87,7 @@#define DISABLED_MASK13 0#define DISABLED_MASK14 0#define DISABLED_MASK15 0 -#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP) +#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP|DISABLE_SHSTK)#define DISABLED_MASK17 0#define DISABLED_MASK18 0#define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 19)
Signature · 1 line
2.17.1
PATCH v7 20/27 x86/cet/shstk: Introduce WRUSS instruction
In reply to Yu-cheng YuWRUSS is a new kernel-mode instruction but writes directly to user shadow stack memory. This is used to construct a return address on the shadow stack for the signal handler.
This instruction can fault if the user shadow stack is invalid shadow stack memory. In that case, the kernel does a fixup.
--- arch/x86/include/asm/special_insns.h | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
arch/x86/include/asm/special_insns.h +32 -0
@@ -250,6 +250,38 @@ static inline void clwb(volatile void *__p) : [pax] "a" (p)); } +#ifdef CONFIG_X86_INTEL_CET +#if defined(CONFIG_IA32_EMULATION ) || defined(CONFIG_X86_X32 ) +static inline int write_user_shstk_32 (unsigned long addr,unsigned int val) +{ + asm_volatile_goto("1: wrussd %1, (%0)\n" + _ASM_EXTABLE(1b, %l[fail]) + :: "r" (addr), "r" (val) + :: fail); +return 0 ; +fail: +return -EPERM ; +} +#else +static inline int write_user_shstk_32 (unsigned long addr,unsigned int val) +{ +WARN_ONCE (1 ,"%s used but not supported.\n" , __func__); +return -EFAULT ; +} +#endif + +static inline int write_user_shstk_64 (unsigned long addr,unsigned long val) +{ + asm_volatile_goto("1: wrussq %1, (%0)\n" + _ASM_EXTABLE(1b, %l[fail]) + :: "r" (addr), "r" (val) + :: fail); +return 0 ; +fail: +return -EPERM ; +} +#endif /* CONFIG_X86_INTEL_CET */ +#define nop () asm volatile ("nop")
Signature · 1 line
2.17.1
PATCH v7 21/27 x86/cet/shstk: Handle signals for shadow stack
In reply to Yu-cheng YuWhen setting up a signal, the kernel creates a shadow stack restore token at the current SHSTK address and then stores the token's address in the signal frame, right after the FPU state. Before restoring a signal, the kernel verifies and then uses the restore token to set the SHSTK pointer.
--- arch/x86/ia32/ia32_signal.c | 8 ++ arch/x86/include/asm/cet.h | 7 ++ arch/x86/include/asm/fpu/internal.h | 2 + arch/x86/include/asm/fpu/signal.h | 2 + arch/x86/include/uapi/asm/sigcontext.h | 15 +++ arch/x86/kernel/cet.c | 141 +++++++++++++++++++++++++ arch/x86/kernel/fpu/signal.c | 67 ++++++++++++ arch/x86/kernel/signal.c | 8 ++ 8 files changed, 250 insertions(+)
arch/x86/ia32/ia32_signal.c +8 -0
@@ -34,6 +34,7 @@#include <asm/sigframe.h> #include <asm/sighandling.h> #include <asm/smap.h> +#include <asm/cet.h> /* * Do a signal return; undo the signal stack.
@@ -235,6 +236,7 @@ static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, ksig->ka.sa.sa_restorer) sp = (unsigned long ) ksig->ka.sa.sa_restorer; + sp =fpu__alloc_sigcontext_ext (sp); sp =fpu__alloc_mathframe (sp,1 , &fx_aligned, &math_size); *fpstate = (struct _fpstate_32 __user *) sp; if (copy_fpstate_to_sigframe(*fpstate, (void __user *)fx_aligned,
@@ -295,6 +297,9 @@ int ia32_setup_frame(int sig, struct ksignal *ksig, restorer = &frame->retcode; } +if (setup_fpu_system_states (1 , (unsigned long )restorer, fpstate)) +return -EFAULT ; + put_user_try {put_user_ex (ptr_to_compat (restorer), &frame->pretcode);
@@ -384,6 +389,9 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig, regs, set->sig[0]); err |=__copy_to_user (&frame->uc.uc_sigmask, set,sizeof (*set)); +if (!err) + err =setup_fpu_system_states (1 , (unsigned long )restorer, fpstate); +if (err)return -EFAULT ;
arch/x86/include/asm/cet.h +7 -0
@@ -6,6 +6,8 @@#include <linux/types.h> struct task_struct ; +struct sc_ext ; +/* * Per-thread CET status */
@@ -19,10 +21,15 @@ struct cet_status {int cet_setup_shstk (void );void cet_disable_shstk (void );void cet_disable_free_shstk (struct task_struct *p); +int cet_restore_signal (bool ia32,struct sc_ext *sc); +int cet_setup_signal (bool ia32,unsigned long rstor,struct sc_ext *sc);#else static inline int cet_setup_shstk (void ) {return -EINVAL ; }static inline void cet_disable_shstk (void ) {}static inline void cet_disable_free_shstk (struct task_struct *p) {} +static inline int cet_restore_signal (bool ia32,struct sc_ext *sc) {return -EINVAL ; } +static inline int cet_setup_signal (bool ia32,unsigned long rstor, +struct sc_ext *sc) {return -EINVAL ; }#endif #define cpu_x86_cet_enabled () \
arch/x86/include/asm/fpu/internal.h +2 -0
@@ -472,6 +472,8 @@ static inline void copy_kernel_to_fpregs(union fpregs_state *fpstate)__copy_kernel_to_fpregs (fpstate,-1 ); } +extern int setup_fpu_system_states (int is_ia32,unsigned long restorer, +void __user *fp);extern int copy_fpstate_to_sigframe (void __user *buf,void __user *fp,int size);/*
arch/x86/include/asm/fpu/signal.h +2 -0
@@ -25,6 +25,8 @@ extern void convert_from_fxsr(struct user_i387_ia32_struct *env,extern void convert_to_fxsr (struct fxregs_state *fxsave,const struct user_i387_ia32_struct *env); +unsigned long fpu__alloc_sigcontext_ext (unsigned long sp); +unsigned long fpu__alloc_mathframe (unsigned long sp,int ia32_frame,unsigned long *buf_fx,unsigned long *size);
arch/x86/include/uapi/asm/sigcontext.h +15 -0
@@ -196,6 +196,21 @@ struct _xstate {/* New processor state extensions go here: */ }; +/* +* Sigcontext extension (struct sc_ext) is located after +* sigcontext->fpstate. Because currently only the shadow +* stack pointer is saved there and the shadow stack depends +* on XSAVES, we can find sc_ext from sigcontext->fpstate. +* +* The 64-bit fpstate has a size of fpu_user_xstate_size, plus +* FP_XSTATE_MAGIC2_SIZE when XSAVE* is used. The struct sc_ext +* is located at the end of sigcontext->fpstate, aligned to 8. +*/ +struct sc_ext { +unsigned long total_size; +unsigned long ssp; +}; +/* * The 32-bit signal frame: */
arch/x86/kernel/cet.c +141 -0
156 lines not on this page · raw message
arch/x86/kernel/fpu/signal.c +67 -0
85 lines not on this page · raw message
arch/x86/kernel/signal.c +8 -0
@@ -46,6 +46,7 @@#include <asm/sigframe.h> #include <asm/signal.h> +#include <asm/cet.h> #define COPY (x) do { \ get_user_ex(regs->x, &sc->x); \
@@ -264,6 +265,7 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, sp = (unsigned long ) ka->sa.sa_restorer; } + sp =fpu__alloc_sigcontext_ext (sp); sp =fpu__alloc_mathframe (sp,IS_ENABLED (CONFIG_X86_32 ), &buf_fx, &math_size); *fpstate = (void __user *)sp;
@@ -493,6 +495,9 @@ static int __setup_rt_frame(int sig, struct ksignal *ksig, err |=setup_sigcontext (&frame->uc.uc_mcontext, fp, regs, set->sig[0 ]); err |=__copy_to_user (&frame->uc.uc_sigmask, set,sizeof (*set)); +if (!err) + err =setup_fpu_system_states (0 , (unsigned long )ksig->ka.sa.sa_restorer, fp); +if (err)return -EFAULT ;
@@ -579,6 +584,9 @@ static int x32_setup_rt_frame(struct ksignal *ksig, regs, set->sig[0]); err |=__copy_to_user (&frame->uc.uc_sigmask, set,sizeof (*set)); +if (!err) + err =setup_fpu_system_states (0 , (unsigned long )ksig->ka.sa.sa_restorer, fpstate); +if (err)return -EFAULT ;
Signature · 1 line
2.17.1
PATCH v7 22/27 binfmt_elf: Extract .note.gnu.property from an ELF file
In reply to Yu-cheng YuAn ELF file's .note.gnu.property indicates features the executable file can support. For example, the property GNU_PROPERTY_X86_FEATURE_1_AND indicates the file supports GNU_PROPERTY_X86_FEATURE_1_IBT and/or GNU_PROPERTY_X86_FEATURE_1_SHSTK.
With this patch, if an arch needs to setup features from ELF properties, it needs CONFIG_ARCH_USE_GNU_PROPERTY to be set, and a specific arch_setup_property().
For example, for X86_64:
int arch_setup_property(void *ehdr, void *phdr, struct file *f, bool inter)
{
int r;
uint32_t property;
r = get_gnu_property(ehdr, phdr, f, GNU_PROPERTY_X86_FEATURE_1_AND,
&property);
...
}--- fs/Kconfig.binfmt | 3 + fs/Makefile | 1 + fs/binfmt_elf.c | 13 ++ fs/gnu_property.c | 351 +++++++++++++++++++++++++++++++++++++++ include/linux/elf.h | 12 ++ include/uapi/linux/elf.h | 14 ++ 6 files changed, 394 insertions(+) create mode 100644 fs/gnu_property.c
fs/Kconfig.binfmt +3 -0
@@ -36,6 +36,9 @@ config COMPAT_BINFMT_ELF config ARCH_BINFMT_ELF_STATE bool +config ARCH_USE_GNU_PROPERTY + bool + config BINFMT_ELF_FDPIC bool "Kernel support for FDPIC ELF binaries" default y if !BINFMT_ELF
fs/Makefile +1 -0
@@ -44,6 +44,7 @@ obj-$(CONFIG_BINFMT_ELF) += binfmt_elf.o obj-$(CONFIG_COMPAT_BINFMT_ELF) += compat_binfmt_elf.o obj-$(CONFIG_BINFMT_ELF_FDPIC) += binfmt_elf_fdpic.o obj-$(CONFIG_BINFMT_FLAT) += binfmt_flat.o +obj-$(CONFIG_ARCH_USE_GNU_PROPERTY) += gnu_property.o obj-$(CONFIG_FS_MBCACHE) += mbcache.o obj-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
fs/binfmt_elf.c +13 -0
@@ -1080,6 +1080,19 @@ static int load_elf_binary(struct linux_binprm *bprm) goto out_free_dentry; } +if (interpreter) { + retval =arch_setup_property (&loc->interp_elf_ex, + interp_elf_phdata, + interpreter, true); + }else { + retval =arch_setup_property (&loc->elf_ex, + elf_phdata, + bprm->file, false); + } + +if (retval <0 ) + goto out_free_dentry; +if (interpreter) {unsigned long interp_map_addr =0 ;
fs/gnu_property.c new file +351 -0
351 lines not on this page · raw message
include/linux/elf.h +12 -0
@@ -56,4 +56,16 @@ static inline int elf_coredump_extra_notes_write(struct coredump_params *cprm) {extern int elf_coredump_extra_notes_size (void );extern int elf_coredump_extra_notes_write (struct coredump_params *cprm);#endif + +#ifdef CONFIG_ARCH_USE_GNU_PROPERTY +extern int arch_setup_property (void *ehdr,void *phdr,struct file *f, +bool interp); +extern int get_gnu_property (void *ehdr_p,void *phdr_p,struct file *f, +u32 pr_type,u32 *feature); +#else +static inline int arch_setup_property (void *ehdr,void *phdr,struct file *f, +bool interp) {return 0 ; } +static inline int get_gnu_property (void *ehdr_p,void *phdr_p,struct file *f, +u32 pr_type,u32 *feature) {return 0 ; } +#endif #endif /* _LINUX_ELF_H */
include/uapi/linux/elf.h +14 -0
@@ -372,6 +372,7 @@ typedef struct elf64_shdr {#define NT_PRFPREG 2#define NT_PRPSINFO 3#define NT_TASKSTRUCT 4 +#define NT_GNU_PROPERTY_TYPE_0 5#define NT_AUXV 6/* * Note to userspace developers: size of NT_SIGINFO note may increase
@@ -443,4 +444,17 @@ typedef struct elf64_note { Elf64_Word n_type; /* Content type */ } Elf64_Nhdr; +/* NT_GNU_PROPERTY_TYPE_0 header */ +struct gnu_property { +__u32 pr_type; +__u32 pr_datasz; +}; + +/* .note.gnu.property types */ +#define GNU_PROPERTY_X86_FEATURE_1_AND (0xc0000002) + +/* Bits of GNU_PROPERTY_X86_FEATURE_1_AND */ +#define GNU_PROPERTY_X86_FEATURE_1_IBT (0x00000001) +#define GNU_PROPERTY_X86_FEATURE_1_SHSTK (0x00000002) +#endif /* _UAPI_LINUX_ELF_H */
Signature · 1 line
2.17.1
Peter Zijlstra
maintainerRe: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 21 lines · Yu-cheng Yu
An ELF file's .note.gnu.property indicates features the executable file can support. For example, the property
GNU_PROPERTY_X86_FEATURE_1_ANDindicates the file supportsGNU_PROPERTY_X86_FEATURE_1_IBTand/orGNU_PROPERTY_X86_FEATURE_1_SHSTK.With this patch, if an arch needs to setup features from ELF properties, it needs
CONFIG_ARCH_USE_GNU_PROPERTYto be set, and a specificarch_setup_property().For example, for
X86_64:int
arch_setup_property(void *ehdr, void *phdr, struct file *f, bool inter){ int r; uint32_t property; r = get_gnu_property(ehdr, phdr, f, GNU_PROPERTY_X86_FEATURE_1_AND, &property); ... }
Quoted 2 lines
Signed-off-by H.J. Lu <[email protected]>Signed-off-by Yu-cheng Yu <[email protected]>
Did HJ write this patch as suggested by that SoB chain? If so, you lost a From: line on top, if not, the SoB thing is invalid.
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 22 lines · Peter Zijlstra
Quoted 21 lines · Yu-cheng Yu
An ELF file's .note.gnu.property indicates features the executable file can support. For example, the property
GNU_PROPERTY_X86_FEATURE_1_ANDindicates the file supportsGNU_PROPERTY_X86_FEATURE_1_IBTand/orGNU_PROPERTY_X86_FEATURE_1_SHSTK.With this patch, if an arch needs to setup features from ELF properties, it needs
CONFIG_ARCH_USE_GNU_PROPERTYto be set, and a specificarch_setup_property().For example, for
X86_64:int
arch_setup_property(void *ehdr, void *phdr, struct file *f, bool inter){ int r; uint32_t property; r = get_gnu_property(ehdr, phdr, f, GNU_PROPERTY_X86_FEATURE_1_AND, &property); ... }
Quoted 5 lines
Quoted 2 lines
Signed-off-by H.J. Lu <[email protected]>Signed-off-by Yu-cheng Yu <[email protected]>Did HJ write this patch as suggested by that SoB chain? If so, you lost a From: line on top, if not, the SoB thing is invalid.
I will fix that.
Yu-cheng
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
In reply to Yu-cheng YuQuoted 12 lines · Yu-cheng Yu
An ELF file's .note.gnu.property indicates features the executable file can support. For example, the property
GNU_PROPERTY_X86_FEATURE_1_ANDindicates the file supportsGNU_PROPERTY_X86_FEATURE_1_IBTand/orGNU_PROPERTY_X86_FEATURE_1_SHSTK.With this patch, if an arch needs to setup features from ELF properties, it needs
CONFIG_ARCH_USE_GNU_PROPERTYto be set, and a specificarch_setup_property().For example, for
X86_64:int
arch_setup_property(void *ehdr, void *phdr, struct file *f, bool inter)
Quoted 8 lines
{
int r;
uint32_t property;
r = get_gnu_property(ehdr, phdr, f, GNU_PROPERTY_X86_FEATURE_1_AND,
&property);
...
}Although this code works for the simple case, I have some concerns about some aspects of the implementation here. There appear to be some bounds checking / buffer overrun issues, and the code seems quite complex.
Maybe this patch tries too hard to be compatible with toolchains that do silly things such as embedding huge notes in an executable, or mixing NT_GNU_PROPERTY_TYPE_0 in a single PT_NOTE with a load of junk not relevant to the loader. I wonder whether Linux can dictate what interpretation(s) of the ELF specs it is prepared to support, rather than trying to support absolutely anything.
I've commented on some potential issues below, but my review isn't exhaustive -- I may also have simply not understood the code in some cases, so I apologise in advance for that!
I've also marked a few coding style nits that make the code harder to read than necessary (but this is partly a matter of taste).
Comments below.
Cheers
---Dave
Quoted 97 lines
Signed-off-by H.J. Lu <[email protected]>Signed-off-by Yu-cheng Yu <[email protected]>--- fs/Kconfig.binfmt | 3 + fs/Makefile | 1 + fs/binfmt_elf.c | 13 ++ fs/gnu_property.c | 351 +++++++++++++++++++++++++++++++++++++++ include/linux/elf.h | 12 ++ include/uapi/linux/elf.h | 14 ++ 6 files changed, 394 insertions(+) create mode 100644 fs/gnu_property.cfs/Kconfig.binfmt +3
-0@@ -36,6 +36,9 @@ config COMPAT_BINFMT_ELF config ARCH_BINFMT_ELF_STATE bool +config ARCH_USE_GNU_PROPERTY + bool + config BINFMT_ELF_FDPIC bool "Kernel support for FDPIC ELF binaries" default y if !BINFMT_ELFfs/Makefile +1
-0@@ -44,6 +44,7 @@ obj-$(CONFIG_BINFMT_ELF) += binfmt_elf.o obj-$(CONFIG_COMPAT_BINFMT_ELF) += compat_binfmt_elf.o obj-$(CONFIG_BINFMT_ELF_FDPIC) += binfmt_elf_fdpic.o obj-$(CONFIG_BINFMT_FLAT) += binfmt_flat.o +obj-$(CONFIG_ARCH_USE_GNU_PROPERTY) += gnu_property.o obj-$(CONFIG_FS_MBCACHE) += mbcache.o obj-$(CONFIG_FS_POSIX_ACL) += posix_acl.ofs/binfmt_elf.c +13
-0@@ -1080,6 +1080,19 @@ static int load_elf_binary(struct linux_binprm *bprm) goto out_free_dentry; } +if (interpreter) { + retval =arch_setup_property (&loc->interp_elf_ex, + interp_elf_phdata, + interpreter, true); + }else { + retval =arch_setup_property (&loc->elf_ex, + elf_phdata, + bprm->file, false); + } + +if (retval <0 ) + goto out_free_dentry; +if (interpreter) {unsigned long interp_map_addr =0 ;fs/gnu_property.c new file +28
-0@@ -0,0 +1,351 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Extract an ELF file's .note.gnu.property. +* +* The path from the ELF header to the note section is the following: +* elfhdr->elf_phdr->elf_note->property[]. +*/ + +#include <uapi/linux/elf-em.h> +#include <linux/processor.h> +#include <linux/binfmts.h> +#include <linux/elf.h> +#include <linux/slab.h> +#include <linux/fs.h> +#include <linux/uaccess.h> +#include <linux/string.h> +#include <linux/compat.h> + +/* +* The .note.gnu.property layout: +* +* struct elf_note { +* u32 n_namesz; --> sizeof(n_name[]); always (4) +* u32 n_ndescsz;--> sizeof(property[]) +* u32 n_type; --> always NT_GNU_PROPERTY_TYPE_0 +* }; +* char n_name[4]; --> always 'GNU\0' +*
fs/gnu_property.c line 38
Quoted 10 lines
fs/gnu_property.c new file +10
-0+* struct { +* struct gnu_property { +* u32 pr_type; +* u32 pr_datasz; +* }; +* u8 pr_data[pr_datasz]; +* }[]; +*/ + +#define BUF_SIZE (PAGE_SIZE / 4)
Nit: magic number in disguise. What does the size of ELF notes have to do with the page size?
Quoted 9 lines
fs/gnu_property.c new file +9
-0+ +typedef bool (test_item_fn )(void *buf,u32 *arg,u32 type); +typedef void *(next_item_fn )(void *buf,u32 *arg,u32 type); + +static inline bool test_note_type (void *buf,u32 *align,u32 note_type) +{ +struct elf_note *n = buf; + + return ((n->n_type == note_type) && (n->n_namesz == 4) &&
fs/gnu_property.c line 57
Quoted 10 lines
fs/gnu_property.c new file +10
-0+ (memcmp(n + 1, "GNU", 4) == 0)); +} + +static inline void *next_note (void *buf,u32 *align,u32 note_type) +{ +struct elf_note *n = buf; +u64 size; + +if (check_add_overflow ((u64 )sizeof (*n), (u64 )n->n_namesz, &size)) +return NULL ;
sizeof(*n) is a small integer under our control, and n->n_namesz is a u32.
So, I'm not sure how we would overflow 64 bits here, although if we can get arbitrarily close to ~(u64)0 then:
fs/gnu_property.c line 59
Quoted 2 lines
fs/gnu_property.c new file +2
-0+ + size =round_up (size, *align);
this can overflow too.
fs/gnu_property.c line 64
Quoted 5 lines
fs/gnu_property.c new file +5
-0+ +if (check_add_overflow (size, (u64 )n->n_descsz, &size)) +return NULL ; + + size =round_up (size, *align);
Similarly here.
fs/gnu_property.c line 66
Quoted 2 lines
fs/gnu_property.c new file +2
-0+ +if (buf + size < buf)
Isn't this undefined behaviour of it overflows? If so, the compiler can probably delete the check entirely, making it useless. Does UBSAN warn about it?
fs/gnu_property.c line 69
Quoted 3 lines
fs/gnu_property.c new file +3
-0+return NULL ; + else + return (buf + size);
Nit: Unnecessary () (There are surplus () all over this patch; I won't comment on them all.)
fs/gnu_property.c line 81
Quoted 12 lines
fs/gnu_property.c new file +12
-0+} + +static inline bool test_property (void *buf,u32 *max_type,u32 pr_type) +{ +struct gnu_property *pr = buf; + +/* +* Property types must be in ascending order. +* Keep track of the max when testing each. +*/ +if (pr->pr_type > *max_type) + *max_type = pr->pr_type;
Is this worthwhile? In general we don't try very hard to check that the ELF file is well-formed.
Ideally we could search by binary chop, but the property size is variable, so the sortedness is useless to us (yay).
fs/gnu_property.c line 86
Quoted 5 lines
fs/gnu_property.c new file +5
-0+ +return (pr->pr_type == pr_type); +} + +static inline void *next_property (void *buf,u32 *max_type,u32 pr_type)
Nit: does this need to be inline? The compiler's guess is usually good enough...
fs/gnu_property.c line 90
Quoted 4 lines
fs/gnu_property.c new file +4
-0+{ +struct gnu_property *pr = buf; + + if ((buf + sizeof(*pr) + pr->pr_datasz < buf) ||
Nit: random extra space, redundant (), etc.
fs/gnu_property.c line 95
Quoted 5 lines
fs/gnu_property.c new file +5
-0+ (pr->pr_type > pr_type) || + (pr->pr_type > *max_type)) +return NULL ; + else + return (buf + sizeof(*pr) + pr->pr_datasz);
We can exceed the underlying buffer bounds here, which is technically undefined behaviour.
I suspect we may be relying on similar tricks all over the kernel, but IT MAy be best avoided anyway.
If we always pass in the buffer base pointer and the size of the buffer, say
static int next_property(void *buf, size_t *offset, size_t bufsz, ...)
then we may be able to use direct comparisons that can't overflow rather than relying on potentially undefined behaviour. For example:
size_t o = *offset; if (o > bufsz || sizeof (*pr) > bufsz - o) return -1; pr = buf + o; if (pr->pr_type > pr_type || pr->pr_type > *max_type) return -1; if (pr->pr_datasz > bufsz - o - sizeof (*pr)) return -1; *offset = o + sizeof (*pr) + pr->pr_datasz; return 0;
(There may be neater ways to do this.)
Quoted 5 lines
fs/gnu_property.c new file +5
-0+} + +/* +* Scan 'buf' for a pattern; return true if found. +* *pos is the distance from the beginning of buf to where
fs/gnu_property.c line 110
Quoted 10 lines
fs/gnu_property.c new file +10
-0+* the searched item or the next item is located. +*/ +static int scan (u8 *buf,u32 buf_size,int item_size,test_item_fn test_item, +next_item_fn next_item,u32 *arg,u32 type,u32 *pos) +{ +int found =0 ; +u8 *p, *max; + + max = buf + buf_size; +if (max < buf)
See comment about undefined behaviour above.
Also, I'm not sure this check adds anything. We know buf_size is <= BUF_SIZE (though we could stick a WARN_ON() here and bail out if we want to make absolutely sure).
If buf is always the base pointer returned by kmalloc(BUF_SIZE), then I think buf_size can never go outside its bounds?
fs/gnu_property.c line 115
Quoted 5 lines
fs/gnu_property.c new file +5
-0+return 0 ; + + p = buf; + +while ((p + item_size < max) && (p + item_size > buf)) {
^ <= ? ^ undefined behaviour?
fs/gnu_property.c line 124
Quoted 9 lines
fs/gnu_property.c new file +9
-0+if (test_item (p, arg, type)) { + found =1 ; +break ; + } + + p =next_item (p, arg, type); + } + + *pos = (p + item_size <= buf) ?0 : (u32)(p - buf);
Can this be written more simply, say:
if (p + item_size > buf) *pos += p - buf;
Also, since next_property() adds pr_datasz onto buf, could we get unlucky and wrap past (void *)~0UL? Then (u32)(p - buf) may be giant. Not sure whether this breaks code elsewhere.
Quoted 70 lines
fs/gnu_property.c new file +70
-0+return found; +} + +/* +* Search an NT_GNU_PROPERTY_TYPE_0 for the property of 'pr_type'. +*/ +static int find_property (struct file *file,unsigned long desc_size, +loff_t file_offset,u8 *buf, +u32 pr_type,u32 *property) +{ +u32 buf_pos; +unsigned long read_size; +unsigned long done; +int found =0 ; +int ret =0 ; +u32 last_pr =0 ; + + *property =0 ; + buf_pos =0 ; + +for (done =0 ; done < desc_size; done += buf_pos) { + read_size = desc_size - done; +if (read_size >BUF_SIZE ) + read_size =BUF_SIZE ; + + ret =kernel_read (file, buf, read_size, &file_offset); + +if (ret != read_size) +return (ret <0 ) ? ret : -EIO ; + + ret =0 ; + found =scan (buf, read_size,sizeof (struct gnu_property ), + test_property, next_property, + &last_pr, pr_type, &buf_pos); + +if ((!buf_pos) || found) +break ; + + file_offset += buf_pos - read_size; + } + +if (found) { +struct gnu_property *pr = + (struct gnu_property *)(buf + buf_pos); + +if (pr->pr_datasz ==4 ) { +u32 *max = (u32 *)(buf + read_size); +u32 *data = (u32 *)((u8 *)pr +sizeof (*pr)); + +if (data +1 <= max) { + *property = *data; + }else { + file_offset += buf_pos - read_size; + file_offset +=sizeof (*pr); + ret =kernel_read (file, property,4 , + &file_offset); + } + } + } + +return ret; +} + +/* +* Search a PT_NOTE segment for NT_GNU_PROPERTY_TYPE_0. +*/ +static int find_note_type_0 (struct file *file,loff_t file_offset, +unsigned long note_size,u32 align, +u32 pr_type,u32 *property) +{
fs/gnu_property.c line 204
Quoted 10 lines
fs/gnu_property.c new file +10
-0+u8 *buf; +u32 buf_pos; +unsigned long read_size; +unsigned long done; +int found =0 ; +int ret =0 ; + + buf =kmalloc (BUF_SIZE ,GFP_KERNEL ); +if (!buf) +return -ENOMEM ;
Do we really need to alloc/free this once per note?
Quoted 41 lines
fs/gnu_property.c new file +41
-0+ + *property =0 ; + buf_pos =0 ; + +for (done =0 ; done < note_size; done += buf_pos) { + read_size = note_size - done; +if (read_size >BUF_SIZE ) + read_size =BUF_SIZE ; + + ret =kernel_read (file, buf, read_size, &file_offset); + +if (ret != read_size) { + ret = (ret <0 ) ? ret : -EIO ; +kfree (buf); +return ret; + } + +/* +* item_size = sizeof(struct elf_note) + elf_note.n_namesz. +* n_namesz is 4 for the note type we look for. +*/ + ret =scan (buf, read_size,sizeof (struct elf_note ) +4 , + test_note_type, next_note, + &align,NT_GNU_PROPERTY_TYPE_0 , &buf_pos); + + file_offset += buf_pos - read_size; + +if (ret && !found) { +struct elf_note *n = + (struct elf_note *)(buf + buf_pos); +u64 start =round_up (sizeof (*n) + n->n_namesz, align); +u64 total =0 ; + +if (check_add_overflow (start, (u64 )n->n_descsz, &total)) { + ret = -EINVAL ; +break ; + } + total =round_up (total, align); + + ret = find_property(file, n->n_descsz, + file_offset + start,
fs/gnu_property.c line 255
Quoted 10 lines
fs/gnu_property.c new file +10
-0+ buf, pr_type, property); + found++; + file_offset += total; + buf_pos += total; + }else if (!buf_pos || ret) { + ret =0 ; + *property =0 ; +break ; + } + }
Do we really need this complexity? How big are the notes realistically going to be?
Since a file with bloated notes is going to be inefficient to exec anyway if we have to scan all the way through them, would it be better just to choke on it and force the toolchain to do something more sensible?
This in one reason why it would be good for the kernel to require PT_GNU_PROPERTY if possible, so we know the precise offset and size without having to search...
Quoted 122 lines
fs/gnu_property.c new file +96
-0+ +kfree (buf); +return ret; +} + +/* +* Look at an ELF file's PT_NOTE segments, then NT_GNU_PROPERTY_TYPE_0, then +* the property of pr_type. +* +* Input: +* file: the file to search; +* phdr: the file's elf header; +* phnum: number of entries in phdr; +* pr_type: the property type. +* +* Output: +* The property found. +* +* Return: +* Zero or error. +*/ +static int scan_segments_64 (struct file *file,struct elf64_phdr *phdr, +int phnum,u32 pr_type,u32 *property) +{ +int i; +int err =0 ; + +for (i =0 ; i < phnum; i++, phdr++) { +if ((phdr->p_type !=PT_NOTE ) || (phdr->p_align !=8 )) +continue ; + +/* +* Search the PT_NOTE segment for NT_GNU_PROPERTY_TYPE_0. +*/ + err =find_note_type_0 (file, phdr->p_offset, phdr->p_filesz, + phdr->p_align, pr_type, property); +if (err) +return err; + } + +return 0 ; +} + +static int scan_segments_32 (struct file *file,struct elf32_phdr *phdr, +int phnum,u32 pr_type,u32 *property) +{ +int i; +int err =0 ; + +for (i =0 ; i < phnum; i++, phdr++) { +if ((phdr->p_type !=PT_NOTE ) || (phdr->p_align !=4 )) +continue ; + +/* +* Search the PT_NOTE segment for NT_GNU_PROPERTY_TYPE_0. +*/ + err =find_note_type_0 (file, phdr->p_offset, phdr->p_filesz, + phdr->p_align, pr_type, property); +if (err) +return err; + } + +return 0 ; +} + +int get_gnu_property (void *ehdr_p,void *phdr_p,struct file *f, +u32 pr_type,u32 *property) +{ +struct elf64_hdr *ehdr64 = ehdr_p; +int err =0 ; + + *property =0 ; + +if (ehdr64->e_ident[EI_CLASS ] ==ELFCLASS64 ) { +struct elf64_phdr *phdr64 = phdr_p; + + err =scan_segments_64 (f, phdr64, ehdr64->e_phnum, + pr_type, property); +if (err <0 ) + goto out; + }else { +struct elf32_hdr *ehdr32 = ehdr_p; + +if (ehdr32->e_ident[EI_CLASS ] ==ELFCLASS32 ) { +struct elf32_phdr *phdr32 = phdr_p; + + err =scan_segments_32 (f, phdr32, ehdr32->e_phnum, + pr_type, property); +if (err <0 ) + goto out; + } + } + +out: +return err; +}include/linux/elf.h +12
-0@@ -56,4 +56,16 @@ static inline int elf_coredump_extra_notes_write(struct coredump_params *cprm) {extern int elf_coredump_extra_notes_size (void );extern int elf_coredump_extra_notes_write (struct coredump_params *cprm);#endif + +#ifdef CONFIG_ARCH_USE_GNU_PROPERTY +extern int arch_setup_property (void *ehdr,void *phdr,struct file *f, +bool interp); +extern int get_gnu_property (void *ehdr_p,void *phdr_p,struct file *f, +u32 pr_type,u32 *feature); +#else +static inline int arch_setup_property (void *ehdr,void *phdr,struct file *f, +bool interp) {return 0 ; } +static inline int get_gnu_property (void *ehdr_p,void *phdr_p,struct file *f, +u32 pr_type,u32 *feature) {return 0 ; } +#endif #endif /* _LINUX_ELF_H */ include/uapi/linux/elf.h +0
-0
include/uapi/linux/elf.h typedef struct elf64_shdr { · line 375
Quoted 4 lines
include/uapi/linux/elf.h +1
-0@@ -372,6 +372,7 @@ typedef struct elf64_shdr {#define NT_PRFPREG 2#define NT_PRPSINFO 3#define NT_TASKSTRUCT 4 +#define NT_GNU_PROPERTY_TYPE_0 5
Should this be in a separate block. This required n_name = "GNU", whereas the rest are "LINUX" notes AFAIK: it's really a separate namespace.
I think the gap between 4 and 6 may be just coincidence: glibc's elf.h already has NT_PLATFORM here (whatever that is).
Quoted 10 lines
include/uapi/linux/elf.h +3
-0#define NT_AUXV 6/* * Note to userspace developers: size of NT_SIGINFO note may increase @@ -443,4 +444,17 @@ typedef struct elf64_note {Elf64_Word n_type;/* Content type */ } Elf64_Nhdr; +/* NT_GNU_PROPERTY_TYPE_0 header */ +struct gnu_property { +__u32 pr_type;
include/uapi/linux/elf.h typedef struct elf64_note { · line 459
Quoted 10 lines
include/uapi/linux/elf.h +10
-0+__u32 pr_datasz; +}; + +/* .note.gnu.property types */ +#define GNU_PROPERTY_X86_FEATURE_1_AND (0xc0000002) + +/* Bits of GNU_PROPERTY_X86_FEATURE_1_AND */ +#define GNU_PROPERTY_X86_FEATURE_1_IBT (0x00000001) +#define GNU_PROPERTY_X86_FEATURE_1_SHSTK (0x00000002) +
Redundant (). The rest of the file doesn't have them; can we conform to the prevailing style there?
Quoted 4 lines
include/uapi/linux/elf.h +0
-0#endif /* _UAPI_LINUX_ELF_H */ Signature · 1 line
2.17.1
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 26 lines · Dave Martin
Quoted 12 lines · Yu-cheng Yu
An ELF file's .note.gnu.property indicates features the executable file can support. For example, the property
GNU_PROPERTY_X86_FEATURE_1_ANDindicates the file supportsGNU_PROPERTY_X86_FEATURE_1_IBTand/orGNU_PROPERTY_X86_FEATURE_1_SHSTK.With this patch, if an arch needs to setup features from ELF properties, it needs
CONFIG_ARCH_USE_GNU_PROPERTYto be set, and a specificarch_setup_property().For example, for
X86_64:int
arch_setup_property(void *ehdr, void *phdr, struct file *f, bool inter)Quoted 8 lines
{ int r; uint32_t property; r = get_gnu_property(ehdr, phdr, f, GNU_PROPERTY_X86_FEATURE_1_AND, &property); ... }Although this code works for the simple case, I have some concerns about some aspects of the implementation here. There appear to be some bounds checking / buffer overrun issues, and the code seems quite complex.
Quoted 6 lines
Maybe this patch tries too hard to be compatible with toolchains that do silly things such as embedding huge notes in an executable, or mixing
NT_GNU_PROPERTY_TYPE_0in a singlePT_NOTEwith a load of junk not relevant to the loader. I wonder whether Linux can dictate what interpretation(s) of the ELF specs it is prepared to support, rather than trying to support absolutely anything.
To me, looking at PT_GNU_PROPERTY and not trying to support anything is a logical choice. And it breaks only a limited set of toolchains.
I will simplify the parser and leave this patch as-is for anyone who wants to back-port. Are there any objections or concerns?
Yu-cheng
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 34 lines · Yu-cheng Yu
Quoted 26 lines · Dave Martin
Quoted 12 lines · Yu-cheng Yu
An ELF file's .note.gnu.property indicates features the executable file can support. For example, the property
GNU_PROPERTY_X86_FEATURE_1_ANDindicates the file supportsGNU_PROPERTY_X86_FEATURE_1_IBTand/orGNU_PROPERTY_X86_FEATURE_1_SHSTK.With this patch, if an arch needs to setup features from ELF properties, it needs
CONFIG_ARCH_USE_GNU_PROPERTYto be set, and a specificarch_setup_property().For example, for
X86_64:int
arch_setup_property(void *ehdr, void *phdr, struct file *f, bool inter)Quoted 8 lines
{ int r; uint32_t property; r = get_gnu_property(ehdr, phdr, f, GNU_PROPERTY_X86_FEATURE_1_AND, &property); ... }Although this code works for the simple case, I have some concerns about some aspects of the implementation here. There appear to be some bounds checking / buffer overrun issues, and the code seems quite complex.
Quoted 6 lines
Maybe this patch tries too hard to be compatible with toolchains that do silly things such as embedding huge notes in an executable, or mixing
NT_GNU_PROPERTY_TYPE_0in a singlePT_NOTEwith a load of junk not relevant to the loader. I wonder whether Linux can dictate what interpretation(s) of the ELF specs it is prepared to support, rather than trying to support absolutely anything.
Quoted 5 lines
To me, looking at
PT_GNU_PROPERTYand not trying to support anything is a logical choice. And it breaks only a limited set of toolchains.I will simplify the parser and leave this patch as-is for anyone who wants to back-port. Are there any objections or concerns?
No objection from me ;) But I'm biased.
Hopefully this change should allow substantial simplification. For one thing, PT_GNU_PROPERTY tells its file offset and size directly in its phdrs entry. That should save us a lot of effort on the kernel side.
Cheers
---Dave
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
In reply to Yu-cheng Yu* Yu-cheng Yu:
Quoted 5 lines
To me, looking at
PT_GNU_PROPERTYand not trying to support anything is a logical choice. And it breaks only a limited set of toolchains.I will simplify the parser and leave this patch as-is for anyone who wants to back-port. Are there any objections or concerns?
Red Hat Enterprise Linux 8 does not use PT_GNU_PROPERTY and is probably the largest collection of CET-enabled binaries that exists today.
My hope was that we would backport the upstream kernel patches for CET, port the glibc dynamic loader to the new kernel interface, and be ready to run with CET enabled in principle (except that porting userspace libraries such as OpenSSL has not really started upstream, so many processes where CET is particularly desirable will still run without it).
I'm not sure if it is a good idea to port the legacy support if it's not part of the mainline kernel because it comes awfully close to creating our own private ABI.
Thanks,
Florian
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 10 lines · Florian Weimer
* Yu-cheng Yu:
Quoted 5 lines
To me, looking at
PT_GNU_PROPERTYand not trying to support anything is a logical choice. And it breaks only a limited set of toolchains.I will simplify the parser and leave this patch as-is for anyone who wants to back-port. Are there any objections or concerns?
Red Hat Enterprise Linux 8 does not use
PT_GNU_PROPERTYand is probably the largest collection of CET-enabled binaries that exists today.
For clarity, RHEL is actively parsing these properties today?
Quoted 10 lines
My hope was that we would backport the upstream kernel patches for CET, port the glibc dynamic loader to the new kernel interface, and be ready to run with CET enabled in principle (except that porting userspace libraries such as OpenSSL has not really started upstream, so many processes where CET is particularly desirable will still run without it).
I'm not sure if it is a good idea to port the legacy support if it's not part of the mainline kernel because it comes awfully close to creating our own private ABI.
I guess we can aim to factor things so that PT_NOTE scanning is available as a fallback on arches for which the absence of PT_GNU_PROPERTY is not authoritative.
Can we argue that the lack of PT_GNU_PROPERTY is an ABI bug, fix it for new binaries and hence limit the efforts we go to to support theoretical binaries that lack the phdrs entry?
If we can make practical simplifications to the parsing, such as limiting the maximum PT_NOTE size that we will search for the program properties to 1K (say), or requiring NT_NOTE_GNU_PROPERTY_TYPE_0 to sit by itself in a single PT_NOTE then that could help minimse the exec overheads and the number of places for bugs to hide in the kernel.
What we can do here depends on what the tools currently do and what binaries are out there.
Cheers
---Dave
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 22 lines · Dave Martin
Quoted 11 lines · Florian Weimer
* Yu-cheng Yu:
Quoted 6 lines
To me, looking at
PT_GNU_PROPERTYand not trying to support anything is a logical choice. And it breaks only a limited set of toolchains.I will simplify the parser and leave this patch as-is for anyone who wants to
back-port. Are there any objections or concerns?Red Hat Enterprise Linux 8 does not use
PT_GNU_PROPERTYand is probably the largest collection of CET-enabled binaries that exists today.For clarity, RHEL is actively parsing these properties today?
Quoted 7 lines
My hope was that we would backport the upstream kernel patches for CET, port the glibc dynamic loader to the new kernel interface, and be ready to run with CET enabled in principle (except that porting userspace libraries such as OpenSSL has not really started upstream, so many processes where CET is particularly desirable will still run without it).
Quoted 7 lines
Quoted 3 lines
I'm not sure if it is a good idea to port the legacy support if it's not part of the mainline kernel because it comes awfully close to creating our own private ABI.
I guess we can aim to factor things so that
PT_NOTEscanning is available as a fallback on arches for which the absence ofPT_GNU_PROPERTYis not authoritative.
We can probably check PT_GNU_PROPERTY first, and fallback (based on ld-linux version?) to PT_NOTE scanning?
Yu-cheng
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 27 lines · Yu-cheng Yu
Quoted 26 lines · Dave Martin
Quoted 11 lines · Florian Weimer
* Yu-cheng Yu:
Quoted 6 lines
To me, looking at
PT_GNU_PROPERTYand not trying to support anything is a logical choice. And it breaks only a limited set of toolchains.I will simplify the parser and leave this patch as-is for anyone who wants to
back-port. Are there any objections or concerns?Red Hat Enterprise Linux 8 does not use
PT_GNU_PROPERTYand is probably the largest collection of CET-enabled binaries that exists today.For clarity, RHEL is actively parsing these properties today?
Quoted 10 lines
My hope was that we would backport the upstream kernel patches for CET, port the glibc dynamic loader to the new kernel interface, and be ready to run with CET enabled in principle (except that porting userspace libraries such as OpenSSL has not really started upstream, so many processes where CET is particularly desirable will still run without it).
I'm not sure if it is a good idea to port the legacy support if it's not part of the mainline kernel because it comes awfully close to creating our own private ABI.
Quoted 6 lines
Quoted 3 lines
I guess we can aim to factor things so that
PT_NOTEscanning is available as a fallback on arches for which the absence ofPT_GNU_PROPERTYis not authoritative.We can probably check
PT_GNU_PROPERTYfirst, and fallback (based on ld-linux version?) toPT_NOTEscanning?
For arm64, we can check for PT_GNU_PROPERTY and then give up unconditionally.
For x86, we would fall back to PT_NOTE scanning, but this will add a bit of cost to binaries that don't have NT_GNU_PROPERTY_TYPE_0. The ld.so version doesn't tell you what ELF ABI a given executable conforms to.
Since this sounds like it's largely a distro-specific issue, maybe there could be a Kconfig option to turn the fallback PT_NOTE scanning on?
Cheers
---Dave
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 40 lines · Dave Martin
Quoted 29 lines · Yu-cheng Yu
Quoted 28 lines · Dave Martin
Quoted 6 lines · Florian Weimer
* Yu-cheng Yu:
Quoted 4 lines
To me, looking at
PT_GNU_PROPERTYand not trying to support anything is a
logical choice. And it breaks only a limited set of toolchains.Quoted 7 lines
Quoted 4 lines
I will simplify the parser and leave this patch as-is for anyone who wants
to
back-port. Are there any objections or concerns?Red Hat Enterprise Linux 8 does not use
PT_GNU_PROPERTYand is probably the largest collection of CET-enabled binaries that exists today.For clarity, RHEL is actively parsing these properties today?
Quoted 10 lines
My hope was that we would backport the upstream kernel patches for CET, port the glibc dynamic loader to the new kernel interface, and be ready to run with CET enabled in principle (except that porting userspace libraries such as OpenSSL has not really started upstream, so many processes where CET is particularly desirable will still run without it).
I'm not sure if it is a good idea to port the legacy support if it's not part of the mainline kernel because it comes awfully close to creating our own private ABI.
Quoted 6 lines
Quoted 3 lines
I guess we can aim to factor things so that
PT_NOTEscanning is available as a fallback on arches for which the absence ofPT_GNU_PROPERTYis not authoritative.We can probably check
PT_GNU_PROPERTYfirst, and fallback (based on ld-linux version?) toPT_NOTEscanning?For arm64, we can check for
PT_GNU_PROPERTYand then give up unconditionally.
Quoted 6 lines
For x86, we would fall back to
PT_NOTEscanning, but this will add a bit of cost to binaries that don't haveNT_GNU_PROPERTY_TYPE_0. The ld.so version doesn't tell you what ELF ABI a given executable conforms to.Since this sounds like it's largely a distro-specific issue, maybe there could be a
Kconfigoption to turn the fallbackPT_NOTEscanning on?
Yes, I will make it a Kconfig option.
Yu-cheng
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 41 lines · Yu-cheng Yu
Quoted 40 lines · Dave Martin
Quoted 29 lines · Yu-cheng Yu
Quoted 28 lines · Dave Martin
Quoted 6 lines · Florian Weimer
* Yu-cheng Yu:
Quoted 4 lines
To me, looking at
PT_GNU_PROPERTYand not trying to support anything is a
logical choice. And it breaks only a limited set of toolchains.Quoted 7 lines
Quoted 4 lines
I will simplify the parser and leave this patch as-is for anyone who wants
to
back-port. Are there any objections or concerns?Red Hat Enterprise Linux 8 does not use
PT_GNU_PROPERTYand is probably the largest collection of CET-enabled binaries that exists today.For clarity, RHEL is actively parsing these properties today?
Quoted 10 lines
My hope was that we would backport the upstream kernel patches for CET, port the glibc dynamic loader to the new kernel interface, and be ready to run with CET enabled in principle (except that porting userspace libraries such as OpenSSL has not really started upstream, so many processes where CET is particularly desirable will still run without it).
I'm not sure if it is a good idea to port the legacy support if it's not part of the mainline kernel because it comes awfully close to creating our own private ABI.
Quoted 6 lines
Quoted 3 lines
I guess we can aim to factor things so that
PT_NOTEscanning is available as a fallback on arches for which the absence ofPT_GNU_PROPERTYis not authoritative.We can probably check
PT_GNU_PROPERTYfirst, and fallback (based on ld-linux version?) toPT_NOTEscanning?For arm64, we can check for
PT_GNU_PROPERTYand then give up unconditionally.
Quoted 8 lines
Quoted 6 lines
For x86, we would fall back to
PT_NOTEscanning, but this will add a bit of cost to binaries that don't haveNT_GNU_PROPERTY_TYPE_0. The ld.so version doesn't tell you what ELF ABI a given executable conforms to.Since this sounds like it's largely a distro-specific issue, maybe there could be a
Kconfigoption to turn the fallbackPT_NOTEscanning on?Yes, I will make it a
Kconfigoption.
OK, that works for me. This would also help keep the PT_NOTE scanning separate from the rest of the code.
For arm64 we could then unconditionally select/deselect that option, where x86 could leave it configurable either way.
Cheers
---Dave
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
In reply to Dave Martin* Dave Martin:
Quoted 38 lines · Yu-cheng Yu
Quoted 27 lines · Dave Martin
Quoted 26 lines · Florian Weimer
Quoted 11 lines
* Yu-cheng Yu:
Quoted 6 lines
To me, looking at
PT_GNU_PROPERTYand not trying to support anything is a logical choice. And it breaks only a limited set of toolchains.I will simplify the parser and leave this patch as-is for anyone who wants to
back-port. Are there any objections or concerns?Red Hat Enterprise Linux 8 does not use
PT_GNU_PROPERTYand is probably the largest collection of CET-enabled binaries that exists today.For clarity, RHEL is actively parsing these properties today?
Quoted 10 lines
My hope was that we would backport the upstream kernel patches for CET, port the glibc dynamic loader to the new kernel interface, and be ready to run with CET enabled in principle (except that porting userspace libraries such as OpenSSL has not really started upstream, so many processes where CET is particularly desirable will still run without it).
I'm not sure if it is a good idea to port the legacy support if it's not part of the mainline kernel because it comes awfully close to creating our own private ABI.
Quoted 6 lines
Quoted 3 lines
I guess we can aim to factor things so that
PT_NOTEscanning is available as a fallback on arches for which the absence ofPT_GNU_PROPERTYis not authoritative.We can probably check
PT_GNU_PROPERTYfirst, and fallback (based on ld-linux version?) toPT_NOTEscanning?For arm64, we can check for
PT_GNU_PROPERTYand then give up unconditionally.
Quoted 6 lines
For x86, we would fall back to
PT_NOTEscanning, but this will add a bit of cost to binaries that don't haveNT_GNU_PROPERTY_TYPE_0. The ld.so version doesn't tell you what ELF ABI a given executable conforms to.Since this sounds like it's largely a distro-specific issue, maybe there could be a
Kconfigoption to turn the fallbackPT_NOTEscanning on?
I'm worried that this causes interop issues similarly to what we see with VSYSCALL today. If we need both and a way to disable it, it should be something like a personality flag which can be configured for each process tree separately. Ideally, we'd settle on one correct approach (i.e., either always process both, or only process PT_GNU_PROPERTY) and enforce that.
Thanks,
Florian
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 15 lines · Florian Weimer
* Dave Martin:
Quoted 7 lines · Yu-cheng Yu
Quoted 2 lines
We can probably check
PT_GNU_PROPERTYfirst, and fallback (based on ld-linux version?) toPT_NOTEscanning?For arm64, we can check for
PT_GNU_PROPERTYand then give up unconditionally.Quoted 6 lines
For x86, we would fall back to
PT_NOTEscanning, but this will add a bit of cost to binaries that don't haveNT_GNU_PROPERTY_TYPE_0. The ld.so version doesn't tell you what ELF ABI a given executable conforms to.Since this sounds like it's largely a distro-specific issue, maybe there could be a
Kconfigoption to turn the fallbackPT_NOTEscanning on?
Quoted 6 lines
I'm worried that this causes interop issues similarly to what we see with VSYSCALL today. If we need both and a way to disable it, it should be something like a personality flag which can be configured for each process tree separately. Ideally, we'd settle on one correct approach (i.e., either always process both, or only process
PT_GNU_PROPERTY) and enforce that.
Chose one and only the one which makes technically sense and is not some horrible vehicle.
Everytime we did those 'oh we need to make x fly workarounds' we regretted it sooner than later.
Thanks,
tglx
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 23 lines · Thomas Gleixner
Quoted 15 lines · Florian Weimer
* Dave Martin:
Quoted 7 lines · Yu-cheng Yu
Quoted 2 lines
We can probably check
PT_GNU_PROPERTYfirst, and fallback (based on ld-linux version?) toPT_NOTEscanning?For arm64, we can check for
PT_GNU_PROPERTYand then give up unconditionally.Quoted 6 lines
For x86, we would fall back to
PT_NOTEscanning, but this will add a bit of cost to binaries that don't haveNT_GNU_PROPERTY_TYPE_0. The ld.so version doesn't tell you what ELF ABI a given executable conforms to.Since this sounds like it's largely a distro-specific issue, maybe there could be a
Kconfigoption to turn the fallbackPT_NOTEscanning on?Quoted 6 lines
I'm worried that this causes interop issues similarly to what we see with VSYSCALL today. If we need both and a way to disable it, it should be something like a personality flag which can be configured for each process tree separately. Ideally, we'd settle on one correct approach (i.e., either always process both, or only process
PT_GNU_PROPERTY) and enforce that.
Quoted 5 lines
Chose one and only the one which makes technically sense and is not some horrible vehicle.
Everytime we did those 'oh we need to make x fly workarounds' we regretted it sooner than later.
So I guess that points to keeping PT_NOTE scanning always available as a fallback on x86. This sucks a bit, but if there are binaries already in the wild that rely on this, I don't think we have much choice...
I'd still favour a Kconfig option to allow this support to be suppressed by arches that don't have a similar legacy to be compatible with.
Cheers
---Dave
Peter Zijlstra
maintainerRe: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 27 lines · Dave Martin
Quoted 26 lines · Thomas Gleixner
Quoted 15 lines · Florian Weimer
* Dave Martin:
Quoted 7 lines · Yu-cheng Yu
Quoted 2 lines
We can probably check
PT_GNU_PROPERTYfirst, and fallback (based on ld-linux version?) toPT_NOTEscanning?For arm64, we can check for
PT_GNU_PROPERTYand then give up unconditionally.Quoted 6 lines
For x86, we would fall back to
PT_NOTEscanning, but this will add a bit of cost to binaries that don't haveNT_GNU_PROPERTY_TYPE_0. The ld.so version doesn't tell you what ELF ABI a given executable conforms to.Since this sounds like it's largely a distro-specific issue, maybe there could be a
Kconfigoption to turn the fallbackPT_NOTEscanning on?Quoted 6 lines
I'm worried that this causes interop issues similarly to what we see with VSYSCALL today. If we need both and a way to disable it, it should be something like a personality flag which can be configured for each process tree separately. Ideally, we'd settle on one correct approach (i.e., either always process both, or only process
PT_GNU_PROPERTY) and enforce that.Chose one and only the one which makes technically sense and is not some horrible vehicle.
Quoted 6 lines
Quoted 2 lines
Everytime we did those 'oh we need to make x fly workarounds' we regretted it sooner than later.
So I guess that points to keeping
PT_NOTEscanning always available as a fallback on x86. This sucks a bit, but if there are binaries already in the wild that rely on this, I don't think we have much choice...
I'm not sure I read Thomas' comment like that. In my reading keeping the PT_NOTE fallback is exactly one of those 'fly workarounds'. By not supporting PT_NOTE only the 'fine' people already shit^Hpping this out of tree are affected, and we don't have to care about them at all.
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
* Peter Zijlstra:
Quoted 4 lines
I'm not sure I read Thomas' comment like that. In my reading keeping the
PT_NOTEfallback is exactly one of those 'fly workarounds'. By not supportingPT_NOTEonly the 'fine' people already shit^Hpping this out of tree are affected, and we don't have to care about them at all.
Just to be clear here: There was an ABI document that required PT_NOTE parsing. The Linux kernel does *not* define the x86-64 ABI, it only implements it. The authoritative source should be the ABI document.
In this particularly case, so far anyone implementing this ABI extension tried to provide value by changing it, sometimes successfully. Which makes me wonder why we even bother to mainatain ABI documentation. The kernel is just very late to the party.
Thanks,
Florian
Peter Zijlstra
maintainerRe: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 9 lines · Florian Weimer
* Peter Zijlstra:
Quoted 4 lines
I'm not sure I read Thomas' comment like that. In my reading keeping the
PT_NOTEfallback is exactly one of those 'fly workarounds'. By not supportingPT_NOTEonly the 'fine' people already shit^Hpping this out of tree are affected, and we don't have to care about them at all.Just to be clear here: There was an ABI document that required
PT_NOTEparsing.
URGH.
Quoted 7 lines
The Linux kernel does *not* define the x86-64 ABI, it only implements it. The authoritative source should be the ABI document.
In this particularly case, so far anyone implementing this ABI extension tried to provide value by changing it, sometimes successfully. Which makes me wonder why we even bother to mainatain ABI documentation. The kernel is just very late to the party.
How can the kernel be late to the party if all of this is spinning wheels without kernel support?
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 16 lines · Peter Zijlstra
Quoted 9 lines · Florian Weimer
* Peter Zijlstra:
Quoted 4 lines
I'm not sure I read Thomas' comment like that. In my reading keeping the
PT_NOTEfallback is exactly one of those 'fly workarounds'. By not supportingPT_NOTEonly the 'fine' people already shit^Hpping this out of tree are affected, and we don't have to care about them at all.Just to be clear here: There was an ABI document that required
PT_NOTEparsing.URGH.
Quoted 3 lines
The Linux kernel does *not* define the x86-64 ABI, it only implements it. The authoritative source should be the ABI document.
Quoted 7 lines
Quoted 4 lines
In this particularly case, so far anyone implementing this ABI extension tried to provide value by changing it, sometimes successfully. Which makes me wonder why we even bother to mainatain ABI documentation. The kernel is just very late to the party.
How can the kernel be late to the party if all of this is spinning wheels without kernel support?
PT_GNU_PROPERTY is mentioned and allocated a p_type value in hjl's spec [1], but otherwise seems underspecified.
In particular, it's not clear whether a PT_GNU_PROPERTY phdr _must_ be emitted for NT_GNU_PROPERTY_TYPE_0. While it seems a no-brainer to emit it, RHEL's linker already doesn't IIUC, and there are binaries in the wild.
Maybe this phdr type is a late addition -- I haven't attempted to dig through the history.
For arm64 we don't have this out-of-tree legacy to support, so we can avoid exhausitvely searching for the note: no PT_GNU_PROPERTY -> no note.
So, can we do the same for x86, forcing RHEL to carry some code out of tree to support their legacy binaries? Or do we accept that there is already a de facto ABI and try to be compatible with it?
Quoted 1 line
From my side, I want to avoid duplication between x86 and arm64, and
keep unneeded complexity out of the ELF loader where possible.
Cheers
---Dave
[1] https://github.com/hjl-tools/linux-abi/wiki/Linux-Extensions-to-gABI
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 41 lines · Dave Martin
Quoted 16 lines · Peter Zijlstra
Quoted 9 lines · Florian Weimer
* Peter Zijlstra:
Quoted 4 lines
I'm not sure I read Thomas' comment like that. In my reading keeping the
PT_NOTEfallback is exactly one of those 'fly workarounds'. By not supportingPT_NOTEonly the 'fine' people already shit^Hpping this out of tree are affected, and we don't have to care about them at all.Just to be clear here: There was an ABI document that required
PT_NOTEparsing.URGH.
Quoted 3 lines
The Linux kernel does *not* define the x86-64 ABI, it only implements it. The authoritative source should be the ABI document.
Quoted 7 lines
Quoted 4 lines
In this particularly case, so far anyone implementing this ABI extension tried to provide value by changing it, sometimes successfully. Which makes me wonder why we even bother to mainatain ABI documentation. The kernel is just very late to the party.
How can the kernel be late to the party if all of this is spinning wheels without kernel support?
PT_GNU_PROPERTYis mentioned and allocated ap_typevalue in hjl's spec [1], but otherwise seems underspecified.In particular, it's not clear whether a
PT_GNU_PROPERTYphdr _must_ be emitted forNT_GNU_PROPERTY_TYPE_0. While it seems a no-brainer to emit it, RHEL's linker already doesn't IIUC, and there are binaries in the wild.Maybe this phdr type is a late addition -- I haven't attempted to dig through the history.
For arm64 we don't have this out-of-tree legacy to support, so we can avoid exhausitvely searching for the note: no
PT_GNU_PROPERTY-> no note.
Quoted 7 lines
So, can we do the same for x86, forcing RHEL to carry some code out of tree to support their legacy binaries? Or do we accept that there is already a de facto ABI and try to be compatible with it?
From my side, I want to avoid duplication between x86 and arm64, and keep unneeded complexity out of the ELF loader where possible.
Hi Florian,
The kernel looks at only ld-linux. Other applications are loaded by ld-linux. So the issues are limited to three versions of ld-linux's. Can we somehow update those??
Thanks,
Yu-cheng
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
* Yu-cheng Yu:
Quoted 3 lines
The kernel looks at only ld-linux. Other applications are loaded by ld-linux. So the issues are limited to three versions of ld-linux's. Can we somehow update those??
I assumed that it would also parse the main executable and make adjustments based on that.
ld.so can certainly provide whatever the kernel needs. We need to tweak the existing loader anyway.
No valid statically-linked binaries exist today, so this is not a consideration at this point.
Thanks,
Florian
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 9 lines · Florian Weimer
* Yu-cheng Yu:
Quoted 4 lines
The kernel looks at only ld-linux. Other applications are loaded by ld- linux.
So the issues are limited to three versions of ld-linux's. Can we somehow update those??I assumed that it would also parse the main executable and make adjustments based on that.
Yes, Linux also looks at the main executable's header, but not its NT_GNU_PROPERTY_TYPE_0 if there is a loader.
Quoted 6 lines
ld.so can certainly provide whatever the kernel needs. We need to tweak the existing loader anyway.
No valid statically-linked binaries exist today, so this is not a consideration at this point.
So from kernel, we look at only PT_GNU_PROPERTY?
Yu-cheng
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
* Yu-cheng Yu:
Quoted 7 lines
Quoted 2 lines
I assumed that it would also parse the main executable and make adjustments based on that.
Yes, Linux also looks at the main executable's header, but not its
NT_GNU_PROPERTY_TYPE_0if there is a loader.Quoted 1 line
Quoted 7 lines
Quoted 5 lines
ld.so can certainly provide whatever the kernel needs. We need to tweak the existing loader anyway.
No valid statically-linked binaries exist today, so this is not a consideration at this point.
So from kernel, we look at only
PT_GNU_PROPERTY?
If you don't parse notes/segments in the executable for CET, then yes. We can put PT_GNU_PROPERTY into the loader.
Thanks,
Florian
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 12 lines · Florian Weimer
* Yu-cheng Yu:
Quoted 10 lines
Quoted 2 lines
I assumed that it would also parse the main executable and make adjustments based on that.
Yes, Linux also looks at the main executable's header, but not its
NT_GNU_PROPERTY_TYPE_0if there is a loader.Quoted 4 lines
ld.so can certainly provide whatever the kernel needs. We need to tweak the existing loader anyway.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
No valid statically-linked binaries exist today, so this is not a consideration at this point.
So from kernel, we look at only
PT_GNU_PROPERTY?If you don't parse notes/segments in the executable for CET, then yes. We can put
PT_GNU_PROPERTYinto the loader.
Thanks!
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 16 lines · Yu-cheng Yu
Quoted 15 lines · Florian Weimer
* Yu-cheng Yu:
Quoted 13 lines
Quoted 2 lines
I assumed that it would also parse the main executable and make adjustments based on that.
Yes, Linux also looks at the main executable's header, but not its
NT_GNU_PROPERTY_TYPE_0if there is a loader.Quoted 6 lines
ld.so can certainly provide whatever the kernel needs. We need to tweak the existing loader anyway.
No valid statically-linked binaries exist today, so this is not a consideration at this point.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
So from kernel, we look at only
PT_GNU_PROPERTY?If you don't parse notes/segments in the executable for CET, then yes. We can put
PT_GNU_PROPERTYinto the loader.Thanks!
Would this require the kernel and ld.so to be updated in a particular order to avoid breakage? I don't know enough about RHEL to know how controversial that might be.
Also:
What about static binaries distrubited as part of RHEL?
A user would also reasonably expect static binaries built using the distro toolchain to work on top of the distro kernel... which might be broken by this.
(When I say "broken" I mean that the binary would run, but CET protections would be silently turned off.)
Cheers
---Dave
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
* Dave Martin:
Quoted 19 lines · Yu-cheng Yu
Quoted 18 lines · Florian Weimer
Quoted 17 lines
* Yu-cheng Yu:
Quoted 14 lines
Quoted 2 lines
I assumed that it would also parse the main executable and make adjustments based on that.
Yes, Linux also looks at the main executable's header, but not its
NT_GNU_PROPERTY_TYPE_0if there is a loader.Quoted 6 lines
ld.so can certainly provide whatever the kernel needs. We need to tweak the existing loader anyway.
No valid statically-linked binaries exist today, so this is not a consideration at this point.
So from kernel, we look at only
PT_GNU_PROPERTY?
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
If you don't parse notes/segments in the executable for CET, then yes. We can put
PT_GNU_PROPERTYinto the loader.Thanks!
Would this require the kernel and ld.so to be updated in a particular order to avoid breakage? I don't know enough about RHEL to know how controversial that might be.
There is no official ld.so that will work with the current userspace interface (in this patch submission). Upstream glibc needs to be updated anyway, so yet another change isn't much of an issue. This is not a problem; we knew that something like this might happen.
Sure, people need a new binutils with backports for PT_GNU_PROPERTY, but given that only very few people will build CET binaries with older binutils, I think that's not a real issue either.
Thanks,
Florian
Re: [PATCH v7 22/27] binfmt_elf: Extract .note.gnu.property from an ELF file
Quoted 30 lines · Florian Weimer
* Dave Martin:
Quoted 19 lines · Yu-cheng Yu
Quoted 18 lines · Florian Weimer
Quoted 17 lines
* Yu-cheng Yu:
Quoted 14 lines
Quoted 2 lines
I assumed that it would also parse the main executable and make adjustments based on that.
Yes, Linux also looks at the main executable's header, but not its
NT_GNU_PROPERTY_TYPE_0if there is a loader.Quoted 6 lines
ld.so can certainly provide whatever the kernel needs. We need to tweak the existing loader anyway.
No valid statically-linked binaries exist today, so this is not a consideration at this point.
So from kernel, we look at only
PT_GNU_PROPERTY?Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
If you don't parse notes/segments in the executable for CET, then yes. We can put
PT_GNU_PROPERTYinto the loader.Thanks!
Would this require the kernel and ld.so to be updated in a particular order to avoid breakage? I don't know enough about RHEL to know how controversial that might be.
Quoted 8 lines
There is no official ld.so that will work with the current userspace interface (in this patch submission). Upstream glibc needs to be updated anyway, so yet another change isn't much of an issue. This is not a problem; we knew that something like this might happen.
Sure, people need a new binutils with backports for
PT_GNU_PROPERTY, but given that only very few people will build CET binaries with older binutils, I think that's not a real issue either.
OK, just wanted to check we weren't missing any requirement for x86.
This approach should satisfy the requirement for arm64 nicely.
Cheers
---Dave
PATCH v7 23/27 x86/cet/shstk: ELF header parsing of Shadow Stack
In reply to Yu-cheng YuLook in .note.gnu.property of an ELF file and check if Shadow Stack needs to be enabled for the task.
--- arch/x86/Kconfig | 1 + arch/x86/kernel/process_64.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+)
arch/x86/Kconfig +1 -0
@@ -1928,6 +1928,7 @@ config X86_INTEL_SHADOW_STACK_USER select ARCH_USES_HIGH_VMA_FLAGS select X86_INTEL_CET select ARCH_HAS_SHSTK + select ARCH_USE_GNU_PROPERTY ---help--- Shadow stack provides hardware protection against program stack corruption. Only when all the following are true will an application
arch/x86/kernel/process_64.c +24 -0
@@ -828,3 +828,27 @@ unsigned long KSTK_ESP(struct task_struct *task) {return task_pt_regs (task)->sp; } + +#ifdef CONFIG_ARCH_USE_GNU_PROPERTY +int arch_setup_property (void *ehdr,void *phdr,struct file *f,bool inter) +{ +int r; +uint32_t property; + + r =get_gnu_property (ehdr, phdr, f,GNU_PROPERTY_X86_FEATURE_1_AND , + &property); + +memset (¤t->thread.cet,0 ,sizeof (struct cet_status )); + +if (r) +return r; + +if (cpu_feature_enabled (X86_FEATURE_SHSTK )) { +if (property &GNU_PROPERTY_X86_FEATURE_1_SHSTK ) + r =cet_setup_shstk (); +if (r <0 ) +return r; + } +return r; +} +#endif
Signature · 1 line
2.17.1
Peter Zijlstra
maintainerRe: [PATCH v7 23/27] x86/cet/shstk: ELF header parsing of Shadow Stack
arch/x86/kernel/process_64.c unsigned long KSTK_ESP(struct task_struct *task) · line 836
Quoted 5 lines · Yu-cheng Yu
arch/x86/kernel/process_64.c +5
-0+#ifdef CONFIG_ARCH_USE_GNU_PROPERTY +int arch_setup_property (void *ehdr,void *phdr,struct file *f,bool inter) +{ +int r; +uint32_t property;
Flip those two lines around.
arch/x86/kernel/process_64.c unsigned long KSTK_ESP(struct task_struct *task) · line 841
Quoted 5 lines
arch/x86/kernel/process_64.c +5
-0+ + r =get_gnu_property (ehdr, phdr, f,GNU_PROPERTY_X86_FEATURE_1_AND , + &property); + +memset (¤t->thread.cet,0 ,sizeof (struct cet_status ));
It seems to me that memset would be better placed before get_gnu_property().
arch/x86/kernel/process_64.c unsigned long KSTK_ESP(struct task_struct *task) · line 846
Quoted 4 lines
arch/x86/kernel/process_64.c +4
-0+if (r) +return r; + +if (cpu_feature_enabled (X86_FEATURE_SHSTK )) {
if (r || !cpu_feature_enabled()) return r;
arch/x86/kernel/process_64.c unsigned long KSTK_ESP(struct task_struct *task) · line 852
Quoted 6 lines
arch/x86/kernel/process_64.c +6
-0+if (property &GNU_PROPERTY_X86_FEATURE_1_SHSTK ) + r =cet_setup_shstk (); +if (r <0 ) +return r; + } +return r;
and loose the indent.
Quoted 5 lines
(quoted diff) +2
-0+} +#endifSignature · 1 line
2.17.1
PATCH v7 24/27 x86/cet/shstk: Handle thread shadow stack
In reply to Yu-cheng YuThe shadow stack for clone/fork is handled as the following:
(1) If ((clone_flags & (CLONE_VFORK | CLONE_VM)) == CLONE_VM), the kernel allocates (and frees on thread exit) a new SHSTK for the child.
It is possible for the kernel to complete the clone syscall and set the child's SHSTK pointer to NULL and let the child thread allocate a SHSTK for itself. There are two issues in this approach: It is not compatible with existing code that does inline syscall and it cannot handle signals before the child can successfully allocate a SHSTK.
(2) For (clone_flags & CLONE_VFORK), the child uses the existing SHSTK.
(3) For all other cases, the SHSTK is copied/reused whenever the parent or the child does a call/ret.
This patch handles cases (1) & (2). Case (3) is handled in the SHSTK page fault patches.
A 64-bit SHSTK has a fixed size of RLIMIT_STACK. A compat-mode thread SHSTK has a fixed size of 1/4 RLIMIT_STACK. This allows more threads to share a 32-bit address space.
--- arch/x86/include/asm/cet.h | 2 ++ arch/x86/include/asm/mmu_context.h | 3 +++ arch/x86/kernel/cet.c | 41 ++++++++++++++++++++++++++++++ arch/x86/kernel/process.c | 1 + arch/x86/kernel/process_64.c | 7 +++++ 5 files changed, 54 insertions(+)
arch/x86/include/asm/cet.h +2 -0
@@ -19,12 +19,14 @@ struct cet_status {#ifdef CONFIG_X86_INTEL_CET int cet_setup_shstk (void ); +int cet_setup_thread_shstk (struct task_struct *p);void cet_disable_shstk (void );void cet_disable_free_shstk (struct task_struct *p);int cet_restore_signal (bool ia32,struct sc_ext *sc);int cet_setup_signal (bool ia32,unsigned long rstor,struct sc_ext *sc);#else static inline int cet_setup_shstk (void ) {return -EINVAL ; } +static inline int cet_setup_thread_shstk (struct task_struct *p) {return 0 ; }static inline void cet_disable_shstk (void ) {}static inline void cet_disable_free_shstk (struct task_struct *p) {}static inline int cet_restore_signal (bool ia32,struct sc_ext *sc) {return -EINVAL ; }
arch/x86/include/asm/mmu_context.h +3 -0
@@ -13,6 +13,7 @@#include <asm/tlbflush.h> #include <asm/paravirt.h> #include <asm/mpx.h> +#include <asm/cet.h> #include <asm/debugreg.h> extern atomic64_t last_mm_ctx_id;
@@ -228,6 +229,8 @@ do { \#else #define deactivate_mm (tsk, mm) \ do { \ + if (!tsk->vfork_done) \ + cet_disable_free_shstk(tsk); \ load_gs_index(0); \ loadsegment(fs, 0); \ } while (0)
arch/x86/kernel/cet.c +41 -0
47 lines not on this page · raw message
arch/x86/kernel/process.c +1 -0
@@ -130,6 +130,7 @@ void exit_thread(struct task_struct *tsk)free_vm86 (t); +cet_disable_free_shstk (tsk);fpu__drop (fpu); }
arch/x86/kernel/process_64.c +7 -0
@@ -421,6 +421,13 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp,if (sp) childregs->sp = sp; +/* Allocate a new shadow stack for pthread */ +if ((clone_flags & (CLONE_VFORK |CLONE_VM )) ==CLONE_VM ) { + err =cet_setup_thread_shstk (p); +if (err) + goto out; + } + err = -ENOMEM ;if (unlikely (test_tsk_thread_flag (me,TIF_IO_BITMAP ))) { p->thread.io_bitmap_ptr = kmemdup(me->thread.io_bitmap_ptr,
Signature · 1 line
2.17.1
PATCH v7 25/27 mm/mmap: Add Shadow stack pages to memory accounting
In reply to Yu-cheng YuAdd shadow stack pages to memory accounting.
--- mm/mmap.c | 5 +++++ 1 file changed, 5 insertions(+)
mm/mmap.c +5 -0
@@ -1703,6 +1703,9 @@ static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)if (file &&is_file_hugepages (file))return 0 ; +if (arch_copy_pte_mapping (vm_flags)) +return 1 ; +return (vm_flags & (VM_NORESERVE |VM_SHARED |VM_WRITE )) ==VM_WRITE ; }
@@ -3319,6 +3322,8 @@ void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) mm->stack_vm += npages; else if (is_data_mapping(flags)) mm->data_vm += npages; + else if (arch_copy_pte_mapping(flags)) + mm->data_vm += npages; }static vm_fault_t special_mapping_fault (struct vm_fault *vmf);
Signature · 1 line
2.17.1
Re: [PATCH v7 25/27] mm/mmap: Add Shadow stack pages to memory accounting
Quoted 13 lines · Yu-cheng Yu
mm/mmap.c +3
-0@@ -1703,6 +1703,9 @@ static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)if (file &&is_file_hugepages (file))return 0 ; +if (arch_copy_pte_mapping (vm_flags)) +return 1 ; +return (vm_flags & (VM_NORESERVE |VM_SHARED |VM_WRITE )) ==VM_WRITE ; }
mm/mmap.c void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) · line 3327
Quoted 6 lines
mm/mmap.c +2
-0@@ -3319,6 +3322,8 @@ void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) mm->stack_vm += npages; else if (is_data_mapping(flags)) mm->data_vm += npages; + else if (arch_copy_pte_mapping(flags)) + mm->data_vm += npages; }
This classifies shadow stack as data instead of stack. That seems a wee bit counterintuitive. Why did you make this choice?
Re: [PATCH v7 25/27] mm/mmap: Add Shadow stack pages to memory accounting
Quoted 15 lines · Dave Hansen
Quoted 14 lines · Yu-cheng Yu
mm/mmap.c +3
-0@@ -1703,6 +1703,9 @@ static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)if (file &&is_file_hugepages (file))return 0 ; +if (arch_copy_pte_mapping (vm_flags)) +return 1 ; +return (vm_flags & (VM_NORESERVE |VM_SHARED |VM_WRITE )) ==VM_WRITE ;}
Quoted 11 lines
mm/mmap.c void vm_stat_account(struct mm_struct *mm, vm_flags_t · line 3328
Quoted 8 lines
mm/mmap.c +2
-0@@ -3319,6 +3322,8 @@ void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) mm->stack_vm += npages; else if (is_data_mapping(flags)) mm->data_vm += npages; + else if (arch_copy_pte_mapping(flags)) + mm->data_vm += npages; }This classifies shadow stack as data instead of stack. That seems a wee bit counterintuitive. Why did you make this choice?
I don't recall the reason; I will change it to stack and test it out.
Yu-cheng
PATCH v7 26/27 x86/cet/shstk: Add arch_prctl functions for Shadow Stack
In reply to Yu-cheng Yuarch_prctl(ARCH_X86_CET_STATUS, unsigned long *addr)
Return CET feature status.
The parameter 'addr' is a pointer to a user buffer. On returning to the caller, the kernel fills the following information:
*addr = SHSTK/IBT status
*(addr + 1) = SHSTK base address
*(addr + 2) = SHSTK size
arch_prctl(ARCH_X86_CET_DISABLE, unsigned long features)
Disable CET features specified in 'features'. Return -EPERM if CET is locked.
arch_prctl(ARCH_X86_CET_LOCK)
Lock in CET feature.
arch_prctl(ARCH_X86_CET_ALLOC_SHSTK, unsigned long *addr)
Allocate a new SHSTK.
The parameter 'addr' is a pointer to a user buffer and indicates the desired SHSTK size to allocate. On returning to the caller the buffer contains the address of the new SHSTK.
There is no CET enabling arch_prctl function. By design, CET is enabled automatically if the binary and the system can support it.
The parameters passed are always unsigned 64-bit. When an ia32 application passing pointers, it should only use the lower 32 bits.
--- arch/x86/include/asm/cet.h | 5 ++ arch/x86/include/uapi/asm/prctl.h | 5 ++ arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/cet.c | 29 +++++++++++ arch/x86/kernel/cet_prctl.c | 85 +++++++++++++++++++++++++++++++ arch/x86/kernel/process.c | 4 +- 6 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 arch/x86/kernel/cet_prctl.c
arch/x86/include/asm/cet.h +5 -0
@@ -14,19 +14,24 @@ struct sc_ext;struct cet_status {unsigned long shstk_base;unsigned long shstk_size; +unsigned int locked:1 ;unsigned int shstk_enabled:1 ; };#ifdef CONFIG_X86_INTEL_CET +int prctl_cet (int option,unsigned long arg2);int cet_setup_shstk (void );int cet_setup_thread_shstk (struct task_struct *p); +int cet_alloc_shstk (unsigned long *arg);void cet_disable_shstk (void );void cet_disable_free_shstk (struct task_struct *p);int cet_restore_signal (bool ia32,struct sc_ext *sc);int cet_setup_signal (bool ia32,unsigned long rstor,struct sc_ext *sc);#else +static inline int prctl_cet (int option,unsigned long arg2) {return -EINVAL ; }static inline int cet_setup_shstk (void ) {return -EINVAL ; }static inline int cet_setup_thread_shstk (struct task_struct *p) {return 0 ; } +static inline int cet_alloc_shstk (unsigned long *arg) {return -EINVAL ; }static inline void cet_disable_shstk (void ) {}static inline void cet_disable_free_shstk (struct task_struct *p) {}static inline int cet_restore_signal (bool ia32,struct sc_ext *sc) {return -EINVAL ; }
arch/x86/include/uapi/asm/prctl.h +5 -0
@@ -14,4 +14,9 @@#define ARCH_MAP_VDSO_32 0x2002#define ARCH_MAP_VDSO_64 0x2003 +#define ARCH_X86_CET_STATUS 0x3001 +#define ARCH_X86_CET_DISABLE 0x3002 +#define ARCH_X86_CET_LOCK 0x3003 +#define ARCH_X86_CET_ALLOC_SHSTK 0x3004 +#endif /* _ASM_X86_PRCTL_H */
arch/x86/kernel/Makefile +1 -1
@@ -140,7 +140,7 @@ obj-$(CONFIG_UNWINDER_ORC) += unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.o obj-$(CONFIG_UNWINDER_GUESS) += unwind_guess.o -obj-$(CONFIG_X86_INTEL_CET) += cet.o +obj-$(CONFIG_X86_INTEL_CET) += cet.o cet_prctl.o ### # 64 bit specific files
arch/x86/kernel/cet.c +29 -0
@@ -127,6 +127,35 @@ static int create_rstor_token(bool ia32, unsigned long ssp,return 0 ; } +int cet_alloc_shstk (unsigned long *arg) +{ +unsigned long len = *arg; +unsigned long addr; +unsigned long token; +unsigned long ssp; + + addr =do_mmap_locked (0 , len,PROT_READ , +MAP_ANONYMOUS |MAP_PRIVATE ,VM_SHSTK ); +if (addr >=TASK_SIZE_MAX ) +return -ENOMEM ; + +/* Restore token is 8 bytes and aligned to 8 bytes */ + ssp = addr + len; + token = ssp; + +if (!in_ia32_syscall ()) + token |=TOKEN_MODE_64 ; + ssp -=8 ; + +if (write_user_shstk_64 (ssp, token)) { +vm_munmap (addr, len); +return -EINVAL ; + } + + *arg = addr; +return 0 ; +} +int cet_setup_shstk (void ) {unsigned long addr, size;
arch/x86/kernel/cet_prctl.c new file +85 -0
85 lines not on this page · raw message
arch/x86/kernel/process.c +2 -2
@@ -873,7 +873,7 @@ long do_arch_prctl_common(struct task_struct *task, int option,return get_cpuid_mode ();case ARCH_SET_CPUID :return set_cpuid_mode (task, cpuid_enabled); +default : +return prctl_cet (option, cpuid_enabled); } - -return -EINVAL ; }
Signature · 1 line
2.17.1
PATCH v7 27/27 x86/cet/shstk: Add Shadow Stack instructions to opcode map
In reply to Yu-cheng YuAdd the following shadow stack management instructions.
INCSSP:
Increment shadow stack pointer by the steps specified.
RDSSP:
Read SSP register into a GPR.
SAVEPREVSSP:
Use "prev ssp" token at top of current shadow stack to create a "restore token" on previous shadow stack.
RSTORSSP:
Restore from a "restore token" pointed by a GPR to SSP.
WRSS:
Write to kernel-mode shadow stack (kernel-mode instruction).
WRUSS:
Write to user-mode shadow stack (kernel-mode instruction).
SETSSBSY:
Verify the "supervisor token" pointed by IA32_PL0_SSP MSR, if valid, set the token to busy, and set SSP to the value of IA32_PL0_SSP MSR.
CLRSSBSY:
Verify the "supervisor token" pointed by a GPR, if valid, clear the busy bit from the token.
--- arch/x86/lib/x86-opcode-map.txt | 26 +++++++++++++------ tools/objtool/arch/x86/lib/x86-opcode-map.txt | 26 +++++++++++++------ 2 files changed, 36 insertions(+), 16 deletions(-)
arch/x86/lib/x86-opcode-map.txt +18 -8
@@ -366,7 +366,7 @@ AVXcode: 1 1b: BNDCN Gv,Ev (F2) | BNDMOV Ev,Gv (66) | BNDMK Gv,Ev (F3) | BNDSTX Ev,Gv 1c: 1d: -1e: +1e: RDSSP Rd (F3),REX.W 1f: NOP Ev # 0x0f 0x20-0x2f 20: MOV Rd,Cd
@@ -610,7 +610,17 @@ fe: paddd Pq,Qq | vpaddd Vx,Hx,Wx (66),(v1) ff: UD0 EndTable -Table: 3-byte opcode 1 (0x0f 0x38) +Table: 3-byte opcode 1 (0x0f 0x01) +Referrer: +AVXcode: +# Skip 0x00-0xe7 +e8: SETSSBSY (f3) +e9: +ea: SAVEPREVSSP (f3) +# Skip 0xeb-0xff +EndTable + +Table: 3-byte opcode 2 (0x0f 0x38) Referrer: 3-byte escape 1 AVXcode: 2 # 0x0f 0x38 0x00-0x0f
@@ -789,12 +799,12 @@ f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v) f3: Grp17 (1A) -f5: BZHI Gy,Ey,By (v) | PEXT Gy,By,Ey (F3),(v) | PDEP Gy,By,Ey (F2),(v) -f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) +f5: BZHI Gy,Ey,By (v) | PEXT Gy,By,Ey (F3),(v) | PDEP Gy,By,Ey (F2),(v) | WRUSS Pq,Qq (66),REX.W +f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSS Pq,Qq (66),REX.W f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) EndTable -Table: 3-byte opcode 2 (0x0f 0x3a) +Table: 3-byte opcode 3 (0x0f 0x3a) Referrer: 3-byte escape 2 AVXcode: 3 # 0x0f 0x3a 0x00-0xff
@@ -948,7 +958,7 @@ GrpTable: Grp7 2: LGDT Ms | XGETBV (000),(11B) | XSETBV (001),(11B) | VMFUNC (100),(11B) | XEND (101)(11B) | XTEST (110)(11B) 3: LIDT Ms 4: SMSW Mw/Rv -5: rdpkru (110),(11B) | wrpkru (111),(11B) +5: rdpkru (110),(11B) | wrpkru (111),(11B) | RSTORSSP Mq (F3) 6: LMSW Ew 7: INVLPG Mb | SWAPGS (o64),(000),(11B) | RDTSCP (001),(11B) EndTable
@@ -1019,8 +1029,8 @@ GrpTable: Grp15 2: vldmxcsr Md (v1) | WRFSBASE Ry (F3),(11B) 3: vstmxcsr Md (v1) | WRGSBASE Ry (F3),(11B) 4: XSAVE | ptwrite Ey (F3),(11B) -5: XRSTOR | lfence (11B) -6: XSAVEOPT | clwb (66) | mfence (11B) +5: XRSTOR | lfence (11B) | INCSSP Rd (F3),REX.W +6: XSAVEOPT | clwb (66) | mfence (11B) | CLRSSBSY Mq (F3) 7: clflush | clflushopt (66) | sfence (11B) EndTable
tools/objtool/arch/x86/lib/x86-opcode-map.txt +18 -8
@@ -366,7 +366,7 @@ AVXcode: 1 1b: BNDCN Gv,Ev (F2) | BNDMOV Ev,Gv (66) | BNDMK Gv,Ev (F3) | BNDSTX Ev,Gv 1c: 1d: -1e: +1e: RDSSP Rd (F3),REX.W 1f: NOP Ev # 0x0f 0x20-0x2f 20: MOV Rd,Cd
@@ -610,7 +610,17 @@ fe: paddd Pq,Qq | vpaddd Vx,Hx,Wx (66),(v1) ff: UD0 EndTable -Table: 3-byte opcode 1 (0x0f 0x38) +Table: 3-byte opcode 1 (0x0f 0x01) +Referrer: +AVXcode: +# Skip 0x00-0xe7 +e8: SETSSBSY (f3) +e9: +ea: SAVEPREVSSP (f3) +# Skip 0xeb-0xff +EndTable + +Table: 3-byte opcode 2 (0x0f 0x38) Referrer: 3-byte escape 1 AVXcode: 2 # 0x0f 0x38 0x00-0x0f
@@ -789,12 +799,12 @@ f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v) f3: Grp17 (1A) -f5: BZHI Gy,Ey,By (v) | PEXT Gy,By,Ey (F3),(v) | PDEP Gy,By,Ey (F2),(v) -f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) +f5: BZHI Gy,Ey,By (v) | PEXT Gy,By,Ey (F3),(v) | PDEP Gy,By,Ey (F2),(v) | WRUSS Pq,Qq (66),REX.W +f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSS Pq,Qq (66),REX.W f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) EndTable -Table: 3-byte opcode 2 (0x0f 0x3a) +Table: 3-byte opcode 3 (0x0f 0x3a) Referrer: 3-byte escape 2 AVXcode: 3 # 0x0f 0x3a 0x00-0xff
@@ -948,7 +958,7 @@ GrpTable: Grp7 2: LGDT Ms | XGETBV (000),(11B) | XSETBV (001),(11B) | VMFUNC (100),(11B) | XEND (101)(11B) | XTEST (110)(11B) 3: LIDT Ms 4: SMSW Mw/Rv -5: rdpkru (110),(11B) | wrpkru (111),(11B) +5: rdpkru (110),(11B) | wrpkru (111),(11B) | RSTORSSP Mq (F3) 6: LMSW Ew 7: INVLPG Mb | SWAPGS (o64),(000),(11B) | RDTSCP (001),(11B) EndTable
@@ -1019,8 +1029,8 @@ GrpTable: Grp15 2: vldmxcsr Md (v1) | WRFSBASE Ry (F3),(11B) 3: vstmxcsr Md (v1) | WRGSBASE Ry (F3),(11B) 4: XSAVE | ptwrite Ey (F3),(11B) -5: XRSTOR | lfence (11B) -6: XSAVEOPT | clwb (66) | mfence (11B) +5: XRSTOR | lfence (11B) | INCSSP Rd (F3),REX.W +6: XSAVEOPT | clwb (66) | mfence (11B) | CLRSSBSY Mq (F3) 7: clflush | clflushopt (66) | sfence (11B) EndTable
Signature · 1 line
2.17.1
Re: [PATCH v7 27/27] x86/cet/shstk: Add Shadow Stack instructions to opcode map
Quoted 21 lines · Yu-cheng Yu
Add the following shadow stack management instructions.
INCSSP:
Increment shadow stack pointer by the steps specified.RDSSP:
Read SSP register into a GPR.SAVEPREVSSP:
Use "prev ssp" token at top of current shadow stack to create a "restore token" on previous shadow stack.RSTORSSP:
Restore from a "restore token" pointed by a GPR to SSP.WRSS:
Write to kernel-mode shadow stack (kernel-mode instruction).WRUSS:
Write to user-mode shadow stack (kernel-mode instruction).
Quoted 8 lines
SETSSBSY:
Verify the "supervisor token" pointed byIA32_PL0_SSPMSR, if valid, set the token to busy, and set SSP to the value ofIA32_PL0_SSPMSR.CLRSSBSY:
Verify the "supervisor token" pointed by a GPR, if valid, clear the busy bit from the token.
Does the notrack prefix also need to be catered for somehow?
Quoted 40 lines
Signed-off-by Yu-cheng Yu <[email protected]>--- arch/x86/lib/x86-opcode-map.txt | 26 +++++++++++++------ tools/objtool/arch/x86/lib/x86-opcode-map.txt | 26 +++++++++++++------ 2 files changed, 36 insertions(+), 16 deletions(-)arch/x86/lib/x86-opcode-map.txt +12
-2@@ -366,7 +366,7 @@ AVXcode: 1 1b: BNDCN Gv,Ev (F2) | BNDMOV Ev,Gv (66) | BNDMK Gv,Ev (F3) | BNDSTX Ev,Gv 1c: 1d: -1e: +1e: RDSSP Rd (F3),REX.W 1f: NOP Ev # 0x0f 0x20-0x2f 20: MOV Rd,Cd@@ -610,7 +610,17 @@ fe: paddd Pq,Qq | vpaddd Vx,Hx,Wx (66),(v1) ff: UD0 EndTable -Table: 3-byte opcode 1 (0x0f 0x38) +Table: 3-byte opcode 1 (0x0f 0x01) +Referrer: +AVXcode: +# Skip 0x00-0xe7 +e8: SETSSBSY (f3) +e9: +ea: SAVEPREVSSP (f3) +# Skip 0xeb-0xff +EndTable + +Table: 3-byte opcode 2 (0x0f 0x38) Referrer: 3-byte escape 1 AVXcode: 2 # 0x0f 0x38 0x00-0x0f
arch/x86/lib/x86-opcode-map.txt f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) · line 803
Quoted 7 lines
arch/x86/lib/x86-opcode-map.txt +2
-2@@ -789,12 +799,12 @@ f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v) f3: Grp17 (1A) -f5: BZHI Gy,Ey,By (v) | PEXT Gy,By,Ey (F3),(v) | PDEP Gy,By,Ey (F2),(v) -f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) +f5: BZHI Gy,Ey,By (v) | PEXT Gy,By,Ey (F3),(v) | PDEP Gy,By,Ey (F2),(v) | WRUSS Pq,Qq (66),REX.W +f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSS Pq,Qq (66),REX.W
AFAICT WRSS does not have 66 prefix
Quoted 97 lines
arch/x86/lib/x86-opcode-map.txt +4
-4f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) EndTable -Table: 3-byte opcode 2 (0x0f 0x3a) +Table: 3-byte opcode 3 (0x0f 0x3a) Referrer: 3-byte escape 2 AVXcode: 3 # 0x0f 0x3a 0x00-0xff@@ -948,7 +958,7 @@ GrpTable: Grp7 2: LGDT Ms | XGETBV (000),(11B) | XSETBV (001),(11B) | VMFUNC (100),(11B) | XEND (101)(11B) | XTEST (110)(11B) 3: LIDT Ms 4: SMSW Mw/Rv -5: rdpkru (110),(11B) | wrpkru (111),(11B) +5: rdpkru (110),(11B) | wrpkru (111),(11B) | RSTORSSP Mq (F3) 6: LMSW Ew 7: INVLPG Mb | SWAPGS (o64),(000),(11B) | RDTSCP (001),(11B) EndTable@@ -1019,8 +1029,8 @@ GrpTable: Grp15 2: vldmxcsr Md (v1) | WRFSBASE Ry (F3),(11B) 3: vstmxcsr Md (v1) | WRGSBASE Ry (F3),(11B) 4: XSAVE | ptwrite Ey (F3),(11B) -5: XRSTOR | lfence (11B) -6: XSAVEOPT | clwb (66) | mfence (11B) +5: XRSTOR | lfence (11B) | INCSSP Rd (F3),REX.W +6: XSAVEOPT | clwb (66) | mfence (11B) | CLRSSBSY Mq (F3) 7: clflush | clflushopt (66) | sfence (11B) EndTabletools/objtool/arch/x86/lib/x86-opcode-map.txt +18
-8@@ -366,7 +366,7 @@ AVXcode: 1 1b: BNDCN Gv,Ev (F2) | BNDMOV Ev,Gv (66) | BNDMK Gv,Ev (F3) | BNDSTX Ev,Gv 1c: 1d: -1e: +1e: RDSSP Rd (F3),REX.W 1f: NOP Ev # 0x0f 0x20-0x2f 20: MOV Rd,Cd@@ -610,7 +610,17 @@ fe: paddd Pq,Qq | vpaddd Vx,Hx,Wx (66),(v1) ff: UD0 EndTable -Table: 3-byte opcode 1 (0x0f 0x38) +Table: 3-byte opcode 1 (0x0f 0x01) +Referrer: +AVXcode: +# Skip 0x00-0xe7 +e8: SETSSBSY (f3) +e9: +ea: SAVEPREVSSP (f3) +# Skip 0xeb-0xff +EndTable + +Table: 3-byte opcode 2 (0x0f 0x38) Referrer: 3-byte escape 1 AVXcode: 2 # 0x0f 0x38 0x00-0x0f@@ -789,12 +799,12 @@ f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v) f3: Grp17 (1A) -f5: BZHI Gy,Ey,By (v) | PEXT Gy,By,Ey (F3),(v) | PDEP Gy,By,Ey (F2),(v) -f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) +f5: BZHI Gy,Ey,By (v) | PEXT Gy,By,Ey (F3),(v) | PDEP Gy,By,Ey (F2),(v) | WRUSS Pq,Qq (66),REX.W +f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSS Pq,Qq (66),REX.W f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) EndTable -Table: 3-byte opcode 2 (0x0f 0x3a) +Table: 3-byte opcode 3 (0x0f 0x3a) Referrer: 3-byte escape 2 AVXcode: 3 # 0x0f 0x3a 0x00-0xff@@ -948,7 +958,7 @@ GrpTable: Grp7 2: LGDT Ms | XGETBV (000),(11B) | XSETBV (001),(11B) | VMFUNC (100),(11B) | XEND (101)(11B) | XTEST (110)(11B) 3: LIDT Ms 4: SMSW Mw/Rv -5: rdpkru (110),(11B) | wrpkru (111),(11B) +5: rdpkru (110),(11B) | wrpkru (111),(11B) | RSTORSSP Mq (F3) 6: LMSW Ew 7: INVLPG Mb | SWAPGS (o64),(000),(11B) | RDTSCP (001),(11B) EndTable@@ -1019,8 +1029,8 @@ GrpTable: Grp15 2: vldmxcsr Md (v1) | WRFSBASE Ry (F3),(11B) 3: vstmxcsr Md (v1) | WRGSBASE Ry (F3),(11B) 4: XSAVE | ptwrite Ey (F3),(11B) -5: XRSTOR | lfence (11B) -6: XSAVEOPT | clwb (66) | mfence (11B) +5: XRSTOR | lfence (11B) | INCSSP Rd (F3),REX.W +6: XSAVEOPT | clwb (66) | mfence (11B) | CLRSSBSY Mq (F3) 7: clflush | clflushopt (66) | sfence (11B) EndTable
Re: [PATCH v7 27/27] x86/cet/shstk: Add Shadow Stack instructions to opcode map
Quoted 27 lines · Adrian Hunter
Quoted 26 lines · Yu-cheng Yu
Add the following shadow stack management instructions.
INCSSP:
Increment shadow stack pointer by the steps specified.RDSSP:
Read SSP register into a GPR.SAVEPREVSSP:
Use "prev ssp" token at top of current shadow stack to create a "restore token" on previous shadow stack.RSTORSSP:
Restore from a "restore token" pointed by a GPR to SSP.WRSS:
Write to kernel-mode shadow stack (kernel-mode instruction).WRUSS:
Write to user-mode shadow stack (kernel-mode instruction).SETSSBSY:
Verify the "supervisor token" pointed byIA32_PL0_SSPMSR, if valid, set the token to busy, and set SSP to the value ofIA32_PL0_SSPMSR.
Quoted 5 lines
Quoted 3 lines
CLRSSBSY:
Verify the "supervisor token" pointed by a GPR, if valid, clear the busy bit from the token.Does the notrack prefix also need to be catered for somehow?
Yes, I will fix it.
Quoted 42 lines
Quoted 42 lines
Signed-off-by Yu-cheng Yu <[email protected]>--- arch/x86/lib/x86-opcode-map.txt | 26 +++++++++++++------ tools/objtool/arch/x86/lib/x86-opcode-map.txt | 26 +++++++++++++------ 2 files changed, 36 insertions(+), 16 deletions(-)arch/x86/lib/x86-opcode-map.txt +12
-2@@ -366,7 +366,7 @@ AVXcode: 1 1b: BNDCN Gv,Ev (F2) | BNDMOV Ev,Gv (66) | BNDMK Gv,Ev (F3) | BNDSTX Ev,Gv 1c: 1d: -1e: +1e: RDSSP Rd (F3),REX.W 1f: NOP Ev # 0x0f 0x20-0x2f 20: MOV Rd,Cd@@ -610,7 +610,17 @@ fe: paddd Pq,Qq | vpaddd Vx,Hx,Wx (66),(v1) ff: UD0 EndTable -Table: 3-byte opcode 1 (0x0f 0x38) +Table: 3-byte opcode 1 (0x0f 0x01) +Referrer: +AVXcode: +# Skip 0x00-0xe7 +e8: SETSSBSY (f3) +e9: +ea: SAVEPREVSSP (f3) +# Skip 0xeb-0xff +EndTable + +Table: 3-byte opcode 2 (0x0f 0x38) Referrer: 3-byte escape 1 AVXcode: 2 # 0x0f 0x38 0x00-0x0f@@ -789,12 +799,12 @@ f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2)
Quoted 8 lines
arch/x86/lib/x86-opcode-map.txt f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) · line 803
Quoted 6 lines
arch/x86/lib/x86-opcode-map.txt +2
-2f2: ANDN Gy,By,Ey (v) f3: Grp17 (1A) -f5: BZHI Gy,Ey,By (v) | PEXT Gy,By,Ey (F3),(v) | PDEP Gy,By,Ey (F2),(v) -f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) +f5: BZHI Gy,Ey,By (v) | PEXT Gy,By,Ey (F3),(v) | PDEP Gy,By,Ey (F2),(v) | WRUSS Pq,Qq (66),REX.W +f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSS Pq,Qq (66),REX.WAFAICT WRSS does not have 66 prefix
I will check.
Thanks,
Yu-cheng