Skip to content

Commit 0dfefc2

Browse files
author
James Morse
committed
arm64: bpf: Add BHB mitigation to the epilogue for cBPF programs
A malicious BPF program may manipulate the branch history to influence what the hardware speculates will happen next. On exit from a BPF program, emit the BHB mititgation sequence. This is only applied for 'classic' cBPF programs that are loaded by seccomp. Signed-off-by: James Morse <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Acked-by: Daniel Borkmann <[email protected]>
1 parent a1152be commit 0dfefc2

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

arch/arm64/include/asm/spectre.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ enum mitigation_state arm64_get_meltdown_state(void);
9797

9898
enum mitigation_state arm64_get_spectre_bhb_state(void);
9999
bool is_spectre_bhb_affected(const struct arm64_cpu_capabilities *entry, int scope);
100+
extern bool __nospectre_bhb;
100101
u8 get_spectre_bhb_loop_value(void);
101102
bool is_spectre_bhb_fw_mitigated(void);
102103
void spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities *__unused);

arch/arm64/kernel/proton-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ static void this_cpu_set_vectors(enum arm64_bp_harden_el1_vectors slot)
10211021
isb();
10221022
}
10231023

1024-
static bool __read_mostly __nospectre_bhb;
1024+
bool __read_mostly __nospectre_bhb;
10251025
static int __init parse_spectre_bhb_param(char *str)
10261026
{
10271027
__nospectre_bhb = true;

arch/arm64/net/bpf_jit_comp.c

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#define pr_fmt(fmt) "bpf_jit: " fmt
99

10+
#include <linux/arm-smccc.h>
1011
#include <linux/bitfield.h>
1112
#include <linux/bpf.h>
1213
#include <linux/filter.h>
@@ -17,6 +18,7 @@
1718
#include <asm/asm-extable.h>
1819
#include <asm/byteorder.h>
1920
#include <asm/cacheflush.h>
21+
#include <asm/cpufeature.h>
2022
#include <asm/debug-monitors.h>
2123
#include <asm/insn.h>
2224
#include <asm/text-patching.h>
@@ -939,7 +941,48 @@ static void build_plt(struct jit_ctx *ctx)
939941
plt->target = (u64)&dummy_tramp;
940942
}
941943

942-
static void build_epilogue(struct jit_ctx *ctx)
944+
/* Clobbers BPF registers 1-4, aka x0-x3 */
945+
static void __maybe_unused build_bhb_mitigation(struct jit_ctx *ctx)
946+
{
947+
const u8 r1 = bpf2a64[BPF_REG_1]; /* aka x0 */
948+
u8 k = get_spectre_bhb_loop_value();
949+
950+
if (!IS_ENABLED(CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY) ||
951+
cpu_mitigations_off() || __nospectre_bhb ||
952+
arm64_get_spectre_v2_state() == SPECTRE_VULNERABLE)
953+
return;
954+
955+
if (supports_clearbhb(SCOPE_SYSTEM)) {
956+
emit(aarch64_insn_gen_hint(AARCH64_INSN_HINT_CLEARBHB), ctx);
957+
return;
958+
}
959+
960+
if (k) {
961+
emit_a64_mov_i64(r1, k, ctx);
962+
emit(A64_B(1), ctx);
963+
emit(A64_SUBS_I(true, r1, r1, 1), ctx);
964+
emit(A64_B_(A64_COND_NE, -2), ctx);
965+
emit(aarch64_insn_gen_dsb(AARCH64_INSN_MB_ISH), ctx);
966+
emit(aarch64_insn_get_isb_value(), ctx);
967+
}
968+
969+
if (is_spectre_bhb_fw_mitigated()) {
970+
emit(A64_ORR_I(false, r1, AARCH64_INSN_REG_ZR,
971+
ARM_SMCCC_ARCH_WORKAROUND_3), ctx);
972+
switch (arm_smccc_1_1_get_conduit()) {
973+
case SMCCC_CONDUIT_HVC:
974+
emit(aarch64_insn_get_hvc_value(), ctx);
975+
break;
976+
case SMCCC_CONDUIT_SMC:
977+
emit(aarch64_insn_get_smc_value(), ctx);
978+
break;
979+
default:
980+
pr_err_once("Firmware mitigation enabled with unknown conduit\n");
981+
}
982+
}
983+
}
984+
985+
static void build_epilogue(struct jit_ctx *ctx, bool was_classic)
943986
{
944987
const u8 r0 = bpf2a64[BPF_REG_0];
945988
const u8 ptr = bpf2a64[TCCNT_PTR];
@@ -952,10 +995,13 @@ static void build_epilogue(struct jit_ctx *ctx)
952995

953996
emit(A64_POP(A64_ZR, ptr, A64_SP), ctx);
954997

998+
if (was_classic)
999+
build_bhb_mitigation(ctx);
1000+
9551001
/* Restore FP/LR registers */
9561002
emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
9571003

958-
/* Set return value */
1004+
/* Move the return value from bpf:r0 (aka x7) to x0 */
9591005
emit(A64_MOV(1, A64_R(0), r0), ctx);
9601006

9611007
/* Authenticate lr */
@@ -1898,7 +1944,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
18981944
}
18991945

19001946
ctx.epilogue_offset = ctx.idx;
1901-
build_epilogue(&ctx);
1947+
build_epilogue(&ctx, was_classic);
19021948
build_plt(&ctx);
19031949

19041950
extable_align = __alignof__(struct exception_table_entry);
@@ -1961,7 +2007,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
19612007
goto out_free_hdr;
19622008
}
19632009

1964-
build_epilogue(&ctx);
2010+
build_epilogue(&ctx, was_classic);
19652011
build_plt(&ctx);
19662012

19672013
/* Extra pass to validate JITed code. */

0 commit comments

Comments
 (0)