Skip to content

Commit d75e30d

Browse files
laoarAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Fix issue in verifying allow_ptr_leaks
After we converted the capabilities of our networking-bpf program from cap_sys_admin to cap_net_admin+cap_bpf, our networking-bpf program failed to start. Because it failed the bpf verifier, and the error log is "R3 pointer comparison prohibited". A simple reproducer as follows, SEC("cls-ingress") int ingress(struct __sk_buff *skb) { struct iphdr *iph = (void *)(long)skb->data + sizeof(struct ethhdr); if ((long)(iph + 1) > (long)skb->data_end) return TC_ACT_STOLEN; return TC_ACT_OK; } Per discussion with Yonghong and Alexei [1], comparison of two packet pointers is not a pointer leak. This patch fixes it. Our local kernel is 6.1.y and we expect this fix to be backported to 6.1.y, so stable is CCed. [1]. https://lore.kernel.org/bpf/CAADnVQ+Nmspr7Si+pxWn8zkE7hX-7s93ugwC+94aXSy4uQ9vBg@mail.gmail.com/ Suggested-by: Yonghong Song <[email protected]> Suggested-by: Alexei Starovoitov <[email protected]> Signed-off-by: Yafang Shao <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 29d67fd commit d75e30d

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

kernel/bpf/verifier.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14041,6 +14041,12 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
1404114041
return -EINVAL;
1404214042
}
1404314043

14044+
/* check src2 operand */
14045+
err = check_reg_arg(env, insn->dst_reg, SRC_OP);
14046+
if (err)
14047+
return err;
14048+
14049+
dst_reg = &regs[insn->dst_reg];
1404414050
if (BPF_SRC(insn->code) == BPF_X) {
1404514051
if (insn->imm != 0) {
1404614052
verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
@@ -14052,25 +14058,20 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
1405214058
if (err)
1405314059
return err;
1405414060

14055-
if (is_pointer_value(env, insn->src_reg)) {
14061+
src_reg = &regs[insn->src_reg];
14062+
if (!(reg_is_pkt_pointer_any(dst_reg) && reg_is_pkt_pointer_any(src_reg)) &&
14063+
is_pointer_value(env, insn->src_reg)) {
1405614064
verbose(env, "R%d pointer comparison prohibited\n",
1405714065
insn->src_reg);
1405814066
return -EACCES;
1405914067
}
14060-
src_reg = &regs[insn->src_reg];
1406114068
} else {
1406214069
if (insn->src_reg != BPF_REG_0) {
1406314070
verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
1406414071
return -EINVAL;
1406514072
}
1406614073
}
1406714074

14068-
/* check src2 operand */
14069-
err = check_reg_arg(env, insn->dst_reg, SRC_OP);
14070-
if (err)
14071-
return err;
14072-
14073-
dst_reg = &regs[insn->dst_reg];
1407414075
is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
1407514076

1407614077
if (BPF_SRC(insn->code) == BPF_K) {

0 commit comments

Comments
 (0)