mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Adethya Srinivasan <adethyasrinivasan@outlook.com>
To: bpf@vger.kernel.org
Cc: Adethya Srinivasan <adethyasrinivasan@outlook.com>,
	yonghong.song@linux.dev, ast@kernel.org, daniel@iogearbox.net,
	andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com,
	martin.lau@linux.dev, song@kernel.org, jolsa@kernel.org,
	emil@etsalapatis.com, ihor.solodrai@linux.dev, shuah@kernel.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: [PATCH bpf v2 2/2] selftests/bpf: Check writable iterator attach to a read-only array
Date: Thu, 12 Nov 2026 15:54:07 +0000	[thread overview]
Message-ID: <LO2P302MB019736CF58FAA4C98C6F78AABC702@LO2P302MB0197.GBRP302.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <LO2P302MB0197C0434BCC2A9031E99A18BC702@LO2P302MB0197.GBRP302.PROD.OUTLOOK.COM>

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@outlook.com>
---
 .../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"))

      parent reply	other threads:[~2026-11-12 15:54 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 15:54 [PATCH bpf v2 0/2] Reject writable map iterators on read-only maps Adethya Srinivasan
2026-11-12 15:54 ` [PATCH bpf v2 1/2] bpf: " Adethya Srinivasan
2026-11-12 15:54 ` Adethya Srinivasan [this message]

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=LO2P302MB019736CF58FAA4C98C6F78AABC702@LO2P302MB0197.GBRP302.PROD.OUTLOOK.COM \
    --to=adethyasrinivasan@outlook.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=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®