Skip to content

Commit acfdc8b

Browse files
laoargregkh
authored andcommitted
bpf: Fix issue in verifying allow_ptr_leaks
commit d75e30d upstream. 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 <yonghong.song@linux.dev> Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com> Signed-off-by: Yafang Shao <laoar.shao@gmail.com> Acked-by: Eduard Zingerman <eddyz87@gmail.com> Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20230823020703.3790-2-laoar.shao@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a3199a1 commit acfdc8b

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13816,6 +13816,12 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
1381613816
return -EINVAL;
1381713817
}
1381813818

13819+
/* check src2 operand */
13820+
err = check_reg_arg(env, insn->dst_reg, SRC_OP);
13821+
if (err)
13822+
return err;
13823+
13824+
dst_reg = &regs[insn->dst_reg];
1381913825
if (BPF_SRC(insn->code) == BPF_X) {
1382013826
if (insn->imm != 0) {
1382113827
verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
@@ -13827,25 +13833,20 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
1382713833
if (err)
1382813834
return err;
1382913835

13830-
if (is_pointer_value(env, insn->src_reg)) {
13836+
src_reg = &regs[insn->src_reg];
13837+
if (!(reg_is_pkt_pointer_any(dst_reg) && reg_is_pkt_pointer_any(src_reg)) &&
13838+
is_pointer_value(env, insn->src_reg)) {
1383113839
verbose(env, "R%d pointer comparison prohibited\n",
1383213840
insn->src_reg);
1383313841
return -EACCES;
1383413842
}
13835-
src_reg = &regs[insn->src_reg];
1383613843
} else {
1383713844
if (insn->src_reg != BPF_REG_0) {
1383813845
verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
1383913846
return -EINVAL;
1384013847
}
1384113848
}
1384213849

13843-
/* check src2 operand */
13844-
err = check_reg_arg(env, insn->dst_reg, SRC_OP);
13845-
if (err)
13846-
return err;
13847-
13848-
dst_reg = &regs[insn->dst_reg];
1384913850
is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
1385013851

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

0 commit comments

Comments
 (0)