diff mbox

arm64: bpf: fix out-of-bounds read in bpf2a64_offset()

Message ID 1435236459-15141-1-git-send-email-xi.wang@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Xi Wang June 25, 2015, 12:47 p.m. UTC
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.

Cc: Zi Shen Lim <zlim.lnx@gmail.com>
Cc: Alexei Starovoitov <ast@plumgrid.com>
Cc: Will Deacon <will.deacon@arm.com>
Fixes: e54bcde3d69d ("arm64: eBPF JIT compiler")
Signed-off-by: Xi Wang <xi.wang@gmail.com>
---
 arch/arm64/net/bpf_jit_comp.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Alexei Starovoitov June 25, 2015, 1:34 p.m. UTC | #1
On 6/25/15 5:47 AM, Xi Wang wrote:
> 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.
>
> Cc: Zi Shen Lim<zlim.lnx@gmail.com>
> Cc: Alexei Starovoitov<ast@plumgrid.com>
> Cc: Will Deacon<will.deacon@arm.com>
> Fixes: e54bcde3d69d ("arm64: eBPF JIT compiler")
> Signed-off-by: Xi Wang<xi.wang@gmail.com>

Nice catch! Looks good to me.
Acked-by: Alexei Starovoitov <ast@plumgrid.com>
Catalin Marinas June 25, 2015, 2:26 p.m. UTC | #2
On Thu, Jun 25, 2015 at 05:47:39AM -0700, Xi Wang wrote:
> 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.
> 
> Cc: Zi Shen Lim <zlim.lnx@gmail.com>
> Cc: Alexei Starovoitov <ast@plumgrid.com>
> Cc: Will Deacon <will.deacon@arm.com>
> Fixes: e54bcde3d69d ("arm64: eBPF JIT compiler")
> Signed-off-by: Xi Wang <xi.wang@gmail.com>

Thanks. Applied.
diff mbox

Patch

diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index dc6a484..c81ddd4 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -113,9 +113,9 @@  static inline void emit_a64_mov_i(const int is64, const int reg,
 static inline int bpf2a64_offset(int bpf_to, int bpf_from,
 				 const struct jit_ctx *ctx)
 {
-	int to = ctx->offset[bpf_to + 1];
+	int to = ctx->offset[bpf_to];
 	/* -1 to account for the Branch instruction */
-	int from = ctx->offset[bpf_from + 1] - 1;
+	int from = ctx->offset[bpf_from] - 1;
 
 	return to - from;
 }
@@ -640,10 +640,11 @@  static int build_body(struct jit_ctx *ctx)
 		const struct bpf_insn *insn = &prog->insnsi[i];
 		int ret;
 
+		ret = build_insn(insn, ctx);
+
 		if (ctx->image == NULL)
 			ctx->offset[i] = ctx->idx;
 
-		ret = build_insn(insn, ctx);
 		if (ret > 0) {
 			i++;
 			continue;