linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, David Woodhouse <dwmw@amazon.co.uk>,
	Thomas Gleixner <tglx@linutronix.de>,
	Arjan van de Ven <arjan@linux.intel.com>,
	gnomes@lxorguk.ukuu.org.uk, Rik van Riel <riel@redhat.com>,
	Andi Kleen <ak@linux.intel.com>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	thomas.lendacky@amd.com, Peter Zijlstra <peterz@infradead.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Jiri Kosina <jikos@kernel.org>,
	Andy Lutomirski <luto@amacapital.net>,
	Dave Hansen <dave.hansen@intel.com>,
	Kees Cook <keescook@google.com>,
	Tim Chen <tim.c.chen@linux.intel.com>,
	Paul Turner <pjt@google.com>,
	Greg Kroah-Hartman <gregkh@linux-foundation.org>
Subject: [PATCH 4.9 14/47] x86/retpoline: Fill RSB on context switch for affected CPUs
Date: Mon, 22 Jan 2018 09:45:25 +0100	[thread overview]
Message-ID: <20180122083926.635189815@linuxfoundation.org> (raw)
In-Reply-To: <20180122083925.568134913@linuxfoundation.org>

4.9-stable review patch.  If anyone has any objections, please let me know.

------------------

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

commit c995efd5a740d9cbafbf58bde4973e8b50b4d761 upstream.

On context switch from a shallow call stack to a deeper one, as the CPU
does 'ret' up the deeper side it may encounter RSB entries (predictions for
where the 'ret' goes to) which were populated in userspace.

This is problematic if neither SMEP nor KPTI (the latter of which marks
userspace pages as NX for the kernel) are active, as malicious code in
userspace may then be executed speculatively.

Overwrite the CPU's return prediction stack with calls which are predicted
to return to an infinite loop, to "capture" speculation if this
happens. This is required both for retpoline, and also in conjunction with
IBRS for !SMEP && !KPTI.

On Skylake+ the problem is slightly different, and an *underflow* of the
RSB may cause errant branch predictions to occur. So there it's not so much
overwrite, as *filling* the RSB to attempt to prevent it getting
empty. This is only a partial solution for Skylake+ since there are many
other conditions which may result in the RSB becoming empty. The full
solution on Skylake+ is to use IBRS, which will prevent the problem even
when the RSB becomes empty. With IBRS, the RSB-stuffing will not be
required on context switch.

[ tglx: Added missing vendor check and slighty massaged comments and
  	changelog ]

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515779365-9032-1-git-send-email-dwmw@amazon.co.uk
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 arch/x86/entry/entry_32.S          |   11 +++++++++++
 arch/x86/entry/entry_64.S          |   11 +++++++++++
 arch/x86/include/asm/cpufeatures.h |    1 +
 arch/x86/kernel/cpu/bugs.c         |   36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 59 insertions(+)

--- a/arch/x86/entry/entry_32.S
+++ b/arch/x86/entry/entry_32.S
@@ -229,6 +229,17 @@ ENTRY(__switch_to_asm)
 	movl	%ebx, PER_CPU_VAR(stack_canary)+stack_canary_offset
 #endif
 
+#ifdef CONFIG_RETPOLINE
+	/*
+	 * When switching from a shallower to a deeper call stack
+	 * the RSB may either underflow or use entries populated
+	 * with userspace addresses. On CPUs where those concerns
+	 * exist, overwrite the RSB with entries which capture
+	 * speculative execution to prevent attack.
+	 */
+	FILL_RETURN_BUFFER %ebx, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
+#endif
+
 	/* restore callee-saved registers */
 	popl	%esi
 	popl	%edi
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -427,6 +427,17 @@ ENTRY(__switch_to_asm)
 	movq	%rbx, PER_CPU_VAR(irq_stack_union)+stack_canary_offset
 #endif
 
