Skip to content

Commit 7a4d2cb

Browse files
Daniel Sneddongregkh
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 gregkh#6, it might speculatively use the address for the instruction after the CALL in gregkh#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 gregkh#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]> [ bp: Adjust patch to account for kvm entry being in c ] Signed-off-by: Suraj Jitindar Singh <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 47e5ac7 commit 7a4d2cb

File tree

8 files changed

+103
-30
lines changed

8 files changed

+103
-30
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
@@ -291,6 +291,7 @@
291291
#define X86_FEATURE_RRSBA_CTRL (11*32+11) /* "" RET prediction control */
292292
#define X86_FEATURE_RETPOLINE (11*32+12) /* "" Generic Retpoline mitigation for Spectre variant 2 */
293293
#define X86_FEATURE_RETPOLINE_LFENCE (11*32+13) /* "" Use LFENCE for Spectre variant 2 */
294+
#define X86_FEATURE_RSB_VMEXIT_LITE (11*32+17) /* "" Fill RSB on VM exit when EIBRS is enabled */
294295

295296
/* AMD-defined CPU features, CPUID level 0x80000008 (EBX), word 13 */
296297
#define X86_FEATURE_CLZERO (13*32+ 0) /* CLZERO instruction */
@@ -405,5 +406,6 @@
405406
#define X86_BUG_MMIO_STALE_DATA X86_BUG(25) /* CPU is affected by Processor MMIO Stale Data vulnerabilities */
406407
#define X86_BUG_MMIO_UNKNOWN X86_BUG(26) /* CPU is too old and its MMIO Stale Data status is unknown */
407408
#define X86_BUG_RETBLEED X86_BUG(27) /* CPU is affected by RETBleed */
409+
#define X86_BUG_EIBRS_PBRSB X86_BUG(28) /* EIBRS is vulnerable to Post Barrier RSB Predictions */
408410

409411
#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
@@ -130,6 +130,10 @@
130130
* are restricted to targets in
131131
* kernel.
132132
*/
133+
#define ARCH_CAP_PBRSB_NO BIT(24) /*
134+
* Not susceptible to Post-Barrier
135+
* Return Stack Buffer Predictions.
136+
*/
133137

134138
#define MSR_IA32_FLUSH_CMD 0x0000010b
135139
#define L1D_FLUSH BIT(0) /*

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@
5959
/* barrier for jnz misprediction */ \
6060
lfence;
6161

62+
#define ISSUE_UNBALANCED_RET_GUARD(sp) \
63+
call 992f; \
64+
int3; \
65+
992: \
66+
add $(BITS_PER_LONG/8), sp; \
67+
lfence;
68+
6269
#ifdef __ASSEMBLY__
6370

