From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932102AbeADOna (ORCPT + 1 other); Thu, 4 Jan 2018 09:43:30 -0500 Received: from bombadil.infradead.org ([65.50.211.133]:50027 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753217AbeADOhX (ORCPT ); Thu, 4 Jan 2018 09:37:23 -0500 From: David Woodhouse To: ak@linux.intel.com Cc: David Woodhouse , Paul Turner , LKML , Linus Torvalds , Greg Kroah-Hartman , Tim Chen , Dave Hansen , tglx@linutronix.de, Kees Cook , Rik van Riel , Peter Zijlstra , Andy Lutomirski , Jiri Kosina , gnomes@lxorguk.ukuu.org.uk Subject: [PATCH v3 01/13] x86/retpoline: Add initial retpoline support Date: Thu, 4 Jan 2018 14:36:58 +0000 Message-Id: <20180104143710.8961-1-dwmw@amazon.co.uk> X-Mailer: git-send-email 2.14.3 In-Reply-To: <1515058213.12987.89.camel@amazon.co.uk> References: <1515058213.12987.89.camel@amazon.co.uk> X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide the corresponding thunks. Provide assembler macros for invoking the thunks in the same way that GCC does, from native and inline assembler. This adds an X86_BUG_NO_RETPOLINE "feature" for runtime patching out of the thunks. This is a placeholder for now; the patches which support the new Intel/AMD microcode features will flesh out the precise conditions under which we disable the retpoline and do other things instead. [Andi Kleen: Rename the macros and add CONFIG_RETPOLINE option] Signed-off-by: David Woodhouse --- arch/x86/Kconfig | 8 ++++++ arch/x86/Makefile | 10 ++++++++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/lib/Makefile | 1 + arch/x86/lib/retpoline.S | 50 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 arch/x86/lib/retpoline.S diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index d4fc98c50378..8b0facfa35be 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -429,6 +429,14 @@ 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 new enough compiler. The kernel may run slower. + config INTEL_RDT bool "Intel Resource Director Technology support" default n diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 3e73bc255e4e..f772b3fef202 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -230,6 +230,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 Retpoline not supported in compiler. System may be insecure.) + endif +endif + archscripts: scripts_basic $(Q)$(MAKE) $(build)=arch/x86/tools relocs diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 07cdd1715705..900fa7016d3f 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -342,5 +342,6 @@ #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 */ +#define X86_BUG_NO_RETPOLINE X86_BUG(15) /* Placeholder: disable retpoline branch thunks */ #endif /* _ASM_X86_CPUFEATURES_H */ diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile index 7b181b61170e..f23934bbaf4e 100644 --- a/arch/x86/lib/Makefile +++ b/arch/x86/lib/Makefile @@ -26,6 +26,7 @@ lib-y += memcpy_$(BITS).o lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o insn-eval.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 diff --git a/arch/x86/lib/retpoline.S b/arch/x86/lib/retpoline.S new file mode 100644 index 000000000000..bbdda5cc136e --- /dev/null +++ b/arch/x86/lib/retpoline.S @@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include +#include +#include +#include +#include + +.macro THUNK sp reg + .section .text.__x86.indirect_thunk.\reg + +ENTRY(__x86.indirect_thunk.\reg) + CFI_STARTPROC + ALTERNATIVE "call 2f", __stringify(jmp *%\reg), X86_BUG_NO_RETPOLINE +1: + lfence + jmp 1b +2: + mov %\reg, (%\sp) + ret + CFI_ENDPROC +ENDPROC(__x86.indirect_thunk.\reg) +.endm + +#ifdef CONFIG_64BIT +.irp reg rax rbx rcx rdx rsi rdi rbp r8 r9 r10 r11 r12 r13 r14 r15 + THUNK rsp \reg +.endr +#else +.irp reg eax ebx ecx edx esi edi ebp + THUNK esp \reg +.endr + +/* + * Also provide the original ret-equivalent retpoline for i386 because it's + * so register-starved, and we don't care about CET compatibility here. + */ +ENTRY(__x86.indirect_thunk) + CFI_STARTPROC + ALTERNATIVE "call 2f", "ret", X86_BUG_NO_RETPOLINE +1: + lfence + jmp 1b +2: + lea 4(%esp), %esp + ret + CFI_ENDPROC +ENDPROC(__x86.indirect_thunk) + +#endif -- 2.14.3