Skip to content

Commit ade8ff3

Browse files
committed
Merge tag 'x86_bugs_post_ibpb' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 IBPB fixes from Borislav Petkov: "This fixes the IBPB implementation of older AMDs (< gen4) that do not flush the RSB (Return Address Stack) so you can still do some leaking when using a "=ibpb" mitigation for Retbleed or SRSO. Fix it by doing the flushing in software on those generations. IBPB is not the default setting so this is not likely to affect anybody in practice" * tag 'x86_bugs_post_ibpb' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/bugs: Do not use UNTRAIN_RET with IBPB on entry x86/bugs: Skip RSB fill at VMEXIT x86/entry: Have entry_ibpb() invalidate return predictions x86/cpufeatures: Add a IBPB_NO_RET BUG flag x86/cpufeatures: Define X86_FEATURE_AMD_IBPB_RET
2 parents 4d93978 + c62fa11 commit ade8ff3

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

arch/x86/entry/entry.S

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <asm/unwind_hints.h>
1010
#include <asm/segment.h>
1111
#include <asm/cache.h>
12+
#include <asm/cpufeatures.h>
13+
#include <asm/nospec-branch.h>
1214

1315
#include "calling.h"
1416

@@ -19,6 +21,9 @@ SYM_FUNC_START(entry_ibpb)
1921
movl $PRED_CMD_IBPB, %eax
2022
xorl %edx, %edx
2123
wrmsr
24+
25+
/* Make sure IBPB clears return stack preductions too. */
26+
FILL_RETURN_BUFFER %rax, RSB_CLEAR_LOOPS, X86_BUG_IBPB_NO_RET
2227
RET
2328
SYM_FUNC_END(entry_ibpb)
2429
/* For KVM */

arch/x86/include/asm/cpufeatures.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@
215215
#define X86_FEATURE_SPEC_STORE_BYPASS_DISABLE ( 7*32+23) /* Disable Speculative Store Bypass. */
216216
#define X86_FEATURE_LS_CFG_SSBD ( 7*32+24) /* AMD SSBD implementation via LS_CFG MSR */
217217
#define X86_FEATURE_IBRS ( 7*32+25) /* "ibrs" Indirect Branch Restricted Speculation */
218-
#define X86_FEATURE_IBPB ( 7*32+26) /* "ibpb" Indirect Branch Prediction Barrier */
218+
#define X86_FEATURE_IBPB ( 7*32+26) /* "ibpb" Indirect Branch Prediction Barrier without a guaranteed RSB flush */
219219
#define X86_FEATURE_STIBP ( 7*32+27) /* "stibp" Single Thread Indirect Branch Predictors */
220220
#define X86_FEATURE_ZEN ( 7*32+28) /* Generic flag for all Zen and newer */
221221
#define X86_FEATURE_L1TF_PTEINV ( 7*32+29) /* L1TF workaround PTE inversion */
@@ -348,6 +348,7 @@
348348
#define X86_FEATURE_CPPC (13*32+27) /* "cppc" Collaborative Processor Performance Control */
349349
#define X86_FEATURE_AMD_PSFD (13*32+28) /* Predictive Store Forwarding Disable */
350350
#define X86_FEATURE_BTC_NO (13*32+29) /* Not vulnerable to Branch Type Confusion */
351+
#define X86_FEATURE_AMD_IBPB_RET (13*32+30) /* IBPB clears return address predictor */
351352
#define X86_FEATURE_BRS (13*32+31) /* "brs" Branch Sampling available */
352353

353354
/* Thermal and Power Management Leaf, CPUID level 0x00000006 (EAX), word 14 */
@@ -523,4 +524,5 @@
523524
#define X86_BUG_DIV0 X86_BUG(1*32 + 1) /* "div0" AMD DIV0 speculation bug */
524525
#define X86_BUG_RFDS X86_BUG(1*32 + 2) /* "rfds" CPU is vulnerable to Register File Data Sampling */
525526
#define X86_BUG_BHI X86_BUG(1*32 + 3) /* "bhi" CPU is affected by Branch History Injection */
527+
#define X86_BUG_IBPB_NO_RET X86_BUG(1*32 + 4) /* "ibpb_no_ret" IBPB omits return target predictions */
526528
#endif /* _ASM_X86_CPUFEATURES_H */

arch/x86/kernel/cpu/bugs.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,8 +1115,25 @@ static void __init retbleed_select_mitigation(void)
11151115

11161116
case RETBLEED_MITIGATION_IBPB:
11171117
setup_force_cpu_cap(X86_FEATURE_ENTRY_IBPB);
1118+
1119+
/*
1120+
* IBPB on entry already obviates the need for
1121+
* software-based untraining so clear those in case some
1122+
* other mitigation like SRSO has selected them.
1123+
*/
1124+
setup_clear_cpu_cap(X86_FEATURE_UNRET);
1125+
setup_clear_cpu_cap(X86_FEATURE_RETHUNK);
1126+
11181127
setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
11191128
mitigate_smt = true;
1129+
1130+
/*
1131+
* There is no need for RSB filling: entry_ibpb() ensures
1132+
* all predictions, including the RSB, are invalidated,
1133+
* regardless of IBPB implementation.
1134+
*/
1135+
setup_clear_cpu_cap(X86_FEATURE_RSB_VMEXIT);
1136+
11201137
break;
11211138

11221139
case RETBLEED_MITIGATION_STUFF:
@@ -2627,6 +2644,14 @@ static void __init srso_select_mitigation(void)
26272644
if (has_microcode) {
26282645
setup_force_cpu_cap(X86_FEATURE_ENTRY_IBPB);
26292646
srso_mitigation = SRSO_MITIGATION_IBPB;
2647+
2648+
/*
2649+
* IBPB on entry already obviates the need for
2650+
* software-based untraining so clear those in case some
2651+
* other mitigation like Retbleed has selected them.
2652+
*/
2653+
setup_clear_cpu_cap(X86_FEATURE_UNRET);
2654+
setup_clear_cpu_cap(X86_FEATURE_RETHUNK);
26302655
}
26312656
} else {
26322657
pr_err("WARNING: kernel not compiled with MITIGATION_IBPB_ENTRY.\n");
@@ -2638,6 +2663,13 @@ static void __init srso_select_mitigation(void)
26382663
if (!boot_cpu_has(X86_FEATURE_ENTRY_IBPB) && has_microcode) {
26392664
setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
26402665
srso_mitigation = SRSO_MITIGATION_IBPB_ON_VMEXIT;
2666+
2667+
/*
2668+
* There is no need for RSB filling: entry_ibpb() ensures
2669+
* all predictions, including the RSB, are invalidated,
2670+
* regardless of IBPB implementation.
2671+
*/
2672+
setup_clear_cpu_cap(X86_FEATURE_RSB_VMEXIT);
26412673
}
26422674
} else {
26432675
pr_err("WARNING: kernel not compiled with MITIGATION_SRSO.\n");

arch/x86/kernel/cpu/common.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,9 @@ static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
14431443
boot_cpu_has(X86_FEATURE_HYPERVISOR)))
14441444
setup_force_cpu_bug(X86_BUG_BHI);
14451445

1446+
if (cpu_has(c, X86_FEATURE_AMD_IBPB) && !cpu_has(c, X86_FEATURE_AMD_IBPB_RET))
1447+
setup_force_cpu_bug(X86_BUG_IBPB_NO_RET);
1448+
14461449
if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN))
14471450
return;
14481451

0 commit comments

Comments
 (0)