Skip to content

Commit 5e38b08

Browse files
Daniel Sneddonsmb49
Daniel Sneddon
authored andcommitted
x86/speculation: Add RSB VM Exit protections
commit 2b12993 upstream. tl;dr: The Enhanced IBRS mitigation for Spectre v2 does not work as documented for RET instructions after VM exits. Mitigate it with a new one-entry RSB stuffing mechanism and a new LFENCE. == Background == Indirect Branch Restricted Speculation (IBRS) was designed to help mitigate Branch Target Injection and Speculative Store Bypass, i.e. Spectre, attacks. IBRS prevents software run in less privileged modes from affecting branch prediction in more privileged modes. IBRS requires the MSR to be written on every privilege level change. To overcome some of the performance issues of IBRS, Enhanced IBRS was introduced. eIBRS is an "always on" IBRS, in other words, just turn it on once instead of writing the MSR on every privilege level change. When eIBRS is enabled, more privileged modes should be protected from less privileged modes, including protecting VMMs from guests. == Problem == Here's a simplification of how guests are run on Linux' KVM: void run_kvm_guest(void) { // Prepare to run guest VMRESUME(); // Clean up after guest runs } The execution flow for that would look something like this to the processor: 1. Host-side: call run_kvm_guest() 2. Host-side: VMRESUME 3. Guest runs, does "CALL guest_function" 4. VM exit, host runs again 5. Host might make some "cleanup" function calls 6. Host-side: RET from run_kvm_guest() Now, when back on the host, there are a couple of possible scenarios of post-guest activity the host needs to do before executing host code: * on pre-eIBRS hardware (legacy IBRS, or nothing at all), the RSB is not touched and Linux has to do a 32-entry stuffing. * on eIBRS hardware, VM exit with IBRS enabled, or restoring the host IBRS=1 shortly after VM exit, has a documented side effect of flushing the RSB except in this PBRSB situation where the software needs to stuff the last RSB entry "by hand". IOW, with eIBRS supported, host RET instructions should no longer be influenced by guest behavior after the host retires a single CALL instruction. However, if the RET instructions are "unbalanced" with CALLs after a VM exit as is the RET in #6, it might speculatively use the address for the instruction after the CALL in #3 as an RSB prediction. This is a problem since the (untrusted) guest controls this address. Balanced CALL/RET instruction pairs such as in step #5 are not affected. == Solution == The PBRSB issue affects a wide variety of Intel processors which support eIBRS. But not all of them need mitigation. Today, X86_FEATURE_RSB_VMEXIT triggers an RSB filling sequence that mitigates PBRSB. Systems setting RSB_VMEXIT need no further mitigation - i.e., eIBRS systems which enable legacy IBRS explicitly. However, such systems (X86_FEATURE_IBRS_ENHANCED) do not set RSB_VMEXIT and most of them need a new mitigation. Therefore, introduce a new feature flag X86_FEATURE_RSB_VMEXIT_LITE which triggers a lighter-weight PBRSB mitigation versus RSB_VMEXIT. The lighter-weight mitigation performs a CALL instruction which is immediately followed by a speculative execution barrier (INT3). This steers speculative execution to the barrier -- just like a retpoline -- which ensures that speculation can never reach an unbalanced RET. Then, ensure this CALL is retired before continuing execution with an LFENCE. In other words, the window of exposure is opened at VM exit where RET behavior is troublesome. While the window is open, force RSB predictions sampling for RET targets to a dead end at the INT3. Close the window with the LFENCE. There is a subset of eIBRS systems which are not vulnerable to PBRSB. Add these systems to the cpu_vuln_whitelist[] as NO_EIBRS_PBRSB. Future systems that aren't vulnerable will set ARCH_CAP_PBRSB_NO. [ bp: Massage, incorporate review comments from Andy Cooper. ] Signed-off-by: Daniel Sneddon <[email protected]> Co-developed-by: Pawan Gupta <[email protected]> Signed-off-by: Pawan Gupta <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> [cascardo: no intra-function validation] Signed-off-by: Thadeu Lima de Souza Cascardo <[email protected]> CVE-2022-29901 Acked-by: Stefan Bader <[email protected]> Acked-by: Tim Gardner <[email protected]> Signed-off-by: Stefan Bader <[email protected]>
1 parent 7ba59ec commit 5e38b08

File tree

8 files changed

+108
-29
lines changed

8 files changed

+108
-29
lines changed

Documentation/admin-guide/hw-vuln/spectre.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,14 @@ The possible values in this file are:
422422
'RSB filling' Protection of RSB on context switch enabled
423423
============= ===========================================
424424

425+
- EIBRS Post-barrier Return Stack Buffer (PBRSB) protection status:
426+
427+
=========================== =======================================================
428+
'PBRSB-eIBRS: SW sequence' CPU is affected and protection of RSB on VMEXIT enabled
429+
'PBRSB-eIBRS: Vulnerable' CPU is vulnerable
430+
'PBRSB-eIBRS: Not affected' CPU is not affected by PBRSB
431+
=========================== =======================================================
432+
425433
Full mitigation might require a microcode update from the CPU
426434
vendor. When the necessary microcode is not available, the kernel will
427435
report vulnerability.

arch/x86/include/asm/cpufeatures.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@
289289
#define X86_FEATURE_RRSBA_CTRL (11*32+11) /* "" RET prediction control */
290290
#define X86_FEATURE_RETPOLINE (11*32+12) /* "" Generic Retpoline mitigation for Spectre variant 2 */
291291
#define X86_FEATURE_RETPOLINE_LFENCE (11*32+13) /* "" Use LFENCE for Spectre variant 2 */
292+
#define X86_FEATURE_RSB_VMEXIT_LITE (11*32+17) /* "" Fill RSB on VM exit when EIBRS is enabled */
292293

293294
/* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */
294295
#define X86_FEATURE_AVX512_BF16 (12*32+ 5) /* AVX512 BFLOAT16 instructions */
@@ -411,6 +412,7 @@
411412
#define X86_BUG_SRBDS X86_BUG(24) /* CPU may leak RNG bits if not mitigated */
412413
#define X86_BUG_MMIO_STALE_DATA X86_BUG(25) /* CPU is affected by Processor MMIO Stale Data vulnerabilities */
413414
#define X86_BUG_RETBLEED X86_BUG(26) /* CPU is affected by RETBleed */
415+
#define X86_BUG_EIBRS_PBRSB X86_BUG(27) /* EIBRS is vulnerable to Post Barrier RSB Predictions */
414416
#define X86_BUG_MMIO_UNKNOWN X86_BUG(28) /* CPU is too old and its MMIO Stale Data status is unknown */
415417

416418
#endif /* _ASM_X86_CPUFEATURES_H */

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@
139139
* are restricted to targets in
140140
* kernel.
141141
*/
142+
#define ARCH_CAP_PBRSB_NO BIT(24) /*
143+
* Not susceptible to Post-Barrier
144+
* Return Stack Buffer Predictions.
145+
*/
142146

143147
#define MSR_IA32_FLUSH_CMD 0x0000010b
144148
#define L1D_FLUSH BIT(0) /*

arch/x86/include/asm/nospec-branch.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,27 @@
137137
#endif
138138
.endm
139139

140+
.macro ISSUE_UNBALANCED_RET_GUARD
141+
call .Lunbalanced_ret_guard_\@
142+
int3
143+
.Lunbalanced_ret_guard_\@:
144+
add $(BITS_PER_LONG/8), %_ASM_SP
145+
lfence
146+
.endm
147+
140148
/*
141149
* A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
142150
* monstrosity above, manually.
143151
*/
144-
.macro FILL_RETURN_BUFFER reg:req nr:req ftr:req
152+
.macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2
153+
.ifb \ftr2
145154
ALTERNATIVE "jmp .Lskip_rsb_\@", "", \ftr
155+
.else
156+
ALTERNATIVE_2 "jmp .Lskip_rsb_\@", "", \ftr, "jmp .Lunbalanced_\@", \ftr2
157+
.endif
146158
__FILL_RETURN_BUFFER(\reg,\nr,%_ASM_SP)
159+
.Lunbalanced_\@:
160+
ISSUE_UNBALANCED_RET_GUARD
147161
.Lskip_rsb_\@:
148162
.endm
149163

arch/x86/kernel/cpu/bugs.c

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,53 @@ static void __init spec_ctrl_disable_kernel_rrsba(void)
11981198
}
11991199
}
12001200

