An arena access added the 32-bit arena offset register to the arena base with a 64-bit add. That relies on the verifier to keep the upper 32 bits zero, and any slip there would let the access escape the 4GB arena. Use the register offset form with UXTW instead. It zero extends the offset as part of the access, which keeps the access within the arena and its guard pages. Without an immediate offset, this saves the add. With one, the add moves to the base, and an offset that does not fit the add immediate needs an extra mov. The atomics and load-acquire/store-release have no register offset form. For them, add the arena offset to the base with UXTW instead. Update the arm64 expectations of the Arena LDSX Disasm test in verifier_ldsx to the new form. Before/after for r0 = *(u64 *)(r1 + 0) and r0 = *(u64 *)(r1 + 8) in the arena: 8b1c000b add x11, x0, x28 f9400168 ldr x8, [x11] f8604b88 ldr x8, [x28, w0, uxtw] 8b1c000b add x11, x0, x28 f9400568 ldr x8, [x11, #8] 9100238b add x11, x28, #0x8 f8604968 ldr x8, [x11, w0, uxtw] Signed-off-by: Daniel Borkmann --- arch/arm64/net/bpf_jit_comp.c | 90 +++++++++++++------ .../selftests/bpf/progs/verifier_ldsx.c | 24 ++--- 2 files changed, 75 insertions(+), 39 deletions(-) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index 083d8c383f5b..fb56b439ee20 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -461,6 +461,26 @@ static void emit_ldst(const u8 code, const u8 rt, const u8 rn, const s32 off, } } +/* + * Load or store at the arena base, plus off added into tmp_addr, plus reg zero + * extended from 32 bits in the access, which keeps it in the arena and its + * guard whatever the upper bits of reg hold. + */ +static void emit_arena_ldst(const u8 code, const u8 rt, const u8 reg, const s32 off, + const u8 tmp_addr, const u8 tmp, struct jit_ctx *ctx) +{ + u8 base = bpf2a64[ARENA_VM_START]; + int scale; + + if (off) { + emit_a64_add_i(1, tmp_addr, base, tmp, off, ctx); + base = tmp_addr; + } + emit(aarch64_insn_gen_load_store_reg(rt, base, reg, AARCH64_INSN_EXTEND_UXTW, + 0, a64_ldst_size(code, &scale), + a64_ldst_type(code, A64_LDST_REG)), ctx); +} + /* generated main prog prologue: * bti c // if CONFIG_ARM64_BTI_KERNEL * mov x9, lr @@ -846,6 +866,7 @@ static int emit_atomic_ld_st(const struct bpf_insn *insn, struct jit_ctx *ctx) const u8 dst = bpf2a64[insn->dst_reg]; const u8 src = bpf2a64[insn->src_reg]; const u8 tmp = bpf2a64[TMP_REG_1]; + const u8 tmp2 = bpf2a64[TMP_REG_2]; u8 reg; switch (imm) { @@ -860,12 +881,21 @@ static int emit_atomic_ld_st(const struct bpf_insn *insn, struct jit_ctx *ctx) return -EINVAL; } - if (off) { - emit_a64_add_i(1, tmp, reg, tmp, off, ctx); - reg = tmp; - } + /* + * The arena base with the offset first and the arena offset zero + * extended onto it, see emit_arena_ldst() + */ if (arena) { - emit(A64_ADD(1, tmp, reg, arena_vm_base), ctx); + u8 base = arena_vm_base; + + if (off) { + emit_a64_add_i(1, tmp, base, tmp, off, ctx); + base = tmp; + } + emit(A64_ADD_UXTW(tmp2, base, reg), ctx); + reg = tmp2; + } else if (off) { + emit_a64_add_i(1, tmp, reg, tmp, off, ctx); reg = tmp; } @@ -919,17 +949,27 @@ static int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx) const u8 src = bpf2a64[insn->src_reg]; const u8 tmp = bpf2a64[TMP_REG_1]; const u8 tmp2 = bpf2a64[TMP_REG_2]; + const u8 tmp3 = bpf2a64[TMP_REG_3]; const bool isdw = BPF_SIZE(code) == BPF_DW; const bool arena = BPF_MODE(code) == BPF_PROBE_ATOMIC; const s16 off = insn->off; u8 reg = dst; - if (off) { - emit_a64_add_i(1, tmp, reg, tmp, off, ctx); - reg = tmp; - } + /* + * The arena base with the offset first and the arena offset zero + * extended onto it, see emit_arena_ldst() + */ if (arena) { - emit(A64_ADD(1, tmp, reg, arena_vm_base), ctx); + u8 base = arena_vm_base; + + if (off) { + emit_a64_add_i(1, tmp, base, tmp, off, ctx); + base = tmp; + } + emit(A64_ADD_UXTW(tmp3, base, reg), ctx); + reg = tmp3; + } else if (off) { + emit_a64_add_i(1, tmp, reg, tmp, off, ctx); reg = tmp; } @@ -1564,7 +1604,6 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn const u8 tmp = bpf2a64[TMP_REG_1]; const u8 tmp2 = bpf2a64[TMP_REG_2]; const u8 tmp3 = bpf2a64[TMP_REG_3]; - const u8 arena_vm_base = bpf2a64[ARENA_VM_START]; const s16 off = insn->off; const s32 imm = insn->imm; const int i = insn - ctx->prog->insnsi; @@ -2062,11 +2101,6 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn case BPF_LDX | BPF_PROBE_MEM32SX | BPF_B: case BPF_LDX | BPF_PROBE_MEM32SX | BPF_H: case BPF_LDX | BPF_PROBE_MEM32SX | BPF_W: - if (BPF_MODE(insn->code) == BPF_PROBE_MEM32 || - BPF_MODE(insn->code) == BPF_PROBE_MEM32SX) { - emit(A64_ADD(1, tmp2, src, arena_vm_base), ctx); - src = tmp2; - } off_adj = off; if (BPF_MODE(insn->code) == BPF_PROBE_MEM || BPF_MODE(insn->code) == BPF_PROBE_MEMSX) { @@ -2086,7 +2120,11 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn emit(A64_MOVZ(1, dst, 0, 0), ctx); emit(A64_B(2), ctx); } - emit_ldst(code, dst, src, off_adj, tmp, ctx); + if (BPF_MODE(insn->code) == BPF_PROBE_MEM32 || + BPF_MODE(insn->code) == BPF_PROBE_MEM32SX) + emit_arena_ldst(code, dst, src, off, tmp2, tmp, ctx); + else + emit_ldst(code, dst, src, off_adj, tmp, ctx); ret = add_exception_handler(insn, ctx, dst); if (ret) @@ -2117,14 +2155,13 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn case BPF_ST | BPF_PROBE_MEM32 | BPF_H: case BPF_ST | BPF_PROBE_MEM32 | BPF_W: case BPF_ST | BPF_PROBE_MEM32 | BPF_DW: - if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) { - emit(A64_ADD(1, tmp3, dst, arena_vm_base), ctx); - dst = tmp3; - } /* A zero comes from the zero register, any other imm through a register */ if (imm) emit_a64_mov_i(1, tmp, imm, ctx); - emit_ldst(code, imm ? tmp : A64_ZR, dst, off, tmp2, ctx); + if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) + emit_arena_ldst(code, imm ? tmp : A64_ZR, dst, off, tmp3, tmp2, ctx); + else + emit_ldst(code, imm ? tmp : A64_ZR, dst, off, tmp2, ctx); ret = add_exception_handler(insn, ctx, DONT_CLEAR); if (ret) @@ -2145,11 +2182,10 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn case BPF_STX | BPF_PROBE_MEM32 | BPF_H: case BPF_STX | BPF_PROBE_MEM32 | BPF_W: case BPF_STX | BPF_PROBE_MEM32 | BPF_DW: - if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) { - emit(A64_ADD(1, tmp2, dst, arena_vm_base), ctx); - dst = tmp2; - } - emit_ldst(code, src, dst, off, tmp, ctx); + if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) + emit_arena_ldst(code, src, dst, off, tmp2, tmp, ctx); + else + emit_ldst(code, src, dst, off, tmp, ctx); ret = add_exception_handler(insn, ctx, DONT_CLEAR); if (ret) diff --git a/tools/testing/selftests/bpf/progs/verifier_ldsx.c b/tools/testing/selftests/bpf/progs/verifier_ldsx.c index 8084a428d9e2..232082428cb9 100644 --- a/tools/testing/selftests/bpf/progs/verifier_ldsx.c +++ b/tools/testing/selftests/bpf/progs/verifier_ldsx.c @@ -274,18 +274,18 @@ __jited("movslq 0x10(%rdi,%r12), %r15") __jited("movswq 0x18(%rdi,%r12), %r15") __jited("movsbq 0x20(%rdi,%r12), %r15") __arch_arm64 -__jited("add x11, x8, x28") -__jited("ldrsw x21, [x11, #0x10]") -__jited("add x11, x8, x28") -__jited("ldrsh x21, [x11, #0x18]") -__jited("add x11, x8, x28") -__jited("ldrsb x21, [x11, #0x20]") -__jited("add x11, x0, x28") -__jited("ldrsw x22, [x11, #0x10]") -__jited("add x11, x0, x28") -__jited("ldrsh x22, [x11, #0x18]") -__jited("add x11, x0, x28") -__jited("ldrsb x22, [x11, #0x20]") +__jited("add x11, x28, #0x10") +__jited("ldrsw x21, [x11, w8, uxtw]") +__jited("add x11, x28, #0x18") +__jited("ldrsh x21, [x11, w8, uxtw]") +__jited("add x11, x28, #0x20") +__jited("ldrsb x21, [x11, w8, uxtw]") +__jited("add x11, x28, #0x10") +__jited("ldrsw x22, [x11, w0, uxtw]") +__jited("add x11, x28, #0x18") +__jited("ldrsh x22, [x11, w0, uxtw]") +__jited("add x11, x28, #0x20") +__jited("ldrsb x22, [x11, w0, uxtw]") __arch_riscv64 __jited("add t2, t6, s7") __jited("lw s3, 0x10(t2)") -- 2.43.0