All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	patches@lists.linux.dev,
	Pawan Gupta <pawan.kumar.gupta@linux.intel.com>,
	Borislav Petkov <bp@suse.de>, Ben Hutchings <ben@decadent.org.uk>,
	Samuel Mendoza-Jonas <samjonas@amazon.com>,
	Suleiman Souhlal <suleiman@google.com>
Subject: [PATCH 4.19 31/34] x86/speculation: Disable RRSBA behavior
Date: Mon, 21 Nov 2022 13:43:53 +0100	[thread overview]
Message-ID: <20221121124152.014179597@linuxfoundation.org> (raw)
In-Reply-To: <20221121124150.886779344@linuxfoundation.org>

From: Pawan Gupta <pawan.kumar.gupta@linux.intel.com>

commit 4ad3278df6fe2b0852b00d5757fc2ccd8e92c26e upstream.

Some Intel processors may use alternate predictors for RETs on
RSB-underflow. This condition may be vulnerable to Branch History
Injection (BHI) and intramode-BTI.

Kernel earlier added spectre_v2 mitigation modes (eIBRS+Retpolines,
eIBRS+LFENCE, Retpolines) which protect indirect CALLs and JMPs against
such attacks. However, on RSB-underflow, RET target prediction may
fallback to alternate predictors. As a result, RET's predicted target
may get influenced by branch history.

A new MSR_IA32_SPEC_CTRL bit (RRSBA_DIS_S) controls this fallback
behavior when in kernel mode. When set, RETs will not take predictions
from alternate predictors, hence mitigating RETs as well. Support for
this is enumerated by CPUID.7.2.EDX[RRSBA_CTRL] (bit2).

For spectre v2 mitigation, when a user selects a mitigation that
protects indirect CALLs and JMPs against BHI and intramode-BTI, set
RRSBA_DIS_S also to protect RETs for RSB-underflow case.

Signed-off-by: Pawan Gupta <pawan.kumar.gupta@linux.intel.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
[bwh: Backported to 5.15: adjust context in scattered.c]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
[sam: Fixed for missing X86_FEATURE_ENTRY_IBPB context]
Signed-off-by: Samuel Mendoza-Jonas <samjonas@amazon.com>
Signed-off-by: Suleiman Souhlal <suleiman@google.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 arch/x86/include/asm/cpufeatures.h |    2 +-
 arch/x86/include/asm/msr-index.h   |    9 +++++++++
 arch/x86/kernel/cpu/bugs.c         |   26 ++++++++++++++++++++++++++
 arch/x86/kernel/cpu/scattered.c    |    1 +
 4 files changed, 37 insertions(+), 1 deletion(-)

--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -288,7 +288,7 @@
 /* FREE!				(11*32+ 8) */
 /* FREE!				(11*32+ 9) */
 /* FREE!				(11*32+10) */
-/* FREE!				(11*32+11) */
+#define X86_FEATURE_RRSBA_CTRL		(11*32+11) /* "" RET prediction control */
 #define X86_FEATURE_RETPOLINE		(11*32+12) /* "" Generic Retpoline mitigation for Spectre variant 2 */
 #define X86_FEATURE_RETPOLINE_LFENCE	(11*32+13) /* "" Use LFENCE for Spectre variant 2 */
 
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -47,6 +47,8 @@
 #define SPEC_CTRL_STIBP			BIT(SPEC_CTRL_STIBP_SHIFT)	/* STIBP mask */
 #define SPEC_CTRL_SSBD_SHIFT		2	   /* Speculative Store Bypass Disable bit */
 #define SPEC_CTRL_SSBD			BIT(SPEC_CTRL_SSBD_SHIFT)	/* Speculative Store Bypass Disable */
+#define SPEC_CTRL_RRSBA_DIS_S_SHIFT	6	   /* Disable RRSBA behavior */
+#define SPEC_CTRL_RRSBA_DIS_S		BIT(SPEC_CTRL_RRSBA_DIS_S_SHIFT)
 
 #define MSR_IA32_PRED_CMD		0x00000049 /* Prediction Command */
 #define PRED_CMD_IBPB			BIT(0)	   /* Indirect Branch Prediction Barrier */
@@ -121,6 +123,13 @@
 						 * bit available to control VERW
 						 * behavior.
 						 */
+#define ARCH_CAP_RRSBA			BIT(19)	/*
+						 * Indicates RET may use predictors
+						 * other than the RSB. With eIBRS
+						 * enabled predictions in kernel mode
+						 * are restricted to targets in
+						 * kernel.
+						 */
 
 #define MSR_IA32_FLUSH_CMD		0x0000010b
 #define L1D_FLUSH			BIT(0)	/*
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -1181,6 +1181,22 @@ static enum spectre_v2_mitigation __init
 	return SPECTRE_V2_RETPOLINE;
 }
 
+/* Disable in-kernel use of non-RSB RET predictors */
+static void __init spec_ctrl_disable_kernel_rrsba(void)
+{
+	u64 ia32_cap;
+
+	if (!boot_cpu_has(X86_FEATURE_RRSBA_CTRL))
+		return;
+
+	ia32_cap = x86_read_arch_cap_msr();
+
+	if (ia32_cap & ARCH_CAP_RRSBA) {
+		x86_spec_ctrl_base |= SPEC_CTRL_RRSBA_DIS_S;
+		write_spec_ctrl_current(x86_spec_ctrl_base, true);
+	}
+}
+
 static void __init spectre_v2_select_mitigation(void)
 {
 	enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
@@ -1274,6 +1290,16 @@ static void __init spectre_v2_select_mit
 		break;
 	}
 
