Skip to contentSkip to conversation

bpf: make reg_not_null() true for CONST_PTR_TO_MAP

PATCH Superseded Ihor Solodrai 2 participants 3 messages May 23, 2025 – May 27, 2025

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 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().

---
 kernel/bpf/verifier.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +2 -1
index d5807d2efc92..f5e23ec83ef7 100644
@@ -405,7 +405,8 @@ static bool reg_not_null(const struct bpf_reg_state *reg)
 		type == PTR_TO_MAP_KEY ||
 		type == PTR_TO_SOCK_COMMON ||
 		(type == PTR_TO_BTF_ID && is_trusted_reg(reg)) ||
-		type == PTR_TO_MEM;
+		type == PTR_TO_MEM ||
+		type == CONST_PTR_TO_MAP;
 }
 
 static struct btf_record *reg_btf_record(const struct bpf_reg_state *reg)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 2/2 selftests/bpf: add a test case with CONST_PTR_TO_MAP null check

The test requires the following to happen:
* CONST_PTR_TO_MAP value is put on the stack
* then this value is checked for null
* the code in the null branch fails verification

I was able to achieve this by using a stack allocated array of maps, populated with values from a global map.

---
 .../selftests/bpf/progs/verifier_map_in_map.c | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)
tools/testing/selftests/bpf/progs/verifier_map_in_map.c +24 -0
index 7d088ba99ea5..52b3e1749e71 100644
@@ -139,4 +139,28 @@ __naked void on_the_inner_map_pointer(void)
 	: __clobber_all);
 }
 
+SEC("socket")
+int map_ptr_is_never_null(void *ctx)
+{
+	struct bpf_map *maps[2] = { 0 };
+	struct bpf_map *map = NULL;
+	int __attribute__((aligned(8))) key = 0;
+
+	for (key = 0; key < 2; key++) {
+		map = bpf_map_lookup_elem(&map_in_map, &key);
+		if (map)
+			maps[key] = map;
+		else
+			return 0;
+	}
+
+	/* After the loop every element of maps is CONST_PTR_TO_MAP so
+	 * the invalid branch should not be explored by the verifier.
+	 */
+	if (!maps[0])
+		asm volatile ("r10 = 0;");
+
+	return 0;
+}
+
 char _license[] SEC("license") = "GPL";
Signature · 1 line
2.47.1
#2 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add a test case with CONST_PTR_TO_MAP null check

Quoted 36 lines · Ihor Solodrai

The test requires the following to happen:
* CONST_PTR_TO_MAP value is put on the stack
* then this value is checked for null
* the code in the null branch fails verification

I was able to achieve this by using a stack allocated array of maps, populated with values from a global map.

---
 .../selftests/bpf/progs/verifier_map_in_map.c | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)
tools/testing/selftests/bpf/progs/verifier_map_in_map.c +14 -0
index 7d088ba99ea5..52b3e1749e71 100644
@@ -139,4 +139,28 @@ __naked void on_the_inner_map_pointer(void)
        : __clobber_all);
 }
 
+SEC("socket")
+int map_ptr_is_never_null(void *ctx)
+{
+       struct bpf_map *maps[2] = { 0 };
+       struct bpf_map *map = NULL;
+       int __attribute__((aligned(8))) key = 0;
+
+       for (key = 0; key < 2; key++) {
+               map = bpf_map_lookup_elem(&map_in_map, &key);
+               if (map)
+                       maps[key] = map;
+               else
+                       return 0;
+       }

tools/testing/selftests/bpf/progs/verifier_map_in_map.c __naked void on_the_inner_map_pointer(void) · line 165

Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_map_in_map.c +10 -0
+
+       /* After the loop every element of maps is CONST_PTR_TO_MAP so
+        * the invalid branch should not be explored by the verifier.
+        */
+       if (!maps[0])
+               asm volatile ("r10 = 0;");
+
+       return 0;
+}
+

I had (slightly redacted) logic like this when I ran into this issue originally:

struct rb_ctx {
    void *rb;
    struct bpf_dynptr dptr;
};

static __always_inline struct rb_ctx __rb_event_reserve(u64 dyn_sz)

{
    struct rb_ctx rb_ctx = {};
    void *rb;
    u32 cpu = bpf_get_smp_processor_id();
    u32 rb_slot = cpu & 1; /* 0 or 1 */

    rb = bpf_map_lookup_elem(&rbs, &rb_slot);
    if (!rb)
        return rb_ctx;

    rb_ctx.rb = rb;
    rb_ctx.has_dptr = true;

    /* can fail, still needs bpf_ringbuf_submit_dynptr() */
    bpf_ringbuf_reserve_dynptr(rb, sz, 0, &rb_ctx.dptr);

    return rb_ctx;
}

static __noinline void __rb_event_submit(struct rb_ctx *ctx)

{
    if (!ctx->rb) /* shouldn't/can't do submit below */
        return;

    /* no-op, if ctx->rb is NULL */
    bpf_ringbuf_submit_dynptr(&ctx->dptr, 0);
}

Where rbs is ARRAY_OF_MAPS of RINGBUF maps. And so when calling __rb_event_reserve(), followed by __rb_event_submit(), no matter whether map-in-map lookup succeeded or not, no matter whether ringbuf_reserve_dynptr() succeeded or not, we'd satisfy ringbuf API rules calling submit_dynptr() iff map-in-map succeeded.

Maybe try using this logic, it's actually a real-world code pattern, so would be good to know it keeps working after your fix.

pw-bot: cr

Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_map_in_map.c +0 -1
 char _license[] SEC("license") = "GPL";
--

2.47.1