Skip to content

Commit 8eee539

Browse files
xiwctmarinas
authored andcommitted
arm64: bpf: fix out-of-bounds read in bpf2a64_offset()
Problems occur when bpf_to or bpf_from has value prog->len - 1 (e.g., "Very long jump backwards" in test_bpf where the last instruction is a jump): since ctx->offset has length prog->len, ctx->offset[bpf_to + 1] or ctx->offset[bpf_from + 1] will cause an out-of-bounds read, leading to a bogus jump offset and kernel panic. This patch moves updating ctx->offset to after calling build_insn(), and changes indexing to use bpf_to and bpf_from without + 1. Fixes: e54bcde ("arm64: eBPF JIT compiler") Cc: <[email protected]> # 3.18+ Cc: Zi Shen Lim <[email protected]> Cc: Will Deacon <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: Xi Wang <[email protected]> Signed-off-by: Catalin Marinas <[email protected]>
1 parent be081d9 commit 8eee539

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

arch/arm64/net/bpf_jit_comp.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ static inline void emit_a64_mov_i(const int is64, const int reg,
113113
static inline int bpf2a64_offset(int bpf_to, int bpf_from,
114114
const struct jit_ctx *ctx)
115115
{
116-
int to = ctx->offset[bpf_to + 1];
116+
int to = ctx->offset[bpf_to];
117117
/* -1 to account for the Branch instruction */
118-
int from = ctx->offset[bpf_from + 1] - 1;
118+
int from = ctx->offset[bpf_from] - 1;
119119

120120
return to - from;
121121
}
@@ -640,10 +640,11 @@ static int build_body(struct jit_ctx *ctx)
640640
const struct bpf_insn *insn = &prog->insnsi[i];
641641
int ret;
642642

643+
ret = build_insn(insn, ctx);
644+
643645
if (ctx->image == NULL)
644646
ctx->offset[i] = ctx->idx;
645647

646-
ret = build_insn(insn, ctx);
647648
if (ret > 0) {
648649
i++;
649650
continue;

0 commit comments

Comments
 (0)