1201+
static void __init spectre_v2_determine_rsb_fill_type_at_vmexit(enum spectre_v2_mitigation mode)
1202+
{
1203+
/*
1204+
* Similar to context switches, there are two types of RSB attacks
1205+
* after VM exit:
1206+
*
1207+
* 1) RSB underflow
1208+
*
1209+
* 2) Poisoned RSB entry
1210+
*
1211+
* When retpoline is enabled, both are mitigated by filling/clearing
1212+
* the RSB.
1213+
*
1214+
* When IBRS is enabled, while #1 would be mitigated by the IBRS branch
1215+
* prediction isolation protections, RSB still needs to be cleared
1216+
* because of #2. Note that SMEP provides no protection here, unlike
1217+
* user-space-poisoned RSB entries.
1218+
*
1219+
* eIBRS should protect against RSB poisoning, but if the EIBRS_PBRSB
1220+
* bug is present then a LITE version of RSB protection is required,
1221+
* just a single call needs to retire before a RET is executed.
1222+
*/
1223+
switch (mode) {
1224+
case SPECTRE_V2_NONE:
1225+
return;
1226+
1227+
case SPECTRE_V2_EIBRS_LFENCE:
1228+
case SPECTRE_V2_EIBRS:
1229+
if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
1230+
setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT_LITE);
1231+
pr_info("Spectre v2 / PBRSB-eIBRS: Retire a single CALL on VMEXIT\n");
1232+
}
1233+
return;
1234+
1235+
case SPECTRE_V2_EIBRS_RETPOLINE:
1236+
case SPECTRE_V2_RETPOLINE:
1237+
case SPECTRE_V2_LFENCE:
1238+
case SPECTRE_V2_IBRS:
1239+
setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT);
1240+
pr_info("Spectre v2 / SpectreRSB : Filling RSB on VMEXIT\n");
1241+
return;
1242+
}
1243+
1244+
pr_warn_once("Unknown Spectre v2 mode, disabling RSB mitigation at VM exit");
1245+
dump_stack();
1246+
}
1247+
12011248
static void __init spectre_v2_select_mitigation(void)
12021249
{
12031250
enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
@@ -1347,28 +1394,7 @@ static void __init spectre_v2_select_mitigation(void)
13471394
setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
13481395
pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
13491396

1350-
/*
1351-
* Similar to context switches, there are two types of RSB attacks
1352-
* after vmexit:
1353-
*
1354-
* 1) RSB underflow
1355-
*
1356-
* 2) Poisoned RSB entry
1357-
*
1358-
* When retpoline is enabled, both are mitigated by filling/clearing
1359-
* the RSB.
1360-
*
1361-
* When IBRS is enabled, while #1 would be mitigated by the IBRS branch
1362-
* prediction isolation protections, RSB still needs to be cleared
1363-
* because of #2. Note that SMEP provides no protection here, unlike
1364-
* user-space-poisoned RSB entries.
1365-
*
1366-
* eIBRS, on the other hand, has RSB-poisoning protections, so it
1367-
* doesn't need RSB clearing after vmexit.
1368-
*/
1369-
if (boot_cpu_has(X86_FEATURE_RETPOLINE) ||
1370-
boot_cpu_has(X86_FEATURE_KERNEL_IBRS))
1371-
setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT);
1397+
spectre_v2_determine_rsb_fill_type_at_vmexit(mode);
13721398

