Skip to content

Commit ddc665a

Browse files
borkmanndavem330
authored andcommitted
bpf, arm64: fix jit branch offset related to ldimm64
When the instruction right before the branch destination is a 64 bit load immediate, we currently calculate the wrong jump offset in the ctx->offset[] array as we only account one instruction slot for the 64 bit load immediate although it uses two BPF instructions. Fix it up by setting the offset into the right slot after we incremented the index. Before (ldimm64 test 1): [...] 00000020: 52800007 mov w7, #0x0 // #0 00000024: d2800060 mov x0, #0x3 // tiwai#3 00000028: d2800041 mov x1, #0x2 // tiwai#2 0000002c: eb01001f cmp x0, x1 00000030: 54ffff82 b.cs 0x00000020 00000034: d29fffe7 mov x7, #0xffff // #65535 00000038: f2bfffe7 movk x7, #0xffff, lsl torvalds#16 0000003c: f2dfffe7 movk x7, #0xffff, lsl torvalds#32 00000040: f2ffffe7 movk x7, #0xffff, lsl torvalds#48 00000044: d29dddc7 mov x7, #0xeeee // #61166 00000048: f2bdddc7 movk x7, #0xeeee, lsl torvalds#16 0000004c: f2ddddc7 movk x7, #0xeeee, lsl torvalds#32 00000050: f2fdddc7 movk x7, #0xeeee, lsl torvalds#48 [...] After (ldimm64 test 1): [...] 00000020: 52800007 mov w7, #0x0 // #0 00000024: d2800060 mov x0, #0x3 // tiwai#3 00000028: d2800041 mov x1, #0x2 // tiwai#2 0000002c: eb01001f cmp x0, x1 00000030: 540000a2 b.cs 0x00000044 00000034: d29fffe7 mov x7, #0xffff // #65535 00000038: f2bfffe7 movk x7, #0xffff, lsl torvalds#16 0000003c: f2dfffe7 movk x7, #0xffff, lsl torvalds#32 00000040: f2ffffe7 movk x7, #0xffff, lsl torvalds#48 00000044: d29dddc7 mov x7, #0xeeee // #61166 00000048: f2bdddc7 movk x7, #0xeeee, lsl torvalds#16 0000004c: f2ddddc7 movk x7, #0xeeee, lsl torvalds#32 00000050: f2fdddc7 movk x7, #0xeeee, lsl torvalds#48 [...] Also, add a couple of test cases to make sure JITs pass this test. Tested on Cavium ThunderX ARMv8. The added test cases all pass after the fix. Fixes: 8eee539 ("arm64: bpf: fix out-of-bounds read in bpf2a64_offset()") Reported-by: David S. Miller <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Cc: Xi Wang <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 85f68fe commit ddc665a

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

arch/arm64/net/bpf_jit_comp.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -776,14 +776,14 @@ static int build_body(struct jit_ctx *ctx)
776776
int ret;
777777

778778
ret = build_insn(insn, ctx);
779-
780-
if (ctx->image == NULL)
781-
ctx->offset[i] = ctx->idx;
782-
783779
if (ret > 0) {
784780
i++;
781+
if (ctx->image == NULL)
782+
ctx->offset[i] = ctx->idx;
785783
continue;
786784
}
785+
if (ctx->image == NULL)
786+
ctx->offset[i] = ctx->idx;
787787
if (ret)
788788
return ret;
789789
}

lib/test_bpf.c

+45
Original file line numberDiff line numberDiff line change
@@ -4761,6 +4761,51 @@ static struct bpf_test tests[] = {
47614761
{ },
47624762
{ { 0, 1 } },
47634763
},
4764+
{
4765+
/* Mainly testing JIT + imm64 here. */
4766+
"JMP_JGE_X: ldimm64 test 1",
4767+
.u.insns_int = {
4768+
BPF_ALU32_IMM(BPF_MOV, R0, 0),
4769+
BPF_LD_IMM64(R1, 3),
4770+
BPF_LD_IMM64(R2, 2),
4771+
BPF_JMP_REG(BPF_JGE, R1, R2, 2),
4772+
BPF_LD_IMM64(R0, 0xffffffffffffffffUL),
4773+
BPF_LD_IMM64(R0, 0xeeeeeeeeeeeeeeeeUL),
4774+
BPF_EXIT_INSN(),
4775+
},
4776+
INTERNAL,
4777+
{ },
4778+
{ { 0, 0xeeeeeeeeU } },
4779+
},
4780+
{
4781+
"JMP_JGE_X: ldimm64 test 2",
4782+
.u.insns_int = {
4783+
BPF_ALU32_IMM(BPF_MOV, R0, 0),
4784+
BPF_LD_IMM64(R1, 3),
4785+
BPF_LD_IMM64(R2, 2),
4786+
BPF_JMP_REG(BPF_JGE, R1, R2, 0),
4787+
BPF_LD_IMM64(R0, 0xffffffffffffffffUL),
4788+
BPF_EXIT_INSN(),
4789+
},
4790+
INTERNAL,
4791+
{ },
4792+
{ { 0, 0xffffffffU } },
4793+
},
4794+
{
4795+
"JMP_JGE_X: ldimm64 test 3",
4796+
.u.insns_int = {
4797+
BPF_ALU32_IMM(BPF_MOV, R0, 1),
4798+
BPF_LD_IMM64(R1, 3),
4799+
BPF_LD_IMM64(R2, 2),
4800+
BPF_JMP_REG(BPF_JGE, R1, R2, 4),
4801+
BPF_LD_IMM64(R0, 0xffffffffffffffffUL),
4802+
BPF_LD_IMM64(R0, 0xeeeeeeeeeeeeeeeeUL),
4803+
BPF_EXIT_INSN(),
4804+
},
4805+
INTERNAL,
4806+
{ },
4807+
{ { 0, 1 } },
4808+
},
47644809
/* BPF_JMP | BPF_JNE | BPF_X */
47654810
{
47664811
"JMP_JNE_X: if (3 != 2) return 1",

0 commit comments

Comments
 (0)