linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tim Chen <tim.c.chen@linux.intel.com>
To: Thomas Gleixner <tglx@linutronix.de>,
	Andy Lutomirski <luto@kernel.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Greg KH <gregkh@linuxfoundation.org>
Cc: Tim Chen <tim.c.chen@linux.intel.com>,
	Dave Hansen <dave.hansen@intel.com>,
	Andrea Arcangeli <aarcange@redhat.com>,
	Andi Kleen <ak@linux.intel.com>,
	Arjan Van De Ven <arjan.van.de.ven@intel.com>,
	linux-kernel@vger.kernel.org, David Woodhouse <dwmw@amazon.co.uk>
Subject: [PATCH 5/7] x86: Use IBRS for firmware update path
Date: Thu,  4 Jan 2018 09:56:46 -0800	[thread overview]
Message-ID: <8d3710432534b27d224283557c4629cd1aa5b0ea.1515086770.git.tim.c.chen@linux.intel.com> (raw)
In-Reply-To: <cover.1515086770.git.tim.c.chen@linux.intel.com>
In-Reply-To: <cover.1515086770.git.tim.c.chen@linux.intel.com>

From: David Woodhouse <dwmw@amazon.co.uk>

We are impervious to the indirect branch prediction attack with retpoline
but firmware won't be, so we still need to set IBRS to protect
firmware code execution when calling into firmware at runtime.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Tim Chen <tim.c.chen@linux.intel.com>
---
 arch/x86/include/asm/apm.h       |  6 ++++++
 arch/x86/include/asm/efi.h       | 16 ++++++++++++++--
 arch/x86/include/asm/spec_ctrl.h | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/apm.h b/arch/x86/include/asm/apm.h
index 4d4015d..1ca4f7b 100644
--- a/arch/x86/include/asm/apm.h
+++ b/arch/x86/include/asm/apm.h
@@ -7,6 +7,8 @@
 #ifndef _ASM_X86_MACH_DEFAULT_APM_H
 #define _ASM_X86_MACH_DEFAULT_APM_H
 
+#include <asm/spec_ctrl.h>
+
 #ifdef APM_ZERO_SEGS
 #	define APM_DO_ZERO_SEGS \
 		"pushl %%ds\n\t" \
