Skip to content

Commit cebc238

Browse files
Daniel Sneddongregkh
authored andcommitted
x86/bpf: Add IBHF call at end of classic BPF
commit 9f725eec8fc0b39bdc07dcc8897283c367c1a163 upstream. Classic BPF programs can be run by unprivileged users, allowing unprivileged code to execute inside the kernel. Attackers can use this to craft branch history in kernel mode that can influence the target of indirect branches. BHI_DIS_S provides user-kernel isolation of branch history, but cBPF can be used to bypass this protection by crafting branch history in kernel mode. To stop intra-mode attacks via cBPF programs, Intel created a new instruction Indirect Branch History Fence (IBHF). IBHF prevents the predicted targets of subsequent indirect branches from being influenced by branch history prior to the IBHF. IBHF is only effective while BHI_DIS_S is enabled. Add the IBHF instruction to cBPF jitted code's exit path. Add the new fence when the hardware mitigation is enabled (i.e., X86_FEATURE_CLEAR_BHB_HW is set) or after the software sequence (X86_FEATURE_CLEAR_BHB_LOOP) is being used in a virtual machine. Note that X86_FEATURE_CLEAR_BHB_HW and X86_FEATURE_CLEAR_BHB_LOOP are mutually exclusive, so the JIT compiler will only emit the new fence, not the SW sequence, when X86_FEATURE_CLEAR_BHB_HW is set. Hardware that enumerates BHI_NO basically has BHI_DIS_S protections always enabled, regardless of the value of BHI_DIS_S. Since BHI_DIS_S doesn't protect against intra-mode attacks, enumerate BHI bug on BHI_NO hardware as well. Signed-off-by: Daniel Sneddon <[email protected]> Signed-off-by: Pawan Gupta <[email protected]> Signed-off-by: Dave Hansen <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Reviewed-by: Alexandre Chartre <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 845c707 commit cebc238

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

arch/x86/kernel/cpu/common.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,9 +1455,12 @@ static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
14551455
if (vulnerable_to_rfds(x86_arch_cap_msr))
14561456
setup_force_cpu_bug(X86_BUG_RFDS);
14571457

1458-
/* When virtualized, eIBRS could be hidden, assume vulnerable */
1459-
if (!(x86_arch_cap_msr & ARCH_CAP_BHI_NO) &&
1460-
!cpu_matches(cpu_vuln_whitelist, NO_BHI) &&
1458+
/*
1459+
* Intel parts with eIBRS are vulnerable to BHI attacks. Parts with
1460+
* BHI_NO still need to use the BHI mitigation to prevent Intra-mode
1461+
* attacks. When virtualized, eIBRS could be hidden, assume vulnerable.
1462+
*/
1463+
if (!cpu_matches(cpu_vuln_whitelist, NO_BHI) &&
14611464
(boot_cpu_has(X86_FEATURE_IBRS_ENHANCED) ||
14621465
boot_cpu_has(X86_FEATURE_HYPERVISOR)))
14631466
setup_force_cpu_bug(X86_BUG_BHI);

arch/x86/net/bpf_jit_comp.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
3636
#define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
3737
#define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
3838
#define EMIT4(b1, b2, b3, b4) EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
39+
#define EMIT5(b1, b2, b3, b4, b5) \
40+
do { EMIT1(b1); EMIT4(b2, b3, b4, b5); } while (0)
3941

4042
#define EMIT1_off32(b1, off) \
4143
do { EMIT1(b1); EMIT(off, 4); } while (0)
@@ -966,6 +968,23 @@ static int emit_spectre_bhb_barrier(u8 **pprog, u8 *ip,
966968
EMIT1(0x59); /* pop rcx */
967969
EMIT1(0x58); /* pop rax */
968970
}
971+
/* Insert IBHF instruction */
972+
if ((cpu_feature_enabled(X86_FEATURE_CLEAR_BHB_LOOP) &&
973+
cpu_feature_enabled(X86_FEATURE_HYPERVISOR)) ||
974+
(cpu_feature_enabled(X86_FEATURE_CLEAR_BHB_HW) &&
975+
IS_ENABLED(CONFIG_X86_64))) {
976+
/*
977+
* Add an Indirect Branch History Fence (IBHF). IBHF acts as a
978+
* fence preventing branch history from before the fence from
979+
* affecting indirect branches after the fence. This is
980+
* specifically used in cBPF jitted code to prevent Intra-mode
981+
* BHI attacks. The IBHF instruction is designed to be a NOP on
982+
* hardware that doesn't need or support it. The REP and REX.W
983+
* prefixes are required by the microcode, and they also ensure
984+
* that the NOP is unlikely to be used in existing code.
985+
*/
986+
EMIT5(0xF3, 0x48, 0x0F, 0x1E, 0xF8); /* ibhf */
987+
}
969988
*pprog = prog;
970989
return 0;
971990
}

0 commit comments

Comments
 (0)