All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Nicholas Carlini <npc@anthropic.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	kkd@meta.com, kernel-team@meta.com
Subject: [PATCH bpf v1 1/8] bpf: Check ancestor frames for rbtree callbacks
Date: Thu,  3 Sep 2026 23:47:47 +0200	[thread overview]
Message-ID: <20260903214758.2727663-2-memxor@gmail.com> (raw)
In-Reply-To: <20260903214758.2727663-1-memxor@gmail.com>

bpf_rbtree_add() invokes its comparator while the caller holds the root
lock. The native insertion code retains raw parent and link pointers across
the callback, so the verifier prohibits unlocking, consuming tree nodes,
or changing RCU state from that callback.

in_rbtree_lock_required_cb() only checks the innermost verifier frame.
Static subprogram calls are permitted while holding a spin lock, and such a
call pushes a frame without in_callback_fn set. Consequently, all callback
restrictions disappear in the nested frame. The subprogram can unlock the
tree, remove and drop the node being compared, then relock. Native insertion
resumes with the stale parent pointer and links freed memory into the tree.

Walk all active frames for the rbtree callback instead. Benign static
subprograms remain permitted, while callback restrictions follow execution
into nested frames.

Fixes: a44b1334aadd ("bpf: Allow calling static subprogs while holding a bpf_spin_lock")
Reported-by: Nicholas Carlini <npc@anthropic.com>
Suggested-by: Nicholas Carlini <npc@anthropic.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 kernel/bpf/verifier.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 32d31fa67036..26139fa09f12 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -10235,9 +10235,10 @@ static void account_current_path(struct bpf_verifier_env *env)
 					frame ? state->frame[frame - 1] : NULL);
 }
 
-/* Are we currently verifying the callback for a rbtree helper that must
- * be called with lock held? If so, no need to complain about unreleased
- * lock
+/*
+ * Are we currently verifying the callback for an rbtree kfunc that must
+ * be called with a lock held, or one of that callback's subprogs? If so,
+ * no need to complain about an unreleased lock.
  */
 static bool in_rbtree_lock_required_cb(struct bpf_verifier_env *env)
 {
@@ -10245,17 +10246,19 @@ static bool in_rbtree_lock_required_cb(struct bpf_verifier_env *env)
 	struct bpf_insn *insn = env->prog->insnsi;
 	struct bpf_func_state *callee;
 	int kfunc_btf_id;
+	u32 frame;
 
-	if (!state->curframe)
-		return false;
-
-	callee = state->frame[state->curframe];
+	for (frame = state->curframe; frame; frame--) {
+		callee = state->frame[frame];
+		if (!callee->in_callback_fn)
+			continue;
 
-	if (!callee->in_callback_fn)
-		return false;
+		kfunc_btf_id = insn[callee->callsite].imm;
+		if (is_rbtree_lock_required_kfunc(kfunc_btf_id))
+			return true;
+	}
 
-	kfunc_btf_id = insn[callee->callsite].imm;
-	return is_rbtree_lock_required_kfunc(kfunc_btf_id);
+	return false;
 }
 
 static bool retval_range_within(struct bpf_retval_range range, const struct bpf_reg_state *reg)
-- 
2.53.0


  reply	other threads:[~2026-09-03 21:48 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-03 21:47 [PATCH bpf v1 0/8] Misc bug fixes - part 2 Kumar Kartikeya Dwivedi
2026-09-03 21:47 ` Kumar Kartikeya Dwivedi [this message]
2026-09-03 22:40   ` [PATCH bpf v1 1/8] bpf: Check ancestor frames for rbtree callbacks Eduard Zingerman
2026-09-03 22:56   ` bot+bpf-ci
2026-09-03 21:47 ` [PATCH bpf v1 2/8] selftests/bpf: Check rbtree callback restrictions in subprogs Kumar Kartikeya Dwivedi
2026-09-03 22:56   ` bot+bpf-ci
2026-09-03 21:47 ` [PATCH bpf v1 3/8] bpf: Mark bpf_btf_find_by_name_kind() as sleepable Kumar Kartikeya Dwivedi
2026-09-03 22:15   ` Eduard Zingerman
2026-09-03 21:47 ` [PATCH bpf v1 4/8] selftests/bpf: Test btf lookup helper sleepability Kumar Kartikeya Dwivedi
2026-09-03 21:47 ` [PATCH bpf v1 5/8] bpf: Mark faultable stack helpers as sleepable Kumar Kartikeya Dwivedi
2026-09-03 22:00   ` Eduard Zingerman
2026-09-03 22:56   ` bot+bpf-ci
2026-09-03 21:47 ` [PATCH bpf v1 6/8] selftests/bpf: Check faultable stack helper contexts Kumar Kartikeya Dwivedi
2026-09-03 22:56   ` bot+bpf-ci
2026-09-03 21:47 ` [PATCH bpf v1 7/8] bpf: Reject legacy packet loads from callbacks Kumar Kartikeya Dwivedi
2026-09-03 21:58   ` Eduard Zingerman
2026-09-04  2:22     ` Alexei Starovoitov
2026-09-03 21:47 ` [PATCH bpf v1 8/8] selftests/bpf: " Kumar Kartikeya Dwivedi
2026-09-04  2:30 ` [PATCH bpf v1 0/8] Misc bug fixes - part 2 patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260903214758.2727663-2-memxor@gmail.com \
    --to=memxor@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=kernel-team@meta.com \
    --cc=kkd@meta.com \
    --cc=npc@anthropic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.