x86/pti: The real thing
Subsystems (9)
Linus,
please pull the latest x86-pti-for-linus git tree from:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git x86-pti-for-linus
This is the final set of enabling page table isolation on x86:
- Infrastructure patches for handling the extra page tables.
- Patches which map the various bits and pieces which are required to get in and out of user space into the user space visible page tables.
- The required changes to have CR3 switching in the entry/exit code.
- Optimizations for the CR3 switching along with documentation how the ASID/PCID mechanism works.
- Updates to dump pagetables to cover the user space page tables for W+X scans and extra debugfs files to analyze both the kernel and the user space visible page tables
The whole functionality is compile time controlled via a config switch and can be turned on/off on the command line as well.
There is a trivial conflict in efi_alloc_page_tables() because the x86/pti branch still has the __GFP_NOTRACK allocation flag as it's based on 4.14 for known reasons.
Thanks,
tglx
------------------>
Andy Lutomirski (6):
x86/mm/pti: Add functions to clone kernel PMDs x86/mm/pti: Share cpu_entry_area with user space page tables x86/mm/pti: Map ESPFIX into user space
x86/mm/64: Make a full PGD-entry size hole in the memory map x86/pti: Put the LDT in its own PGD if PTI is on x86/pti: Map the vsyscall page if needed
Borislav Petkov (2):
x86/pti: Add the pti= cmdline option and documentation x86/mm/dump_pagetables: Add page table directory to the debugfs VFS hierarchy
Dave Hansen (10):
x86/mm/pti: Disable global pages if PAGE_TABLE_ISOLATION=y x86/mm/pti: Prepare the x86/entry assembly code for entry/exit CR3 switching x86/mm/pti: Add mapping helper functions
x86/mm/pti: Allow NX poison to be set in p4d/pgd
x86/mm/pti: Allocate a separate user PGD
x86/mm/pti: Populate user PGD
x86/mm: Allow flushing for future ASID switches
x86/mm: Abstract switching CR3
x86/mm: Use INVPCID for __native_flush_tlb_single()
x86/mm/pti: Add Kconfig
Hugh Dickins (1):
x86/events/intel/ds: Map debug buffers in cpu_entry_area
Peter Zijlstra (3):
x86/mm: Use/Fix PCID to optimize user/kernel switches x86/mm: Optimize RESTORE_CR3
x86/mm: Clarify the whole ASID/kernel PCID/user PCID naming
Thomas Gleixner (9):
x86/cpufeatures: Add X86_BUG_CPU_INSECURE
x86/mm/pti: Add infrastructure for page table isolation x86/mm/pti: Force entry through trampoline when PTI active x86/entry: Align entry text section to PMD boundary x86/mm/pti: Share entry text PMD
x86/cpu_entry_area: Add debugstore entries to cpu_entry_area x86/mm/dump_pagetables: Check user space page table for WX pages x86/mm/dump_pagetables: Allow dumping current pagetables x86/ldt: Make the LDT mapping RO
Vlastimil Babka (1):
x86/dumpstack: Indicate in Oops whether PTI is configured and enabled
Documentation/admin-guide/kernel-parameters.txt | 8 + Documentation/x86/x86_64/mm.txt | 5 +- arch/x86/boot/compressed/pagetable.c | 3 + arch/x86/entry/calling.h | 145 +++++++++ arch/x86/entry/entry_64.S | 48 ++- arch/x86/entry/entry_64_compat.S | 24 +- arch/x86/entry/vsyscall/vsyscall_64.c | 6 +- arch/x86/events/intel/ds.c | 130 +++++--- arch/x86/events/perf_event.h | 23 +- arch/x86/include/asm/cpu_entry_area.h | 13 + arch/x86/include/asm/cpufeatures.h | 4 +- arch/x86/include/asm/desc.h | 2 + arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/intel_ds.h | 36 +++ arch/x86/include/asm/mmu_context.h | 59 +++- arch/x86/include/asm/pgalloc.h | 11 + arch/x86/include/asm/pgtable.h | 30 +- arch/x86/include/asm/pgtable_64.h | 92 ++++++ arch/x86/include/asm/pgtable_64_types.h | 8 +- arch/x86/include/asm/processor-flags.h | 5 + arch/x86/include/asm/processor.h | 23 +- arch/x86/include/asm/pti.h | 14 + arch/x86/include/asm/tlbflush.h | 202 +++++++++++-- arch/x86/include/asm/vsyscall.h | 1 + arch/x86/include/uapi/asm/processor-flags.h | 7 +- arch/x86/kernel/asm-offsets.c | 4 + arch/x86/kernel/cpu/common.c | 9 +- arch/x86/kernel/dumpstack.c | 6 +- arch/x86/kernel/head_64.S | 30 +- arch/x86/kernel/ldt.c | 144 ++++++++- arch/x86/kernel/tls.c | 11 +- arch/x86/kernel/vmlinux.lds.S | 8 + arch/x86/mm/Makefile | 7 +- arch/x86/mm/cpu_entry_area.c | 27 ++ arch/x86/mm/debug_pagetables.c | 80 ++++- arch/x86/mm/dump_pagetables.c | 43 ++- arch/x86/mm/init.c | 80 +++-- arch/x86/mm/pgtable.c | 5 +- arch/x86/mm/pti.c | 387 ++++++++++++++++++++++++ arch/x86/mm/tlb.c | 58 +++- arch/x86/platform/efi/efi_64.c | 5 +- include/linux/pti.h | 11 + init/main.c | 3 + security/Kconfig | 10 + tools/testing/selftests/x86/ldt_gdt.c | 3 +- 45 files changed, 1636 insertions(+), 202 deletions(-) create mode 100644 arch/x86/include/asm/intel_ds.h create mode 100644 arch/x86/include/asm/pti.h create mode 100644 arch/x86/mm/pti.c create mode 100644 include/linux/pti.h
Documentation/admin-guide/kernel-parameters.txt +8 -0
@@ -2685,6 +2685,8 @@ steal time is computed, but won't influence scheduler behaviour + nopti [X86-64] Disable kernel page table isolation + nolapic [X86-32,APIC] Do not enable or use the local APIC. nolapic_timer [X86-32,APIC] Do not use the local APIC timer.
@@ -3253,6 +3255,12 @@ pt. [PARIDE] See Documentation/blockdev/paride.txt. + pti= [X86_64] + Control user/kernel address space isolation: + on - enable + off - disable + auto - default setting + pty.legacy_count= [KNL] Number of legacy pty's. Overwrites compiled-in default number.
Documentation/x86/x86_64/mm.txt +3 -2
@@ -12,6 +12,7 @@ ffffea0000000000 - ffffeaffffffffff (=40 bits) virtual memory map (1TB) ... unused hole ... ffffec0000000000 - fffffbffffffffff (=44 bits) kasan shadow memory (16TB) ... unused hole ... +fffffe0000000000 - fffffe7fffffffff (=39 bits) LDT remap for PTI fffffe8000000000 - fffffeffffffffff (=39 bits) cpu_entry_area mapping ffffff0000000000 - ffffff7fffffffff (=39 bits) %esp fixup stacks ... unused hole ...
@@ -29,8 +30,8 @@ Virtual memory map with 5 level page tables: hole caused by [56:63] sign extension ff00000000000000 - ff0fffffffffffff (=52 bits) guard hole, reserved for hypervisor ff10000000000000 - ff8fffffffffffff (=55 bits) direct mapping of all phys. memory -ff90000000000000 - ff91ffffffffffff (=49 bits) hole -ff92000000000000 - ffd1ffffffffffff (=54 bits) vmalloc/ioremap space +ff90000000000000 - ff9fffffffffffff (=52 bits) LDT remap for PTI +ffa0000000000000 - ffd1ffffffffffff (=54 bits) vmalloc/ioremap space (12800 TB) ffd2000000000000 - ffd3ffffffffffff (=49 bits) hole ffd4000000000000 - ffd5ffffffffffff (=49 bits) virtual memory map (512TB) ... unused hole ...
arch/x86/boot/compressed/pagetable.c +3 -0
@@ -23,6 +23,9 @@*/ #undef CONFIG_AMD_MEM_ENCRYPT +/* No PAGE_TABLE_ISOLATION support needed either: */ +#undef CONFIG_PAGE_TABLE_ISOLATION +#include "misc.h" /* These actually do the work of building the kernel identity maps. */
arch/x86/entry/calling.h +145 -0
157 lines not on this page · raw message
arch/x86/entry/entry_64.S +41 -7
158 lines not on this page · raw message
arch/x86/entry/entry_64_compat.S +23 -1
@@ -49,6 +49,10 @@ ENTRY(entry_SYSENTER_compat) /* Interrupts are off on entry. */ SWAPGS + + /* We are about to clobber %rsp anyway, clobbering here is OK */ + SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp + movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp /*
@@ -216,6 +220,12 @@ GLOBAL(entry_SYSCALL_compat_after_hwframe) pushq $0 /* pt_regs->r15 = 0 */ /* + * We just saved %rdi so it is safe to clobber. It is not + * preserved during the C calls inside TRACE_IRQS_OFF anyway. + */ + SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi + + /* * User mode is traced as though IRQs are on, and SYSENTER * turned them off. */
@@ -256,10 +266,22 @@ sysret32_from_system_call: * when the system call started, which is already known to user * code. We zero R8-R10 to avoid info leaks. */ + movq RSP-ORIG_RAX(%rsp), %rsp + + /* + * The original userspace %rsp (RSP-ORIG_RAX(%rsp)) is stored + * on the process stack which is not mapped to userspace and + * not readable after we SWITCH_TO_USER_CR3. Delay the CR3 + * switch until after after the last reference to the process + * stack. + * + * %r8/%r9 are zeroed before the sysret, thus safe to clobber. + */ + SWITCH_TO_USER_CR3_NOSTACK scratch_reg=%r8 scratch_reg2=%r9 + xorq %r8, %r8 xorq %r9, %r9 xorq %r10, %r10 - movq RSP-ORIG_RAX(%rsp), %rsp swapgs sysretl END(entry_SYSCALL_compat)
arch/x86/entry/vsyscall/vsyscall_64.c +3 -3
@@ -344,14 +344,14 @@ int in_gate_area_no_mm(unsigned long addr)* vsyscalls but leave the page not present. If so, we skip calling * this. */ -static void __init set_vsyscall_pgtable_user_bits (void ) +void __init set_vsyscall_pgtable_user_bits (pgd_t *root) {pgd_t *pgd;p4d_t *p4d;pud_t *pud;pmd_t *pmd; - pgd =pgd_offset_k (VSYSCALL_ADDR ); + pgd =pgd_offset_pgd (root,VSYSCALL_ADDR );set_pgd (pgd,__pgd (pgd_val (*pgd) | _PAGE_USER)); p4d =p4d_offset (pgd,VSYSCALL_ADDR );#if CONFIG_PGTABLE_LEVELS >=5
@@ -373,7 +373,7 @@ void __init map_vsyscall(void) vsyscall_mode ==NATIVE ?PAGE_KERNEL_VSYSCALL : PAGE_KERNEL_VVAR); -set_vsyscall_pgtable_user_bits (); +set_vsyscall_pgtable_user_bits (swapper_pg_dir); } BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE) !=
arch/x86/events/intel/ds.c +83 -47
214 lines not on this page · raw message
arch/x86/events/perf_event.h +4 -19
@@ -14,6 +14,8 @@#include <linux/perf_event.h> +#include <asm/intel_ds.h> +/* To enable MSR tracing please use the generic trace points. */ /*
@@ -77,8 +79,6 @@ struct amd_nb {struct event_constraint event_constraints[X86_PMC_IDX_MAX ]; }; -/* The maximal number of PEBS events: */ -#define MAX_PEBS_EVENTS 8#define PEBS_COUNTER_MASK ((1ULL << MAX_PEBS_EVENTS) - 1)/*
@@ -95,23 +95,6 @@ struct amd_nb { PERF_SAMPLE_TRANSACTION | PERF_SAMPLE_PHYS_ADDR | \ PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_REGS_USER) -/* -* A debug store configuration. -* -* We only support architectures that use 64bit fields. -*/ -struct debug_store { -u64 bts_buffer_base; -u64 bts_index; -u64 bts_absolute_maximum; -u64 bts_interrupt_threshold; -u64 pebs_buffer_base; -u64 pebs_index; -u64 pebs_absolute_maximum; -u64 pebs_interrupt_threshold; -u64 pebs_event_reset[MAX_PEBS_EVENTS ]; -}; -#define PEBS_REGS \ (PERF_REG_X86_AX | \ PERF_REG_X86_BX | \
@@ -216,6 +199,8 @@ struct cpu_hw_events {* Intel DebugStore bits */ struct debug_store *ds; +void *ds_pebs_vaddr; +void *ds_bts_vaddr;u64 pebs_enabled;int n_pebs;int n_large_pebs;
arch/x86/include/asm/cpu_entry_area.h +13 -0
@@ -5,6 +5,7 @@#include <linux/percpu-defs.h> #include <asm/processor.h> +#include <asm/intel_ds.h> /* * cpu_entry_area is a percpu region that contains things needed by the CPU
@@ -40,6 +41,18 @@ struct cpu_entry_area {*/ char exception_stacks[(N_EXCEPTION_STACKS -1 ) *EXCEPTION_STKSZ +DEBUG_STKSZ ];#endif +#ifdef CONFIG_CPU_SUP_INTEL +/* +* Per CPU debug store for Intel performance monitoring. Wastes a +* full page at the moment. +*/ +struct debug_store cpu_debug_store; +/* +* The actual PEBS/BTS buffers must be mapped to user space +* Reserve enough fixmap PTEs. +*/ +struct debug_store_buffers cpu_debug_buffers; +#endif };#define CPU_ENTRY_AREA_SIZE (sizeof(struct cpu_entry_area))
arch/x86/include/asm/cpufeatures.h +3 -1
@@ -197,11 +197,12 @@#define X86_FEATURE_CAT_L3 ( 7*32+ 4)/* Cache Allocation Technology L3 */ #define X86_FEATURE_CAT_L2 ( 7*32+ 5)/* Cache Allocation Technology L2 */ #define X86_FEATURE_CDP_L3 ( 7*32+ 6)/* Code and Data Prioritization L3 */ +#define X86_FEATURE_INVPCID_SINGLE ( 7*32+ 7)/* Effectively INVPCID && CR4.PCIDE=1 */ #define X86_FEATURE_HW_PSTATE ( 7*32+ 8)/* AMD HW-PState */ #define X86_FEATURE_PROC_FEEDBACK ( 7*32+ 9)/* AMD ProcFeedbackInterface */ #define X86_FEATURE_SME ( 7*32+10)/* AMD Secure Memory Encryption */ - +#define X86_FEATURE_PTI ( 7*32+11)/* Kernel Page Table Isolation enabled */ #define X86_FEATURE_INTEL_PPIN ( 7*32+14)/* Intel Processor Inventory Number */ #define X86_FEATURE_INTEL_PT ( 7*32+15)/* Intel Processor Trace */ #define X86_FEATURE_AVX512_4VNNIW ( 7*32+16)/* AVX-512 Neural Network Instructions */
@@ -340,5 +341,6 @@#define X86_BUG_SWAPGS_FENCE X86_BUG(11)/* SWAPGS without input dep on GS */ #define X86_BUG_MONITOR X86_BUG(12)/* IPI required to wake up remote CPU */ #define X86_BUG_AMD_E400 X86_BUG(13)/* CPU is among the affected by Erratum 400 */ +#define X86_BUG_CPU_INSECURE X86_BUG(14)/* CPU is insecure and needs kernel page table isolation */ #endif /* _ASM_X86_CPUFEATURES_H */
arch/x86/include/asm/desc.h +2 -0
@@ -21,6 +21,8 @@ static inline void fill_ldt(struct desc_struct *desc, const struct user_desc *in desc->type = (info->read_exec_only ^1 ) <<1 ; desc->type |= info->contents <<2 ; +/* Set the ACCESS bit so it can be mapped RO */ + desc->type |=1 ; desc->s =1 ; desc->dpl =0x3 ;
arch/x86/include/asm/disabled-features.h +7 -1
@@ -44,6 +44,12 @@# define DISABLE_LA57 (1<<(X86_FEATURE_LA57 & 31))#endif +#ifdef CONFIG_PAGE_TABLE_ISOLATION +# define DISABLE_PTI 0 +#else +# define DISABLE_PTI (1 << (X86_FEATURE_PTI & 31)) +#endif +/* * Make sure to add features to the correct mask */
@@ -54,7 +60,7 @@#define DISABLED_MASK4 (DISABLE_PCID)#define DISABLED_MASK5 0#define DISABLED_MASK6 0 -#define DISABLED_MASK7 0 +#define DISABLED_MASK7 (DISABLE_PTI)#define DISABLED_MASK8 0#define DISABLED_MASK9 (DISABLE_MPX)#define DISABLED_MASK10 0
arch/x86/include/asm/intel_ds.h new file +36 -0
@@ -0,0 +1,36 @@ +#ifndef _ASM_INTEL_DS_H +#define _ASM_INTEL_DS_H + +#include <linux/percpu-defs.h> + +#define BTS_BUFFER_SIZE (PAGE_SIZE << 4) +#define PEBS_BUFFER_SIZE (PAGE_SIZE << 4) + +/* The maximal number of PEBS events: */ +#define MAX_PEBS_EVENTS 8 + +/* +* A debug store configuration. +* +* We only support architectures that use 64bit fields. +*/ +struct debug_store { +u64 bts_buffer_base; +u64 bts_index; +u64 bts_absolute_maximum; +u64 bts_interrupt_threshold; +u64 pebs_buffer_base; +u64 pebs_index; +u64 pebs_absolute_maximum; +u64 pebs_interrupt_threshold; +u64 pebs_event_reset[MAX_PEBS_EVENTS ]; +}__aligned(PAGE_SIZE) ; + +DECLARE_PER_CPU_PAGE_ALIGNED(struct debug_store, cpu_debug_store); + +struct debug_store_buffers { +char bts_buffer[BTS_BUFFER_SIZE ]; +char pebs_buffer[PEBS_BUFFER_SIZE ]; +}; + +#endif
arch/x86/include/asm/mmu_context.h +53 -6
92 lines not on this page · raw message
arch/x86/include/asm/pgalloc.h +11 -0
@@ -30,6 +30,17 @@ static inline void paravirt_release_p4d(unsigned long pfn) {}*/ extern gfp_t __userpte_alloc_gfp; +#ifdef CONFIG_PAGE_TABLE_ISOLATION +/* +* Instead of one PGD, we acquire two PGDs. Being order-1, it is +* both 8k in size and 8k-aligned. That lets us just flip bit 12 +* in a pointer to swap between the two 4k halves. +*/ +#define PGD_ALLOCATION_ORDER 1 +#else +#define PGD_ALLOCATION_ORDER 0 +#endif +/* * Allocate and free page tables. */
arch/x86/include/asm/pgtable.h +26 -4
@@ -28,6 +28,7 @@ extern pgd_t early_top_pgt[PTRS_PER_PGD];int __init __early_make_pgtable (unsigned long address,pmdval_t pmd);void ptdump_walk_pgd_level (struct seq_file *m,pgd_t *pgd); +void ptdump_walk_pgd_level_debugfs (struct seq_file *m,pgd_t *pgd,bool user);void ptdump_walk_pgd_level_checkwx (void );#ifdef CONFIG_DEBUG_WX
@@ -846,7 +847,12 @@ static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)static inline int p4d_bad (p4d_t p4d) { -return (p4d_flags (p4d) & ~(_KERNPG_TABLE | _PAGE_USER)) !=0 ; +unsigned long ignore_flags = _KERNPG_TABLE | _PAGE_USER; + +if (IS_ENABLED (CONFIG_PAGE_TABLE_ISOLATION )) + ignore_flags |= _PAGE_NX; + +return (p4d_flags (p4d) & ~ignore_flags) !=0 ; }#endif /* CONFIG_PGTABLE_LEVELS > 3 */
@@ -880,7 +886,12 @@ static inline p4d_t *p4d_offset(pgd_t *pgd, unsigned long address)static inline int pgd_bad (pgd_t pgd) { -return (pgd_flags (pgd) & ~_PAGE_USER) != _KERNPG_TABLE; +unsigned long ignore_flags = _PAGE_USER; + +if (IS_ENABLED (CONFIG_PAGE_TABLE_ISOLATION )) + ignore_flags |= _PAGE_NX; + +return (pgd_flags (pgd) & ~ignore_flags) != _KERNPG_TABLE; }static inline int pgd_none (pgd_t pgd)
@@ -909,7 +920,11 @@ static inline int pgd_none(pgd_t pgd)* pgd_offset() returns a (pgd_t *) * pgd_index() is used get the offset into the pgd page's array of pgd_t's; */ -#define pgd_offset (mm, address) ((mm)->pgd + pgd_index((address))) +#define pgd_offset_pgd (pgd, address) (pgd + pgd_index((address))) +/* +* a shortcut to get a pgd_t in a given mm +*/ +#define pgd_offset (mm, address) pgd_offset_pgd((mm)->pgd, (address))/* * a shortcut which implies the use of the kernel's pgd, instead * of a process's
@@ -1105,7 +1120,14 @@ static inline void pmdp_set_wrprotect(struct mm_struct *mm,*/ static inline void clone_pgd_range (pgd_t *dst,pgd_t *src,int count) { -memcpy (dst, src, count *sizeof (pgd_t)); +memcpy (dst, src, count *sizeof (pgd_t)); +#ifdef CONFIG_PAGE_TABLE_ISOLATION +if (!static_cpu_has (X86_FEATURE_PTI )) +return ; +/* Clone the user space pgd as well */ +memcpy (kernel_to_user_pgdp (dst),kernel_to_user_pgdp (src), + count *sizeof (pgd_t)); +#endif }#define PTE_SHIFT ilog2(PTRS_PER_PTE)
arch/x86/include/asm/pgtable_64.h +92 -0
108 lines not on this page · raw message
arch/x86/include/asm/pgtable_64_types.h +6 -2
@@ -79,13 +79,17 @@ typedef struct { pteval_t pte; } pte_t;#define MAXMEM _AC(__AC(1, UL) << MAX_PHYSMEM_BITS, UL)#ifdef CONFIG_X86_5LEVEL -# define VMALLOC_SIZE_TB _AC(16384, UL) -# define __VMALLOC_BASE _AC(0xff92000000000000, UL) +# define VMALLOC_SIZE_TB _AC(12800, UL) +# define __VMALLOC_BASE _AC(0xffa0000000000000, UL)# define __VMEMMAP_BASE _AC(0xffd4000000000000, UL) +# define LDT_PGD_ENTRY _AC(-112, UL) +# define LDT_BASE_ADDR (LDT_PGD_ENTRY << PGDIR_SHIFT)#else # define VMALLOC_SIZE_TB _AC(32, UL)# define __VMALLOC_BASE _AC(0xffffc90000000000, UL)# define __VMEMMAP_BASE _AC(0xffffea0000000000, UL) +# define LDT_PGD_ENTRY _AC(-4, UL) +# define LDT_BASE_ADDR (LDT_PGD_ENTRY << PGDIR_SHIFT)#endif #ifdef CONFIG_RANDOMIZE_MEMORY
arch/x86/include/asm/processor-flags.h +5 -0
@@ -38,6 +38,11 @@#define CR3_ADDR_MASK __sme_clr(0x7FFFFFFFFFFFF000ull)#define CR3_PCID_MASK 0xFFFull#define CR3_NOFLUSH BIT_ULL(63) + +#ifdef CONFIG_PAGE_TABLE_ISOLATION +# define X86_CR3_PTI_SWITCH_BIT 11 +#endif +#else /* * CR3_ADDR_MASK needs at least bits 31:5 set on PAE systems, and we save
arch/x86/include/asm/processor.h +16 -7
@@ -851,13 +851,22 @@ static inline void spin_lock_prefetch(const void *x)#else /* -* User space process size. 47bits minus one guard page. The guard -* page is necessary on Intel CPUs: if a SYSCALL instruction is at -* the highest possible canonical userspace address, then that -* syscall will enter the kernel with a non-canonical return -* address, and SYSRET will explode dangerously. We avoid this -* particular problem by preventing anything from being mapped -* at the maximum canonical address. +* User space process size. This is the first address outside the user range. +* There are a few constraints that determine this: +* +* On Intel CPUs, if a SYSCALL instruction is at the highest canonical +* address, then that syscall will enter the kernel with a +* non-canonical return address, and SYSRET will explode dangerously. +* We avoid this particular problem by preventing anything executable +* from being mapped at the maximum canonical address. +* +* On AMD CPUs in the Ryzen family, there's a nasty bug in which the +* CPUs malfunction if they execute code from the highest canonical page. +* They'll speculate right off the end of the canonical space, and +* bad things happen. This is worked around in the same way as the +* Intel problem. +* +* With page table isolation enabled, we map the LDT in ... [stay tuned] */ #define TASK_SIZE_MAX ((1UL << __VIRTUAL_MASK_SHIFT) - PAGE_SIZE)
arch/x86/include/asm/pti.h new file +14 -0
@@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef _ASM_X86_PTI_H +#define _ASM_X86_PTI_H +#ifndef __ASSEMBLY__ + +#ifdef CONFIG_PAGE_TABLE_ISOLATION +extern void pti_init (void ); +extern void pti_check_boottime_disable (void ); +#else +static inline void pti_check_boottime_disable (void ) { } +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* _ASM_X86_PTI_H */
arch/x86/include/asm/tlbflush.h +171 -31
292 lines not on this page · raw message
arch/x86/include/asm/vsyscall.h +1 -0
@@ -7,6 +7,7 @@#ifdef CONFIG_X86_VSYSCALL_EMULATION extern void map_vsyscall (void ); +extern void set_vsyscall_pgtable_user_bits (pgd_t *root);/* * Called on instruction fetch fault in vsyscall page.
arch/x86/include/uapi/asm/processor-flags.h +6 -1
@@ -78,7 +78,12 @@#define X86_CR3_PWT _BITUL(X86_CR3_PWT_BIT)#define X86_CR3_PCD_BIT 4/* Page Cache Disable */ #define X86_CR3_PCD _BITUL(X86_CR3_PCD_BIT) -#define X86_CR3_PCID_MASK _AC(0x00000fff,UL)/* PCID Mask */ + +#define X86_CR3_PCID_BITS 12 +#define X86_CR3_PCID_MASK (_AC((1UL << X86_CR3_PCID_BITS) - 1, UL)) + +#define X86_CR3_PCID_NOFLUSH_BIT 63/* Preserve old PCID */ +#define X86_CR3_PCID_NOFLUSH _BITULL(X86_CR3_PCID_NOFLUSH_BIT)/* * Intel CPU features in CR4
arch/x86/kernel/asm-offsets.c +4 -0
@@ -17,6 +17,7 @@#include <asm/sigframe.h> #include <asm/bootparam.h> #include <asm/suspend.h> +#include <asm/tlbflush.h> #ifdef CONFIG_XEN #include <xen/interface/xen.h>
@@ -94,6 +95,9 @@ void common(void) {BLANK ();DEFINE (PTREGS_SIZE ,sizeof (struct pt_regs )); +/* TLB state for the entry code */ +OFFSET (TLB_STATE_user_pcid_flush_mask, tlb_state, user_pcid_flush_mask); +/* Layout info for cpu_entry_area */ OFFSET (CPU_ENTRY_AREA_tss, cpu_entry_area, tss);OFFSET (CPU_ENTRY_AREA_entry_trampoline, cpu_entry_area, entry_trampoline);
arch/x86/kernel/cpu/common.c +8 -1
@@ -898,6 +898,10 @@ static void __init early_identify_cpu(struct cpuinfo_x86 *c) }setup_force_cpu_cap (X86_FEATURE_ALWAYS ); + +/* Assume for now that ALL x86 CPUs are insecure */ +setup_force_cpu_bug (X86_BUG_CPU_INSECURE ); +fpu__init_system (c);#ifdef CONFIG_X86_32
@@ -1335,7 +1339,10 @@ void syscall_init(void) (entry_SYSCALL_64_trampoline - _entry_trampoline);wrmsr (MSR_STAR ,0 , (__USER32_CS <<16 ) | __KERNEL_CS); -wrmsrl (MSR_LSTAR , SYSCALL64_entry_trampoline); +if (static_cpu_has (X86_FEATURE_PTI )) +wrmsrl (MSR_LSTAR , SYSCALL64_entry_trampoline); +else +wrmsrl (MSR_LSTAR , (unsigned long )entry_SYSCALL_64);#ifdef CONFIG_IA32_EMULATION wrmsrl (MSR_CSTAR , (unsigned long )entry_SYSCALL_compat);
arch/x86/kernel/dumpstack.c +4 -2
@@ -297,11 +297,13 @@ int __die(const char *str, struct pt_regs *regs, long err)unsigned long sp;#endif printk (KERN_DEFAULT -"%s: %04lx [#%d]%s%s%s%s\n" , str, err &0xffff , ++die_counter, +"%s: %04lx [#%d]%s%s%s%s%s\n" , str, err &0xffff , ++die_counter,IS_ENABLED (CONFIG_PREEMPT ) ?" PREEMPT" :"" ,IS_ENABLED (CONFIG_SMP ) ?" SMP" :"" ,debug_pagealloc_enabled () ?" DEBUG_PAGEALLOC" :"" , -IS_ENABLED (CONFIG_KASAN ) ?" KASAN" :"" ); +IS_ENABLED (CONFIG_KASAN ) ?" KASAN" :"" , +IS_ENABLED (CONFIG_PAGE_TABLE_ISOLATION ) ? + (boot_cpu_has (X86_FEATURE_PTI ) ?" PTI" :" NOPTI" ) :"" );if (notify_die (DIE_OOPS , str, regs, err, current->thread.trap_nr,SIGSEGV ) ==NOTIFY_STOP )
arch/x86/kernel/head_64.S +27 -3
@@ -341,6 +341,27 @@ GLOBAL(early_recursion_flag) .balign PAGE_SIZE; \ GLOBAL(name) +#ifdef CONFIG_PAGE_TABLE_ISOLATION +/* + * Each PGD needs to be 8k long and 8k aligned. We do not + * ever go out to userspace with these, so we do not + * strictly *need* the second page, but this allows us to + * have a single set_pgd() implementation that does not + * need to worry about whether it has 4k or 8k to work + * with. + * + * This ensures PGDs are 8k long: + */ +#define PTI_USER_PGD_FILL 512 +/* This ensures they are 8k-aligned: */ +#define NEXT_PGD_PAGE(name) \ + .balign 2 * PAGE_SIZE; \ +GLOBAL(name) +#else +#define NEXT_PGD_PAGE(name) NEXT_PAGE(name) +#define PTI_USER_PGD_FILL 0 +#endif + /* Automate the creation of 1 to 1 mapping pmd entries */ #define PMDS(START, PERM, COUNT) \ i = 0 ; \
@@ -350,13 +371,14 @@ GLOBAL(name) .endr __INITDATA -NEXT_PAGE(early_top_pgt) +NEXT_PGD_PAGE(early_top_pgt) .fill 511,8,0 #ifdef CONFIG_X86_5LEVEL .quad level4_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC #else .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC #endif + .fill PTI_USER_PGD_FILL,8,0 NEXT_PAGE(early_dynamic_pgts) .fill 512*EARLY_DYNAMIC_PAGE_TABLES,8,0
@@ -364,13 +386,14 @@ NEXT_PAGE(early_dynamic_pgts) .data #if defined(CONFIG_XEN_PV) || defined(CONFIG_XEN_PVH) -NEXT_PAGE(init_top_pgt) +NEXT_PGD_PAGE(init_top_pgt) .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC .org init_top_pgt + PGD_PAGE_OFFSET*8, 0 .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC .org init_top_pgt + PGD_START_KERNEL*8, 0 /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */ .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC + .fill PTI_USER_PGD_FILL,8,0 NEXT_PAGE(level3_ident_pgt) .quad level2_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC
@@ -381,8 +404,9 @@ NEXT_PAGE(level2_ident_pgt) */ PMDS(0, __PAGE_KERNEL_IDENT_LARGE_EXEC, PTRS_PER_PMD) #else -NEXT_PAGE(init_top_pgt) +NEXT_PGD_PAGE(init_top_pgt) .fill 512,8,0 + .fill PTI_USER_PGD_FILL,8,0 #endif #ifdef CONFIG_X86_5LEVEL
arch/x86/kernel/ldt.c +141 -3
188 lines not on this page · raw message
arch/x86/kernel/tls.c +2 -9
@@ -93,17 +93,10 @@ static void set_tls_desc(struct task_struct *p, int idx, cpu =get_cpu ();while (n-- >0 ) { -if (LDT_empty (info) ||LDT_zero (info)) { +if (LDT_empty (info) ||LDT_zero (info))memset (desc,0 ,sizeof (*desc)); - }else { +else fill_ldt (desc, info); - -/* -* Always set the accessed bit so that the CPU -* doesn't try to write to the (read-only) GDT. -*/ - desc->type |=1 ; - } ++info; ++desc; }
arch/x86/kernel/vmlinux.lds.S +8 -0
@@ -61,11 +61,17 @@ jiffies_64 = jiffies; . = ALIGN(HPAGE_SIZE); \ __end_rodata_hpage_align = .; +#define ALIGN_ENTRY_TEXT_BEGIN . = ALIGN(PMD_SIZE); +#define ALIGN_ENTRY_TEXT_END . = ALIGN(PMD_SIZE); + #else #define X64_ALIGN_RODATA_BEGIN #define X64_ALIGN_RODATA_END +#define ALIGN_ENTRY_TEXT_BEGIN +#define ALIGN_ENTRY_TEXT_END + #endif PHDRS {
@@ -102,8 +108,10 @@ SECTIONS CPUIDLE_TEXT LOCK_TEXT KPROBES_TEXT + ALIGN_ENTRY_TEXT_BEGIN ENTRY_TEXT IRQENTRY_TEXT + ALIGN_ENTRY_TEXT_END SOFTIRQENTRY_TEXT *(.fixup) *(.gnu.warning)
arch/x86/mm/Makefile +4 -3
@@ -43,9 +43,10 @@ obj-$(CONFIG_AMD_NUMA) += amdtopology.o obj-$(CONFIG_ACPI_NUMA) += srat.o obj-$(CONFIG_NUMA_EMU) += numa_emulation.o -obj-$(CONFIG_X86_INTEL_MPX) += mpx.o -obj-$(CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS) += pkeys.o -obj-$(CONFIG_RANDOMIZE_MEMORY) += kaslr.o +obj-$(CONFIG_X86_INTEL_MPX) += mpx.o +obj-$(CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS) += pkeys.o +obj-$(CONFIG_RANDOMIZE_MEMORY) += kaslr.o +obj-$(CONFIG_PAGE_TABLE_ISOLATION) += pti.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_boot.o
arch/x86/mm/cpu_entry_area.c +27 -0
@@ -38,6 +38,32 @@ cea_map_percpu_pages(void *cea_vaddr, void *ptr, int pages, pgprot_t prot)cea_set_pte (cea_vaddr,per_cpu_ptr_to_phys (ptr), prot); } +static void percpu_setup_debug_store (int cpu) +{ +#ifdef CONFIG_CPU_SUP_INTEL +int npages; +void *cea; + +if (boot_cpu_data.x86_vendor !=X86_VENDOR_INTEL ) +return ; + + cea = &get_cpu_entry_area (cpu)->cpu_debug_store; + npages =sizeof (struct debug_store ) /PAGE_SIZE ; +BUILD_BUG_ON (sizeof (struct debug_store ) %PAGE_SIZE !=0 ); +cea_map_percpu_pages (cea, &per_cpu (cpu_debug_store, cpu), npages, +PAGE_KERNEL ); + + cea = &get_cpu_entry_area (cpu)->cpu_debug_buffers; +/* +* Force the population of PMDs for not yet allocated per cpu +* memory like debug store buffers. +*/ + npages =sizeof (struct debug_store_buffers ) /PAGE_SIZE ; +for (; npages; npages--, cea +=PAGE_SIZE ) +cea_set_pte (cea,0 ,PAGE_NONE ); +#endif +} +/* Setup the fixmap mappings only once per-processor */ static void __init setup_cpu_entry_area (int cpu) {
@@ -109,6 +135,7 @@ static void __init setup_cpu_entry_area(int cpu)cea_set_pte (&get_cpu_entry_area (cpu)->entry_trampoline,__pa_symbol (_entry_trampoline),PAGE_KERNEL_RX );#endif +percpu_setup_debug_store (cpu); }static __init void setup_cpu_entry_area_ptes (void )
arch/x86/mm/debug_pagetables.c +74 -6
102 lines not on this page · raw message
arch/x86/mm/dump_pagetables.c +38 -5
83 lines not on this page · raw message
arch/x86/mm/init.c +49 -31
119 lines not on this page · raw message
arch/x86/mm/pgtable.c +3 -2
@@ -355,14 +355,15 @@ static inline void _pgd_free(pgd_t *pgd) kmem_cache_free(pgd_cache, pgd); }#else +static inline pgd_t *_pgd_alloc (void ) { -return (pgd_t *)__get_free_page (PGALLOC_GFP ); +return (pgd_t *)__get_free_pages (PGALLOC_GFP ,PGD_ALLOCATION_ORDER ); }static inline void _pgd_free (pgd_t *pgd) { -free_page ((unsigned long )pgd); +free_pages ((unsigned long )pgd,PGD_ALLOCATION_ORDER ); }#endif /* CONFIG_X86_PAE */
arch/x86/mm/pti.c new file +387 -0
387 lines not on this page · raw message
arch/x86/mm/tlb.c +56 -2
88 lines not on this page · raw message
arch/x86/platform/efi/efi_64.c +4 -1
@@ -195,6 +195,9 @@ static pgd_t *efi_pgd;* because we want to avoid inserting EFI region mappings (EFI_VA_END * to EFI_VA_START) into the standard kernel page tables. Everything * else can be shared, see efi_sync_low_kernel_mappings(). +* +* We don't want the pgd on the pgd_list and cannot use pgd_alloc() for the +* allocation. */ int __init efi_alloc_page_tables (void ) {
@@ -207,7 +210,7 @@ int __init efi_alloc_page_tables(void)return 0 ; gfp_mask =GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO; - efi_pgd = (pgd_t *)__get_free_page (gfp_mask); + efi_pgd = (pgd_t *)__get_free_pages (gfp_mask,PGD_ALLOCATION_ORDER );if (!efi_pgd)return -ENOMEM ;
include/linux/pti.h new file +11 -0
@@ -0,0 +1,11 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef _INCLUDE_PTI_H +#define _INCLUDE_PTI_H + +#ifdef CONFIG_PAGE_TABLE_ISOLATION +#include <asm/pti.h> +#else +static inline void pti_init (void ) { } +#endif + +#endif
init/main.c +3 -0
@@ -75,6 +75,7 @@#include <linux/slab.h> #include <linux/perf_event.h> #include <linux/ptrace.h> +#include <linux/pti.h> #include <linux/blkdev.h> #include <linux/elevator.h> #include <linux/sched_clock.h>
@@ -506,6 +507,8 @@ static void __init mm_init(void)ioremap_huge_init ();/* Should be run before the first non-init thread is created */ init_espfix_bsp (); +/* Should be run after espfix64 is set up. */ +pti_init (); } asmlinkage __visible void __init start_kernel(void)
security/Kconfig +10 -0
@@ -54,6 +54,16 @@ config SECURITY_NETWORK implement socket and networking access controls. If you are unsure how to answer this question, answer N. +config PAGE_TABLE_ISOLATION + bool "Remove the kernel mapping in user mode" + depends on X86_64 && !UML + help + This feature reduces the number of hardware side channels by + ensuring that the majority of kernel addresses are not mapped + into userspace. + + See Documentation/x86/pagetable-isolation.txt for more details. + config SECURITY_INFINIBAND bool "Infiniband Security Hooks" depends on SECURITY && INFINIBAND
tools/testing/selftests/x86/ldt_gdt.c +1 -2
@@ -122,8 +122,7 @@ static void check_valid_segment(uint16_t index, int ldt,* NB: Different Linux versions do different things with the * accessed bit in set_thread_area(). */ -if (ar != expected_ar && - (ldt || ar != (expected_ar |AR_ACCESSED ))) { +if (ar != expected_ar && ar != (expected_ar |AR_ACCESSED )) {printf ("[FAIL]\t%s entry %hu has AR 0x%08X but expected 0x%08X\n" , (ldt ?"LDT" :"GDT" ), index, ar, expected_ar); nerrs++;
So we have an entirely new memory model and this is sent after -rc5?
That's not really how things are supposed to work, are they?
Quoted 6 lines · Thomas Gleixner
please pull the latest x86-pti-for-linus git tree from:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.gitx86-pti-for-linusThis is the final set of enabling page table isolation on x86:
Ok, after that late MCORE2 scare that held things up, this is now merged in my tree and pushed out. "WorksForMe(tm)".
Linus
Quoted 10 lines · Linus Torvalds
Quoted 6 lines · Thomas Gleixner
please pull the latest x86-pti-for-linus git tree from:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.gitx86-pti-for-linusThis is the final set of enabling page table isolation on x86:
Ok, after that late MCORE2 scare that held things up, this is now merged in my tree and pushed out. "WorksForMe(tm)".
But it definitely ain't free. NFS hanging out in the kernel for 82ms at a whack may not be such a bad idea after all ;-)
i4790+tbench
CONFIG_PAGE_TABLE_ISOLATION=y
Throughput 3467.72 MB/sec 8 clients 8 procs max_latency=3.499 ms
CONFIG_PAGE_TABLE_ISOLATION=n
Throughput 3700.81 MB/sec 8 clients 8 procs max_latency=4.893 ms
CONFIG_PAGE_TABLE_ISOLATION=y CONFIG_PAGE_TABLE_ISOLATION=n 4.48% [k] syscall_return_via_sysret 3.56% [k] copy_user_enhanced_fast_string 3.41% [k] copy_user_enhanced_fast_string 2.54% [.] child_run 2.48% [.] child_run 1.76% [k] tcp_transmit_skb 1.64% [.] __strchr_sse2 1.74% [k] tcp_recvmsg 1.60% [k] tcp_transmit_skb 1.69% [.] __strchr_sse2 1.55% [k] tcp_recvmsg 1.56% [.] __GI_____strtoll_l_internal 1.46% [k] ipt_do_table 1.44% [k] ipt_do_table 1.45% [.] __GI_____strtoll_l_internal 1.42% [k] tcp_sendmsg_locked 1.25% [k] tcp_in_window 1.34% [k] tcp_write_xmit 1.21% [.] next_token 1.34% [k] tcp_clean_rtx_queue 1.17% [k] tcp_clean_rtx_queue 1.26% [.] next_token 1.16% [k] tcp_sendmsg_locked 1.24% [k] tcp_in_window 1.15% [k] __switch_to 1.19% [k] nf_conntrack_in 1.10% [k] switch_mm_irqs_off 1.17% [k] __switch_to 1.09% [k] tcp_write_xmit 1.13% [.] __strcasecmp_l_avx 1.09% [k] tcp_ack 1.10% [k] switch_mm_irqs_off 1.08% [k] nf_hook_slow 1.08% [k] tcp_v4_rcv 1.04% [k] __sched_text_start 1.06% [k] nf_hook_slow 0.99% [.] __strcasecmp_l_avx 1.05% [k] tcp_ack
Le 30/12/17 à 10:04, Mike Galbraith a écrit :
Quoted 35 lines · Linus Torvalds
Quoted 10 lines · Thomas Gleixner
Quoted 6 lines
please pull the latest x86-pti-for-linus git tree from:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.gitx86-pti-for-linusThis is the final set of enabling page table isolation on x86:
Ok, after that late MCORE2 scare that held things up, this is now merged in my tree and pushed out. "WorksForMe(tm)".
But it definitely ain't free. NFS hanging out in the kernel for 82ms at a whack may not be such a bad idea after all ;-)
i4790+tbench
CONFIG_PAGE_TABLE_ISOLATION=y
Throughput 3467.72 MB/sec 8 clients 8 procs max_latency=3.499 ms
CONFIG_PAGE_TABLE_ISOLATION=n
Throughput 3700.81 MB/sec 8 clients 8 procs max_latency=4.893 msCONFIG_PAGE_TABLE_ISOLATION=y CONFIG_PAGE_TABLE_ISOLATION=n 4.48% [k] syscall_return_via_sysret 3.56% [k] copy_user_enhanced_fast_string 3.41% [k] copy_user_enhanced_fast_string 2.54% [.] child_run 2.48% [.] child_run 1.76% [k] tcp_transmit_skb 1.64% [.] __strchr_sse2 1.74% [k] tcp_recvmsg 1.60% [k] tcp_transmit_skb 1.69% [.] __strchr_sse2 1.55% [k] tcp_recvmsg 1.56% [.] __GI_____strtoll_l_internal 1.46% [k] ipt_do_table 1.44% [k] ipt_do_table 1.45% [.] __GI_____strtoll_l_internal 1.42% [k] tcp_sendmsg_locked 1.25% [k] tcp_in_window 1.34% [k] tcp_write_xmit 1.21% [.] next_token 1.34% [k] tcp_clean_rtx_queue 1.17% [k] tcp_clean_rtx_queue 1.26% [.] next_token
Quoted 9 lines
1.16% [k] tcp_sendmsg_locked 1.24% [k] tcp_in_window 1.15% [k] __switch_to 1.19% [k] nf_conntrack_in 1.10% [k] switch_mm_irqs_off 1.17% [k] __switch_to 1.09% [k] tcp_write_xmit 1.13% [.] __strcasecmp_l_avx 1.09% [k] tcp_ack 1.10% [k] switch_mm_irqs_off 1.08% [k] nf_hook_slow 1.08% [k] tcp_v4_rcv 1.04% [k] __sched_text_start 1.06% [k] nf_hook_slow 0.99% [.] __strcasecmp_l_avx 1.05% [k] tcp_ack
The KConfig text refers to Documentation/x86/pagetable-isolation.txt, which doesn't exists...
François Valenduc