x86/pti updates for 4.15
Subsystems (18)
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 update contains:
- A PTI bugfix to avoid setting reserved CR3 bits when PCID is disabled. This seems to cause issues on a virtual machine at least and is incorrect according to the AMD manual.
- A PTI bugfix which disables the perf BTS facility if PTI is enabled. The BTS AUX buffer is not globally visible and causes the CPU to fault when the mapping disappears on switching CR3 to user space. A full fix which restores BTS on PTI is non trivial and will be worked on.
- PTI bugfixes for EFI and trusted boot which make sure that the user space visible page table entries have the NX bit cleared
- Removal of dead code in the PTI pagetable setup functions
- Add PTI documentation
- Add a selftest for vsyscall to verify that the kernel actually implements what it advertises.
- A sysfs interface to expose vulnerability and mitigation information so there is a coherent way for users to retrieve the status.
- The initial spectre_v2 mitigations, aka. retpoline.
- The necessary ASM thunk and compiler support
- The ASM variants of retpoline and the conversion of affected ASM code
- Make LFENCE serializing on AMD so it can be used as speculation trap
- The RSB fill after vmexit
- Initial objtool support for retpoline
As I said in the status mail this is the most of the set of patches which should go into 4.15 except two straight forward patches still on hold:
- The retpoline add on of LFENCE which waits for ACKs
- The RSB fill after context switch
Both should be ready to go early next week and with that we'll have covered the major holes of spectre_v2 and go back to normality.
Thanks,
Thomas
------------------>
Andi Kleen (1):
x86/retpoline/irq32: Convert assembler indirect jumps
Andy Lutomirski (1):
selftests/x86: Add test_vsyscall
Borislav Petkov (1):
x86/alternatives: Fix optimize_nops() checking
Dave Hansen (3):
x86/Documentation: Add PTI description
x86/tboot: Unbreak tboot with PTI enabled x86/pti: Make unpoison of pgd for trusted boot work for real
David Woodhouse (11):
x86/cpufeatures: Add X86_BUG_SPECTRE_V[12]
sysfs/cpu: Fix typos in vulnerability documentation x86/retpoline: Add initial retpoline support x86/spectre: Add boot time option to select Spectre v2 mitigation x86/retpoline/crypto: Convert crypto assembler indirect jumps x86/retpoline/entry: Convert entry assembler indirect jumps x86/retpoline/ftrace: Convert ftrace assembler indirect jumps x86/retpoline/hyperv: Convert assembler indirect jumps x86/retpoline/xen: Convert Xen hypercall indirect jumps x86/retpoline/checksum32: Convert assembler indirect jumps x86/retpoline: Fill return stack buffer on vmexit
Jike Song (1):
x86/mm/pti: Remove dead logic in pti_user_pagetable_walk*()
Jiri Kosina (1):
x86/pti: Unbreak EFI old_memmap
Josh Poimboeuf (2):
objtool: Detect jumps to retpoline thunks
objtool: Allow alternatives to be ignored
Peter Zijlstra (1):
x86,perf: Disable intel_bts when PTI
Thomas Gleixner (3):
sysfs/cpu: Add vulnerability folder
x86/cpu: Implement CPU vulnerabilites sysfs functions x86/pti: Fix !PCID and sanitize defines
Tom Lendacky (2):
x86/cpu/AMD: Make LFENCE a serializing instruction x86/cpu/AMD: Use LFENCE_RDTSC in preference to MFENCE_RDTSC
W. Trevor King (1):
security/Kconfig: Correct the Documentation reference for PTI
Documentation/ABI/testing/sysfs-devices-system-cpu | 16 + Documentation/admin-guide/kernel-parameters.txt | 49 +- Documentation/x86/pti.txt | 186 ++++++++ arch/x86/Kconfig | 14 + arch/x86/Makefile | 10 + arch/x86/crypto/aesni-intel_asm.S | 5 +- arch/x86/crypto/camellia-aesni-avx-asm_64.S | 3 +- arch/x86/crypto/camellia-aesni-avx2-asm_64.S | 3 +- arch/x86/crypto/crc32c-pcl-intel-asm_64.S | 3 +- arch/x86/entry/calling.h | 36 +- arch/x86/entry/entry_32.S | 5 +- arch/x86/entry/entry_64.S | 12 +- arch/x86/events/intel/bts.c | 18 + arch/x86/include/asm/asm-prototypes.h | 25 ++ arch/x86/include/asm/cpufeatures.h | 4 + arch/x86/include/asm/mshyperv.h | 18 +- arch/x86/include/asm/msr-index.h | 3 + arch/x86/include/asm/nospec-branch.h | 214 +++++++++ arch/x86/include/asm/processor-flags.h | 2 +- arch/x86/include/asm/tlbflush.h | 6 +- arch/x86/include/asm/xen/hypercall.h | 5 +- arch/x86/kernel/alternative.c | 7 +- arch/x86/kernel/cpu/amd.c | 28 +- arch/x86/kernel/cpu/bugs.c | 185 ++++++++ arch/x86/kernel/cpu/common.c | 3 + arch/x86/kernel/ftrace_32.S | 6 +- arch/x86/kernel/ftrace_64.S | 8 +- arch/x86/kernel/irq_32.c | 9 +- arch/x86/kernel/tboot.c | 11 + arch/x86/kvm/svm.c | 4 + arch/x86/kvm/vmx.c | 4 + arch/x86/lib/Makefile | 1 + arch/x86/lib/checksum_32.S | 7 +- arch/x86/lib/retpoline.S | 48 ++ arch/x86/mm/pti.c | 32 +- arch/x86/platform/efi/efi_64.c | 2 + drivers/base/Kconfig | 3 + drivers/base/cpu.c | 48 ++ include/linux/cpu.h | 7 + security/Kconfig | 2 +- tools/objtool/check.c | 69 ++- tools/objtool/check.h | 2 +- tools/testing/selftests/x86/Makefile | 2 +- tools/testing/selftests/x86/test_vsyscall.c | 500 +++++++++++++++++++++ 44 files changed, 1525 insertions(+), 100 deletions(-) create mode 100644 Documentation/x86/pti.txt create mode 100644 arch/x86/include/asm/nospec-branch.h create mode 100644 arch/x86/lib/retpoline.S create mode 100644 tools/testing/selftests/x86/test_vsyscall.c
Documentation/ABI/testing/sysfs-devices-system-cpu +16 -0
@@ -373,3 +373,19 @@ Contact: Linux kernel mailing list <[email protected]> Description: information about CPUs heterogeneity. cpu_capacity: capacity of cpu#. + +What: /sys/devices/system/cpu/vulnerabilities + /sys/devices/system/cpu/vulnerabilities/meltdown + /sys/devices/system/cpu/vulnerabilities/spectre_v1 + /sys/devices/system/cpu/vulnerabilities/spectre_v2 +Date: January 2018 +Contact: Linux kernel mailing list <[email protected]> +Description: Information about CPU vulnerabilities + + The files are named after the code names of CPU + vulnerabilities. The output of those files reflects the + state of the CPUs in the system. Possible output values: + + "Not affected" CPU is not affected by the vulnerability + "Vulnerable" CPU is affected and no mitigation in effect + "Mitigation: $M" CPU is affected and mitigation $M is in effect
Documentation/admin-guide/kernel-parameters.txt +42 -7
73 lines not on this page · raw message
Documentation/x86/pti.txt new file +186 -0
186 lines not on this page · raw message
arch/x86/Kconfig +14 -0
@@ -89,6 +89,7 @@ config X86 select GENERIC_CLOCKEVENTS_MIN_ADJUST select GENERIC_CMOS_UPDATE select GENERIC_CPU_AUTOPROBE + select GENERIC_CPU_VULNERABILITIES select GENERIC_EARLY_IOREMAP select GENERIC_FIND_FIRST_BIT select GENERIC_IOMAP
@@ -428,6 +429,19 @@ config GOLDFISH def_bool y depends on X86_GOLDFISH +config RETPOLINE + bool "Avoid speculative indirect branches in kernel" + default y + help + Compile kernel with the retpoline compiler options to guard against + kernel-to-user data leaks by avoiding speculative indirect + branches. Requires a compiler with -mindirect-branch=thunk-extern + support for full protection. The kernel may run slower. + + Without compiler support, at least indirect branches in assembler + code are eliminated. Since this includes the syscall entry path, + it is not entirely pointless. + config INTEL_RDT bool "Intel Resource Director Technology support" default n
arch/x86/Makefile +10 -0
@@ -235,6 +235,16 @@ KBUILD_CFLAGS += -Wno-sign-compare # KBUILD_CFLAGS += -fno-asynchronous-unwind-tables +# Avoid indirect branches in kernel to deal with Spectre +ifdef CONFIG_RETPOLINE + RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch=thunk-extern -mindirect-branch-register) + ifneq ($(RETPOLINE_CFLAGS),) + KBUILD_CFLAGS += $(RETPOLINE_CFLAGS) -DRETPOLINE + else + $(warning CONFIG_RETPOLINE=y, but not supported by the compiler. Toolchain update recommended.) + endif +endif + archscripts: scripts_basic $(Q)$(MAKE) $(build)=arch/x86/tools relocs
arch/x86/crypto/aesni-intel_asm.S +3 -2
@@ -32,6 +32,7 @@ #include <linux/linkage.h> #include <asm/inst.h> #include <asm/frame.h> +#include <asm/nospec-branch.h> /* * The following macros are used to move an (un)aligned 16 byte value to/from
@@ -2884,7 +2885,7 @@ ENTRY(aesni_xts_crypt8) pxor INC, STATE4 movdqu IV, 0x30(OUTP) - call *%r11 + CALL_NOSPEC %r11 movdqu 0x00(OUTP), INC pxor INC, STATE1
@@ -2929,7 +2930,7 @@ ENTRY(aesni_xts_crypt8) _aesni_gf128mul_x_ble() movups IV, (IVP) - call *%r11 + CALL_NOSPEC %r11 movdqu 0x40(OUTP), INC pxor INC, STATE1
arch/x86/crypto/camellia-aesni-avx-asm_64.S +2 -1
@@ -17,6 +17,7 @@ #include <linux/linkage.h> #include <asm/frame.h> +#include <asm/nospec-branch.h> #define CAMELLIA_TABLE_BYTE_LEN 272
@@ -1227,7 +1228,7 @@ camellia_xts_crypt_16way: vpxor 14 * 16(%rax), %xmm15, %xmm14; vpxor 15 * 16(%rax), %xmm15, %xmm15; - call *%r9; + CALL_NOSPEC %r9; addq $(16 * 16), %rsp;
arch/x86/crypto/camellia-aesni-avx2-asm_64.S +2 -1
@@ -12,6 +12,7 @@ #include <linux/linkage.h> #include <asm/frame.h> +#include <asm/nospec-branch.h> #define CAMELLIA_TABLE_BYTE_LEN 272
@@ -1343,7 +1344,7 @@ camellia_xts_crypt_32way: vpxor 14 * 32(%rax), %ymm15, %ymm14; vpxor 15 * 32(%rax), %ymm15, %ymm15; - call *%r9; + CALL_NOSPEC %r9; addq $(16 * 32), %rsp;
arch/x86/crypto/crc32c-pcl-intel-asm_64.S +2 -1
@@ -45,6 +45,7 @@ #include <asm/inst.h> #include <linux/linkage.h> +#include <asm/nospec-branch.h> ## ISCSI CRC 32 Implementation with crc32 and pclmulqdq Instruction
@@ -172,7 +173,7 @@ continue_block: movzxw (bufp, %rax, 2), len lea crc_array(%rip), bufp lea (bufp, len, 1), bufp - jmp *bufp + JMP_NOSPEC bufp ################################################################ ## 2a) PROCESS FULL BLOCKS:
arch/x86/entry/calling.h +19 -17
@@ -198,8 +198,11 @@ For 32-bit we have the following conventions - kernel is built with* PAGE_TABLE_ISOLATION PGDs are 8k. Flip bit 12 to switch between the two * halves: */ -#define PTI_SWITCH_PGTABLES_MASK (1<<PAGE_SHIFT) -#define PTI_SWITCH_MASK (PTI_SWITCH_PGTABLES_MASK|(1<<X86_CR3_PTI_SWITCH_BIT)) +#define PTI_USER_PGTABLE_BIT PAGE_SHIFT +#define PTI_USER_PGTABLE_MASK (1 << PTI_USER_PGTABLE_BIT) +#define PTI_USER_PCID_BIT X86_CR3_PTI_PCID_USER_BIT +#define PTI_USER_PCID_MASK (1 << PTI_USER_PCID_BIT) +#define PTI_USER_PGTABLE_AND_PCID_MASK (PTI_USER_PCID_MASK | PTI_USER_PGTABLE_MASK) .macro SET_NOFLUSH_BIT reg:req bts $X86_CR3_PCID_NOFLUSH_BIT, \reg
@@ -208,7 +211,7 @@ For 32-bit we have the following conventions - kernel is built with .macro ADJUST_KERNEL_CR3 reg:req ALTERNATIVE "", "SET_NOFLUSH_BIT \reg", X86_FEATURE_PCID /* Clear PCID and "PAGE_TABLE_ISOLATION bit", point CR3 at kernel pagetables: */ - andq $(~PTI_SWITCH_MASK), \reg + andq $(~PTI_USER_PGTABLE_AND_PCID_MASK), \reg .endm .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
@@ -239,15 +242,19 @@ For 32-bit we have the following conventions - kernel is built with/* Flush needed, clear the bit */ btr \scratch_reg, THIS_CPU_user_pcid_flush_mask movq \scratch_reg2, \scratch_reg - jmp .Lwrcr3_\@ + jmp .Lwrcr3_pcid_\@ .Lnoflush_\@: movq \scratch_reg2, \scratch_reg SET_NOFLUSH_BIT \scratch_reg +.Lwrcr3_pcid_\@: +/* Flip the ASID to the user version */ + orq $(PTI_USER_PCID_MASK), \scratch_reg + .Lwrcr3_\@: -/* Flip the PGD and ASID to the user version */ - orq $(PTI_SWITCH_MASK), \scratch_reg +/* Flip the PGD to the user version */ + orq $(PTI_USER_PGTABLE_MASK), \scratch_reg mov \scratch_reg, %cr3 .Lend_\@: .endm
@@ -263,17 +270,12 @@ For 32-bit we have the following conventions - kernel is built with movq %cr3, \scratch_reg movq \scratch_reg, \save_reg /* -* Is the "switch mask" all zero? That means that both of -* these are zero: -* -* 1. The user/kernel PCID bit, and -* 2. The user/kernel "bit" that points CR3 to the -* bottom half of the 8k PGD -* -* That indicates a kernel CR3 value, not a user CR3. + * Test the user pagetable bit. If set, then the user page tables + * are active. If clear CR3 already has the kernel page table + * active. */ - testq $(PTI_SWITCH_MASK), \scratch_reg - jz .Ldone_\@ + bt $PTI_USER_PGTABLE_BIT, \scratch_reg + jnc .Ldone_\@ ADJUST_KERNEL_CR3 \scratch_reg movq \scratch_reg, %cr3
@@ -290,7 +292,7 @@ For 32-bit we have the following conventions - kernel is built with* KERNEL pages can always resume with NOFLUSH as we do * explicit flushes. */ - bt $X86_CR3_PTI_SWITCH_BIT, \save_reg + bt $PTI_USER_PGTABLE_BIT, \save_reg jnc .Lnoflush_\@/*
arch/x86/entry/entry_32.S +3 -2
@@ -44,6 +44,7 @@ #include <asm/asm.h> #include <asm/smap.h> #include <asm/frame.h> +#include <asm/nospec-branch.h> .section .entry.text, "ax"
@@ -290,7 +291,7 @@ ENTRY(ret_from_fork) /* kernel thread */ 1: movl %edi, %eax - call *%ebx + CALL_NOSPEC %ebx /* * A kernel thread is allowed to return here after successfully * calling do_execve(). Exit to userspace to complete the execve()
@@ -919,7 +920,7 @@ common_exception: movl %ecx, %es TRACE_IRQS_OFF movl %esp, %eax # pt_regs pointer - call *%edi + CALL_NOSPEC %edi jmp ret_from_exception END(common_exception)
arch/x86/entry/entry_64.S +9 -3
@@ -37,6 +37,7 @@ #include <asm/pgtable_types.h> #include <asm/export.h> #include <asm/frame.h> +#include <asm/nospec-branch.h> #include <linux/err.h> #include "calling.h"
@@ -187,7 +188,7 @@ ENTRY(entry_SYSCALL_64_trampoline) */ pushq %rdi movq $entry_SYSCALL_64_stage2, %rdi - jmp *%rdi + JMP_NOSPEC %rdi END(entry_SYSCALL_64_trampoline) .popsection
@@ -266,7 +267,12 @@ entry_SYSCALL_64_fastpath: * It might end up jumping to the slow path. If it jumps, RAX * and all argument registers are clobbered. */ +#ifdef CONFIG_RETPOLINE + movq sys_call_table(, %rax, 8), %rax + call __x86_indirect_thunk_rax +#else call *sys_call_table(, %rax, 8) +#endif .Lentry_SYSCALL_64_after_fastpath_call: movq %rax, RAX(%rsp)
@@ -438,7 +444,7 @@ ENTRY(stub_ptregs_64) jmp entry_SYSCALL64_slow_path 1: - jmp *%rax /* Called from C */ + JMP_NOSPEC %rax /* Called from C */ END(stub_ptregs_64) .macro ptregs_stub func
@@ -517,7 +523,7 @@ ENTRY(ret_from_fork) 1: /* kernel thread */ movq %r12, %rdi - call *%rbx + CALL_NOSPEC %rbx /* * A kernel thread is allowed to return here after successfully * calling do_execve(). Exit to userspace to complete the execve()
arch/x86/events/intel/bts.c +18 -0
@@ -582,6 +582,24 @@ static __init int bts_init(void)if (!boot_cpu_has (X86_FEATURE_DTES64 ) || !x86_pmu.bts)return -ENODEV ; +if (boot_cpu_has (X86_FEATURE_PTI )) { +/* +* BTS hardware writes through a virtual memory map we must +* either use the kernel physical map, or the user mapping of +* the AUX buffer. +* +* However, since this driver supports per-CPU and per-task inherit +* we cannot use the user mapping since it will not be availble +* if we're not running the owning process. +* +* With PTI we can't use the kernal map either, because its not +* there when we run userspace. +* +* For now, disable this driver when using PTI. +*/ +return -ENODEV ; + } + bts_pmu.capabilities =PERF_PMU_CAP_AUX_NO_SG |PERF_PMU_CAP_ITRACE |PERF_PMU_CAP_EXCLUSIVE ; bts_pmu.task_ctx_nr = perf_sw_context;
arch/x86/include/asm/asm-prototypes.h +25 -0
@@ -11,7 +11,32 @@#include <asm/pgtable.h> #include <asm/special_insns.h> #include <asm/preempt.h> +#include <asm/asm.h> #ifndef CONFIG_X86_CMPXCHG64 extern void cmpxchg8b_emu (void );#endif + +#ifdef CONFIG_RETPOLINE +#ifdef CONFIG_X86_32 +#define INDIRECT_THUNK (reg) extern asmlinkage void __x86_indirect_thunk_e ## reg(void); +#else +#define INDIRECT_THUNK (reg) extern asmlinkage void __x86_indirect_thunk_r ## reg(void); +INDIRECT_THUNK(8) +INDIRECT_THUNK(9) +INDIRECT_THUNK(10) +INDIRECT_THUNK(11) +INDIRECT_THUNK(12) +INDIRECT_THUNK(13) +INDIRECT_THUNK(14) +INDIRECT_THUNK(15) +#endif +INDIRECT_THUNK(ax) +INDIRECT_THUNK(bx) +INDIRECT_THUNK(cx) +INDIRECT_THUNK(dx) +INDIRECT_THUNK(si) +INDIRECT_THUNK(di) +INDIRECT_THUNK(bp) +INDIRECT_THUNK(sp) +#endif /* CONFIG_RETPOLINE */
arch/x86/include/asm/cpufeatures.h +4 -0
@@ -203,6 +203,8 @@#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_RETPOLINE ( 7*32+12)/* Generic Retpoline mitigation for Spectre variant 2 */ +#define X86_FEATURE_RETPOLINE_AMD ( 7*32+13)/* AMD Retpoline mitigation for Spectre variant 2 */ #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 */
@@ -342,5 +344,7 @@#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_MELTDOWN X86_BUG(14)/* CPU is affected by meltdown attack and needs kernel page table isolation */ +#define X86_BUG_SPECTRE_V1 X86_BUG(15)/* CPU is affected by Spectre variant 1 attack with conditional branches */ +#define X86_BUG_SPECTRE_V2 X86_BUG(16)/* CPU is affected by Spectre variant 2 attack with indirect branches */ #endif /* _ASM_X86_CPUFEATURES_H */
arch/x86/include/asm/mshyperv.h +10 -8
@@ -7,6 +7,7 @@#include <linux/nmi.h> #include <asm/io.h> #include <asm/hyperv.h> +#include <asm/nospec-branch.h> /* * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
@@ -186,10 +187,11 @@ static inline u64 hv_do_hypercall(u64 control, void *input, void *output)return U64_MAX ; __asm__ __volatile__("mov %4, %%r8\n" -"call *%5" +CALL_NOSPEC : "=a" (hv_status), ASM_CALL_CONSTRAINT,"+c" (control),"+d" (input_address) - :"r" (output_address),"m" (hv_hypercall_pg) + : "r" (output_address), + THUNK_TARGET(hv_hypercall_pg) :"cc" ,"memory" ,"r8" ,"r9" ,"r10" ,"r11" );#else u32 input_address_hi =upper_32_bits (input_address);
@@ -200,13 +202,13 @@ static inline u64 hv_do_hypercall(u64 control, void *input, void *output)if (!hv_hypercall_pg)return U64_MAX ; - __asm__ __volatile__("call *%7" + __asm__ __volatile__(CALL_NOSPEC : "=A" (hv_status), "+c" (input_address_lo), ASM_CALL_CONSTRAINT : "A" (control), "b" (input_address_hi), "D"(output_address_hi), "S"(output_address_lo), -"m" (hv_hypercall_pg) + THUNK_TARGET(hv_hypercall_pg) : "cc", "memory");#endif /* !x86_64 */ return hv_status;
@@ -227,10 +229,10 @@ static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)#ifdef CONFIG_X86_64 { - __asm__ __volatile__("call *%4" + __asm__ __volatile__(CALL_NOSPEC : "=a" (hv_status), ASM_CALL_CONSTRAINT, "+c" (control), "+d" (input1) - :"m" (hv_hypercall_pg) + : THUNK_TARGET(hv_hypercall_pg) : "cc", "r8", "r9", "r10", "r11"); }#else
@@ -238,13 +240,13 @@ static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)u32 input1_hi =upper_32_bits (input1);u32 input1_lo =lower_32_bits (input1); - __asm__ __volatile__ ("call *%5" + __asm__ __volatile__ (CALL_NOSPEC : "=A"(hv_status), "+c"(input1_lo), ASM_CALL_CONSTRAINT : "A" (control), "b" (input1_hi), -"m" (hv_hypercall_pg) + THUNK_TARGET(hv_hypercall_pg) : "cc", "edi", "esi"); }#endif
arch/x86/include/asm/msr-index.h +3 -0
@@ -352,6 +352,9 @@#define FAM10H_MMIO_CONF_BASE_MASK 0xfffffffULL#define FAM10H_MMIO_CONF_BASE_SHIFT 20#define MSR_FAM10H_NODE_ID 0xc001100c +#define MSR_F10H_DECFG 0xc0011029 +#define MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT 1 +#define MSR_F10H_DECFG_LFENCE_SERIALIZE BIT_ULL(MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT)/* K8 MSRs */ #define MSR_K8_TOP_MEM1 0xc001001a
arch/x86/include/asm/nospec-branch.h new file +214 -0
214 lines not on this page · raw message
arch/x86/include/asm/processor-flags.h +1 -1
@@ -40,7 +40,7 @@#define CR3_NOFLUSH BIT_ULL(63)#ifdef CONFIG_PAGE_TABLE_ISOLATION -# define X86_CR3_PTI_SWITCH_BIT 11 +# define X86_CR3_PTI_PCID_USER_BIT 11#endif #else
arch/x86/include/asm/tlbflush.h +3 -3
@@ -81,13 +81,13 @@ static inline u16 kern_pcid(u16 asid)* Make sure that the dynamic ASID space does not confict with the * bit we are using to switch between user and kernel ASIDs. */ -BUILD_BUG_ON (TLB_NR_DYN_ASIDS >= (1 <<X86_CR3_PTI_SWITCH_BIT )); +BUILD_BUG_ON (TLB_NR_DYN_ASIDS >= (1 <<X86_CR3_PTI_PCID_USER_BIT ));/* * The ASID being passed in here should have respected the * MAX_ASID_AVAILABLE and thus never have the switch bit set. */ -VM_WARN_ON_ONCE (asid & (1 <<X86_CR3_PTI_SWITCH_BIT )); +VM_WARN_ON_ONCE (asid & (1 <<X86_CR3_PTI_PCID_USER_BIT ));#endif /* * The dynamically-assigned ASIDs that get passed in are small
@@ -112,7 +112,7 @@ static inline u16 user_pcid(u16 asid) {u16 ret =kern_pcid (asid);#ifdef CONFIG_PAGE_TABLE_ISOLATION - ret |=1 <<X86_CR3_PTI_SWITCH_BIT ; + ret |=1 <<X86_CR3_PTI_PCID_USER_BIT ;#endif return ret; }
arch/x86/include/asm/xen/hypercall.h +3 -2
@@ -44,6 +44,7 @@#include <asm/page.h> #include <asm/pgtable.h> #include <asm/smap.h> +#include <asm/nospec-branch.h> #include <xen/interface/xen.h> #include <xen/interface/sched.h>
@@ -217,9 +218,9 @@ privcmd_call(unsigned call,__HYPERCALL_5ARG (a1, a2, a3, a4, a5);stac (); - asmvolatile ("call *%[call]" + asmvolatile (CALL_NOSPEC : __HYPERCALL_5PARAM - : [call]"a" (&hypercall_page[call]) + : [thunk_target] "a" (&hypercall_page[call]) : __HYPERCALL_CLOBBER5);clac ();
arch/x86/kernel/alternative.c +5 -2
@@ -344,9 +344,12 @@ recompute_jump(struct alt_instr *a, u8 *orig_insn, u8 *repl_insn, u8 *insnbuf) static void __init_or_module noinline optimize_nops(struct alt_instr *a, u8 *instr) {unsigned long flags; +int i; -if (instr[0 ] !=0x90 ) -return ; +for (i =0 ; i < a->padlen; i++) { +if (instr[i] !=0x90 ) +return ; + }local_irq_save (flags);add_nops (instr + (a->instrlen - a->padlen), a->padlen);
arch/x86/kernel/cpu/amd.c +26 -2
@@ -829,8 +829,32 @@ static void init_amd(struct cpuinfo_x86 *c)set_cpu_cap (c,X86_FEATURE_K8 );if (cpu_has (c,X86_FEATURE_XMM2 )) { -/* MFENCE stops RDTSC speculation */ -set_cpu_cap (c,X86_FEATURE_MFENCE_RDTSC ); +unsigned long long val; +int ret; + +/* +* A serializing LFENCE has less overhead than MFENCE, so +* use it for execution serialization. On families which +* don't have that MSR, LFENCE is already serializing. +* msr_set_bit() uses the safe accessors, too, even if the MSR +* is not present. +*/ +msr_set_bit (MSR_F10H_DECFG , +MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT ); + +/* +* Verify that the MSR write was successful (could be running +* under a hypervisor) and only then assume that LFENCE is +* serializing. +*/ + ret =rdmsrl_safe (MSR_F10H_DECFG , &val); +if (!ret && (val &MSR_F10H_DECFG_LFENCE_SERIALIZE )) { +/* A serializing LFENCE stops RDTSC speculation */ +set_cpu_cap (c,X86_FEATURE_LFENCE_RDTSC ); + }else { +/* MFENCE stops RDTSC speculation */ +set_cpu_cap (c,X86_FEATURE_MFENCE_RDTSC ); + } }/*
arch/x86/kernel/cpu/bugs.c +185 -0
206 lines not on this page · raw message
arch/x86/kernel/cpu/common.c +3 -0
@@ -902,6 +902,9 @@ static void __init early_identify_cpu(struct cpuinfo_x86 *c)if (c->x86_vendor !=X86_VENDOR_AMD )setup_force_cpu_bug (X86_BUG_CPU_MELTDOWN ); +setup_force_cpu_bug (X86_BUG_SPECTRE_V1 ); +setup_force_cpu_bug (X86_BUG_SPECTRE_V2 ); +fpu__init_system (c);#ifdef CONFIG_X86_32
arch/x86/kernel/ftrace_32.S +4 -2
@@ -8,6 +8,7 @@ #include <asm/segment.h> #include <asm/export.h> #include <asm/ftrace.h> +#include <asm/nospec-branch.h> #ifdef CC_USING_FENTRY # define function_hook __fentry__
@@ -197,7 +198,8 @@ ftrace_stub: movl 0x4(%ebp), %edx subl $MCOUNT_INSN_SIZE, %eax - call *ftrace_trace_function + movl ftrace_trace_function, %ecx + CALL_NOSPEC %ecx popl %edx popl %ecx
@@ -241,5 +243,5 @@ return_to_handler: movl %eax, %ecx popl %edx popl %eax - jmp *%ecx + JMP_NOSPEC %ecx #endif
arch/x86/kernel/ftrace_64.S +4 -4
@@ -7,7 +7,7 @@ #include <asm/ptrace.h> #include <asm/ftrace.h> #include <asm/export.h> - +#include <asm/nospec-branch.h> .code64 .section .entry.text, "ax"
@@ -286,8 +286,8 @@ trace: * ip and parent ip are used and the list function is called when * function tracing is enabled. */ - call *ftrace_trace_function - + movq ftrace_trace_function, %r8 + CALL_NOSPEC %r8 restore_mcount_regs jmp fgraph_trace
@@ -329,5 +329,5 @@ GLOBAL(return_to_handler) movq 8(%rsp), %rdx movq (%rsp), %rax addq $24, %rsp - jmp *%rdi + JMP_NOSPEC %rdi #endif
arch/x86/kernel/irq_32.c +5 -4
@@ -20,6 +20,7 @@#include <linux/mm.h> #include <asm/apic.h> +#include <asm/nospec-branch.h> #ifdef CONFIG_DEBUG_STACKOVERFLOW
@@ -55,11 +56,11 @@ DEFINE_PER_CPU(struct irq_stack *, softirq_stack);static void call_on_stack (void *func,void *stack) { asmvolatile ("xchgl %%ebx,%%esp \n" -"call *%%edi \n" +CALL_NOSPEC "movl %%ebx,%%esp \n" :"=b" (stack) :"0" (stack), -"D" (func) + [thunk_target]"D" (func) :"memory" ,"cc" ,"edx" ,"ecx" ,"eax" ); }
@@ -95,11 +96,11 @@ static inline int execute_on_irq_stack(int overflow, struct irq_desc *desc)call_on_stack (print_stack_overflow, isp); asmvolatile ("xchgl %%ebx,%%esp \n" -"call *%%edi \n" +CALL_NOSPEC "movl %%ebx,%%esp \n" :"=a" (arg1),"=b" (isp) :"0" (desc),"1" (isp), -"D" (desc->handle_irq) + [thunk_target]"D" (desc->handle_irq) :"memory" ,"cc" ,"ecx" );return 1 ; }
arch/x86/kernel/tboot.c +11 -0
@@ -138,6 +138,17 @@ static int map_tboot_page(unsigned long vaddr, unsigned long pfn,return -1 ;set_pte_at (&tboot_mm, vaddr, pte,pfn_pte (pfn, prot));pte_unmap (pte); + +/* +* PTI poisons low addresses in the kernel page tables in the +* name of making them unusable for userspace. To execute +* code at such a low address, the poison must be cleared. +* +* Note: 'pgd' actually gets set in p4d_alloc() _or_ +* pud_alloc() depending on 4/5-level paging. +*/ + pgd->pgd &= ~_PAGE_NX; +return 0 ; }
arch/x86/kvm/svm.c +4 -0
@@ -45,6 +45,7 @@#include <asm/debugreg.h> #include <asm/kvm_para.h> #include <asm/irq_remapping.h> +#include <asm/nospec-branch.h> #include <asm/virtext.h> #include "trace.h"
@@ -4985,6 +4986,9 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu)#endif ); +/* Eliminate branch target predictions from guest mode */ +vmexit_fill_RSB (); +#ifdef CONFIG_X86_64 wrmsrl (MSR_GS_BASE , svm->host.gs_base);#else
arch/x86/kvm/vmx.c +4 -0
@@ -50,6 +50,7 @@#include <asm/apic.h> #include <asm/irq_remapping.h> #include <asm/mmu_context.h> +#include <asm/nospec-branch.h> #include "trace.h" #include "pmu.h"
@@ -9403,6 +9404,9 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)#endif ); +/* Eliminate branch target predictions from guest mode */ +vmexit_fill_RSB (); +/* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ if (debugctlmsr)update_debugctlmsr (debugctlmsr);
arch/x86/lib/Makefile +1 -0
@@ -26,6 +26,7 @@ lib-y += memcpy_$(BITS).o lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o lib-$(CONFIG_RANDOMIZE_BASE) += kaslr.o +lib-$(CONFIG_RETPOLINE) += retpoline.o obj-y += msr.o msr-reg.o msr-reg-export.o hweight.o
arch/x86/lib/checksum_32.S +4 -3
@@ -29,7 +29,8 @@ #include <asm/errno.h> #include <asm/asm.h> #include <asm/export.h> - +#include <asm/nospec-branch.h> + /* * computes a partial checksum, e.g. for TCP/UDP fragments */
@@ -156,7 +157,7 @@ ENTRY(csum_partial) negl %ebx lea 45f(%ebx,%ebx,2), %ebx testl %esi, %esi - jmp *%ebx + JMP_NOSPEC %ebx # Handle 2-byte-aligned regions 20: addw (%esi), %ax
@@ -439,7 +440,7 @@ ENTRY(csum_partial_copy_generic) andl $-32,%edx lea 3f(%ebx,%ebx), %ebx testl %esi, %esi - jmp *%ebx + JMP_NOSPEC %ebx 1: addl $64,%esi addl $64,%edi SRC(movb -32(%edx),%bl) ; SRC(movb (%edx),%bl)
arch/x86/lib/retpoline.S new file +48 -0
48 lines not on this page · raw message
arch/x86/mm/pti.c +6 -26
@@ -149,7 +149,7 @@ pgd_t __pti_set_user_pgd(pgd_t *pgdp, pgd_t pgd)* * Returns a pointer to a P4D on success, or NULL on failure. */ -static p4d_t *pti_user_pagetable_walk_p4d (unsigned long address) +static __init p4d_t *pti_user_pagetable_walk_p4d (unsigned long address) {pgd_t *pgd =kernel_to_user_pgdp (pgd_offset_k (address));gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
@@ -164,12 +164,7 @@ static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)if (!new_p4d_page)return NULL ; -if (pgd_none (*pgd)) { -set_pgd (pgd,__pgd (_KERNPG_TABLE |__pa (new_p4d_page))); - new_p4d_page =0 ; - } -if (new_p4d_page) -free_page (new_p4d_page); +set_pgd (pgd,__pgd (_KERNPG_TABLE |__pa (new_p4d_page))); }BUILD_BUG_ON (pgd_large (*pgd) !=0 );
@@ -182,7 +177,7 @@ static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)* * Returns a pointer to a PMD on success, or NULL on failure. */ -static pmd_t *pti_user_pagetable_walk_pmd (unsigned long address) +static __init pmd_t *pti_user_pagetable_walk_pmd (unsigned long address) {gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);p4d_t *p4d =pti_user_pagetable_walk_p4d (address);
@@ -194,12 +189,7 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)if (!new_pud_page)return NULL ; -if (p4d_none (*p4d)) { -set_p4d (p4d,__p4d (_KERNPG_TABLE |__pa (new_pud_page))); - new_pud_page =0 ; - } -if (new_pud_page) -free_page (new_pud_page); +set_p4d (p4d,__p4d (_KERNPG_TABLE |__pa (new_pud_page))); } pud =pud_offset (p4d, address);
@@ -213,12 +203,7 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)if (!new_pmd_page)return NULL ; -if (pud_none (*pud)) { -set_pud (pud,__pud (_KERNPG_TABLE |__pa (new_pmd_page))); - new_pmd_page =0 ; - } -if (new_pmd_page) -free_page (new_pmd_page); +set_pud (pud,__pud (_KERNPG_TABLE |__pa (new_pmd_page))); }return pmd_offset (pud, address);
@@ -251,12 +236,7 @@ static __init pte_t *pti_user_pagetable_walk_pte(unsigned long address)if (!new_pte_page)return NULL ; -if (pmd_none (*pmd)) { -set_pmd (pmd,__pmd (_KERNPG_TABLE |__pa (new_pte_page))); - new_pte_page =0 ; - } -if (new_pte_page) -free_page (new_pte_page); +set_pmd (pmd,__pmd (_KERNPG_TABLE |__pa (new_pte_page))); } pte =pte_offset_kernel (pmd, address);
arch/x86/platform/efi/efi_64.c +2 -0
@@ -134,7 +134,9 @@ pgd_t * __init efi_call_phys_prolog(void) pud[j] = *pud_offset (p4d_k, vaddr); } } +pgd_offset_k (pgd *PGDIR_SIZE )->pgd &= ~_PAGE_NX; } + out:__flush_tlb_all ();
drivers/base/Kconfig +3 -0
@@ -235,6 +235,9 @@ config GENERIC_CPU_DEVICES config GENERIC_CPU_AUTOPROBE bool +config GENERIC_CPU_VULNERABILITIES + bool + config SOC_BUS bool select GLOB
drivers/base/cpu.c +48 -0
58 lines not on this page · raw message
include/linux/cpu.h +7 -0
@@ -47,6 +47,13 @@ extern void cpu_remove_dev_attr(struct device_attribute *attr);extern int cpu_add_dev_attr_group (struct attribute_group *attrs);extern void cpu_remove_dev_attr_group (struct attribute_group *attrs); +extern ssize_t cpu_show_meltdown (struct device *dev, +struct device_attribute *attr,char *buf); +extern ssize_t cpu_show_spectre_v1 (struct device *dev, +struct device_attribute *attr,char *buf); +extern ssize_t cpu_show_spectre_v2 (struct device *dev, +struct device_attribute *attr,char *buf); + extern __printf(4, 5) struct device *cpu_device_create(struct device *parent, void *drvdata, const struct attribute_group **groups,
security/Kconfig +1 -1
@@ -63,7 +63,7 @@ config PAGE_TABLE_ISOLATION ensuring that the majority of kernel addresses are not mapped into userspace. - See Documentation/x86/pagetable-isolation.txt for more details. + See Documentation/x86/pti.txt for more details. config SECURITY_INFINIBAND bool "Infiniband Security Hooks"
tools/objtool/check.c +63 -6
116 lines not on this page · raw message
tools/objtool/check.h +1 -1
@@ -44,7 +44,7 @@ struct instruction {unsigned int len;unsigned char type;unsigned long immediate; -bool alt_group, visited, dead_end, ignore, hint, save, restore; +bool alt_group, visited, dead_end, ignore, hint, save, restore, ignore_alts;struct symbol *call_dest;struct instruction *jump_dest;struct list_head alts;
tools/testing/selftests/x86/Makefile +1 -1
@@ -7,7 +7,7 @@ include ../lib.mk TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt ptrace_syscall test_mremap_vdso \ check_initial_reg_state sigreturn ldt_gdt iopl mpx-mini-test ioperm \ - protection_keys test_vdso + protection_keys test_vdso test_vsyscall TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault test_syscall_vdso unwind_vdso \ test_FCMOV test_FCOMI test_FISTTP \ vdso_restorer
tools/testing/selftests/x86/test_vsyscall.c new file +500 -0
500 lines not on this page · raw message
Quoted 2 lines · Thomas Gleixner
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.gitx86-pti-for-linus
So I do think this:
$(warning CONFIG_RETPOLINE=y, but not supported by the
compiler. Toolchain update recommended.)
needs to be removed.
Yes, yes, I understand why it's warning. It's still both annoying and wrong.
It's wrong because it will just make people turn off RETPOLINE, and the asm updates - and return stack clearing - that are independent of the compiler are likely the most important parts because they are likely the ones easiest to target.
And it's annoying because most people won't be able to do anything about it. The number of people building their own compiler? Very small. So if their distro hasn't got a compiler yet (and pretty much nobody does), the warning is just annoying crap.
It is already properly reported as part of the sysfs interface. The compile-time warning only encourages bad things.
Linus
Quoted 18 lines · Linus Torvalds
Quoted 2 lines · Thomas Gleixner
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.gitx86-pti-for-linusSo I do think this:
$(warning CONFIG_RETPOLINE=y, but not supported by thecompiler. Toolchain update recommended.)
needs to be removed.
Yes, yes, I understand why it's warning. It's still both annoying and wrong.
It's wrong because it will just make people turn off RETPOLINE, and the asm updates - and return stack clearing - that are independent of the compiler are likely the most important parts because they are likely the ones easiest to target.
Quoted 7 lines
And it's annoying because most people won't be able to do anything about it. The number of people building their own compiler? Very small. So if their distro hasn't got a compiler yet (and pretty much nobody does), the warning is just annoying crap.
It is already properly reported as part of the sysfs interface. The compile-time warning only encourages bad things.
Good point. I'll queue a patch to that effect or do you just want to do that yourself?
Thanks,
tglx
Quoted 3 lines · Thomas Gleixner
Good point. I'll queue a patch to that effect or do you just want to do that yourself?
I don't think it's critical, and I don't care for rc8, so it's not timing-sensitive.
Maybe David should do it as part of the "the compiler people changed the name of the thunks" series anyway, since now I think that RETPOLINE_CFLAGS test might want to be extended too (to see which _version_ of the thunks we need to export).
Or maybe we'd just export the damn thunks under both names, just to allow people to use both versions of the compilers.
Christ, what a mess.
My "let's not warn" is not important, and the thunk naming thing should be resolved first. Gaah.
So I think letting David worry about it sounds about right.
Linus
Quoted 15 lines · Linus Torvalds
Quoted 5 lines · Thomas Gleixner
Good point. I'll queue a patch to that effect or do you just want to do
that yourself?I don't think it's critical, and I don't care for rc8, so it's not timing-sensitive.
Maybe David should do it as part of the "the compiler people changed the name of the thunks" series anyway, since now I think that
RETPOLINE_CFLAGStest might want to be extended too (to see which _version_ of the thunks we need to export).
Quoted 4 lines
Or maybe we'd just export the damn thunks under both names, just to allow people to use both versions of the compilers.
Christ, what a mess.
No, screw that. We're updating, and there's only one version of anything. Let's not mess around with compatibility for thunk names that never made the light of day in either a GCC or a Linux (even -rc) release.
Quoted 4 lines
My "let's not warn" is not important, and the thunk naming thing should be resolved first. Gaah.
So I think letting David worry about it sounds about right.
Thanks. :)
Quoted 7 lines · Linus Torvalds
Quoted 3 lines · Thomas Gleixner
Good point. I'll queue a patch to that effect or do you just want to do that yourself?
I don't think it's critical, and I don't care for rc8, so it's not timing-sensitive.
The removal patch can be pulled from:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git x86-pti-for-linus
It's better we get rid of it now.
Thanks,
tglx
------------------>
Thomas Gleixner (1):
x86/retpoline: Remove compile time warning
arch/x86/Makefile | 2 -- 1 file changed, 2 deletions(-)
arch/x86/Makefile +0 -2
@@ -240,8 +240,6 @@ ifdef CONFIG_RETPOLINE RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch=thunk-extern -mindirect-branch-register) ifneq ($(RETPOLINE_CFLAGS),) KBUILD_CFLAGS += $(RETPOLINE_CFLAGS) -DRETPOLINE - else - $(warning CONFIG_RETPOLINE=y, but not supported by the compiler. Toolchain update recommended.) endif endif
tip-bot for Thomas Gleixner
merge bot[tip:x86/pti] x86/retpoline: Remove compile time warning
In reply to Linus Torvaldsx86/retpoline: Remove compile time warning
Commit-ID: b8b9ce4b5aec8de9e23cabb0a26b78641f9ab1d6 Gitweb: https://git.kernel.org/tip/b8b9ce4b5aec8de9e23cabb0a26b78641f9ab1d6 Author: Thomas Gleixner <[email protected]> AuthorDate: Sun, 14 Jan 2018 22:13:29 +0100 Committer: Thomas Gleixner <[email protected]> CommitDate: Sun, 14 Jan 2018 22:29:36 +0100
x86/retpoline: Remove compile time warning
Remove the compile time warning when CONFIG_RETPOLINE=y and the compiler does not have retpoline support. Linus rationale for this is:
It's wrong because it will just make people turn off RETPOLINE, and the asm updates - and return stack clearing - that are independent of the compiler are likely the most important parts because they are likely the ones easiest to target.
And it's annoying because most people won't be able to do anything about it. The number of people building their own compiler? Very small. So if their distro hasn't got a compiler yet (and pretty much nobody does), the warning is just annoying crap.
It is already properly reported as part of the sysfs interface. The compile-time warning only encourages bad things.
--- arch/x86/Makefile | 2 -- 1 file changed, 2 deletions(-)
arch/x86/Makefile +0 -2
@@ -240,8 +240,6 @@ ifdef CONFIG_RETPOLINE RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch=thunk-extern -mindirect-branch-register) ifneq ($(RETPOLINE_CFLAGS),) KBUILD_CFLAGS += $(RETPOLINE_CFLAGS) -DRETPOLINE - else - $(warning CONFIG_RETPOLINE=y, but not supported by the compiler. Toolchain update recommended.) endif endif