@@ -28,6 +30,7 @@ static inline void apm_bios_call_asm(u32 func, u32 ebx_in, u32 ecx_in,
 					u32 *eax, u32 *ebx, u32 *ecx,
 					u32 *edx, u32 *esi)
 {
+	unprotected_firmware_begin();
 	/*
 	 * N.B. We do NOT need a cld after the BIOS call
 	 * because we always save and restore the flags.
@@ -44,6 +47,7 @@ static inline void apm_bios_call_asm(u32 func, u32 ebx_in, u32 ecx_in,
 		  "=S" (*esi)
 		: "a" (func), "b" (ebx_in), "c" (ecx_in)
 		: "memory", "cc");
+	unprotected_formware_end();
 }
 
 static inline bool apm_bios_call_simple_asm(u32 func, u32 ebx_in,
@@ -52,6 +56,7 @@ static inline bool apm_bios_call_simple_asm(u32 func, u32 ebx_in,
 	int	cx, dx, si;
 	bool	error;
 
+	unprotected_firmware_begin();
 	/*
 	 * N.B. We do NOT need a cld after the BIOS call
 	 * because we always save and restore the flags.
@@ -68,6 +73,7 @@ static inline bool apm_bios_call_simple_asm(u32 func, u32 ebx_in,
 		  "=S" (si)
 		: "a" (func), "b" (ebx_in), "c" (ecx_in)
 		: "memory", "cc");
+	unprotected_formware_end();
 	return error;
 }
 
diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h
index 85f6ccb..25bd506 100644
--- a/arch/x86/include/asm/efi.h
+++ b/arch/x86/include/asm/efi.h
@@ -6,6 +6,7 @@
 #include <asm/pgtable.h>
 #include <asm/processor-flags.h>
 #include <asm/tlb.h>
+#include <asm/spec_ctrl.h>
 
 /*
  * We map the EFI regions needed for runtime services non-contiguously,
@@ -36,8 +37,17 @@
 
 extern asmlinkage unsigned long efi_call_phys(void *, ...);
 
-#define arch_efi_call_virt_setup()	kernel_fpu_begin()
-#define arch_efi_call_virt_teardown()	kernel_fpu_end()
+#define arch_efi_call_virt_setup()					\
+{(									\
+	kernel_fpu_begin();						\
+	unprotected_firmware_begin();					\
+)}
+
+#define arch_efi_call_virt_teardown()					\
+{(									\
+	unprotected_firmware_end();					\
+	kernel_fpu_end();						\
+)}
 
 /*
  * Wrap all the virtual calls in a way that forces the parameters on the stack.
@@ -73,6 +83,7 @@ struct efi_scratch {
 	efi_sync_low_kernel_mappings();					\
 	preempt_disable();						\
 	__kernel_fpu_begin();						\
+	unprotected_firmware_begin();					\
 									\
 	if (efi_scratch.use_pgd) {					\
 		efi_scratch.prev_cr3 = __read_cr3();			\
@@ -91,6 +102,7 @@ struct efi_scratch {
 		__flush_tlb_all();					\
 	}								\
 									\
+	unprotected_firmware_end();					\
 	__kernel_fpu_end();						\
 	preempt_enable();						\
 })
diff --git a/arch/x86/include/asm/spec_ctrl.h b/arch/x86/include/asm/spec_ctrl.h
index 28b0314..23b2804 100644
--- a/arch/x86/include/asm/spec_ctrl.h
+++ b/arch/x86/include/asm/spec_ctrl.h
@@ -113,5 +113,42 @@ static inline void unprotected_speculation_end(void)
 		rmb();
 }
 
+
+#if defined(RETPOLINE)
+/*
+ * RETPOLINE does not protect against indirect speculation
+ * in firmware code.  Enable IBRS to protect firmware execution.
+ */
+static inline void unprotected_firmware_begin(void)
+{
+	if (boot_cpu_has(X86_FEATURE_SPEC_CTRL))
+                __disable_indirect_speculation();
+	else
+		/*
+		 * If we intended to disable indirect speculation
+		 * but come here due to mis-speculation, we need
+		 * to stop the mis-speculation with rmb.
+		 */
+		rmb();
+}
+
+static inline void unprotected_firmware_end(void)
+{
+	if (boot_cpu_has(X86_FEATURE_SPEC_CTRL))
+                __enable_indirect_speculation();
+}
+
+#else
+static inline void unprotected_firmware_begin(void)
+{
+	return;
+}
+
+static inline void unprotected_firmware_end(void)
+{
+	return;
+}
+#endif
+
 #endif /* __ASSEMBLY__ */
 #endif /* _ASM_X86_SPEC_CTRL_H */
-- 
2.9.4

  parent reply	other threads:[~2018-01-04 18:18 UTC|newest]

Thread overview: 114+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-04 17:56 [PATCH 0/7] IBRS patch series Tim Chen
2018-01-04 17:56 ` [PATCH 1/7] x86/feature: Detect the x86 feature to control Speculation Tim Chen
2018-01-04 19:58   ` Greg KH
2018-01-04 20:47     ` Tim Chen
2018-01-05 11:14   ` David Woodhouse
2018-01-05 15:14     ` Tom Lendacky
2018-01-05 17:07       ` Tim Chen
2018-01-05 13:09   ` Thomas Gleixner
2018-01-05 13:44     ` Andrea Arcangeli
2018-01-05 13:51       ` Thomas Gleixner
2018-01-04 17:56 ` [PATCH 2/7] x86/enter: MACROS to set/clear IBRS Tim Chen
2018-01-04 22:16   ` Peter Zijlstra
2018-01-04 22:21     ` Tim Chen
2018-01-04 22:23       ` Dave Hansen
2018-01-05  4:54         ` Andy Lutomirski
2018-01-05  5:05           ` Dave Hansen
2018-01-05 13:19       ` Thomas Gleixner
2018-01-04 17:56 ` [PATCH 3/7] x86/enter: Use IBRS on syscall and interrupts Tim Chen
2018-01-04 20:00   ` Greg KH
2018-01-04 20:26     ` Tim Chen
2018-01-04 20:45   ` Dave Hansen
2018-01-04 22:33   ` Peter Zijlstra
2018-01-04 23:12     ` Andrea Arcangeli
2018-01-05  0:08     ` Dave Hansen
2018-01-05  4:51       ` Andy Lutomirski
2018-01-05  5:11         ` Dave Hansen
2018-01-05 12:01           ` Alan Cox
2018-01-05 13:35   ` Thomas Gleixner
2018-01-04 17:56 ` [PATCH 4/7] x86/idle: Disable IBRS entering idle and enable it on wakeup Tim Chen
2018-01-04 22:47   ` Peter Zijlstra
2018-01-04 23:00     ` Andrea Arcangeli
2018-01-04 23:22     ` Tim Chen
2018-01-04 23:42       ` Andrea Arcangeli
2018-01-04 23:45         ` Thomas Gleixner
2018-01-05  0:03           ` Andrea Arcangeli
2018-01-08  8:24       ` Peter Zijlstra
2018-01-04 17:56 ` Tim Chen [this message]
2018-01-04 18:48   ` [PATCH 5/7] x86: Use IBRS for firmware update path Alan Cox
2018-01-04 20:05   ` Greg KH
2018-01-04 20:08     ` Woodhouse, David
2018-01-05 16:08       ` gregkh
2018-01-05 16:37         ` Andrea Arcangeli
2018-01-04 20:21     ` Andrew Cooper
2018-01-04 20:48     ` Andrea Arcangeli
2018-01-04 20:51   ` Yves-Alexis Perez
2018-01-04 21:13     ` Tim Chen
2018-01-04 22:51   ` Peter Zijlstra
2018-01-05 13:40   ` Thomas Gleixner
2018-01-04 17:56 ` [PATCH 6/7] x86/spec_ctrl: Add sysctl knobs to enable/disable SPEC_CTRL feature Tim Chen
2018-01-04 18:33   ` Borislav Petkov
2018-01-04 18:36     ` Dave Hansen
2018-01-04 18:52       ` Borislav Petkov
2018-01-04 18:57         ` Andrea Arcangeli
2018-01-04 18:59         ` Dave Hansen
2018-01-04 19:06           ` Borislav Petkov
2018-01-05 13:48       ` Thomas Gleixner
2018-01-04 18:38     ` Andrea Arcangeli
2018-01-04 18:54       ` Dave Hansen
2018-01-04 18:56         ` Borislav Petkov
2018-01-04 18:55       ` Borislav Petkov
2018-01-04 18:34   ` Andrea Arcangeli
2018-01-04 19:02     ` Tim Chen
2018-01-04 18:50   ` Alan Cox
2018-01-04 20:16   ` Greg KH
2018-01-04 20:58     ` Tim Chen
2018-01-04 22:54   ` Peter Zijlstra
2018-01-04 23:26     ` Tim Chen
2018-01-04 23:51       ` Andrea Arcangeli
2018-01-04 23:59         ` Borislav Petkov
2018-01-05  0:07           ` Thomas Gleixner
2018-01-05 11:16   ` David Woodhouse
2018-01-06  1:29     ` Tim Chen
2018-01-04 17:56 ` [PATCH 7/7] x86/microcode: Recheck IBRS features on microcode reload Tim Chen
2018-01-04 18:28   ` Borislav Petkov
2018-01-04 18:34     ` Andrea Arcangeli
2018-01-04 18:50       ` Borislav Petkov
2018-01-04 19:10         ` Tim Chen
2018-01-05 13:32         ` Greg KH
2018-01-05 13:37           ` Borislav Petkov
2018-01-05 13:47             ` Greg KH
2018-01-05 15:28           ` Andrea Arcangeli
2018-01-04 19:00 ` [PATCH 0/7] IBRS patch series Linus Torvalds
2018-01-04 19:19   ` David Woodhouse
2018-01-04 19:33     ` Linus Torvalds
2018-01-04 19:39       ` David Woodhouse
2018-01-04 19:40       ` Andrew Cooper
2018-01-04 19:46         ` David Woodhouse
2018-01-04 21:22       ` Van De Ven, Arjan
2018-01-05 11:32         ` Paolo Bonzini
2018-01-05 12:09           ` Paul Turner
2018-01-05 14:45           ` Van De Ven, Arjan
2018-01-05 14:43         ` Andrea Arcangeli
2018-01-05 14:52           ` Van De Ven, Arjan
2018-01-05 15:03             ` Andrea Arcangeli
2018-01-05 14:54           ` Thomas Gleixner
2018-01-05 11:52       ` Paul Turner
2018-01-05 14:28         ` David Woodhouse
2018-01-05 14:42           ` Van De Ven, Arjan
2018-01-05 15:38             ` David Woodhouse
2018-01-05 16:05               ` Andrea Arcangeli
2018-01-05 16:37                 ` David Woodhouse
2018-01-05 16:42                   ` Andrea Arcangeli
2018-01-05 16:44                     ` Van De Ven, Arjan
2018-01-05 16:46                     ` David Woodhouse
2018-01-05  5:25   ` Florian Weimer
2018-01-05 11:05     ` David Woodhouse
2018-01-04 19:05 ` Justin Forbes
2018-01-04 19:10   ` Tim Chen
2018-01-04 21:01     ` Yves-Alexis Perez
2018-01-05 13:28       ` Greg KH
2018-01-05 13:47         ` Yves-Alexis Perez
2018-01-05 14:01           ` Greg KH
2018-01-05 14:26             ` Paolo Bonzini
2018-01-05 14:54               ` Yves-Alexis Perez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8d3710432534b27d224283557c4629cd1aa5b0ea.1515086770.git.tim.c.chen@linux.intel.com \
    --to=tim.c.chen@linux.intel.com \
    --cc=aarcange@redhat.com \
    --cc=ak@linux.intel.com \
    --cc=arjan.van.de.ven@intel.com \
    --cc=dave.hansen@intel.com \
    --cc=dwmw@amazon.co.uk \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).