From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Nicholas Carlini <npc@anthropic.com>,
Ning Ding <dingning04@gmail.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 3/8] bpf: Keep refcount_acquire nullable for borrowed RCU kptrs
Date: Fri, 4 Sep 2026 08:36:41 +0200 [thread overview]
Message-ID: <20260904063650.3877826-4-memxor@gmail.com> (raw)
In-Reply-To: <20260904063650.3877826-1-memxor@gmail.com>
From: Ning Ding <dingning04@gmail.com>
bpf_refcount_acquire() is fallible for a borrowed reference because the
object may have reached a zero refcount. The verifier therefore keeps
KF_RET_NULL on the return value unless the argument is an owning reference.
An RCU-protected load of a local kptr is marked MEM_ALLOC, but it only
receives NON_OWN_REF when the pointee contains a graph node. A refcounted
object without a graph node consequently looks like an owning reference
even though the loaded register has no acquired reference state. If the
program drops the last real reference while remaining in the RCU critical
section, refcount_inc_not_zero() returns NULL while the verifier treats the
result as non-NULL.
Only classify the argument as owning when it is backed by a verifier-tracked
reference. This retains the non-NULL return for pointers from bpf_obj_new(),
bpf_kptr_xchg(), or an earlier successful acquisition, while requiring a
NULL check for borrowed RCU kptrs.
Fixes: 1b12171533a9 ("bpf: Mark direct ld of stashed bpf_{rb,list}_node as non-owning ref")
Reported-by: Nicholas Carlini <npc@anthropic.com>
Suggested-by: Nicholas Carlini <npc@anthropic.com>
Signed-off-by: Ning Ding <dingning04@gmail.com>
[ kkd: Rewrote commit log ]
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
kernel/bpf/verifier.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 68353aa61fa1..bc0abf96cc89 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -13178,7 +13178,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me
bpf_diag_reg_type_plain(env, reg->type));
return -EINVAL;
}
- if (!type_is_non_owning_ref(reg->type))
+ if (!type_is_non_owning_ref(reg->type) && reg_is_referenced(env, reg))
meta->arg_owning_ref = true;
rec = reg_btf_record(reg);
--
2.53.0
next prev parent reply other threads:[~2026-09-04 6:36 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-04 6:36 [PATCH bpf v1 0/8] Misc bug fixes - part 3 Kumar Kartikeya Dwivedi
2026-09-04 6:36 ` [PATCH bpf v1 1/8] bpf: Require MEM_PERCPU for percpu kptr stores Kumar Kartikeya Dwivedi
2026-09-04 6:36 ` [PATCH bpf v1 2/8] selftests/bpf: Reject non-percpu values in percpu kptr fields Kumar Kartikeya Dwivedi
2026-09-04 6:47 ` sashiko-bot
2026-09-04 6:36 ` Kumar Kartikeya Dwivedi [this message]
2026-09-04 6:36 ` [PATCH bpf v1 4/8] selftests/bpf: Test borrowed refcount acquisition nullability Kumar Kartikeya Dwivedi
2026-09-04 7:43 ` bot+bpf-ci
2026-09-04 6:36 ` [PATCH bpf v1 5/8] bpf: Clear NON_OWN_REF after RCU protection ends Kumar Kartikeya Dwivedi
2026-09-04 6:36 ` [PATCH bpf v1 6/8] selftests/bpf: Reject graph kptr use after RCU unlock Kumar Kartikeya Dwivedi
2026-09-04 7:43 ` bot+bpf-ci
2026-09-04 6:36 ` [PATCH bpf v1 7/8] bpf: Reject untrusted allocated-object pointers Kumar Kartikeya Dwivedi
2026-09-04 6:36 ` [PATCH bpf v1 8/8] selftests/bpf: Reject refcount acquisition after RCU unlock Kumar Kartikeya Dwivedi
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=20260904063650.3877826-4-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=dingning04@gmail.com \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox