Skip to content

Commit db78721

Browse files
theihorKernel Patches Daemon
authored andcommitted
bpf: make reg_not_null() true for CONST_PTR_TO_MAP
When reg->type is CONST_PTR_TO_MAP, it can not be null. However the verifier explores the branches under rX == 0 in check_cond_jmp_op() even if reg->type is CONST_PTR_TO_MAP, because it was not checked for in reg_not_null(). Fix this by adding CONST_PTR_TO_MAP to the set of types that are considered non nullable in reg_not_null(). An old "unpriv: cmp map pointer with zero" selftest fails with this change, because now early out correctly triggers in check_cond_jmp_op(), making the verification to pass. In practice verifier may allow pointer to null comparison in unpriv, since in many cases the relevant branch and comparison op are removed as dead code. So change the expected test result to __success_unpriv. Signed-off-by: Ihor Solodrai <[email protected]> Acked-by: Andrii Nakryiko <[email protected]>
1 parent ccbb215 commit db78721

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

kernel/bpf/verifier.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ static bool reg_not_null(const struct bpf_reg_state *reg)
405405
type == PTR_TO_MAP_KEY ||
406406
type == PTR_TO_SOCK_COMMON ||
407407
(type == PTR_TO_BTF_ID && is_trusted_reg(reg)) ||
408-
type == PTR_TO_MEM;
408+
type == PTR_TO_MEM ||
409+
type == CONST_PTR_TO_MAP;
409410
}
410411

411412
static struct btf_record *reg_btf_record(const struct bpf_reg_state *reg)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ __naked void pass_pointer_to_tail_call(void)
619619

620620
SEC("socket")
621621
__description("unpriv: cmp map pointer with zero")
622-
__success __failure_unpriv __msg_unpriv("R1 pointer comparison")
622+
__success __success_unpriv
623623
__retval(0)
624624
__naked void cmp_map_pointer_with_zero(void)
625625
{

0 commit comments

Comments
 (0)