[PATCH bpf v2 2/2] selftests/bpf: Check writable iterator attach to a read-only array

From: Adethya Srinivasan

Date: Thu Nov 12 2026 - 11:10:21 EST


Reuse dump_bpf_array_map(), which writes to ctx->value, and attempt
to attach it to an array with BPF_F_RDONLY_PROG set. Assert that the
attach returns -EACCES and destroy the link if it unexpectedly succeeds.

The test fails on an unfixed kernel and passes with the fix applied.

Signed-off-by: Adethya Srinivasan <adethyasrinivasan@xxxxxxxxxxx>
---
.../selftests/bpf/prog_tests/bpf_iter.c | 35 +++++++++++++++++++
1 file changed, 35 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
index c69080ca1..5249b07cd 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
@@ -1100,6 +1100,39 @@ static void test_bpf_array_map(void)
bpf_iter_bpf_array_map__destroy(skel);
}

+static void test_bpf_array_map_rdonly(void)
+{
+ DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
+ struct bpf_iter_bpf_array_map *skel;
+ union bpf_iter_link_info linfo = {};
+ struct bpf_link *link;
+ int err;
+
+ skel = bpf_iter_bpf_array_map__open();
+ if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_array_map__open"))
+ return;
+
+ err = bpf_map__set_map_flags(skel->maps.arraymap1, BPF_F_RDONLY_PROG);
+ if (!ASSERT_OK(err, "set_map_flags"))
+ goto out;
+
+ err = bpf_iter_bpf_array_map__load(skel);
+ if (!ASSERT_OK(err, "bpf_iter_bpf_array_map__load"))
+ goto out;
+
+ linfo.map.map_fd = bpf_map__fd(skel->maps.arraymap1);
+ opts.link_info = &linfo;
+ opts.link_info_len = sizeof(linfo);
+ link = bpf_program__attach_iter(skel->progs.dump_bpf_array_map, &opts);
+ err = libbpf_get_error(link);
+ ASSERT_EQ(err, -EACCES, "attach_rdonly_write_iter");
+ if (!err)
+ bpf_link__destroy(link);
+
+out:
+ bpf_iter_bpf_array_map__destroy(skel);
+}
+
static void test_bpf_array_map_iter_fd(void)
{
struct bpf_iter_bpf_array_map *skel;
@@ -1770,6 +1803,8 @@ void test_bpf_iter(void)
test_bpf_percpu_hash_map();
if (test__start_subtest("bpf_array_map"))
test_bpf_array_map();
+ if (test__start_subtest("bpf_array_map_rdonly"))
+ test_bpf_array_map_rdonly();
if (test__start_subtest("bpf_array_map_iter_fd"))
test_bpf_array_map_iter_fd();
if (test__start_subtest("bpf_percpu_array_map"))