13731399
/*
13741400
* Retpoline protects the kernel, but doesn't protect firmware. IBRS
@@ -2108,6 +2134,19 @@ static char *ibpb_state(void)
21082134
return "";
21092135
}
21102136

2137+
static char *pbrsb_eibrs_state(void)
2138+
{
2139+
if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
2140+
if (boot_cpu_has(X86_FEATURE_RSB_VMEXIT_LITE) ||
2141+
boot_cpu_has(X86_FEATURE_RSB_VMEXIT))
2142+
return ", PBRSB-eIBRS: SW sequence";
2143+
else
2144+
return ", PBRSB-eIBRS: Vulnerable";
2145+
} else {
2146+
return ", PBRSB-eIBRS: Not affected";
2147+
}
2148+
}
2149+
21112150
static ssize_t spectre_v2_show_state(char *buf)
21122151
{
21132152
if (spectre_v2_enabled == SPECTRE_V2_LFENCE)
@@ -2120,12 +2159,13 @@ static ssize_t spectre_v2_show_state(char *buf)
21202159
spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
21212160
return sprintf(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
21222161

2123-
return sprintf(buf, "%s%s%s%s%s%s\n",
2162+
return sprintf(buf, "%s%s%s%s%s%s%s\n",
21242163
spectre_v2_strings[spectre_v2_enabled],
21252164
ibpb_state(),
21262165
boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
21272166
stibp_state(),
21282167
boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
2168+
pbrsb_eibrs_state(),
21292169
spectre_v2_module_string());
21302170
}
21312171

arch/x86/kernel/cpu/common.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
10251025
#define NO_SWAPGS BIT(6)
10261026
#define NO_ITLB_MULTIHIT BIT(7)
10271027
#define NO_SPECTRE_V2 BIT(8)
1028+
#define NO_EIBRS_PBRSB BIT(9)
10281029
#define NO_MMIO BIT(10)
10291030

10301031
#define VULNWL(_vendor, _family, _model, _whitelist) \
@@ -1071,7 +1072,7 @@ static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
10711072

10721073
VULNWL_INTEL(ATOM_GOLDMONT, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
10731074
VULNWL_INTEL(ATOM_GOLDMONT_D, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
1074-
VULNWL_INTEL(ATOM_GOLDMONT_PLUS, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
1075+
VULNWL_INTEL(ATOM_GOLDMONT_PLUS, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB),
10751076

10761077
/*
10771078
* Technically, swapgs isn't serializing on AMD (despite it previously
@@ -1081,7 +1082,9 @@ static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
10811082
* good enough for our purposes.
10821083
*/
10831084

