From: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>
To: "Ning Ding" <dingning04@gmail.com>, <bpf@vger.kernel.org>
Cc: <greg@kroah.com>, <sashiko-bot@kernel.org>,
"Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Eduard Zingerman" <eddyz87@gmail.com>,
"Martin KaFai Lau" <martin.lau@linux.dev>,
"Song Liu" <song@kernel.org>,
"Yonghong Song" <yonghong.song@linux.dev>,
"Jiri Olsa" <jolsa@kernel.org>,
"Emil Tsalapatis" <emil@etsalapatis.com>,
"Shuah Khan" <shuah@kernel.org>,
"Viktor Malik" <vmalik@redhat.com>,
"Justin Suess" <utilityemal77@gmail.com>,
"Kaitao Cheng" <chengkaitao@kylinos.cn>,
"Leon Hwang" <leon.hwang@linux.dev>,
"Yiyang Chen" <chenyy23@mails.tsinghua.edu.cn>,
<linux-kselftest@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf v3 4/4] selftests/bpf: Test untrusted allocated-object pointers
Date: Thu, 13 Aug 2026 04:32:24 +0200 [thread overview]
Message-ID: <DKNGPMSHGC89.1WT6CHDFCPRJR@gmail.com> (raw)
In-Reply-To: <20260803112218.3361213-5-dingning04@gmail.com>
On Mon Aug 3, 2026 at 1:22 PM CEST, Ning Ding wrote:
> The verifier previously allowed pointers used after RCU protection ended
> to reach bpf_refcount_acquire() and, for one object layout, a direct write.
> If the object was freed and reused, these operations could access stale
> memory.
>
> Add tests that keep BPF_PROBE_MEM reads accepted but reject reference
> acquisition and direct writes after RCU protection ends. Cover both tested
> object layouts.
>
> Reported-by: sashiko-bot@kernel.org
> Link: https://lore.kernel.org/r/20260726021304.97ED91F000E9@smtp.kernel.org
> Assisted-by: Codex:gpt-5
> Signed-off-by: Ning Ding <dingning04@gmail.com>
> ---
> .../selftests/bpf/progs/refcounted_kptr.c | 100 ++++++++++++++++++
> .../bpf/progs/refcounted_kptr_fail.c | 27 +++++
> 2 files changed, 127 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/progs/refcounted_kptr.c b/tools/testing/selftests/bpf/progs/refcounted_kptr.c
> index fd35093285c0d..b70be8b52ff80 100644
> --- a/tools/testing/selftests/bpf/progs/refcounted_kptr.c
> +++ b/tools/testing/selftests/bpf/progs/refcounted_kptr.c
> @@ -893,6 +893,106 @@ long refcount_acquire_rcu_map_kptr_null_checked(void *ctx)
> return 0;
> }
>
> +SEC("?tc")
> +__success
> +long map_kptr_read_after_rcu_unlock(void *ctx)
> +{
> + struct map_value_refcount_only *mapval;
> + struct node_refcount_only *n;
> + int idx = 0;
> +
> + mapval = bpf_map_lookup_elem(&stashed_refcount_only, &idx);
> + if (!mapval)
> + return 0;
> +
> + bpf_rcu_read_lock();
> + n = mapval->node;
> + if (!n) {
> + bpf_rcu_read_unlock();
> + return 0;
> + }
> + bpf_rcu_read_unlock();
> +
> + return n->key;
Here, n->key won't read untrusted pointer, since tc progs have RCU protection,
so I don't think the pointer becomes untrusted.
> +}
> +
> +SEC("?tc")
> +__failure __msg("is neither owning or non-owning ref")
> +long refcount_acquire_graph_after_rcu_unlock(void *ctx)
> +{
> + struct map_value *mapval;
> + struct node_data *n, *m;
> + int idx = 0;
> +
> + mapval = bpf_map_lookup_elem(&stashed_nodes, &idx);
> + if (!mapval)
> + return 0;
> +
> + bpf_rcu_read_lock();
> + n = mapval->node;
> + if (!n) {
> + bpf_rcu_read_unlock();
> + return 0;
> + }
> + bpf_rcu_read_unlock();
> +
> + m = bpf_refcount_acquire(n);
> + if (m)
> + bpf_obj_drop(m);
> +
> + return 0;
> +}
I don't think you ran these tests, they have unexpected success, mostly because
tc has RCU read protection already, so n passed to refcount acquire is not
untrusted.
> +
> +SEC("?tc")
> +__failure __msg("only read is supported")
> +long graph_map_kptr_write_after_rcu_unlock(void *ctx)
> +{
> + struct map_value *mapval;
> + struct node_data *n;
> + int idx = 0;
> +
> + mapval = bpf_map_lookup_elem(&stashed_nodes, &idx);
> + if (!mapval)
> + return 1;
> +
> + bpf_rcu_read_lock();
> + n = mapval->node;
> + if (!n) {
> + bpf_rcu_read_unlock();
> + return 2;
> + }
> + bpf_rcu_read_unlock();
> +
> + n->key = 1;
> + return 0;
> +}
> +
Same for write here, it succeeds...
> +SEC("?tc")
> +__success
> +long graph_map_kptr_read_after_spin_unlock(void *ctx)
> +{
> + struct map_value *mapval;
> + struct node_data *n;
> + int idx = 0;
> +
> + mapval = bpf_map_lookup_elem(&stashed_nodes, &idx);
> + if (!mapval)
> + return 0;
> +
> + bpf_rcu_read_lock();
> + n = mapval->node;
> + if (!n) {
> + bpf_rcu_read_unlock();
> + return 0;
> + }
> + bpf_rcu_read_unlock();
> +
> + bpf_spin_lock(&lock);
> + bpf_spin_unlock(&lock);
> +
> + return n->key;
> +}
> +
Similar case.
> static long __stash_map_empty_xchg(struct node_data *n, int idx)
> {
> struct map_value *mapval = bpf_map_lookup_elem(&stashed_nodes, &idx);
> diff --git a/tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c b/tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c
> index acd3e81a39168..3408f68ad444d 100644
> --- a/tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c
> +++ b/tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c
> @@ -127,6 +127,33 @@ long refcount_acquire_rcu_map_kptr_unchecked_drop(void *ctx)
> return 0;
> }
>
> +SEC("?tc")
> +__failure __msg("is neither owning or non-owning ref")
> +long refcount_acquire_after_rcu_unlock(void *ctx)
> +{
> + struct map_value_refcount_only *mapval;
> + struct node_refcount_only *n, *m;
> + int idx = 0;
> +
> + mapval = bpf_map_lookup_elem(&stashed_refcount_only, &idx);
> + if (!mapval)
> + return 1;
> +
> + bpf_rcu_read_lock();
> + n = mapval->node;
> + if (!n) {
> + bpf_rcu_read_unlock();
> + return 2;
> + }
> + bpf_rcu_read_unlock();
> +
> + m = bpf_refcount_acquire(n);
> + if (m)
> + bpf_obj_drop(m);
> +
> + return 0;
> +}
This also passes.
> +
> SEC("?tc")
> __failure __msg("Unreleased reference id=3 alloc_insn={{[0-9]+}}")
> long rbtree_refcounted_node_ref_escapes_owning_input(void *ctx)
Please respin and only send patches after you have tested them properly.
The right fix might be to use fentry.s for these tests to trigger untrusted
marking of pointers. The fixes themselves do make sense.
For the next version, target bpf-next and drop Cc: stable.
pw-bot: cr
next prev parent reply other threads:[~2026-08-13 2:32 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-03 11:22 [PATCH bpf v3 0/4] bpf: Fix refcount_acquire handling for borrowed kptrs Ning Ding
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 2/4] selftests/bpf: Test refcount_acquire return nullability Ning Ding
2026-08-03 13:59 ` Amery Hung
2026-08-03 22:48 ` Ning Ding
2026-08-03 11:22 ` [PATCH bpf v3 3/4] bpf: Reject untrusted allocated-object pointers Ning Ding
2026-08-03 11:22 ` [PATCH bpf v3 4/4] selftests/bpf: Test " Ning Ding
2026-08-13 2:32 ` Kumar Kartikeya Dwivedi [this message]
2026-08-13 3:40 ` Ning Ding
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=DKNGPMSHGC89.1WT6CHDFCPRJR@gmail.com \
--to=memxor@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=chengkaitao@kylinos.cn \
--cc=chenyy23@mails.tsinghua.edu.cn \
--cc=daniel@iogearbox.net \
--cc=dingning04@gmail.com \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=greg@kroah.com \
--cc=jolsa@kernel.org \
--cc=leon.hwang@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=sashiko-bot@kernel.org \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=utilityemal77@gmail.com \
--cc=vmalik@redhat.com \
--cc=yonghong.song@linux.dev \
/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.