Skip to contentSkip to conversation

bpf: Keep fault protection when merging pointer types

PATCH In review Daniel Borkmann 4 participants 23 messages Aug 14 – Aug 17

Subsystems (5)
Kselftest BPF HID BPF selftests +1 more subsystems
lore
Share & tools
lore

Daniel Borkmann

author maintainer
#0 lore raw

PATCH v2 1/6 bpf: Keep fault protection when merging pointer types

When the same BPF_LDX instruction is reached through paths that yield different pointer types, save_aux_ptr_type() merges them into a single type which is later used by bpf_convert_ctx_accesses() to decide whether the load has to be rewritten into a BPF_PROBE_MEM one.

Before f2362a57aeff ("bpf: allow void* cast using bpf_rdonly_cast()") the merge only accepted two PTR_TO_BTF_ID pointers and unconditionally fell back to PTR_TO_BTF_ID | PTR_UNTRUSTED, so the merged type was always one that gets the BPF_PROBE_MEM rewrite. However, the mentioned commit widened the merge to also cover a PTR_TO_MEM base and replaced the fallback by a union of the PTR_UNTRUSTED and MEM_RDONLY flags.

A union of flags though cannot express the property the later rewrite is built upon, some examples:

- PTR_TO_MEM merged with PTR_TO_BTF_ID | PTR_UNTRUSTED gets PTR_TO_MEM | PTR_UNTRUSTED but only the MEM_RDONLY variant is valid
- PTR_TO_MEM merged with a plain PTR_TO_BTF_ID gets PTR_TO_MEM dropping the rewrite the latter type would have gotten
- PTR_TO_MEM | MEM_RDONLY merged with a plain PTR_TO_BTF_ID gets PTR_TO_MEM | MEM_RDONLY which is not rewritten either since only its PTR_UNTRUSTED variant is

In all three cases a program can take the unsafe path at runtime with a NULL or otherwise bad pointer and panic the kernel on the faulting load:

  BUG: kernel NULL pointer dereference, address: 0000000000000038
  RIP: 0010:bpf_prog_77531a87032eeaf1_mixed_mem_btf_id_type+0x4b/0x65
  Call Trace:
   <TASK>
   bpf_test_run+0x20b/0x460
   bpf_prog_test_run_skb+0x650/0xbe0
   __sys_bpf+0xb96/0x3140
   __x64_sys_bpf+0x2c/0x40
   do_syscall_64+0xba/0x590
  Kernel panic - not syncing: Fatal exception in interrupt

Note that the last two shapes have to be fixed right here, otherwise the merged type retains nothing which marks the load as fault prone, thus no rule in bpf_convert_ctx_accesses() can recover it. Fix it by normalizing the merged type instead.

Reuse it in is_load_acq_unsafe() to avoid open coding, and trim the overly verbose comment which is more of an implementation detail of bpf_convert_ctx_accesses() anyway.

Fixes f2362a57aeff ("bpf: allow void* cast using bpf_rdonly_cast()")

---
v1 -> v2:
- update commit msg that this commit is still needed

 include/linux/bpf_verifier.h | 10 ++++++++
 kernel/bpf/verifier.c        | 49 +++++++++++++++++-------------------
 2 files changed, 33 insertions(+), 26 deletions(-)
include/linux/bpf_verifier.h +10 -0
index 93f7c2075eea..114ae4540d2c 100644
@@ -1297,6 +1297,16 @@ static inline u32 type_flag(u32 type)
 	return type & ~BPF_BASE_TYPE_MASK;
 }
 
+static inline bool bpf_may_fault_on_deref(enum bpf_reg_type type)
+{
+	/*
+	 * The pointer types which must not be dereferenced without fault
+	 * protection, that is, the ones bpf_convert_ctx_accesses() has to
+	 * turn a BPF_LDX into a BPF_PROBE_MEM one for.
+	 */
+	return type == PTR_TO_BTF_ID || (type_flag(type) & PTR_UNTRUSTED);
+}
+
 static inline bool bpf_prog_has_arena_ctx_arg(const struct bpf_prog *prog)
 {
 	int i;
kernel/bpf/verifier.c +23 -26

73 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v2 2/6 bpf: Treat a fault prone PTR_TO_MEM as a pointer type mismatch

reg_type_mismatch_ok() enumerates the pointer types which must not silently share a BPF_LDX with a different one, since the type recorded for the insn drives a rewrite in bpf_convert_ctx_accesses().

f2362a57aeff ("bpf: allow void* cast using bpf_rdonly_cast()") added PTR_TO_MEM | MEM_RDONLY | PTR_UNTRUSTED as another type in need of one, namely the BPF_PROBE_MEM rewrite, but did not add it there. Fix it by adding the missing case to reg_type_mismatch_ok(), so that a PTR_TO_MEM which may fault on deref is not mismatch ok anymore. The triage in save_aux_ptr_type() then merges them.

Fixes f2362a57aeff ("bpf: allow void* cast using bpf_rdonly_cast()")

---
v1 -> v2:
- new patch to address PTR_TO_MEM case differently (Eduard)

 kernel/bpf/verifier.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/bpf/verifier.c +2 -0
index a3d165e92175..2e6992569187 100644
@@ -16974,6 +16974,8 @@ static bool reg_type_mismatch_ok(enum bpf_reg_type type)
 	case PTR_TO_BTF_ID:
 	case PTR_TO_ARENA:
 		return false;
+	case PTR_TO_MEM:
+		return !bpf_may_fault_on_deref(type);
 	default:
 		return true;
 	}
Signature · 1 line
2.43.0
maintainer Acked-by on v2 2/6
#2 lore raw

Re: [PATCH bpf-next v2 2/6] bpf: Treat a fault prone PTR_TO_MEM as a pointer type mismatch

Quoted 11 lines · Daniel Borkmann

reg_type_mismatch_ok() enumerates the pointer types which must not silently share a BPF_LDX with a different one, since the type recorded for the insn drives a rewrite in bpf_convert_ctx_accesses().

f2362a57aeff ("bpf: allow void* cast using bpf_rdonly_cast()") added PTR_TO_MEM | MEM_RDONLY | PTR_UNTRUSTED as another type in need of one, namely the BPF_PROBE_MEM rewrite, but did not add it there. Fix it by adding the missing case to reg_type_mismatch_ok(), so that a PTR_TO_MEM which may fault on deref is not mismatch ok anymore. The triage in save_aux_ptr_type() then merges them.

Quoted 5 lines
Fixes f2362a57aeff ("bpf: allow void* cast using bpf_rdonly_cast()")

---
 v1 -> v2:
   - new patch to address PTR_TO_MEM case differently (Eduard)

author maintainer
#3 lore raw

PATCH v2 3/6 bpf: Reject a store through a fault prone pointer

In reply to Daniel Borkmann

check_ptr_to_btf_access() allows the program to store before the default BTF access path gets to reject a non read access. ac65c710cc64 ("bpf:
Reject writes through untrusted BTF pointers") closed that for a PTR_UNTRUSTED pointer, but a bare PTR_TO_BTF_ID may fault on a dereference just the same and is let through.

A BPF_LDX gets the BPF_PROBE_MEM rewrite in bpf_convert_ctx_accesses() and a bad address is handled, but a BPF_STX does not and cannot, there is no probed store to rewrite. The store is emitted as a plain one without an exception table entry and a bad address panics the kernel.

A bpf_qdisc program can reach this, bpf_qdisc_btf_struct_access() permits a write to Qdisc::limit and Qdisc::next_sched is a plain struct Qdisc pointer which the walk turns into the compat type:

  struct Qdisc *next = sch->next_sched;

  next->limit = 1000;
  BUG: kernel NULL pointer dereference, address: 0000000000000014
  RIP: 0010:bpf_prog_c6e14e7f32c8e325_bpf_fifo_enqueue+0x3a/0x12b
  Code: [...] bf e8 03 00 00 <89> 7e 14 41 8b 7f 14 [...]
  Kernel panic - not syncing: Fatal exception in interrupt

Fix by widen the check to bpf_may_fault_on_deref() so that it covers both.

Fixes 27ae7997a661 ("bpf: Introduce BPF_PROG_TYPE_STRUCT_OPS")

---
v1 -> v2:
- new patch

 kernel/bpf/verifier.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/verifier.c +1 -1
index 2e6992569187..6610e2437047 100644
@@ -5789,7 +5789,7 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,
 		return -EACCES;
 	}
 
-	if (atype != BPF_READ && (type_flag(reg->type) & PTR_UNTRUSTED)) {
+	if (atype != BPF_READ && bpf_may_fault_on_deref(reg->type)) {
 		verbose(env, "only read is supported\n");
 		return -EACCES;
 	}
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH bpf-next v2 3/6] bpf: Reject a store through a fault prone pointer

Does this change also reject HID-BPF writes to struct hid_device fields?

kernel/bpf/verifier.c static int check_ptr_to_btf_access(struct bpf_verifier_env *env, · line 5794

Quoted 13 lines
kernel/bpf/verifier.c +1 -1
index 814096ed835d..cbbb0c82d22c 100644
@@ -5788,7 +5788,7 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,
 		return -EACCES;
 	}
 
-	if (atype != BPF_READ && (type_flag(reg->type) & PTR_UNTRUSTED)) {
+	if (atype != BPF_READ && bpf_may_fault_on_deref(reg->type)) {
 		verbose(env, "only read is supported\n");
 		return -EACCES;
 	}

Does this change also reject HID-BPF writes to struct hid_device fields?

The HID struct_ops hid_bpf_ops_btf_struct_access() whitelists writes to hid_device::name, ::uniq, and ::phys via WRITE_RANGE entries in drivers/hid/bpf/hid_bpf_struct_ops.c. A HID program receives a 'struct hid_bpf_ctx *' argument that is PTR_TO_BTF_ID | PTR_TRUSTED, but walking hctx->hid produces a bare PTR_TO_BTF_ID because struct hid_bpf_ctx appears in no BTF_TYPE_SAFE_TRUSTED list in check_ptr_to_btf_access().

Before this commit, 'hctx->hid->name[i] = c' passed the PTR_UNTRUSTED test and reached env->ops->btf_struct_access(), where hid_bpf_ops_btf_struct_access() returned 0 for offsets in the whitelisted ranges and the store succeeded. After this commit, bpf_may_fault_on_deref(PTR_TO_BTF_ID) is true and the store is rejected with 'only read is supported' before the btf_struct_access callback runs.

The three WRITE_RANGE(hid_device, ...) entries become unreachable. The commit message notes the bpf_qdisc Qdisc::next_sched case but does not mention HID-BPF. If the intent is to preserve HID-BPF write capability, hid_bpf_ctx::hid would need a trusted annotation (BTF_TYPE_SAFE_TRUSTED(struct hid_bpf_ctx) { struct hid_device *hid; }) so the walked register stays PTR_TRUSTED. Otherwise, should the removal be documented?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/31845722500

author maintainer
#5 lore raw

Re: [PATCH bpf-next v2 3/6] bpf: Reject a store through a fault prone pointer

Quoted 16 lines · On 8/15/26 12:40 AM, [email protected] wrote:

kernel/bpf/verifier.c static int check_ptr_to_btf_access(struct bpf_verifier_env *env, · line 5794

Quoted 13 lines
kernel/bpf/verifier.c +1 -1
index 814096ed835d..cbbb0c82d22c 100644
@@ -5788,7 +5788,7 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,
  		return -EACCES;
  	}
 
-	if (atype != BPF_READ && (type_flag(reg->type) & PTR_UNTRUSTED)) {
+	if (atype != BPF_READ && bpf_may_fault_on_deref(reg->type)) {
  		verbose(env, "only read is supported\n");
  		return -EACCES;
  	}

Does this change also reject HID-BPF writes to struct hid_device fields?

Quoted 7 lines

The HID struct_ops hid_bpf_ops_btf_struct_access() whitelists writes to hid_device::name, ::uniq, and ::phys via WRITE_RANGE entries in drivers/hid/bpf/hid_bpf_struct_ops.c. A HID program receives a 'struct hid_bpf_ctx *' argument that is PTR_TO_BTF_ID | PTR_TRUSTED, but walking hctx->hid produces a bare PTR_TO_BTF_ID because struct hid_bpf_ctx appears in no BTF_TYPE_SAFE_TRUSTED list in check_ptr_to_btf_access().

Hm, that doesn't seem right.. potentially sth like:

drivers/hid/bpf/hid_bpf_struct_ops.c +6 -0
index 702c22fae136..56c53aca4511 100644
@@ -62,6 +62,10 @@ struct hid_bpf_offset_write_range {
        u32 end;
 };
 
+struct hid_bpf_ctx__safe_trusted {
+       struct hid_device *hid;
+};
+
 static int hid_bpf_ops_btf_struct_access(struct bpf_verifier_log *log,
                                           const struct bpf_reg_state *reg,
                                           int off, int size)
@@ -86,6 +90,8 @@ static int hid_bpf_ops_btf_struct_access(struct bpf_verifier_log *log,
        const char *cur = NULL;
        int i;
 
+       BTF_TYPE_EMIT(struct hid_bpf_ctx__safe_trusted);
+
        t = btf_type_by_id(reg->btf, reg->btf_id);
 
        for (i = 0; i < ARRAY_SIZE(write_ranges); i++) {
maintainer
#6 lore raw

Re: [PATCH bpf-next v2 3/6] bpf: Reject a store through a fault prone pointer

In reply to Daniel Borkmann
Quoted 43 lines · Daniel Borkmann

check_ptr_to_btf_access() allows the program to store before the default BTF access path gets to reject a non read access. ac65c710cc64 ("bpf:
Reject writes through untrusted BTF pointers") closed that for a PTR_UNTRUSTED pointer, but a bare PTR_TO_BTF_ID may fault on a dereference just the same and is let through.

A BPF_LDX gets the BPF_PROBE_MEM rewrite in bpf_convert_ctx_accesses() and a bad address is handled, but a BPF_STX does not and cannot, there is no probed store to rewrite. The store is emitted as a plain one without an exception table entry and a bad address panics the kernel.

A bpf_qdisc program can reach this, bpf_qdisc_btf_struct_access() permits a write to Qdisc::limit and Qdisc::next_sched is a plain struct Qdisc pointer which the walk turns into the compat type:

  struct Qdisc *next = sch->next_sched;

  next->limit = 1000;

  BUG: kernel NULL pointer dereference, address: 0000000000000014
  RIP: 0010:bpf_prog_c6e14e7f32c8e325_bpf_fifo_enqueue+0x3a/0x12b
  Code: [...] bf e8 03 00 00 <89> 7e 14 41 8b 7f 14 [...]
  Kernel panic - not syncing: Fatal exception in interrupt

Fix by widen the check to bpf_may_fault_on_deref() so that it covers both.

Fixes 27ae7997a661 ("bpf: Introduce BPF_PROG_TYPE_STRUCT_OPS")

---
 v1 -> v2:
   - new patch

 kernel/bpf/verifier.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/verifier.c +0 -0
index 2e6992569187..6610e2437047 100644
@@ -5789,7 +5789,7 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,
  		return -EACCES;
  	}

kernel/bpf/verifier.c static int check_ptr_to_btf_access(struct bpf_verifier_env *env, · line 5792

Quoted 2 lines
kernel/bpf/verifier.c +1 -1
-	if (atype != BPF_READ && (type_flag(reg->type) & PTR_UNTRUSTED)) {
+	if (atype != BPF_READ && bpf_may_fault_on_deref(reg->type)) {

The change is correct, but it appears that the if condition could be simplified as just 'atype != BPF_READ'.
The function is named check_ptr_to_btf_access() and it is only called when reg->type == PTR_TO_BTF_ID.

Quoted 3 lines

  verbose(env, "only read is supported\n"); return -EACCES;
  }

maintainer Acked-by
#7 lore raw

Re: [PATCH bpf-next v2 3/6] bpf: Reject a store through a fault prone pointer

Quoted 36 lines · Eduard Zingerman
Quoted 35 lines · Daniel Borkmann

check_ptr_to_btf_access() allows the program to store before the default BTF access path gets to reject a non read access. ac65c710cc64 ("bpf:
Reject writes through untrusted BTF pointers") closed that for a PTR_UNTRUSTED pointer, but a bare PTR_TO_BTF_ID may fault on a dereference just the same and is let through.

A BPF_LDX gets the BPF_PROBE_MEM rewrite in bpf_convert_ctx_accesses() and a bad address is handled, but a BPF_STX does not and cannot, there is no probed store to rewrite. The store is emitted as a plain one without an exception table entry and a bad address panics the kernel.

A bpf_qdisc program can reach this, bpf_qdisc_btf_struct_access() permits a write to Qdisc::limit and Qdisc::next_sched is a plain struct Qdisc pointer which the walk turns into the compat type:

  struct Qdisc *next = sch->next_sched;

  next->limit = 1000;

  BUG: kernel NULL pointer dereference, address: 0000000000000014
  RIP: 0010:bpf_prog_c6e14e7f32c8e325_bpf_fifo_enqueue+0x3a/0x12b
  Code: [...] bf e8 03 00 00 <89> 7e 14 41 8b 7f 14 [...]
  Kernel panic - not syncing: Fatal exception in interrupt

Fix by widen the check to bpf_may_fault_on_deref() so that it covers both.

Fixes 27ae7997a661 ("bpf: Introduce BPF_PROG_TYPE_STRUCT_OPS")

---
 v1 -> v2:
   - new patch

 kernel/bpf/verifier.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

kernel/bpf/verifier.c static int check_ptr_to_btf_access(struct bpf_verifier_env *env, · line 5792

Quoted 10 lines
kernel/bpf/verifier.c +0 -0
index 2e6992569187..6610e2437047 100644
@@ -5789,7 +5789,7 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,
  		return -EACCES;
  	}

kernel/bpf/verifier.c +1 -1
-	if (atype != BPF_READ && (type_flag(reg->type) & PTR_UNTRUSTED)) {
+	if (atype != BPF_READ && bpf_may_fault_on_deref(reg->type)) {

The change is correct, but it appears that the if condition could be simplified as just 'atype != BPF_READ'.
The function is named check_ptr_to_btf_access() and it is only called when reg->type == PTR_TO_BTF_ID.

Nah, scratch that, callers check base type.
Sorry for the noise.

author maintainer
#8 lore raw

PATCH v2 4/6 bpf: Rewrite any fault prone load out of a mem or btf_id pointer

In reply to Daniel Borkmann

bpf_convert_ctx_accesses() turns a BPF_LDX into a BPF_PROBE_MEM one by matching the type recorded for the insn against a list of exact pointer types. The list cannot keep up with the flag combinations the verifier produces, and a type which is missing from it ends up as a plain load without an exception table entry, so a bad address panics the kernel instead of being handled.

Two such types exist today and are reachable:

  - PTR_TO_BTF_ID | PTR_UNTRUSTED | MEM_ALLOC | NON_OWN_REF
  - PTR_TO_BTF_ID | PTR_UNTRUSTED | MEM_RCU

Rather than adding the two, just drop the list and state the property itself in the default case of the switch. This is a superset of what the list matched, the untrusted PTR_TO_MEM does not have to carry MEM_RDONLY for it anymore, and it stays in sync with the verifier side which uses the same match in save_aux_ptr_type() and reg_type_mismatch_ok().

Assert that a fault prone type which does not get the rewrite for whatever reason is rejected at load time rather than left to fault at runtime to catch any future cases.

Fixes 1b12171533a9 ("bpf: Mark direct ld of stashed bpf_{rb,list}_node as non-owning ref")
Fixes 6fcd486b3a0a ("bpf: Refactor RCU enforcement in the verifier.")

---
v1 -> v2:
- new patch, don't match on full types (Eduard)

 include/linux/bpf_verifier.h | 11 +++++++++
 kernel/bpf/fixups.c          | 47 ++++++++++++++++++++----------------
 kernel/bpf/verifier.c        | 15 ++----------
 3 files changed, 39 insertions(+), 34 deletions(-)
include/linux/bpf_verifier.h +11 -0
index 114ae4540d2c..7239cffa7b51 100644
@@ -1297,6 +1297,17 @@ static inline u32 type_flag(u32 type)
 	return type & ~BPF_BASE_TYPE_MASK;
 }
 
+static inline bool bpf_is_ptr_to_mem_or_btf_id(enum bpf_reg_type type)
+{
+	switch (base_type(type)) {
+	case PTR_TO_MEM:
+	case PTR_TO_BTF_ID:
+		return true;
+	default:
+		return false;
+	}
+}
+
 static inline bool bpf_may_fault_on_deref(enum bpf_reg_type type)
 {
 	/*
kernel/bpf/fixups.c +26 -21

71 lines not on this page · raw message

kernel/bpf/verifier.c +2 -13
index 6610e2437047..6fff370749bf 100644
@@ -16999,17 +16999,6 @@ static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev)
 			       !reg_type_mismatch_ok(prev));
 }
 
-static bool is_ptr_to_mem_or_btf_id(enum bpf_reg_type type)
-{
-	switch (base_type(type)) {
-	case PTR_TO_MEM:
-	case PTR_TO_BTF_ID:
-		return true;
-	default:
-		return false;
-	}
-}
-
 static bool is_ptr_to_mem(enum bpf_reg_type type)
 {
 	return base_type(type) == PTR_TO_MEM;
@@ -17049,8 +17038,8 @@ static int save_aux_ptr_type(struct bpf_verifier_env *env, enum bpf_reg_type typ
 		 * Reject it.
 		 */
 		if (allow_trust_mismatch &&
-		    is_ptr_to_mem_or_btf_id(type) &&
-		    is_ptr_to_mem_or_btf_id(*prev_type)) {
+		    bpf_is_ptr_to_mem_or_btf_id(type) &&
+		    bpf_is_ptr_to_mem_or_btf_id(*prev_type)) {
 			/*
 			 * Have to support a use case when one path through
 			 * the program yields a TRUSTED pointer while another
Signature · 1 line
2.43.0
maintainer Acked-by on v2 4/6
#9 lore raw

Re: [PATCH bpf-next v2 4/6] bpf: Rewrite any fault prone load out of a mem or btf_id pointer

Quoted 22 lines · Daniel Borkmann

bpf_convert_ctx_accesses() turns a BPF_LDX into a BPF_PROBE_MEM one by matching the type recorded for the insn against a list of exact pointer types. The list cannot keep up with the flag combinations the verifier produces, and a type which is missing from it ends up as a plain load without an exception table entry, so a bad address panics the kernel instead of being handled.

Two such types exist today and are reachable:

  - PTR_TO_BTF_ID | PTR_UNTRUSTED | MEM_ALLOC | NON_OWN_REF
  - PTR_TO_BTF_ID | PTR_UNTRUSTED | MEM_RCU

Rather than adding the two, just drop the list and state the property itself in the default case of the switch. This is a superset of what the list matched, the untrusted PTR_TO_MEM does not have to carry MEM_RDONLY for it anymore, and it stays in sync with the verifier side which uses the same match in save_aux_ptr_type() and reg_type_mismatch_ok().

Assert that a fault prone type which does not get the rewrite for whatever reason is rejected at load time rather than left to fault at runtime to catch any future cases.

Quoted 6 lines
Fixes 1b12171533a9 ("bpf: Mark direct ld of stashed bpf_{rb,list}_node as non-owning ref")
Fixes 6fcd486b3a0a ("bpf: Refactor RCU enforcement in the verifier.")

---
 v1 -> v2:
   - new patch, don't match on full types (Eduard)

author maintainer
#10 lore raw

PATCH v2 5/6 selftests/bpf: Add tests for pointer type merge at a shared load

In reply to Daniel Borkmann

Cover the ways in which the type recorded for a shared load used to lose the BPF_PROBE_MEM rewrite which would then trigger a NULL deref if not handled properly.

# LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t mem_rdonly_untrusted [...]

  #238/1   mem_rdonly_untrusted/btf_id_to_ptr_mem:OK
  #238/2   mem_rdonly_untrusted/ldx_is_ok_bad_addr:OK
  #238/3   mem_rdonly_untrusted/ldx_is_ok_good_addr:OK
  #238/4   mem_rdonly_untrusted/offset_not_tracked:OK
  #238/5   mem_rdonly_untrusted/stx_not_ok:OK
  #238/6   mem_rdonly_untrusted/atomic_not_ok:OK
  #238/7   mem_rdonly_untrusted/atomic_rmw_not_ok:OK
  #238/8   mem_rdonly_untrusted/kfunc_param_not_ok:OK
  #238/9   mem_rdonly_untrusted/mixed_mem_type:OK
  #238/10  mem_rdonly_untrusted/mixed_mem_untrusted_btf_id_type:OK
  #238/11  mem_rdonly_untrusted/mixed_mem_btf_id_type:OK
  #238/12  mem_rdonly_untrusted/mixed_rdonly_mem_btf_id_type:OK
  #238/13  mem_rdonly_untrusted/mixed_mem_mem_type:OK
  #238/14  mem_rdonly_untrusted/mixed_map_value_mem_type:OK
  #238/15  mem_rdonly_untrusted/mixed_stack_mem_type:OK
  #238/16  mem_rdonly_untrusted/diff_size_access:OK
  #238/17  mem_rdonly_untrusted/misaligned_access:OK
  #238/18  mem_rdonly_untrusted/null_check:OK
  #238/19  mem_rdonly_untrusted/ldx_is_ok_commuted_addr:OK
  #238/20  mem_rdonly_untrusted/helper_param_not_ok:OK
  #238     mem_rdonly_untrusted:OK

Summary: 1/20 PASSED, 0 SKIPPED, 0/0 FAILED

---
 .../bpf/progs/mem_rdonly_untrusted.c          | 254 ++++++++++++++++++
 1 file changed, 254 insertions(+)
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c +254 -0
index b91271d4caa4..ac8bba3e7d02 100644
@@ -3,6 +3,7 @@
 #include <vmlinux.h>
 #include <bpf/bpf_core_read.h>
 #include "bpf_misc.h"
+#include "bpf_kfuncs.h"
 #include "../test_kmods/bpf_testmod_kfunc.h"
 
 SEC("tp_btf/sys_enter")
@@ -164,6 +165,259 @@ int mixed_mem_type(void *ctx)
 	return *p;
 }
 
+struct {
+	__uint(type, BPF_MAP_TYPE_RINGBUF);
+	__uint(max_entries, 4096);
+} ringbuf SEC(".maps");
+
+int zero;
+
+static __noinline u64 *get_mem_or_untrusted_addr(u64 *mem)
+{
+	/*
+	 * Try to avoid compiler hoisting load to if branches by using
+	 * __noinline func.
+	 */
+	if (zero)
+		return mem;
+	else
+		return bpf_rdonly_cast(0, bpf_core_type_id_kernel(struct sock));
+}
+
+SEC("socket")
+__success
+__log_level(2)
+__msg("= *(u64 *)(r{{[0-9]}} +0){{.*}}=untrusted_ptr_sock")
+__msg("= *(u64 *)(r{{[0-9]}} +0){{.*}}=ringbuf_mem")
+__retval(0)
+int mixed_mem_untrusted_btf_id_type(void *ctx)
+{
+	u64 *p, v;
+
+	p = bpf_ringbuf_reserve(&ringbuf, sizeof(*p), 0);
+	if (!p)
+		return 1;
+	*p = 42;
+	/*
+	 * The load below is reached with PTR_TO_MEM | MEM_RINGBUF on one
+	 * path and with PTR_TO_BTF_ID | PTR_UNTRUSTED on the other. The
+	 * merged type has to keep the BPF_PROBE_MEM rewrite, otherwise
+	 * the NULL deref taken at runtime panics the kernel instead of
+	 * returning 0.
+	 */
+	v = *get_mem_or_untrusted_addr(p);
+	bpf_ringbuf_discard(p, 0);
+	return v;
+}
+
+static __noinline u32 *get_mem_or_btf_id_addr(u32 *mem)
+{
+	struct task_struct *task;
+
+	/*
+	 * Try to avoid compiler hoisting load to if branches by using
+	 * __noinline func.
+	 */
+	if (zero)
+		return mem;
+
+	task = bpf_get_current_task_btf();
+	/*
+	 * A plain BTF pointer walk yields a bare PTR_TO_BTF_ID, and
+	 * task->nameidata is NULL unless the task currently is in the
+	 * middle of a path lookup.
+	 */
+	return (u32 *)&task->nameidata->flags;
+}
+
+SEC("socket")
+__success
+__log_level(2)
+__msg("= *(u32 *)(r{{[0-9]}} +0){{.*}}=ptr_nameidata")
+__msg("= *(u32 *)(r{{[0-9]}} +0){{.*}}=ringbuf_mem")
+__retval(0)
+int mixed_mem_btf_id_type(void *ctx)
+{
+	u32 *p, v;
+
+	p = bpf_ringbuf_reserve(&ringbuf, sizeof(*p), 0);
+	if (!p)
+		return 1;
+	*p = 42;
+	/*
+	 * Same as above, except that the other path yields a bare
+	 * PTR_TO_BTF_ID. Merging it with PTR_TO_MEM used to drop the
+	 * BPF_PROBE_MEM rewrite the bare PTR_TO_BTF_ID would have
+	 * gotten on its own.
+	 */
+	v = *get_mem_or_btf_id_addr(p);
+	bpf_ringbuf_discard(p, 0);
+	return v;
+}
+
+char dynptr_data[8];
+
+static __noinline u32 *get_rdonly_mem_or_btf_id_addr(u32 *mem)
+{
+	struct task_struct *task;
+
+	/*
+	 * Try to avoid compiler hoisting load to if branches by using
+	 * __noinline func.
+	 */
+	if (zero)
+		return mem;
+
+	task = bpf_get_current_task_btf();
+	return (u32 *)&task->nameidata->flags;
+}
+
+SEC("socket")
+__success
+__log_level(2)
+__msg("r8 = *(u32 *)(r7 +0){{.*}}R7=ptr_nameidata")
+__msg("r8 = *(u32 *)(r7 +0){{.*}}R7=rdonly_mem")
+__retval(0)
+int mixed_rdonly_mem_btf_id_type(void *ctx)
+{
+	struct bpf_dynptr dptr;
+	char buf[sizeof(u32)];
+	u32 *p;
+	u64 v;
+
+	if (bpf_dynptr_from_mem(dynptr_data, sizeof(dynptr_data), 0, &dptr))
+		return 1;
+	p = bpf_dynptr_slice(&dptr, 0, buf, sizeof(buf));
+	if (!p)
+		return 1;
+	/*
+	 * Same as above, except that the PTR_TO_MEM side already carries
+	 * MEM_RDONLY. Merging it with a bare PTR_TO_BTF_ID used to yield
+	 * PTR_TO_MEM | MEM_RDONLY, which is not rewritten either since
+	 * only its PTR_UNTRUSTED variant is.
+	 */
+	p = get_rdonly_mem_or_btf_id_addr(p);
+	/* asm block to have reliable match target for __msg. */
+	asm volatile (
+	"r7 = %[p];"
+	"r8 = *(u32 *)(r7 + 0);"
+	"%[v] = r8;"
+	: [v]"=r"(v)
+	: [p]"r"(p)
+	: "r7", "r8");
+	return v;
+}
+
+static __noinline u64 *get_mem_or_rdonly_untrusted_mem_addr(u64 *mem)
+{
+	u64 *p = bpf_rdonly_cast(0, 0);
+
+	/*
+	 * Hoist the cast above the branch so that the PTR_TO_MEM |
+	 * MEM_RINGBUF path is verified first and thus gets its type
+	 * recorded first.
+	 */
+	if (zero == 0)
+		return p;
+	return mem;
+}
+
+SEC("socket")
+__success
+__log_level(2)
+__msg("= *(u64 *)(r{{[0-9]}} +0){{.*}}=ringbuf_mem")
+__msg("= *(u64 *)(r{{[0-9]}} +0){{.*}}=rdonly_untrusted_mem")
+__retval(0)
+int mixed_mem_mem_type(void *ctx)
+{
+	u64 *p, v;
+
+	p = bpf_ringbuf_reserve(&ringbuf, sizeof(*p), 0);
+	if (!p)
+		return 1;
+	*p = 42;
+	/*
+	 * Both paths are PTR_TO_MEM based, so they used to not trip the
+	 * type mismatch check and skipped the merge altogether, leaving
+	 * the insn with the PTR_TO_MEM | MEM_RINGBUF recorded first and
+	 * hence without the BPF_PROBE_MEM rewrite the other path needs.
+	 */
+	v = *get_mem_or_rdonly_untrusted_mem_addr(p);
+	bpf_ringbuf_discard(p, 0);
+	return v;
+}
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, u32);
+	__type(value, u64);
+} array SEC(".maps");
+
+static __noinline u64 *get_map_value_or_rdonly_untrusted_mem_addr(u64 *mem)
+{
+	u64 *p = bpf_rdonly_cast(0, 0);
+
+	/*
+	 * Hoist the cast above the branch so that the PTR_TO_MAP_VALUE
+	 * path is verified first and thus gets its type recorded first.
+	 */
+	if (zero == 0)
+		return p;
+	return mem;
+}
+
+SEC("socket")
+__failure
+__msg("same insn cannot be used with different pointers")
+int mixed_map_value_mem_type(void *ctx)
+{
+	u64 *p, v;
+	u32 key = 0;
+
+	p = bpf_map_lookup_elem(&array, &key);
+	if (!p)
+		return 1;
+	/*
+	 * PTR_TO_MAP_VALUE is neither PTR_TO_MEM nor PTR_TO_BTF_ID based,
+	 * so it cannot be merged into a type which keeps the BPF_PROBE_MEM
+	 * rewrite the PTR_TO_MEM | MEM_RDONLY | PTR_UNTRUSTED of the other
+	 * path needs. Both bases were mismatch ok, hence the load used to be
+	 * accepted with the PTR_TO_MAP_VALUE recorded and the NULL deref on
+	 * the second path panicked the kernel.
+	 */
+	v = *get_map_value_or_rdonly_untrusted_mem_addr(p);
+	return v;
+}
+
+SEC("socket")
+__failure
+__msg("same insn cannot be used with different pointers")
+int mixed_stack_mem_type(void *ctx)
+{
+	u64 *p = bpf_rdonly_cast(0, 0);
+	u64 s = 42, v;
+
+	/*
+	 * Same as above, but for a PTR_TO_STACK on the other path. A
+	 * subprog cannot return one, so the branch and the load have to
+	 * be kept in the same frame, and the asm block is there to have
+	 * the compiler not duplicate the latter.
+	 */
+	asm volatile (
+	"r7 = %[p];"
+	"if %[zero] == 0 goto +1;"
+	"r7 = %[s];"
+	"r8 = *(u64 *)(r7 + 0);"
+	"%[v] = r8;"
+	: [v]"=r"(v)
+	: [p]"r"(p),
+	  [s]"r"(&s),
+	  [zero]"r"(zero)
+	: "r7", "r8");
+	return v;
+}
+
 __attribute__((__aligned__(8)))
 u8 global[] = {
 	0x11, 0x22, 0x33, 0x44,
Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH bpf-next v2 5/6] selftests/bpf: Add tests for pointer type merge at a shared load

A subsystem pattern flags this as potentially concerning: these six tests cover only two of the four commits in the series. The three merge shapes listed in dea41ba6e67d's changelog map 1:1 onto the…
Quoted 4 lines
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c +0 -0
index b91271d4caa4..ac8bba3e7d02 100644

[ ... ]

Quoted 7 lines
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c +7 -0
+SEC("socket")
+__success
+__log_level(2)
+__msg("= *(u64 *)(r{{[0-9]}} +0){{.*}}=untrusted_ptr_sock")
+__msg("= *(u64 *)(r{{[0-9]}} +0){{.*}}=ringbuf_mem")
+__retval(0)
+int mixed_mem_untrusted_btf_id_type(void *ctx)

[ ... ]

Quoted 7 lines
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c +7 -0
+SEC("socket")
+__success
+__log_level(2)
+__msg("= *(u32 *)(r{{[0-9]}} +0){{.*}}=ptr_nameidata")
+__msg("= *(u32 *)(r{{[0-9]}} +0){{.*}}=ringbuf_mem")
+__retval(0)
+int mixed_mem_btf_id_type(void *ctx)

[ ... ]

Quoted 7 lines
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c +7 -0
+SEC("socket")
+__success
+__log_level(2)
+__msg("r8 = *(u32 *)(r7 +0){{.*}}R7=ptr_nameidata")
+__msg("r8 = *(u32 *)(r7 +0){{.*}}R7=rdonly_mem")
+__retval(0)
+int mixed_rdonly_mem_btf_id_type(void *ctx)

[ ... ]

Quoted 7 lines
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c +7 -0
+SEC("socket")
+__success
+__log_level(2)
+__msg("= *(u64 *)(r{{[0-9]}} +0){{.*}}=ringbuf_mem")
+__msg("= *(u64 *)(r{{[0-9]}} +0){{.*}}=rdonly_untrusted_mem")
+__retval(0)
+int mixed_mem_mem_type(void *ctx)

[ ... ]

Quoted 4 lines
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c +4 -0
+SEC("socket")
+__failure
+__msg("same insn cannot be used with different pointers")
+int mixed_map_value_mem_type(void *ctx)

[ ... ]

tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c int mixed_mem_type(void *ctx) · line 396

Quoted 4 lines
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c +4 -0
+SEC("socket")
+__failure
+__msg("same insn cannot be used with different pointers")
+int mixed_stack_mem_type(void *ctx)

[ ... ]

A subsystem pattern flags this as potentially concerning: these six tests cover only two of the four commits in the series. The three merge shapes listed in dea41ba6e67d's changelog map 1:1 onto the mixed_mem_untrusted_btf_id_type, mixed_mem_btf_id_type, and mixed_rdonly_mem_btf_id_type tests, and the mixed_mem_mem_type, mixed_map_value_mem_type, and mixed_stack_mem_type tests cover the new PTR_TO_MEM mismatch rule from fbffa57329b8.

However, the other two commits appear untested:

bcfd0d6a160f changed check_ptr_to_btf_access() from checking type_flag(reg->type) & PTR_UNTRUSTED to calling bpf_may_fault_on_deref(), so a BPF_STX through a bare PTR_TO_BTF_ID is now rejected with "only read is supported". The changelog documents a reachable bpf_qdisc panic (next = sch->next_sched; next->limit = 1000;), but no __failure / __msg("only read is supported") case is added here or in progs/bpf_qdisc_*.c. Only verifier_global_ptr_args.c and verifier_netfilter_ctx.c assert that message, and both do so for PTR_UNTRUSTED pointers that the old check already caught, so the newly-closed hole is untested.

bef47cf76444 states that PTR_TO_BTF_ID|PTR_UNTRUSTED|MEM_ALLOC|NON_OWN_REF and PTR_TO_BTF_ID|PTR_UNTRUSTED|MEM_RCU were reachable and left as plain loads (two Fixes: tags). The added tests only produce ringbuf/rdonly/bare-BTF-ID types, all of which the old exact-match list in bpf_convert_ctx_accesses() already handled or which the merge path covers.

Would it make sense to add tests for the bcfd0d6a160f store-rejection path and the bef47cf76444 MEM_ALLOC|NON_OWN_REF and MEM_RCU cases here?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/31845722500

author maintainer
#12 lore raw

Re: [PATCH bpf-next v2 5/6] selftests/bpf: Add tests for pointer type merge at a shared load

On 8/15/26 12:56 AM, [email protected] wrote:
[...]

Quoted 12 lines

However, the other two commits appear untested:

bcfd0d6a160f changed check_ptr_to_btf_access() from checking type_flag(reg->type) & PTR_UNTRUSTED to calling bpf_may_fault_on_deref(), so a BPF_STX through a bare PTR_TO_BTF_ID is now rejected with "only read is supported". The changelog documents a reachable bpf_qdisc panic (next = sch->next_sched; next->limit = 1000;), but no __failure / __msg("only read is supported") case is added here or in progs/bpf_qdisc_*.c. Only verifier_global_ptr_args.c and verifier_netfilter_ctx.c assert that message, and both do so for PTR_UNTRUSTED pointers that the old check already caught, so the newly-closed hole is untested.

I can either follow-up together with the hid-bpf one or do a v3 and then add also a qdisc write test if desired.

Quoted 6 lines

bef47cf76444 states that PTR_TO_BTF_ID|PTR_UNTRUSTED|MEM_ALLOC|NON_OWN_REF and PTR_TO_BTF_ID|PTR_UNTRUSTED|MEM_RCU were reachable and left as plain loads (two Fixes: tags). The added tests only produce ringbuf/rdonly/bare-BTF-ID types, all of which the old exact-match list in bpf_convert_ctx_accesses() already handled or which the merge path covers.

Covered by the selftest in patch 6/6 (PTR_TO_BTF_ID | MEM_ALLOC | PTR_UNTRUSTED | NON_OWN_REF and PTR_TO_BTF_ID | PTR_UNTRUSTED | MEM_RCU).

Quoted 2 lines

Would it make sense to add tests for the bcfd0d6a160f store-rejection path and the bef47cf76444 MEM_ALLOC|NON_OWN_REF and MEM_RCU cases here?

maintainer Acked-by on v2 5/6
#13 lore raw

Re: [PATCH bpf-next v2 5/6] selftests/bpf: Add tests for pointer type merge at a shared load

In reply to Daniel Borkmann
Quoted 29 lines · Daniel Borkmann

Cover the ways in which the type recorded for a shared load used to lose the BPF_PROBE_MEM rewrite which would then trigger a NULL deref if not handled properly.

  # LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t mem_rdonly_untrusted [...]
  #238/1   mem_rdonly_untrusted/btf_id_to_ptr_mem:OK
  #238/2   mem_rdonly_untrusted/ldx_is_ok_bad_addr:OK
  #238/3   mem_rdonly_untrusted/ldx_is_ok_good_addr:OK
  #238/4   mem_rdonly_untrusted/offset_not_tracked:OK
  #238/5   mem_rdonly_untrusted/stx_not_ok:OK
  #238/6   mem_rdonly_untrusted/atomic_not_ok:OK
  #238/7   mem_rdonly_untrusted/atomic_rmw_not_ok:OK
  #238/8   mem_rdonly_untrusted/kfunc_param_not_ok:OK
  #238/9   mem_rdonly_untrusted/mixed_mem_type:OK
  #238/10  mem_rdonly_untrusted/mixed_mem_untrusted_btf_id_type:OK
  #238/11  mem_rdonly_untrusted/mixed_mem_btf_id_type:OK
  #238/12  mem_rdonly_untrusted/mixed_rdonly_mem_btf_id_type:OK
  #238/13  mem_rdonly_untrusted/mixed_mem_mem_type:OK
  #238/14  mem_rdonly_untrusted/mixed_map_value_mem_type:OK
  #238/15  mem_rdonly_untrusted/mixed_stack_mem_type:OK
  #238/16  mem_rdonly_untrusted/diff_size_access:OK
  #238/17  mem_rdonly_untrusted/misaligned_access:OK
  #238/18  mem_rdonly_untrusted/null_check:OK
  #238/19  mem_rdonly_untrusted/ldx_is_ok_commuted_addr:OK
  #238/20  mem_rdonly_untrusted/helper_param_not_ok:OK
  #238     mem_rdonly_untrusted:OK
  Summary: 1/20 PASSED, 0 SKIPPED, 0/0 FAILED

Quoted 2 lines

---

...

maintainer
#14 lore raw

Re: [PATCH bpf-next v2 5/6] selftests/bpf: Add tests for pointer type merge at a shared load

In reply to Daniel Borkmann
Quoted 29 lines · Daniel Borkmann

Cover the ways in which the type recorded for a shared load used to lose the BPF_PROBE_MEM rewrite which would then trigger a NULL deref if not handled properly.

  # LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t mem_rdonly_untrusted [...]
  #238/1   mem_rdonly_untrusted/btf_id_to_ptr_mem:OK
  #238/2   mem_rdonly_untrusted/ldx_is_ok_bad_addr:OK
  #238/3   mem_rdonly_untrusted/ldx_is_ok_good_addr:OK
  #238/4   mem_rdonly_untrusted/offset_not_tracked:OK
  #238/5   mem_rdonly_untrusted/stx_not_ok:OK
  #238/6   mem_rdonly_untrusted/atomic_not_ok:OK
  #238/7   mem_rdonly_untrusted/atomic_rmw_not_ok:OK
  #238/8   mem_rdonly_untrusted/kfunc_param_not_ok:OK
  #238/9   mem_rdonly_untrusted/mixed_mem_type:OK
  #238/10  mem_rdonly_untrusted/mixed_mem_untrusted_btf_id_type:OK
  #238/11  mem_rdonly_untrusted/mixed_mem_btf_id_type:OK
  #238/12  mem_rdonly_untrusted/mixed_rdonly_mem_btf_id_type:OK
  #238/13  mem_rdonly_untrusted/mixed_mem_mem_type:OK
  #238/14  mem_rdonly_untrusted/mixed_map_value_mem_type:OK
  #238/15  mem_rdonly_untrusted/mixed_stack_mem_type:OK
  #238/16  mem_rdonly_untrusted/diff_size_access:OK
  #238/17  mem_rdonly_untrusted/misaligned_access:OK
  #238/18  mem_rdonly_untrusted/null_check:OK
  #238/19  mem_rdonly_untrusted/ldx_is_ok_commuted_addr:OK
  #238/20  mem_rdonly_untrusted/helper_param_not_ok:OK
  #238     mem_rdonly_untrusted:OK
  Summary: 1/20 PASSED, 0 SKIPPED, 0/0 FAILED

Quoted 2 lines

---

Hi Daniel,

GCC-BPF fails for the series [1]: because of the difference in codegen some __msg patterns do not match. How would you like to handle this, land first 4 patches and do the tests as a follow-up or as a v3?

[1] https://github.com/kernel-patches/bpf/actions/runs/31970050902/job/95222785689

...

author maintainer
#15 lore raw

Re: [PATCH bpf-next v2 5/6] selftests/bpf: Add tests for pointer type merge at a shared load

Quoted 31 lines · Eduard Zingerman
Quoted 30 lines · Daniel Borkmann

Cover the ways in which the type recorded for a shared load used to lose the BPF_PROBE_MEM rewrite which would then trigger a NULL deref if not handled properly.

  # LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t mem_rdonly_untrusted [...]
  #238/1   mem_rdonly_untrusted/btf_id_to_ptr_mem:OK
  #238/2   mem_rdonly_untrusted/ldx_is_ok_bad_addr:OK
  #238/3   mem_rdonly_untrusted/ldx_is_ok_good_addr:OK
  #238/4   mem_rdonly_untrusted/offset_not_tracked:OK
  #238/5   mem_rdonly_untrusted/stx_not_ok:OK
  #238/6   mem_rdonly_untrusted/atomic_not_ok:OK
  #238/7   mem_rdonly_untrusted/atomic_rmw_not_ok:OK
  #238/8   mem_rdonly_untrusted/kfunc_param_not_ok:OK
  #238/9   mem_rdonly_untrusted/mixed_mem_type:OK
  #238/10  mem_rdonly_untrusted/mixed_mem_untrusted_btf_id_type:OK
  #238/11  mem_rdonly_untrusted/mixed_mem_btf_id_type:OK
  #238/12  mem_rdonly_untrusted/mixed_rdonly_mem_btf_id_type:OK
  #238/13  mem_rdonly_untrusted/mixed_mem_mem_type:OK
  #238/14  mem_rdonly_untrusted/mixed_map_value_mem_type:OK
  #238/15  mem_rdonly_untrusted/mixed_stack_mem_type:OK
  #238/16  mem_rdonly_untrusted/diff_size_access:OK
  #238/17  mem_rdonly_untrusted/misaligned_access:OK
  #238/18  mem_rdonly_untrusted/null_check:OK
  #238/19  mem_rdonly_untrusted/ldx_is_ok_commuted_addr:OK
  #238/20  mem_rdonly_untrusted/helper_param_not_ok:OK
  #238     mem_rdonly_untrusted:OK
  Summary: 1/20 PASSED, 0 SKIPPED, 0/0 FAILED

[...]

Quoted 4 lines

GCC-BPF fails for the series [1]: because of the difference in codegen some __msg patterns do not match. How would you like to handle this, land first 4 patches and do the tests as a follow-up or as a v3?

Yeap, I'll push the 4 fixes and send the selftests as follow-up and add one as well for the bpf qdisc one.

Thanks for the review!
Daniel

Quoted 4 lines

[1] https://github.com/kernel-patches/bpf/actions/runs/31970050902/job/95222785689

...

author maintainer
#16 lore raw

PATCH v2 6/6 selftests/bpf: Add tests for fault prone loads out of RCU pointers

In reply to Daniel Borkmann

Cover the two loads which used to lose the BPF_PROBE_MEM rewrite, both reached from an RCU read-side critical section.

# LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t rcu_read_lock [...]

  #332/1   rcu_read_lock/success:OK
  #332/2   rcu_read_lock/rcuptr_acquire:OK
  #332/3   rcu_read_lock/negative_tests_inproper_region:OK
  #332/4   rcu_read_lock/negative_tests_rcuptr_misuse:OK
  #332     rcu_read_lock:OK

Summary: 1/4 PASSED, 0 SKIPPED, 0/0 FAILED

---
v1 -> v2:
- new patch

 .../selftests/bpf/prog_tests/rcu_read_lock.c  |  2 +
 .../selftests/bpf/progs/rcu_read_lock.c       | 76 +++++++++++++++++++
 2 files changed, 78 insertions(+)
tools/testing/selftests/bpf/prog_tests/rcu_read_lock.c +2 -0
index 246eb259c08a..6a07b2b418d1 100644
@@ -34,6 +34,8 @@ static void test_success(void)
 	bpf_program__set_autoload(skel->progs.rcu_read_lock_global_subprog, true);
 	bpf_program__set_autoload(skel->progs.rcu_read_lock_subprog_lock, true);
 	bpf_program__set_autoload(skel->progs.rcu_read_lock_subprog_unlock, true);
+	bpf_program__set_autoload(skel->progs.non_own_ref_untrusted_ld, true);
+	bpf_program__set_autoload(skel->progs.rcu_untrusted_union_ld, true);
 	err = rcu_read_lock__load(skel);
 	if (!ASSERT_OK(err, "skel_load"))
 		goto out;
tools/testing/selftests/bpf/progs/rcu_read_lock.c +76 -0

79 lines not on this page · raw message

Signature · 1 line
2.43.0
#17 lore raw

Re: [PATCH bpf-next v2 6/6] selftests/bpf: Add tests for fault prone loads out of RCU pointers

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The newly added BPF programs do not execute the vulnerable instructions at runtime and lack…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The newly added BPF programs do not execute the vulnerable instructions at runtime and lack explicit verifier log assertions, failing to provide robust regression coverage. --

--- Patch [6]: [PATCH bpf-next v2 6/6] selftests/bpf: Add tests for fault prone loads out of RCU pointers --- commit 6d523ae9972a5903a07a65fd29336ef6414702f5
Author: Daniel Borkmann <[email protected]>

selftests/bpf: Add tests for fault prone loads out of RCU pointers

This commit adds new BPF tests for ensuring the verifier correctly applies the BPF_PROBE_MEM rewrite to fault-prone loads reached from an RCU read-side critical section. Two new subprograms are added and autoloaded in the rcu_read_lock test.

tools/testing/selftests/bpf/prog_tests/rcu_read_lock.c static void test_success(void) · line 39

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/rcu_read_lock.c +2 -0
@@ -34,6 +34,8 @@ static void test_success(void)
 	bpf_program__set_autoload(skel->progs.rcu_read_lock_global_subprog, true);
 	bpf_program__set_autoload(skel->progs.rcu_read_lock_subprog_lock, true);
 	bpf_program__set_autoload(skel->progs.rcu_read_lock_subprog_unlock, true);
+	bpf_program__set_autoload(skel->progs.non_own_ref_untrusted_ld, true);
+	bpf_program__set_autoload(skel->progs.rcu_untrusted_union_ld, true);
 	err = rcu_read_lock__load(skel);

[Severity: Medium]
Is rcu_untrusted_union_ld reliably triggered by the test harness?

Looking at test_success(), the harness executes syscall(SYS_getpgid) to trigger the fentry program, but there doesn't seem to be a trigger for the tp_btf/tcp_probe program.

Also, would it be safer to use test_loader for these tests so we can statically assert the verifier log output contains the BPF_PROBE_MEM rewrite, rather than relying only on runtime execution?

[ ... ]

Quoted 3 lines
tools/testing/selftests/bpf/progs/rcu_read_lock.c +0 -0

[ ... ]

Quoted 6 lines
tools/testing/selftests/bpf/progs/rcu_read_lock.c +6 -0
+SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
+int non_own_ref_untrusted_ld(void *ctx)
+{
+	struct rcu_node_stash *stash;
+	struct rcu_node_data *node;
+	int key = 0;

tools/testing/selftests/bpf/progs/rcu_read_lock.c int rcu_read_lock_sleepable_global_subprog_indirect(void *ctx) · line 592

Quoted 10 lines
tools/testing/selftests/bpf/progs/rcu_read_lock.c +10 -0
+
+	stash = bpf_map_lookup_elem(&node_stash, &key);
+	if (!stash)
+		return 0;
+	bpf_rcu_read_lock();
+	node = stash->node;
+	if (!node) {
+		bpf_rcu_read_unlock();
+		return 0;
+	}

[Severity: Medium]
Does this program end up returning early here on every run?

Since the node_stash map is never populated from the userspace test harness, stash->node appears to always be NULL. If it returns early, the test never actually reaches the fault-prone load below to trigger a kernel panic if the rewrite were to fail.

Quoted 9 lines
tools/testing/selftests/bpf/progs/rcu_read_lock.c +9 -0
+	bpf_rcu_read_unlock();
+	/*
+	 * The unlock leaves node as PTR_TO_BTF_ID | MEM_ALLOC | PTR_UNTRUSTED
+	 * | NON_OWN_REF, and the load below has to get the BPF_PROBE_MEM
+	 * rewrite for it, otherwise a bad address panics the kernel.
+	 */
+	non_own_ref_key = node->key;
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author maintainer
#18 lore raw

Re: [PATCH bpf-next v2 6/6] selftests/bpf: Add tests for fault prone loads out of RCU pointers

Quoted 2 lines · On 8/15/26 12:03 AM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The newly added BPF programs do not execute the vulnerable instructions at runtime and lack explicit verifier log assertions, failing to provide robust regression coverage.

Nope, its basically load coverage (ASSERT_OK(err, "skel_load") in test_success()), meaning we make sure the assertion on patch 4/6 in bpf_convert_ctx_accesses() doesn't trigger if this is forgotten to get rewritten as probed pointer:

if (verifier_bug_if(bpf_may_fault_on_deref(ptr_type), env,
"access to a fault prone pointer is not rewritten as a probed one"))
return -EFAULT;

maintainer Acked-by on v2 6/6
#19 lore raw

Re: [PATCH bpf-next v2 6/6] selftests/bpf: Add tests for fault prone loads out of RCU pointers

In reply to Daniel Borkmann
Quoted 12 lines · Daniel Borkmann

Cover the two loads which used to lose the BPF_PROBE_MEM rewrite, both reached from an RCU read-side critical section.

  # LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t rcu_read_lock [...]
  #332/1   rcu_read_lock/success:OK
  #332/2   rcu_read_lock/rcuptr_acquire:OK
  #332/3   rcu_read_lock/negative_tests_inproper_region:OK
  #332/4   rcu_read_lock/negative_tests_rcuptr_misuse:OK
  #332     rcu_read_lock:OK
  Summary: 1/4 PASSED, 0 SKIPPED, 0/0 FAILED

Quoted 5 lines

---
 v1 -> v2:
   - new patch

#20 lore raw
In reply to Daniel Borkmann
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] The verifier fails to emit BPF_PROBE_MEM instructions when a safe pointer type (e.g., PTR_TO_STACK or PTR_TO_MAP_VALUE) and an untrusted memory pointer type (PTR_TO_MEM | PTR_UNTRUSTED) reach the same load instruction via different branches. This completely bypasses the newly introduced merge_ptr_types logic. --

--- Patch [1]: [PATCH bpf-next v2 1/6] bpf: Keep fault protection when merging pointer types --- commit f6edd0237d3f5f1dd3f7b1e12e20100d5ce150cd
Author: Daniel Borkmann <[email protected]>

bpf: Keep fault protection when merging pointer types

This commit fixes an issue where merging different pointer types in save_aux_ptr_type() drops flags like PTR_UNTRUSTED. It introduces merge_ptr_types() to properly normalize and combine pointer types so that the BPF_PROBE_MEM rewrite remains intact.

Quoted 4 lines
kernel/bpf/verifier.c +0 -0
index 6ac1afced20bf..ad6ccd5cf51db 100644

[ ... ]

Quoted 13 lines
kernel/bpf/verifier.c +4 -4
@@ -17092,20 +17097,12 @@ static int save_aux_ptr_type(struct bpf_verifier_env *env, enum bpf_reg_type typ
 		    is_ptr_to_mem_or_btf_id(*prev_type)) {
 			/*
 			 * Have to support a use case when one path through
-			 * the program yields TRUSTED pointer while another
-			 * is UNTRUSTED. Fallback to UNTRUSTED to generate
-			 * BPF_PROBE_MEM/BPF_PROBE_MEMSX.
-			 * Same behavior of MEM_RDONLY flag.
+			 * the program yields a TRUSTED pointer while another
+			 * is UNTRUSTED. Merge them into a type which keeps
+			 * the BPF_PROBE_MEM/BPF_PROBE_MEMSX rewrite when
+			 * either side needs it.
 			 */

kernel/bpf/verifier.c static int save_aux_ptr_type(struct bpf_verifier_env *env, enum bpf_reg_type typ · line 17105

Quoted 10 lines
kernel/bpf/verifier.c +1 -9
-			if (is_ptr_to_mem(type) || is_ptr_to_mem(*prev_type))
-				merged_type = PTR_TO_MEM;
-			else
-				merged_type = PTR_TO_BTF_ID;
-			if ((type & PTR_UNTRUSTED) || (*prev_type & PTR_UNTRUSTED))
-				merged_type |= PTR_UNTRUSTED;
-			if ((type & MEM_RDONLY) || (*prev_type & MEM_RDONLY))
-				merged_type |= MEM_RDONLY;
-			*prev_type = merged_type;
+			*prev_type = merge_ptr_types(type, *prev_type);

[Severity: Critical]
This is a pre-existing issue, but does this bypass conflict resolution entirely if reg_type_mismatch() returns false for both types?

If one branch provides a safe pointer (like PTR_TO_STACK) and another branch provides an untrusted pointer (PTR_TO_MEM | PTR_UNTRUSTED), they evaluate as:

kernel/bpf/verifier.c:reg_type_mismatch() {
	return src != prev && (!reg_type_mismatch_ok(src) ||
			       !reg_type_mismatch_ok(prev));
}

Since neither base type is in the reg_type_mismatch_ok() switch statement, it returns true for both types, causing reg_type_mismatch() to return false.

If the safe pointer reaches save_aux_ptr_type() first, we wouldn't enter the else if (reg_type_mismatch(type, *prev_type)) block at all.

Does this mean merge_ptr_types() is skipped, the untrusted pointer type is silently ignored, and we might fail to emit the required BPF_PROBE_MEM rewrite?

Quoted 3 lines
kernel/bpf/verifier.c +0 -0
 		} else {
 			verbose(env, "same insn cannot be used with different pointers\n");
 			return -EINVAL;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#21 lore raw
Quoted 4 lines · On 8/15/26 12:15 AM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] The verifier fails to emit BPF_PROBE_MEM instructions when a safe pointer type (e.g., PTR_TO_STACK or PTR_TO_MAP_VALUE) and an untrusted memory pointer type (PTR_TO_MEM | PTR_UNTRUSTED) reach the same load instruction via different branches. This completely bypasses the newly introduced merge_ptr_types logic.

(this is already addressed in 2/6 in the series)

maintainer Acked-by on v2 1/6
#22 lore raw
In reply to Daniel Borkmann
Quoted 42 lines · Daniel Borkmann

When the same BPF_LDX instruction is reached through paths that yield different pointer types, save_aux_ptr_type() merges them into a single type which is later used by bpf_convert_ctx_accesses() to decide whether the load has to be rewritten into a BPF_PROBE_MEM one.

Before f2362a57aeff ("bpf: allow void* cast using bpf_rdonly_cast()") the merge only accepted two PTR_TO_BTF_ID pointers and unconditionally fell back to PTR_TO_BTF_ID | PTR_UNTRUSTED, so the merged type was always one that gets the BPF_PROBE_MEM rewrite. However, the mentioned commit widened the merge to also cover a PTR_TO_MEM base and replaced the fallback by a union of the PTR_UNTRUSTED and MEM_RDONLY flags.

A union of flags though cannot express the property the later rewrite is built upon, some examples:

  - PTR_TO_MEM merged with PTR_TO_BTF_ID | PTR_UNTRUSTED gets PTR_TO_MEM | PTR_UNTRUSTED but only the MEM_RDONLY variant is valid
  - PTR_TO_MEM merged with a plain PTR_TO_BTF_ID gets PTR_TO_MEM dropping the rewrite the latter type would have gotten
  - PTR_TO_MEM | MEM_RDONLY merged with a plain PTR_TO_BTF_ID gets PTR_TO_MEM | MEM_RDONLY which is not rewritten either since only its PTR_UNTRUSTED variant is

In all three cases a program can take the unsafe path at runtime with a NULL or otherwise bad pointer and panic the kernel on the faulting load:

  BUG: kernel NULL pointer dereference, address: 0000000000000038
  RIP: 0010:bpf_prog_77531a87032eeaf1_mixed_mem_btf_id_type+0x4b/0x65
  Call Trace:
   <TASK>
   bpf_test_run+0x20b/0x460
   bpf_prog_test_run_skb+0x650/0xbe0
   __sys_bpf+0xb96/0x3140
   __x64_sys_bpf+0x2c/0x40
   do_syscall_64+0xba/0x590
  Kernel panic - not syncing: Fatal exception in interrupt

Note that the last two shapes have to be fixed right here, otherwise the merged type retains nothing which marks the load as fault prone, thus no rule in bpf_convert_ctx_accesses() can recover it. Fix it by normalizing the merged type instead.

Quoted 7 lines

Reuse it in is_load_acq_unsafe() to avoid open coding, and trim the overly verbose comment which is more of an implementation detail of bpf_convert_ctx_accesses() anyway.

Fixes f2362a57aeff ("bpf: allow void* cast using bpf_rdonly_cast()")

---

...