Skip to content

Commit a1aca22

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-fix-verifier-crash-on-bpf_neg-with-pointer-register'
Brahmajit Das says: ==================== bpf: Fix verifier crash on BPF_NEG with pointer register This patch fixes a crash in the BPF verifier triggered when the BPF_NEG operation is applied to a pointer-typed register. The verifier now checks that the destination register is not a pointer before performing the operation. Tested with syzkaller reproducer and new BPF sefltest. Closes: https://syzkaller.appspot.com/bug?extid=d36d5ae81e1b0a53ef58 Changes v4: Cleaning up, instead of using __is_pointer_value it's further simplified by checking if regs[insn->dst_reg].type of SCALAR_VALUE Link: Changes in v3: using __is_pointer_value to check if register if of pointer type Link: https://lore.kernel.org/all/20251001095613.267475-1-listout@listout.xyz/ Changes in v2: Checking if reg->map_ptr is NULL in bpf/log.c but with cleaner approach (wrong approach) Link: https://lore.kernel.org/all/20250923174738.1713751-1-listout@listout.xyz/ Changes in v1: Checking if reg->map_ptr is NULL in bpf/log.c (wrong approach) Link: https://lore.kernel.org/all/20250923164144.1573636-1-listout@listout.xyz/ ==================== Link: https://patch.msgid.link/20251001191739.2323644-1-listout@listout.xyz Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 0c342bf + 8709c16 commit a1aca22

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

kernel/bpf/verifier.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15645,7 +15645,8 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
1564515645
}
1564615646

1564715647
/* check dest operand */
15648-
if (opcode == BPF_NEG) {
15648+
if (opcode == BPF_NEG &&
15649+
regs[insn->dst_reg].type == SCALAR_VALUE) {
1564915650
err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
1565015651
err = err ?: adjust_scalar_min_max_vals(env, insn,
1565115652
&regs[insn->dst_reg],

tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,24 @@ l0_%=: exit; \
146146
: __clobber_all);
147147
}
148148

149+
SEC("socket")
150+
__description("map_ptr illegal alu op, map_ptr = -map_ptr")
151+
__failure __msg("R0 invalid mem access 'scalar'")
152+
__failure_unpriv __msg_unpriv("R0 pointer arithmetic prohibited")
153+
__flag(BPF_F_ANY_ALIGNMENT)
154+
__naked void map_ptr_illegal_alu_op(void)
155+
{
156+
asm volatile (" \
157+
r0 = %[map_hash_48b] ll; \
158+
r0 = -r0; \
159+
r1 = 22; \
160+
*(u64*)(r0 + 0) = r1; \
161+
exit; \
162+
" :
163+
: __imm_addr(map_hash_48b)
164+
: __clobber_all);
165+
}
166+
149167
SEC("flow_dissector")
150168
__description("flow_keys illegal alu op with variable offset")
151169
__failure __msg("R7 pointer arithmetic on flow_keys prohibited")

0 commit comments

Comments
 (0)