1084-
VULNWL_INTEL(ATOM_TREMONT_D, NO_ITLB_MULTIHIT),
1085+
VULNWL_INTEL(ATOM_TREMONT, NO_EIBRS_PBRSB),
1086+
VULNWL_INTEL(ATOM_TREMONT_L, NO_EIBRS_PBRSB),
1087+
VULNWL_INTEL(ATOM_TREMONT_D, NO_ITLB_MULTIHIT | NO_EIBRS_PBRSB),
10851088

10861089
/* AMD Family 0xf - 0x12 */
10871090
VULNWL_AMD(0x0f, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
@@ -1265,6 +1268,11 @@ static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
12651268
setup_force_cpu_bug(X86_BUG_RETBLEED);
12661269
}
12671270

1271+
if (cpu_has(c, X86_FEATURE_IBRS_ENHANCED) &&
1272+
!cpu_matches(cpu_vuln_whitelist, NO_EIBRS_PBRSB) &&
1273+
!(ia32_cap & ARCH_CAP_PBRSB_NO))
1274+
setup_force_cpu_bug(X86_BUG_EIBRS_PBRSB);
1275+
12681276
if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN))
12691277
return;
12701278

arch/x86/kvm/vmx/vmenter.S

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,13 @@ SYM_INNER_LABEL(vmx_vmexit, SYM_L_GLOBAL)
185185
* entries and (in some cases) RSB underflow.
186186
*
187187
* eIBRS has its own protection against poisoned RSB, so it doesn't
188-
* need the RSB filling sequence. But it does need to be enabled
189-
* before the first unbalanced RET.
188+
* need the RSB filling sequence. But it does need to be enabled, and a
189+
* single call to retire, before the first unbalanced RET.
190190
*/
191191

192-
FILL_RETURN_BUFFER %_ASM_CX, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_VMEXIT
192+
FILL_RETURN_BUFFER %_ASM_CX, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_VMEXIT,\
193+
X86_FEATURE_RSB_VMEXIT_LITE
194+
193195

194196
pop %_ASM_ARG2 /* @flags */
195197
pop %_ASM_ARG1 /* @vmx */

tools/arch/x86/include/asm/cpufeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
#define X86_FEATURE_CQM_MBM_LOCAL (11*32+ 3) /* LLC Local MBM monitoring */
285285
#define X86_FEATURE_FENCE_SWAPGS_USER (11*32+ 4) /* "" LFENCE in user entry SWAPGS path */
286286
#define X86_FEATURE_FENCE_SWAPGS_KERNEL (11*32+ 5) /* "" LFENCE in kernel entry SWAPGS path */
287+
#define X86_FEATURE_RSB_VMEXIT_LITE (11*32+17) /* "" Fill RSB on VM-Exit when EIBRS is enabled */
287288

288289
/* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */
289290
#define X86_FEATURE_AVX512_BF16 (12*32+ 5) /* AVX512 BFLOAT16 instructions */

0 commit comments

Comments
 (0)