check_cond_jmp_op() propagates PTR_MAYBE_NULL nullness through mark_ptr_or_null_regs() when it detects a BPF_JEQ / BPF_JNE comparison against zero. The existing code handles only the 'forward' operand order: dst_reg = nullable pointer, src_reg = known-zero scalar (e.g. 'if r0 == 0') The symmetric case is left unhandled: dst_reg = known-zero scalar, src_reg = nullable pointer (e.g. 'if r6 == r0' where r6 has been proven zero) When the symmetric case occurs: - type_may_be_null(dst_reg->type) is false (dst is scalar zero) - The null-check block is skipped entirely - mark_ptr_or_null_regs() is never called for src_reg - src_reg retains PTR_MAYBE_NULL in both branches Additionally, the early unprivileged-mode pointer-comparison guard (line 18058) rejected any conditional jump where src_reg is a pointer type, which would have made the new block unreachable for unprivileged programs. Comparing a nullable pointer against a register proven to be zero is not a pointer leak -- it is semantically identical to the already-permitted forward form 'if (r_nullable == 0)'. Exempt the symmetric null check from that guard. Fix: add an 'else if' branch that mirrors the forward logic for the symmetric operand order and extend the early guard to permit it. Add corresponding symmetric JEQ, JNE, and negative non-zero dst test cases into verifier_jeq_infer_not_null.c with __success_unpriv to exercise both privileged and unprivileged paths. Commit 6aed0134d3cd ('bpf: Mark the zero register precise for a register-form NULL check') introduced the precision marking for the forward case; the symmetric case was overlooked there. Signed-off-by: Rahad Bhuiya --- kernel/bpf/verifier.c | 40 ++++++++- .../bpf/progs/verifier_jeq_infer_not_null.c | 82 +++++++++++++++++++ 2 files changed, 119 insertions(+), 3 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 353bde9ae227..68b23d11766d 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -18057,9 +18057,22 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, src_reg = ®s[insn->src_reg]; if (!(reg_is_pkt_pointer_any(dst_reg) && reg_is_pkt_pointer_any(src_reg)) && is_pointer_value(env, insn->src_reg)) { - verbose(env, "R%d pointer comparison prohibited\n", - insn->src_reg); - return -EACCES; + /* + * Symmetric NULL check: "if (r_zero == r_nullable)". + * Comparing a nullable pointer against a register proven + * to be zero is not a pointer leak -- it is the reversed + * form of the already-permitted "if (r_nullable == 0)". + * Permit it; reject every other src_reg pointer comparison. + */ + if (BPF_CLASS(insn->code) == BPF_JMP32 || + (BPF_OP(insn->code) != BPF_JEQ && + BPF_OP(insn->code) != BPF_JNE) || + !type_may_be_null(src_reg->type) || + !bpf_register_is_null(dst_reg)) { + verbose(env, "R%d pointer comparison prohibited\n", + insn->src_reg); + return -EACCES; + } } if (src_reg->type == PTR_TO_STACK) @@ -18248,6 +18261,27 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, opcode == BPF_JNE); mark_ptr_or_null_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ); + } else if (!is_jmp32 && BPF_SRC(insn->code) == BPF_X && + (opcode == BPF_JEQ || opcode == BPF_JNE) && + type_may_be_null(src_reg->type) && + bpf_register_is_null(dst_reg)) { + /* + * Symmetric case: src_reg is the nullable pointer, dst_reg is + * a register whose value is known to be zero. This arises when + * the programmer writes the comparison in reversed operand order, + * e.g. "if (0 == map_val)" or "if (r0 == map_val)" where r0 has + * been proven zero by the verifier. + * + * The zero is a property of this execution path, so dst_reg + * must be marked precise before we propagate nullness. + */ + err = mark_chain_precision(env, insn->dst_reg); + if (err) + return err; + mark_ptr_or_null_regs(this_branch, insn->src_reg, + opcode == BPF_JNE); + mark_ptr_or_null_regs(other_branch, insn->src_reg, + opcode == BPF_JEQ); } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], this_branch, other_branch) && is_pointer_value(env, insn->dst_reg)) { diff --git a/tools/testing/selftests/bpf/progs/verifier_jeq_infer_not_null.c b/tools/testing/selftests/bpf/progs/verifier_jeq_infer_not_null.c index 3c789c565b18..ee2e9374969d 100644 --- a/tools/testing/selftests/bpf/progs/verifier_jeq_infer_not_null.c +++ b/tools/testing/selftests/bpf/progs/verifier_jeq_infer_not_null.c @@ -273,6 +273,88 @@ __naked void jne_reg_reg_null_check(void) : __clobber_all); } +/* Symmetric case: dst_reg is known 0, src_reg is nullable pointer. + * "if (r1 == r0) goto 1f;" (reversed operand order). + * Verify that non-nullness is propagated to src_reg in the non-null branch. + */ +SEC("socket") +__description("null check: symmetric form (dst 0 reg, src nullable) JEQ") +__success __success_unpriv +__retval(0) +__naked void jeq_reg_reg_null_check_symmetric(void) +{ + asm volatile (" \ + *(u32*)(r10 - 8) = 0; \ + r1 = %[map_hash] ll; \ + r2 = r10; \ + r2 += -8; \ + call %[bpf_map_lookup_elem]; \ + r1 = 0; \ + /* r1 is known 0, r0 is nullable ptr */ \ + if r1 == r0 goto 1f; \ + r0 = *(u32*)(r0 + 0); \ +1: r0 = 0; \ + exit; \ +" : + : __imm(bpf_map_lookup_elem), + __imm_addr(map_hash) + : __clobber_all); +} + +/* Symmetric case with JNE: "if (r1 != r0) goto 1f;" + * Taken branch: r0 is non-null. Fall-through: r0 is null. + */ +SEC("socket") +__description("null check: symmetric form (dst 0 reg, src nullable) JNE") +__success __success_unpriv +__retval(0) +__naked void jne_reg_reg_null_check_symmetric(void) +{ + asm volatile (" \ + *(u32*)(r10 - 8) = 0; \ + r1 = %[map_hash] ll; \ + r2 = r10; \ + r2 += -8; \ + call %[bpf_map_lookup_elem]; \ + r1 = 0; \ + if r1 != r0 goto 1f; \ + goto 2f; \ +1: r0 = *(u32*)(r0 + 0); \ +2: r0 = 0; \ + exit; \ +" : + : __imm(bpf_map_lookup_elem), + __imm_addr(map_hash) + : __clobber_all); +} + +/* Negative test: dst_reg is a non-zero scalar (r1 = 1). + * "if (r1 == r0)" must NOT be treated as a null check. + * Dereference must fail because r0 remains nullable. + */ +SEC("socket") +__description("null check: symmetric form with non-zero dst must not elide null check") +__failure +__msg("R0 invalid mem access 'map_value_or_null'") +__naked void null_check_symmetric_nonzero_dst_rejected(void) +{ + asm volatile (" \ + *(u32*)(r10 - 8) = 0; \ + r1 = %[map_hash] ll; \ + r2 = r10; \ + r2 += -8; \ + call %[bpf_map_lookup_elem]; \ + r1 = 1; \ + if r1 == r0 goto 1f; \ + r0 = *(u32*)(r0 + 0); \ +1: r0 = 0; \ + exit; \ +" : + : __imm(bpf_map_lookup_elem), + __imm_addr(map_hash) + : __clobber_all); +} + /* * A comparison between PTR_TO_MEM | MEM_RDONLY | PTR_UNTRUSTED and * PTR_TO_MAP_VALUE_OR_NULL should not infer that map pointer is not null. -- 2.55.0.windows.3