Skip to content

Commit dfce9cb

Browse files
Yonghong Songanakryiko
Yonghong Song
authored andcommitted
bpf: Fix a verifier bug due to incorrect branch offset comparison with cpu=v4
Bpf cpu=v4 support is introduced in [1] and Commit 4cd58e9 ("bpf: Support new 32bit offset jmp instruction") added support for new 32bit offset jmp instruction. Unfortunately, in function bpf_adj_delta_to_off(), for new branch insn with 32bit offset, the offset (plus/minor a small delta) compares to 16-bit offset bound [S16_MIN, S16_MAX], which caused the following verification failure: $ ./test_progs-cpuv4 -t verif_scale_pyperf180 ... insn 10 cannot be patched due to 16-bit range ... libbpf: failed to load object 'pyperf180.bpf.o' scale_test:FAIL:expect_success unexpected error: -12 (errno 12) #405 verif_scale_pyperf180:FAIL Note that due to recent llvm18 development, the patch [2] (already applied in bpf-next) needs to be applied to bpf tree for testing purpose. The fix is rather simple. For 32bit offset branch insn, the adjusted offset compares to [S32_MIN, S32_MAX] and then verification succeeded. [1] https://lore.kernel.org/all/[email protected] [2] https://lore.kernel.org/bpf/[email protected] Fixes: 4cd58e9 ("bpf: Support new 32bit offset jmp instruction") Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 830139e commit dfce9cb

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

kernel/bpf/core.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,18 @@ static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, s32 end_old,
371371
static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, s32 end_old,
372372
s32 end_new, s32 curr, const bool probe_pass)
373373
{
374-
const s32 off_min = S16_MIN, off_max = S16_MAX;
374+
s64 off_min, off_max, off;
375375
s32 delta = end_new - end_old;
376-
s32 off;
377376

378-
if (insn->code == (BPF_JMP32 | BPF_JA))
377+
if (insn->code == (BPF_JMP32 | BPF_JA)) {
379378
off = insn->imm;
380-
else
379+
off_min = S32_MIN;
380+
off_max = S32_MAX;
381+
} else {
381382
off = insn->off;
383+
off_min = S16_MIN;
384+
off_max = S16_MAX;
385+
}
382386

383387
if (curr < pos && curr + off + 1 >= end_old)
384388
off += delta;

0 commit comments

Comments
 (0)