+#ifdef CONFIG_RETPOLINE
+	/*
+	 * When switching from a shallower to a deeper call stack
+	 * the RSB may either underflow or use entries populated
+	 * with userspace addresses. On CPUs where those concerns
+	 * exist, overwrite the RSB with entries which capture
+	 * speculative execution to prevent attack.
+	 */
+	FILL_RETURN_BUFFER %r12, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
+#endif
+
 	/* restore callee-saved registers */
 	popq	%r15
 	popq	%r14
--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -200,6 +200,7 @@
 #define X86_FEATURE_INTEL_PT	( 7*32+15) /* Intel Processor Trace */
 #define X86_FEATURE_AVX512_4VNNIW (7*32+16) /* AVX-512 Neural Network Instructions */
 #define X86_FEATURE_AVX512_4FMAPS (7*32+17) /* AVX-512 Multiply Accumulation Single precision */
+#define X86_FEATURE_RSB_CTXSW		( 7*32+19) /* Fill RSB on context switches */
 
 /* Because the ALTERNATIVE scheme is for members of the X86_FEATURE club... */
 #define X86_FEATURE_KAISER	( 7*32+31) /* CONFIG_PAGE_TABLE_ISOLATION w/o nokaiser */
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -22,6 +22,7 @@
 #include <asm/alternative.h>
 #include <asm/pgtable.h>
 #include <asm/cacheflush.h>
+#include <asm/intel-family.h>
 
 static void __init spectre_v2_select_mitigation(void);
 
@@ -154,6 +155,23 @@ disable:
 	return SPECTRE_V2_CMD_NONE;
 }
 
+/* Check for Skylake-like CPUs (for RSB handling) */
+static bool __init is_skylake_era(void)
+{
+	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
+	    boot_cpu_data.x86 == 6) {
+		switch (boot_cpu_data.x86_model) {
+		case INTEL_FAM6_SKYLAKE_MOBILE:
+		case INTEL_FAM6_SKYLAKE_DESKTOP:
+		case INTEL_FAM6_SKYLAKE_X:
+		case INTEL_FAM6_KABYLAKE_MOBILE:
+		case INTEL_FAM6_KABYLAKE_DESKTOP:
+			return true;
+		}
+	}
+	return false;
+}
+
 static void __init spectre_v2_select_mitigation(void)
 {
 	enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
@@ -212,6 +230,24 @@ retpoline_auto:
 
 	spectre_v2_enabled = mode;
 	pr_info("%s\n", spectre_v2_strings[mode]);
+
+	/*
+	 * If neither SMEP or KPTI are available, there is a risk of
+	 * hitting userspace addresses in the RSB after a context switch
+	 * from a shallow call stack to a deeper one. To prevent this fill
+	 * the entire RSB, even when using IBRS.
+	 *
+	 * Skylake era CPUs have a separate issue with *underflow* of the
+	 * RSB, when they will predict 'ret' targets from the generic BTB.
+	 * The proper mitigation for this is IBRS. If IBRS is not supported
+	 * or deactivated in favour of retpolines the RSB fill on context
+	 * switch is required.
+	 */
+	if ((!boot_cpu_has(X86_FEATURE_KAISER) &&
+	     !boot_cpu_has(X86_FEATURE_SMEP)) || is_skylake_era()) {
+		setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
+		pr_info("Filling RSB on context switch\n");
+	}
 }
 
 #undef pr_fmt

  parent reply	other threads:[~2018-01-22  8:47 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-22  8:45 [PATCH 4.9 00/47] 4.9.78-stable review Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 01/47] libnvdimm, btt: Fix an incompatibility in the log layout Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 02/47] scsi: sg: disable SET_FORCE_LOW_DMA Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 03/47] futex: Prevent overflow by strengthen input validation Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 04/47] ALSA: seq: Make ioctls race-free Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 05/47] ALSA: pcm: Remove yet superfluous WARN_ON() Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 06/47] ALSA: hda - Apply headphone noise quirk for another Dell XPS 13 variant Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 07/47] ALSA: hda - Apply the existing quirk to iMac 14,1 Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 08/47] timers: Unconditionally check deferrable base Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 09/47] af_key: fix buffer overread in verify_address_len() Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 10/47] af_key: fix buffer overread in parse_exthdrs() Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 11/47] iser-target: Fix possible use-after-free in connection establishment error Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 12/47] scsi: hpsa: fix volume offline state Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 13/47] sched/deadline: Zero out positive runtime after throttling constrained tasks Greg Kroah-Hartman