6471
/*
@@ -263,9 +270,11 @@ static __always_inline void vmexit_fill_RSB(void)
263270
unsigned long loops;
264271

265272
asm volatile (ANNOTATE_NOSPEC_ALTERNATIVE
266-
ALTERNATIVE("jmp 910f",
267-
__stringify(__FILL_RETURN_BUFFER(%0, RSB_CLEAR_LOOPS, %1)),
268-
X86_FEATURE_RSB_VMEXIT)
273+
ALTERNATIVE_2("jmp 910f", "", X86_FEATURE_RSB_VMEXIT,
274+
"jmp 911f", X86_FEATURE_RSB_VMEXIT_LITE)
275+
__stringify(__FILL_RETURN_BUFFER(%0, RSB_CLEAR_LOOPS, %1))
276+
"911:"
277+
__stringify(ISSUE_UNBALANCED_RET_GUARD(%1))
269278
"910:"
270279
: "=r" (loops), ASM_CALL_CONSTRAINT
271280
: : "memory" );

arch/x86/kernel/cpu/bugs.c

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,54 @@ static void __init spec_ctrl_disable_kernel_rrsba(void)
11961196
}
11971197
}
11981198

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

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

13711398
/*
13721399
* Retpoline protects the kernel, but doesn't protect firmware. IBRS
@@ -2094,6 +2121,19 @@ static char *ibpb_state(void)
20942121
return "";
20952122
}
20962123

2124+
static char *pbrsb_eibrs_state(void)
2125+
{
2126+
if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
2127+
if (boot_cpu_has(X86_FEATURE_RSB_VMEXIT_LITE) ||
2128+
boot_cpu_has(X86_FEATURE_RSB_VMEXIT))
2129+
return ", PBRSB-eIBRS: SW sequence";
2130+
else
2131+
return ", PBRSB-eIBRS: Vulnerable";
2132+
} else {
2133+
return ", PBRSB-eIBRS: Not affected";
2134+
}
2135+
}
2136+
20972137
static ssize_t spectre_v2_show_state(char *buf)
20982138
{
20992139
if (spectre_v2_enabled == SPECTRE_V2_LFENCE)
@@ -2106,12 +2146,13 @@ static ssize_t spectre_v2_show_state(char *buf)
21062146
spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
21072147
return sprintf(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
21082148

2109-
return sprintf(buf, "%s%s%s%s%s%s\n",
2149+
return sprintf(buf, "%s%s%s%s%s%s%s\n",
21102150
spectre_v2_strings[spectre_v2_enabled],
21112151
ibpb_state(),
21122152
boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
21132153
stibp_state(),
21142154
boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
2155+
pbrsb_eibrs_state(),
21152156
spectre_v2_module_string());
21162157
}
21172158

arch/x86/kernel/cpu/common.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
906906
#define NO_SWAPGS BIT(6)
907907
#define NO_ITLB_MULTIHIT BIT(7)
908908
#define NO_MMIO BIT(8)
909+
#define NO_EIBRS_PBRSB BIT(9)
909910

910911
#define VULNWL(_vendor, _family, _model, _whitelist) \
911912
{ X86_VENDOR_##_vendor, _family, _model, X86_FEATURE_ANY, _whitelist }
@@ -947,7 +948,7 @@ static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
947948

948949
VULNWL_INTEL(ATOM_GOLDMONT, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
949950
VULNWL_INTEL(ATOM_GOLDMONT_X, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
950-
VULNWL_INTEL(ATOM_GOLDMONT_PLUS, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
951+
VULNWL_INTEL(ATOM_GOLDMONT_PLUS, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB),
951952

952953
/*
953954
* Technically, swapgs isn't serializing on AMD (despite it previously
@@ -957,7 +958,9 @@ static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
957958
* good enough for our purposes.
958959
*/
959960

960-
VULNWL_INTEL(ATOM_TREMONT_X, NO_ITLB_MULTIHIT),
961+
VULNWL_INTEL(ATOM_TREMONT, NO_EIBRS_PBRSB),
962+
VULNWL_INTEL(ATOM_TREMONT_L, NO_EIBRS_PBRSB),
963+
VULNWL_INTEL(ATOM_TREMONT_X, NO_ITLB_MULTIHIT | NO_EIBRS_PBRSB),
961964

962965
/* AMD Family 0xf - 0x12 */
963966
VULNWL_AMD(0x0f, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
@@ -1129,6 +1132,11 @@ static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
11291132
setup_force_cpu_bug(X86_BUG_RETBLEED);
11301133
}
11311134

1135+
if (cpu_has(c, X86_FEATURE_IBRS_ENHANCED) &&
1136+
!cpu_matches(cpu_vuln_whitelist, NO_EIBRS_PBRSB) &&
1137+
!(ia32_cap & ARCH_CAP_PBRSB_NO))
1138+
setup_force_cpu_bug(X86_BUG_EIBRS_PBRSB);
1139+
11321140
if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN))
11331141
return;
11341142

arch/x86/kvm/vmx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10001,8 +10001,8 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
1000110001
* entries and (in some cases) RSB underflow.
1000210002
*
1000310003
* eIBRS has its own protection against poisoned RSB, so it doesn't
10004-
* need the RSB filling sequence. But it does need to be enabled
10005-
* before the first unbalanced RET.
10004+
* need the RSB filling sequence. But it does need to be enabled, and a
10005+
* single call to retire, before the first unbalanced RET.
1000610006
*
1000710007
* So no RETs before vmx_spec_ctrl_restore_host() below.
1000810008
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270

271271
/* Intel-defined CPU QoS Sub-leaf, CPUID level 0x0000000F:0 (EDX), word 11 */
272272
#define X86_FEATURE_CQM_LLC (11*32+ 1) /* LLC QoS if 1 */
273+
#define X86_FEATURE_RSB_VMEXIT_LITE (11*32+17) /* "" Fill RSB on VM-Exit when EIBRS is enabled */
273274

274275
/* Intel-defined CPU QoS Sub-leaf, CPUID level 0x0000000F:1 (EDX), word 12 */
275276
#define X86_FEATURE_CQM_OCCUP_LLC (12*32+ 0) /* LLC occupancy monitoring */

0 commit comments

Comments
 (0)