From: Ihor Solodrai <ihor.solodrai@linux.dev>
To: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Amery Hung <ameryhung@gmail.com>,
Emil Tsalapatis <emil@etsalapatis.com>,
Nicholas Carlini <npc@anthropic.com>,
bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
kernel-team@meta.com
Subject: [PATCH bpf v1 4/4] selftests/bpf: Cover callback-frame map key lifetime
Date: Fri, 6 Nov 2026 16:11:57 -0700 [thread overview]
Message-ID: <20261106231157.2840635-5-ihor.solodrai@linux.dev> (raw)
In-Reply-To: <20261106231157.2840635-1-ihor.solodrai@linux.dev>
Array and per-CPU array callbacks to bpf_for_each_map_elem() receive
a key on the helper's stack.
Check that saving this key in the callback context and dereferencing
it after iteration is rejected.
Also require the same pattern to keep verifying for a hash key and
an array value, whose storage outlives the callback. These controls
guard the array-only and key-only scope of the lifetime restriction.
Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
---
.../bpf/progs/verifier_iterating_callbacks.c | 91 +++++++++++++++++++
1 file changed, 91 insertions(+)
diff --git a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
index 4dd98acc6436..da8068a1f004 100644
--- a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
+++ b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
@@ -9,6 +9,20 @@ struct {
__type(value, __u64);
} map SEC(".maps");
+struct {
+ __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+ __uint(max_entries, 8);
+ __type(key, __u32);
+ __type(value, __u64);
+} percpu_map SEC(".maps");
+
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __uint(max_entries, 8);
+ __type(key, __u32);
+ __type(value, __u64);
+} hash_map SEC(".maps");
+
struct {
__uint(type, BPF_MAP_TYPE_USER_RINGBUF);
__uint(max_entries, 8);
@@ -804,4 +818,81 @@ __naked void check_add_const_regsafe_off(void)
: __clobber_common);
}
+struct key_ctx {
+ __u32 *key;
+};
+
+static long park_key_cb(struct bpf_map *map, __u32 *key, __u64 *value,
+ void *context)
+{
+ struct key_ctx *c = context;
+
+ c->key = key;
+ return 0;
+}
+
+/* bpf_for_each_array_elem() passes a key from its own stack frame. */
+SEC("?raw_tp")
+__failure __msg("invalid mem access 'scalar'")
+int array_park_map_key(void *ctx)
+{
+ struct key_ctx c = {};
+
+ bpf_for_each_map_elem(&map, park_key_cb, &c, 0);
+ if (c.key)
+ return *c.key;
+ return 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("invalid mem access 'scalar'")
+int percpu_array_park_map_key(void *ctx)
+{
+ struct key_ctx c = {};
+
+ bpf_for_each_map_elem(&percpu_map, park_key_cb, &c, 0);
+ if (c.key)
+ return *c.key;
+ return 0;
+}
+
+/* A hash key points into the element, which outlives the callback. */
+SEC("?raw_tp")
+__success
+int hash_park_map_key(void *ctx)
+{
+ struct key_ctx c = {};
+
+ bpf_for_each_map_elem(&hash_map, park_key_cb, &c, 0);
+ if (c.key)
+ return *c.key;
+ return 0;
+}
+
+struct value_ctx {
+ __u64 *value;
+};
+
+static long park_value_cb(struct bpf_map *map, __u32 *key, __u64 *value,
+ void *context)
+{
+ struct value_ctx *c = context;
+
+ c->value = value;
+ return 0;
+}
+
+/* Only the key is frame-scoped; the element lives until map teardown. */
+SEC("?raw_tp")
+__success
+int array_park_map_value(void *ctx)
+{
+ struct value_ctx c = {};
+
+ bpf_for_each_map_elem(&map, park_value_cb, &c, 0);
+ if (c.value)
+ return *c.value;
+ return 0;
+}
+
char _license[] SEC("license") = "GPL";
--
2.56.0
next prev parent reply other threads:[~2026-11-06 23:12 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-06 23:11 [PATCH bpf v1 0/4] bpf: Scope callback arguments to their frame Ihor Solodrai
2026-11-06 23:11 ` [PATCH bpf v1 1/4] bpf: Scope the bpf_user_ringbuf_drain() dynptr to its callback frame Ihor Solodrai
2026-11-06 23:11 ` [PATCH bpf v1 2/4] selftests/bpf: Cover the user ringbuf callback dynptr lifetime Ihor Solodrai
2026-11-06 23:11 ` [PATCH bpf v1 3/4] bpf: Scope the bpf_for_each_map_elem() array key to the callback frame Ihor Solodrai
2026-11-06 23:11 ` Ihor Solodrai [this message]
2026-11-06 23:15 ` [PATCH bpf v1 0/4] bpf: Scope callback arguments to their frame Ihor Solodrai
2026-11-06 23:22 ` Kumar Kartikeya Dwivedi
2026-11-06 23:32 ` Ihor Solodrai
2026-11-07 3:40 ` Alexei Starovoitov
2026-11-12 20:57 ` Alexei Starovoitov
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=20261106231157.2840635-5-ihor.solodrai@linux.dev \
--to=ihor.solodrai@linux.dev \
--cc=ameryhung@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=linux-kernel@vger.kernel.org \
--cc=memxor@gmail.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.