Skip to content

Commit 4ad3278

Browse files
pa1guptasuryasaimadhu
authored andcommitted
x86/speculation: Disable RRSBA behavior
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 <[email protected]> Signed-off-by: Borislav Petkov <[email protected]>
1 parent 697977d commit 4ad3278

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

arch/x86/include/asm/cpufeatures.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@
297297
#define X86_FEATURE_SGX1 (11*32+ 8) /* "" Basic SGX */
298298
#define X86_FEATURE_SGX2 (11*32+ 9) /* "" SGX Enclave Dynamic Memory Management (EDMM) */
299299
#define X86_FEATURE_ENTRY_IBPB (11*32+10) /* "" Issue an IBPB on kernel entry */
300-
/* FREE! (11*32+11) */
300+
#define X86_FEATURE_RRSBA_CTRL (11*32+11) /* "" RET prediction control */
301301
#define X86_FEATURE_RETPOLINE (11*32+12) /* "" Generic Retpoline mitigation for Spectre variant 2 */
302302
#define X86_FEATURE_RETPOLINE_LFENCE (11*32+13) /* "" Use LFENCE for Spectre variant 2 */
303303
#define X86_FEATURE_RETHUNK (11*32+14) /* "" Use REturn THUNK */

arch/x86/include/asm/msr-index.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
#define SPEC_CTRL_STIBP BIT(SPEC_CTRL_STIBP_SHIFT) /* STIBP mask */
5252
#define SPEC_CTRL_SSBD_SHIFT 2 /* Speculative Store Bypass Disable bit */
5353
#define SPEC_CTRL_SSBD BIT(SPEC_CTRL_SSBD_SHIFT) /* Speculative Store Bypass Disable */
54+
#define SPEC_CTRL_RRSBA_DIS_S_SHIFT 6 /* Disable RRSBA behavior */
55+
#define SPEC_CTRL_RRSBA_DIS_S BIT(SPEC_CTRL_RRSBA_DIS_S_SHIFT)
5456

5557
#define MSR_IA32_PRED_CMD 0x00000049 /* Prediction Command */
5658
#define PRED_CMD_IBPB BIT(0) /* Indirect Branch Prediction Barrier */
@@ -141,6 +143,13 @@
141143
* bit available to control VERW
142144
* behavior.
143145
*/
146+
#define ARCH_CAP_RRSBA BIT(19) /*
147+
* Indicates RET may use predictors
148+
* other than the RSB. With eIBRS
149+
* enabled predictions in kernel mode
150+
* are restricted to targets in
151+
* kernel.
152+
*/
144153

145154
#define MSR_IA32_FLUSH_CMD 0x0000010b
146155
#define L1D_FLUSH BIT(0) /*

arch/x86/kernel/cpu/bugs.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,22 @@ static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void)
13181318
return SPECTRE_V2_RETPOLINE;
13191319
}
13201320

1321+
/* Disable in-kernel use of non-RSB RET predictors */
1322+
static void __init spec_ctrl_disable_kernel_rrsba(void)
1323+
{
1324+
u64 ia32_cap;
1325+
1326+
if (!boot_cpu_has(X86_FEATURE_RRSBA_CTRL))
1327+
return;
1328+
1329+
ia32_cap = x86_read_arch_cap_msr();
1330+
1331+
if (ia32_cap & ARCH_CAP_RRSBA) {
1332+
x86_spec_ctrl_base |= SPEC_CTRL_RRSBA_DIS_S;
1333+
write_spec_ctrl_current(x86_spec_ctrl_base, true);
1334+
}
1335+
}
1336+
13211337
static void __init spectre_v2_select_mitigation(void)
13221338
{
13231339
enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
@@ -1412,6 +1428,16 @@ static void __init spectre_v2_select_mitigation(void)
14121428
break;
14131429
}
14141430

1431+
/*
1432+
* Disable alternate RSB predictions in kernel when indirect CALLs and
1433+
* JMPs gets protection against BHI and Intramode-BTI, but RET
1434+
* prediction from a non-RSB predictor is still a risk.
1435+
*/
1436+
if (mode == SPECTRE_V2_EIBRS_LFENCE ||
1437+
mode == SPECTRE_V2_EIBRS_RETPOLINE ||
1438+
mode == SPECTRE_V2_RETPOLINE)
1439+
spec_ctrl_disable_kernel_rrsba();
1440+
14151441
spectre_v2_enabled = mode;
14161442
pr_info("%s\n", spectre_v2_strings[mode]);
14171443

arch/x86/kernel/cpu/scattered.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static const struct cpuid_bit cpuid_bits[] = {
2727
{ X86_FEATURE_APERFMPERF, CPUID_ECX, 0, 0x00000006, 0 },
2828
{ X86_FEATURE_EPB, CPUID_ECX, 3, 0x00000006, 0 },
2929
{ X86_FEATURE_INTEL_PPIN, CPUID_EBX, 0, 0x00000007, 1 },
30+
{ X86_FEATURE_RRSBA_CTRL, CPUID_EDX, 2, 0x00000007, 2 },
3031
{ X86_FEATURE_CQM_LLC, CPUID_EDX, 1, 0x0000000f, 0 },
3132
{ X86_FEATURE_CQM_OCCUP_LLC, CPUID_EDX, 0, 0x0000000f, 1 },
3233
{ X86_FEATURE_CQM_MBM_TOTAL, CPUID_EDX, 1, 0x0000000f, 1 },

tools/arch/x86/include/asm/msr-index.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
#define SPEC_CTRL_STIBP BIT(SPEC_CTRL_STIBP_SHIFT) /* STIBP mask */
5252
#define SPEC_CTRL_SSBD_SHIFT 2 /* Speculative Store Bypass Disable bit */
5353
#define SPEC_CTRL_SSBD BIT(SPEC_CTRL_SSBD_SHIFT) /* Speculative Store Bypass Disable */
54+
#define SPEC_CTRL_RRSBA_DIS_S_SHIFT 6 /* Disable RRSBA behavior */
55+
#define SPEC_CTRL_RRSBA_DIS_S BIT(SPEC_CTRL_RRSBA_DIS_S_SHIFT)
5456

5557
#define MSR_IA32_PRED_CMD 0x00000049 /* Prediction Command */
5658
#define PRED_CMD_IBPB BIT(0) /* Indirect Branch Prediction Barrier */
@@ -140,6 +142,13 @@
140142
* bit available to control VERW
141143
* behavior.
142144
*/
145+
#define ARCH_CAP_RRSBA BIT(19) /*
146+
* Indicates RET may use predictors
147+
* other than the RSB. With eIBRS
148+
* enabled predictions in kernel mode
149+
* are restricted to targets in
150+
* kernel.
151+
*/
143152

144153
#define MSR_IA32_FLUSH_CMD 0x0000010b
145154
#define L1D_FLUSH BIT(0) /*

0 commit comments

Comments
 (0)