2018-01-22  8:45 ` Greg Kroah-Hartman [this message]
2018-01-22  8:45 ` [PATCH 4.9 15/47] x86/retpoline: Add LFENCE to the retpoline/RSB filling RSB macros Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 16/47] objtool: Improve error message for bad file argument Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 17/47] x86/cpufeature: Move processor tracing out of scattered features Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 18/47] module: Add retpoline tag to VERMAGIC Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 19/47] x86/mm/pkeys: Fix fill_sig_info_pkey Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 20/47] x86/tsc: Fix erroneous TSC rate on Skylake Xeon Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 21/47] pipe: avoid round_pipe_size() nr_pages overflow on 32-bit Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 22/47] x86/apic/vector: Fix off by one in error path Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 23/47] perf tools: Fix build with ARCH=x86_64 Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 24/47] Input: ALPS - fix multi-touch decoding on SS4 plus touchpads Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 25/47] Input: 88pm860x-ts - fix child-node lookup Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 26/47] Input: twl6040-vibra " Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 27/47] Input: twl4030-vibra - fix sibling-node lookup Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 28/47] tracing: Fix converting enums from the map in trace_event_eval_update() Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 29/47] phy: work around phys references to usb-nop-xceiv devices Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 30/47] ARM: sunxi_defconfig: Enable CMA Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 31/47] ARM: dts: kirkwood: fix pin-muxing of MPP7 on OpenBlocks A7 Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 32/47] can: peak: fix potential bug in packet fragmentation Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 33/47] scripts/gdb/linux/tasks.py: fix get_thread_info Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 34/47] proc: fix coredump vs read /proc/*/stat race Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 35/47] libata: apply MAX_SEC_1024 to all LITEON EP1 series devices Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 36/47] workqueue: avoid hard lockups in show_workqueue_state() Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 37/47] dm btree: fix serious bug in btree_split_beneath() Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 38/47] dm thin metadata: THIN_MAX_CONCURRENT_LOCKS should be 6 Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 39/47] arm64: KVM: Fix SMCCC handling of unimplemented SMC/HVC calls Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 40/47] x86/cpu, x86/pti: Do not enable PTI on AMD processors Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 41/47] usbip: fix warning in vhci_hcd_probe/lockdep_init_map Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 42/47] x86/mce: Make machine check speculation protected Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 43/47] retpoline: Introduce start/end markers of indirect thunk Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 44/47] kprobes/x86: Blacklist indirect thunk functions for kprobes Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 45/47] kprobes/x86: Disable optimizing on the function jumps to indirect thunk Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 46/47] x86/pti: Document fix wrong index Greg Kroah-Hartman
2018-01-22  8:45 ` [PATCH 4.9 47/47] x86/retpoline: Optimize inline assembler for vmexit_fill_RSB Greg Kroah-Hartman
2018-01-22 18:50 ` [PATCH 4.9 00/47] 4.9.78-stable review Naresh Kamboju
2018-01-22 19:09 ` Guenter Roeck
2018-01-22 20:08 ` Shuah Khan
2018-01-23  0:08 ` kernelci.org bot

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=20180122083926.635189815@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=ak@linux.intel.com \
    --cc=arjan@linux.intel.com \
    --cc=dave.hansen@intel.com \
    --cc=dwmw@amazon.co.uk \
    --cc=gnomes@lxorguk.ukuu.org.uk \
    --cc=gregkh@linux-foundation.org \
    --cc=jikos@kernel.org \
    --cc=jpoimboe@redhat.com \
    --cc=keescook@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=peterz@infradead.org \
    --cc=pjt@google.com \
    --cc=riel@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=thomas.lendacky@amd.com \
    --cc=tim.c.chen@linux.intel.com \
    --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).