+	/*
+	 * Disable alternate RSB predictions in kernel when indirect CALLs and
+	 * JMPs gets protection against BHI and Intramode-BTI, but RET
+	 * prediction from a non-RSB predictor is still a risk.
+	 */
+	if (mode == SPECTRE_V2_EIBRS_LFENCE ||
+	    mode == SPECTRE_V2_EIBRS_RETPOLINE ||
+	    mode == SPECTRE_V2_RETPOLINE)
+		spec_ctrl_disable_kernel_rrsba();
+
 	spectre_v2_enabled = mode;
 	pr_info("%s\n", spectre_v2_strings[mode]);
 
--- a/arch/x86/kernel/cpu/scattered.c
+++ b/arch/x86/kernel/cpu/scattered.c
@@ -21,6 +21,7 @@ struct cpuid_bit {
 static const struct cpuid_bit cpuid_bits[] = {
 	{ X86_FEATURE_APERFMPERF,       CPUID_ECX,  0, 0x00000006, 0 },
 	{ X86_FEATURE_EPB,		CPUID_ECX,  3, 0x00000006, 0 },
+	{ X86_FEATURE_RRSBA_CTRL,	CPUID_EDX,  2, 0x00000007, 2 },
 	{ X86_FEATURE_CQM_LLC,		CPUID_EDX,  1, 0x0000000f, 0 },
 	{ X86_FEATURE_CQM_OCCUP_LLC,	CPUID_EDX,  0, 0x0000000f, 1 },
 	{ X86_FEATURE_CQM_MBM_TOTAL,	CPUID_EDX,  1, 0x0000000f, 1 },



  parent reply	other threads:[~2022-11-21 12:45 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-21 12:43 [PATCH 4.19 00/34] 4.19.266-rc1 review Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 01/34] Revert "x86/speculation: Add RSB VM Exit protections" Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 02/34] Revert "x86/cpu: Add a steppings field to struct x86_cpu_id" Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 03/34] x86/cpufeature: Add facility to check for min microcode revisions Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 04/34] x86/cpufeature: Fix various quality problems in the <asm/cpu_device_hd.h> header Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 05/34] x86/devicetable: Move x86 specific macro out of generic code Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 06/34] x86/cpu: Add consistent CPU match macros Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 07/34] x86/cpu: Add a steppings field to struct x86_cpu_id Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 08/34] x86/cpufeatures: Move RETPOLINE flags to word 11 Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 09/34] x86/bugs: Report AMD retbleed vulnerability Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 10/34] x86/bugs: Add AMD retbleed= boot parameter Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 11/34] x86/bugs: Keep a per-CPU IA32_SPEC_CTRL value Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 12/34] x86/entry: Remove skip_r11rcx Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 13/34] x86/entry: Add kernel IBRS implementation Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 14/34] x86/bugs: Optimize SPEC_CTRL MSR writes Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 15/34] x86/speculation: Add spectre_v2=ibrs option to support Kernel IBRS Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 16/34] x86/bugs: Split spectre_v2_select_mitigation() and spectre_v2_user_select_mitigation() Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 17/34] x86/bugs: Report Intel retbleed vulnerability Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 18/34] intel_idle: Disable IBRS during long idle Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 19/34] x86/speculation: Change FILL_RETURN_BUFFER to work with objtool Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 20/34] x86/speculation: Fix RSB filling with CONFIG_RETPOLINE=n Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 21/34] x86/speculation: Fix firmware entry SPEC_CTRL handling Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 22/34] x86/speculation: Fix SPEC_CTRL write on SMT state change Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 23/34] x86/speculation: Use cached host SPEC_CTRL value for guest entry/exit Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 24/34] x86/speculation: Remove x86_spec_ctrl_mask Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 25/34] KVM: VMX: Prevent guest RSB poisoning attacks with eIBRS Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 26/34] KVM: VMX: Fix IBRS handling after vmexit Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 27/34] x86/speculation: Fill RSB on vmexit for IBRS Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 28/34] x86/common: Stamp out the stepping madness Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 29/34] x86/cpu/amd: Enumerate BTC_NO Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 30/34] x86/bugs: Add Cannon lake to RETBleed affected CPU list Greg Kroah-Hartman
2022-11-21 12:43 ` Greg Kroah-Hartman [this message]
2022-11-21 12:43 ` [PATCH 4.19 32/34] x86/speculation: Use DECLARE_PER_CPU for x86_spec_ctrl_current Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 33/34] x86/bugs: Warn when "ibrs" mitigation is selected on Enhanced IBRS parts Greg Kroah-Hartman
2022-11-21 12:43 ` [PATCH 4.19 34/34] x86/speculation: Add RSB VM Exit protections Greg Kroah-Hartman
2022-11-21 18:47 ` [PATCH 4.19 00/34] 4.19.266-rc1 review Shuah Khan
2022-11-21 19:02 ` Pavel Machek
2022-11-22  6:29 ` Naresh Kamboju
2022-11-22 10:16 ` Jon Hunter
2022-11-22 16:58 ` Guenter Roeck
  -- strict thread matches above, loose matches on Subject: below --
2022-11-17  9:19 [PATCH 4.19 00/34] Intel RETBleed mitigations for 4.19 Suleiman Souhlal
2022-11-17  9:19 ` [PATCH 4.19 31/34] x86/speculation: Disable RRSBA behavior Suleiman Souhlal

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=20221121124152.014179597@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=ben@decadent.org.uk \
    --cc=bp@suse.de \
    --cc=patches@lists.linux.dev \
    --cc=pawan.kumar.gupta@linux.intel.com \
    --cc=samjonas@amazon.com \
    --cc=stable@vger.kernel.org \
    --cc=suleiman@google.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.