* [PATCH bpf v3 1/4] bpf: Keep refcount_acquire nullable for borrowed RCU kptrs [not found] <20260803112218.3361213-1-dingning04@gmail.com> @ 2026-08-03 11:22 ` Ning Ding 2026-08-03 12:53 ` bot+bpf-ci 2026-08-03 11:22 ` [PATCH bpf v3 3/4] bpf: Reject untrusted allocated-object pointers Ning Ding 1 sibling, 1 reply; 4+ messages in thread From: Ning Ding @ 2026-08-03 11:22 UTC (permalink / raw) To: bpf Cc: memxor, greg, Ning Ding, stable, Alexei Starovoitov, Daniel Borkmann, John Fastabend, Andrii Nakryiko, Eduard Zingerman, Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Dave Marchevsky, linux-kernel The verifier can mistake an RCU-loaded map kptr for an owned reference and assume bpf_refcount_acquire() cannot return NULL. The kfunc uses refcount_inc_not_zero(), so it can return NULL after the last reference is dropped. Passing that unchecked result to bpf_obj_drop() can crash the kernel. Only treat the argument as owned when the verifier tracks an acquired reference for it. Borrowed pointers remain nullable, while owned pointers keep the existing behavior. Fixes: 1b12171533a9 ("bpf: Mark direct ld of stashed bpf_{rb,list}_node as non-owning ref") Cc: stable@vger.kernel.org Assisted-by: Codex:gpt-5.5 Assisted-by: ChatGPT:GPT-5.6-Thinking Signed-off-by: Ning Ding <dingning04@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 fdc5fbb1f78ca..c47328be2505e 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12419,7 +12419,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ reg_arg_name(env, argno)); 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.43.0 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH bpf v3 1/4] bpf: Keep refcount_acquire nullable for borrowed RCU kptrs 2026-08-03 11:22 ` [PATCH bpf v3 1/4] bpf: Keep refcount_acquire nullable for borrowed RCU kptrs Ning Ding @ 2026-08-03 12:53 ` bot+bpf-ci 2026-08-03 13:45 ` Amery Hung 0 siblings, 1 reply; 4+ messages in thread From: bot+bpf-ci @ 2026-08-03 12:53 UTC (permalink / raw) To: dingning04, bpf Cc: memxor, greg, dingning04, stable, ast, daniel, john.fastabend, andrii, eddyz87, martin.lau, song, yonghong.song, jolsa, emil, davemarchevsky, linux-kernel, ast, andrii, daniel, martin.lau, eddyz87, yonghong.song, clm, ihor.solodrai [-- Attachment #1: Type: text/plain, Size: 1953 bytes --] > bpf: Keep refcount_acquire nullable for borrowed RCU kptrs > > The verifier can mistake an RCU-loaded map kptr for an owned reference and > assume bpf_refcount_acquire() cannot return NULL. The kfunc uses > refcount_inc_not_zero(), so it can return NULL after the last reference is > dropped. Passing that unchecked result to bpf_obj_drop() can crash the > kernel. > > Only treat the argument as owned when the verifier tracks an acquired > reference for it. Borrowed pointers remain nullable, while owned pointers > keep the existing behavior. > > Fixes: 1b12171533a9 ("bpf: Mark direct ld of stashed bpf_{rb,list}_node as non-owning ref") Does this Fixes tag point to the right commit? Looking at the code change, the fix adds && reg_is_referenced(env, reg) to an existing condition that checks !type_is_non_owning_ref(reg->type). The problem being fixed is that the verifier incorrectly assumes pointers that are not non-owning refs must be owning refs, which fails to account for borrowed RCU pointers. The commit 1b12171533a9 added logic to mark certain pointers as non-owning refs, but the bug appears to originate from commit 7793fc3babe9 ("bpf: Make bpf_refcount_acquire fallible for non-owning refs"), which introduced the logic: if (!type_is_non_owning_ref(reg->type)) meta->arg_owning_ref = true; That commit incorrectly assumed pointers that are not non-owning refs must be owning refs, missing the case of borrowed RCU pointers which are neither. The current fix addresses this by verifying the verifier tracks an acquired reference before treating the pointer as owned. Should the Fixes tag be: Fixes: 7793fc3babe9 ("bpf: Make bpf_refcount_acquire fallible for non-owning refs") --- 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/30810949269 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH bpf v3 1/4] bpf: Keep refcount_acquire nullable for borrowed RCU kptrs 2026-08-03 12:53 ` bot+bpf-ci @ 2026-08-03 13:45 ` Amery Hung 0 siblings, 0 replies; 4+ messages in thread From: Amery Hung @ 2026-08-03 13:45 UTC (permalink / raw) To: bot+bpf-ci Cc: dingning04, bpf, memxor, greg, stable, ast, daniel, john.fastabend, andrii, eddyz87, martin.lau, song, yonghong.song, jolsa, emil, davemarchevsky, linux-kernel, martin.lau, clm, ihor.solodrai On Mon, Aug 3, 2026 at 6:14 AM <bot+bpf-ci@kernel.org> wrote: > > > bpf: Keep refcount_acquire nullable for borrowed RCU kptrs > > > > The verifier can mistake an RCU-loaded map kptr for an owned reference and > > assume bpf_refcount_acquire() cannot return NULL. The kfunc uses > > refcount_inc_not_zero(), so it can return NULL after the last reference is > > dropped. Passing that unchecked result to bpf_obj_drop() can crash the > > kernel. > > > > Only treat the argument as owned when the verifier tracks an acquired > > reference for it. Borrowed pointers remain nullable, while owned pointers > > keep the existing behavior. > > > > Fixes: 1b12171533a9 ("bpf: Mark direct ld of stashed bpf_{rb,list}_node as non-owning ref") ^^^ The fix tag is correct. Reviewed-by: Amery Hung <ameryhung@gmail.com> > > Does this Fixes tag point to the right commit? > > Looking at the code change, the fix adds && reg_is_referenced(env, reg) > to an existing condition that checks !type_is_non_owning_ref(reg->type). > The problem being fixed is that the verifier incorrectly assumes pointers > that are not non-owning refs must be owning refs, which fails to account > for borrowed RCU pointers. > > The commit 1b12171533a9 added logic to mark certain pointers as > non-owning refs, but the bug appears to originate from commit > 7793fc3babe9 ("bpf: Make bpf_refcount_acquire fallible for non-owning > refs"), which introduced the logic: > > if (!type_is_non_owning_ref(reg->type)) > meta->arg_owning_ref = true; > > That commit incorrectly assumed pointers that are not non-owning refs > must be owning refs, missing the case of borrowed RCU pointers which are > neither. The current fix addresses this by verifying the verifier tracks > an acquired reference before treating the pointer as owned. > > Should the Fixes tag be: > > Fixes: 7793fc3babe9 ("bpf: Make bpf_refcount_acquire fallible for non-owning refs") > > > --- > 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/30810949269 ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH bpf v3 3/4] bpf: Reject untrusted allocated-object pointers [not found] <20260803112218.3361213-1-dingning04@gmail.com> 2026-08-03 11:22 ` [PATCH bpf v3 1/4] bpf: Keep refcount_acquire nullable for borrowed RCU kptrs Ning Ding @ 2026-08-03 11:22 ` Ning Ding 1 sibling, 0 replies; 4+ messages in thread From: Ning Ding @ 2026-08-03 11:22 UTC (permalink / raw) To: bpf Cc: memxor, greg, Ning Ding, sashiko-bot, stable, Alexei Starovoitov, Daniel Borkmann, John Fastabend, Andrii Nakryiko, Eduard Zingerman, Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Dave Marchevsky, linux-kernel type_is_ptr_alloc_obj() currently treats PTR_UNTRUSTED pointers as valid allocated objects. This allows a pointer that is no longer protected by RCU to be passed to bpf_refcount_acquire(). If the object was freed and its address reused, the verifier may acquire a reference through stale memory. Make type_is_ptr_alloc_obj() reject PTR_UNTRUSTED. Add type_is_untrusted_ptr_alloc_obj() for the read path, where these pointers are still allowed for BPF_PROBE_MEM reads. This keeps checks strict without breaking safe reads. Fixes: 1b12171533a9 ("bpf: Mark direct ld of stashed bpf_{rb,list}_node as non-owning ref") Reported-by: sashiko-bot@kernel.org Link: https://lore.kernel.org/r/20260726021304.97ED91F000E9@smtp.kernel.org Cc: stable@vger.kernel.org Assisted-by: Codex:gpt-5 Signed-off-by: Ning Ding <dingning04@gmail.com> --- include/linux/bpf_verifier.h | 11 ++++++++++- kernel/bpf/verifier.c | 8 +++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 39a851e690ec4..7014d62fe3d1c 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -1307,7 +1307,16 @@ static inline bool bpf_type_has_unsafe_modifiers(u32 type) static inline bool type_is_ptr_alloc_obj(u32 type) { - return base_type(type) == PTR_TO_BTF_ID && type_flag(type) & MEM_ALLOC; + return base_type(type) == PTR_TO_BTF_ID && + type_flag(type) & MEM_ALLOC && + !(type_flag(type) & PTR_UNTRUSTED); +} + +static inline bool type_is_untrusted_ptr_alloc_obj(u32 type) +{ + return base_type(type) == PTR_TO_BTF_ID && + type_flag(type) & MEM_ALLOC && + type_flag(type) & PTR_UNTRUSTED; } static inline bool type_is_non_owning_ref(u32 type) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index c47328be2505e..8b35c63780798 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5809,7 +5809,13 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env, return -EACCES; } - if (type_is_alloc(reg->type) && !type_is_non_owning_ref(reg->type) && + /* + * Skip this referenced-ID sanity check for untrusted allocated objects; + * the access check above already rejects writes through them. + */ + if (type_is_alloc(reg->type) && + !type_is_untrusted_ptr_alloc_obj(reg->type) && + !type_is_non_owning_ref(reg->type) && !(reg->type & MEM_RCU) && !reg_is_referenced(env, reg)) { verifier_bug(env, "allocated object must have a referenced id"); return -EFAULT; -- 2.43.0 ^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-08-03 13:46 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20260803112218.3361213-1-dingning04@gmail.com>
2026-08-03 11:22 ` [PATCH bpf v3 1/4] bpf: Keep refcount_acquire nullable for borrowed RCU kptrs Ning Ding
2026-08-03 12:53 ` bot+bpf-ci
2026-08-03 13:45 ` Amery Hung
2026-08-03 11:22 ` [PATCH bpf v3 3/4] bpf: Reject untrusted allocated-object pointers Ning Ding
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox