BPF List
 help / color / mirror / Atom feed
* [PATCH] bpf: Require a bpf_spin_lock for bpf_list_head and rb_root
@ 2026-11-26  1:14 Quan Sun
  2026-11-26  1:44 ` bot+bpf-ci
  2026-11-26  2:41 ` Alexei Starovoitov
  0 siblings, 2 replies; 3+ messages in thread
From: Quan Sun @ 2026-11-26  1:14 UTC (permalink / raw)
  To: bpf, ast, daniel; +Cc: andrii, eddyz87, emxor, Quan Sun

Commit 0de2046137f9 ("bpf: Implement verifier support for rqspinlock")
relaxed the btf_parse_fields() check so that a bpf_list_head or
bpf_rb_root is accepted with only a bpf_res_spin_lock in the containing
type:

	if ((btf_record_has_field(rec, BPF_LIST_HEAD) ||
-	     btf_record_has_field(rec, BPF_RB_ROOT)) && rec->spin_lock_off < 0) {
+	     btf_record_has_field(rec, BPF_RB_ROOT)) &&
+		 (rec->spin_lock_off < 0 && rec->res_spin_lock_off < 0)) {

The release path was not updated to match.  bpf_obj_free_fields() still
only knows struct bpf_spin_lock:

	case BPF_RB_ROOT:
		if (WARN_ON_ONCE(rec->spin_lock_off < 0))
			continue;
		bpf_rb_root_free(field, field_ptr, obj + rec->spin_lock_off);

so the first map of this shape that is freed splats:

  struct poc_graph {
  	struct bpf_res_spin_lock lock;
  	struct bpf_rb_root root __contains(poc_node, node);
  };

  WARNING: kernel/bpf/syscall.c:880 at bpf_obj_free_fields+0x321/0x350, CPU#3: kworker/u16:5/67
  Workqueue: events_unbound bpf_map_free_deferred
  RIP: 0010:bpf_obj_free_fields+0x321/0x350
  RDX: ffffffffffffffea
  Call stack:
   array_map_free+0xa4/0x230
   bpf_map_free_deferred+0x122/0x200
   process_one_work+0x53e/0xdd0

RDX is -22, i.e. rec->spin_lock_off == -EINVAL.  The WARN_ON_ONCE() is
the only thing standing between the release path and

	bpf_rb_root_free(field, field_ptr, obj + -22)

which would run __bpf_spin_lock_irqsave() on a pointer before the start
of the object and then treat whatever it finds there as a lock.  The
guard holds today only because btf_parse_fields() stores a negative
errno in spin_lock_off; it is a load-bearing coincidence, not an
invariant the code states anywhere.

Nothing in the tree relies on the relaxed form yet: no type in kernel/
or tools/testing/selftests/bpf/ has both a bpf_res_spin_lock and a
bpf_list_head/bpf_rb_root, and the commit being fixed came without a
test for that combination.  Restore the original requirement and let a
follow-up teach bpf_{list_head,rb_root}_free() about rqspinlock if
res-only guarding is actually wanted.  The BPF_F_LOCK checks in
syscall.c already use btf_record_has_field(rec, BPF_SPIN_LOCK) and are
unaffected.

With this patch the BTF of the map above is rejected, so a record with a
res-only guarded container cannot be created at all:

  libbpf: BTF loading error: -EINVAL

This change was drafted with an AI coding assistant during a BPF
subsystem audit. I reviewed the code, built the kernel and ran the
reproducer before and after, and take responsibility for it.

Fixes: 0de2046137f9 ("bpf: Implement verifier support for rqspinlock")
Signed-off-by: Quan Sun <m202672221@hust.edu.cn>
---

Reproduce: boot bpf-next master (8dfce13340ae, 7.4.0-rc3-g8dfce13340ae,
CONFIG_KASAN=y) in qemu and only create then close an ARRAY map whose
value type has a bpf_res_spin_lock and a bpf_rb_root with a
__contains() tag.  No BPF program and no bpf_rbtree_add() is needed:
bpf_map_free_deferred() hits the WARN on the plain free path.

 kernel/bpf/btf.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index c4386a9a1ad..1008bc4c073 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -4302,8 +4302,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type
 
 	/* bpf_{list_head, rb_node} require bpf_spin_lock */
 	if ((btf_record_has_field(rec, BPF_LIST_HEAD) ||
-	     btf_record_has_field(rec, BPF_RB_ROOT)) &&
-		 (rec->spin_lock_off < 0 && rec->res_spin_lock_off < 0)) {
+	     btf_record_has_field(rec, BPF_RB_ROOT)) && rec->spin_lock_off < 0) {
 		ret = -EINVAL;
 		goto end;
 	}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-26  2:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  1:14 [PATCH] bpf: Require a bpf_spin_lock for bpf_list_head and rb_root Quan Sun
2026-11-26  1:44 ` bot+bpf-ci
2026-11-26  2:41 ` Alexei Starovoitov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox