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>,
David Woodhouse <dwmw@amazon.co.uk>,
linux-kernel@vger.kernel.org
Subject: [PATCH v2 8/8] x86: Use IBRS for firmware update path
Date: Fri, 5 Jan 2018 18:12:23 -0800 [thread overview]
Message-ID: <cda39b693d0fec41bd65aa17e7d7ea6bcbf48cc0.1515204614.git.tim.c.chen@linux.intel.com> (raw)
In-Reply-To: <cover.1515204614.git.tim.c.chen@linux.intel.com>
In-Reply-To: <cover.1515204614.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 | 17 +++++++++++++--
arch/x86/include/asm/spec_ctrl.h | 3 +++
arch/x86/kernel/cpu/spec_ctrl.c | 45 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 69 insertions(+), 2 deletions(-)
diff --git a/arch/x86/include/asm/apm.h b/arch/x86/include/asm/apm.h
index 4d4015d..fbf4212 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_firmware_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_firmware_end();
return error;
}
diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h
index 85f6ccb..439bd55 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,18 @@
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 +84,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 +103,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 be08ae7..ce21d24 100644
--- a/arch/x86/include/asm/spec_ctrl.h
+++ b/arch/x86/include/asm/spec_ctrl.h
@@ -11,6 +11,9 @@ void scan_spec_ctrl_feature(struct cpuinfo_x86 *c);
void rescan_spec_ctrl_feature(struct cpuinfo_x86 *c);
bool ibrs_inuse(void);
+void unprotected_firmware_begin(void);
+void unprotected_firmware_end(void);
+
extern unsigned int dynamic_ibrs;
static inline void __disable_indirect_speculation(void)
diff --git a/arch/x86/kernel/cpu/spec_ctrl.c b/arch/x86/kernel/cpu/spec_ctrl.c
index 076c470..59bf561 100644
--- a/arch/x86/kernel/cpu/spec_ctrl.c
+++ b/arch/x86/kernel/cpu/spec_ctrl.c
@@ -10,6 +10,10 @@
unsigned int dynamic_ibrs __read_mostly;
EXPORT_SYMBOL_GPL(dynamic_ibrs);
+#if defined(RETPOLINE)
+static unsigned int firmware_ibrs __read_mostly;
+#endif
+
enum {
IBRS_DISABLED,
/* in host kernel, disabled in guest and userland */
@@ -31,6 +35,8 @@ static inline void set_ibrs_feature(void)
dynamic_ibrs = 1;
ibrs_enabled = IBRS_ENABLED;
}
+#else
+ firmware_ibrs = 1;
#endif
}
@@ -162,3 +168,42 @@ static int __init debugfs_spec_ctrl(void)
return 0;
}
late_initcall(debugfs_spec_ctrl);
+
+#if defined(RETPOLINE)
+/*
+ * RETPOLINE does not protect against indirect speculation
+ * in firmware code. Enable IBRS to protect firmware execution.
+ */
+void unprotected_firmware_begin(void)
+{
+ if (firmware_ibrs) {
+ __disable_indirect_speculation();
+ } else {
+ /*
+ * rmb prevent unwanted speculation when we
+ * are setting IBRS
+ */
+ rmb();
+ }
+}
+EXPORT_SYMBOL_GPL(unprotected_firmware_begin);
+
+void unprotected_firmware_end(void)
+{
+ if (firmware_ibrs) {
+ __enable_indirect_speculation();
+ }
+}
+EXPORT_SYMBOL_GPL(unprotected_firmware_end);
+
+#else
+void unprotected_firmware_begin(void)
+{
+}
+EXPORT_SYMBOL_GPL(unprotected_firmware_begin);
+
+void unprotected_firmware_end(void)
+{
+}
+EXPORT_SYMBOL_GPL(unprotected_firmware_end);
+#endif
--
2.9.4
next prev parent reply other threads:[~2018-01-06 2:33 UTC|newest]
Thread overview: 70+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-06 2:12 [PATCH v2 0/8] IBRS patch series Tim Chen
2018-01-06 2:12 ` [PATCH v2 1/8] x86/feature: Detect the x86 IBRS feature to control Speculation Tim Chen
2018-01-06 12:56 ` Borislav Petkov
2018-01-07 17:14 ` Tim Chen
2018-01-07 18:31 ` Borislav Petkov
2018-01-09 18:13 ` Dave Hansen
2018-01-09 18:55 ` Borislav Petkov
2018-01-08 16:14 ` Paolo Bonzini
2018-01-09 10:39 ` Paolo Bonzini
2018-01-09 17:53 ` Tim Chen
2018-01-09 17:58 ` Paolo Bonzini
2018-01-09 22:59 ` Tim Chen
2018-01-18 23:28 ` Andy Lutomirski
2018-01-06 2:12 ` [PATCH v2 2/8] x86/enter: MACROS to set/clear IBRS Tim Chen
2018-01-07 12:03 ` Borislav Petkov
2018-01-07 17:12 ` Tim Chen
2018-01-07 18:44 ` Borislav Petkov
2018-01-08 22:24 ` Tim Chen
2018-01-06 2:12 ` [PATCH v2 3/8] x86/enter: Use IBRS on syscall and interrupts Tim Chen
2018-01-07 19:27 ` Borislav Petkov
2018-01-06 2:12 ` [PATCH v2 4/8] x86/spec_ctrl: Add sysctl knobs to enable/disable SPEC_CTRL feature Tim Chen
2018-01-06 3:12 ` Dave Hansen
2018-01-08 12:47 ` Peter Zijlstra
2018-01-08 16:14 ` Peter Zijlstra
2018-01-08 17:28 ` Tim Chen
2018-01-08 17:42 ` Peter Zijlstra
2018-01-08 19:34 ` Woodhouse, David
2018-01-08 19:52 ` Lu, Hongjiu
2018-01-09 10:40 ` Thomas Gleixner
2018-01-09 17:55 ` Tim Chen
2018-01-09 18:13 ` David Woodhouse
2018-01-09 20:31 ` Tim Chen
2018-01-27 13:59 ` Konrad Rzeszutek Wilk
2018-01-27 14:26 ` David Woodhouse
2018-01-06 8:54 ` Greg KH
2018-01-06 18:10 ` Tim Chen
2018-01-06 21:25 ` Konrad Rzeszutek Wilk
2018-01-07 8:20 ` Greg KH
2018-01-06 14:41 ` Konrad Rzeszutek Wilk
2018-01-06 17:33 ` Dave Hansen
2018-01-06 17:41 ` Van De Ven, Arjan
2018-01-06 19:22 ` Dave Hansen
2018-01-06 19:47 ` Thomas Gleixner
2018-01-06 21:32 ` Konrad Rzeszutek Wilk
2018-01-06 21:34 ` Van De Ven, Arjan
2018-01-06 21:41 ` Konrad Rzeszutek Wilk
2018-01-06 21:44 ` Van De Ven, Arjan
2018-01-06 21:39 ` Thomas Gleixner
2018-01-06 21:46 ` Is: Linus, name for 'spectre' variable. Was:Re: " Konrad Rzeszutek Wilk
2018-01-06 18:23 ` Tim Chen
2018-01-06 18:20 ` Tim Chen
2018-01-08 15:08 ` Peter Zijlstra
2018-01-08 15:29 ` Van De Ven, Arjan
2018-01-08 17:02 ` Tim Chen
2018-01-08 15:11 ` Peter Zijlstra
2018-01-08 15:15 ` Peter Zijlstra
2018-01-08 15:53 ` Peter Zijlstra
2018-01-09 0:29 ` Borislav Petkov
2018-01-09 18:05 ` Tim Chen
2018-01-06 2:12 ` [PATCH v2 5/8] x86/idle: Disable IBRS entering idle and enable it on wakeup Tim Chen
2018-01-06 2:12 ` [PATCH v2 6/8] x86/microcode: Recheck IBRS features on microcode reload Tim Chen
2018-01-06 12:09 ` Woodhouse, David
2018-01-09 0:34 ` Borislav Petkov
2018-01-06 2:12 ` [PATCH v2 7/8] x86: Do not use dynamic IBRS if retpoline is enabled Tim Chen
2018-01-06 2:12 ` Tim Chen [this message]
2018-01-06 8:55 ` [PATCH v2 8/8] x86: Use IBRS for firmware update path Greg KH
2018-01-06 8:57 ` Greg KH
2018-01-06 6:43 ` [PATCH v2 0/8] IBRS patch series Tim Chen
2018-01-06 12:00 ` Woodhouse, David
2018-01-06 12:11 ` Woodhouse, David
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=cda39b693d0fec41bd65aa17e7d7ea6bcbf48cc0.1515204614.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
Powered by JetHome