Skip to contentSkip to conversation

bpftool: Use appropriate permissions for map access

PATCH Superseded Slava Imameev 2 participants 4 messages Jun 11, 2025 – Jun 16, 2025

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 bpftool: Use appropriate permissions for map access

Modify several functions in tools/bpf/bpftool/common.c to allow specification of requested access for file descriptors, such as read-only access.

Update bpftool to request only read access for maps when write access is not required. This fixes errors when reading from maps that are protected from modification via security_bpf_map.

---
Changes in v2:
- fix for a test compilation error: "conflicting types for 'bpf_fentry_test1'" Changes in v3:
- Addressed review feedback
- Converted the check for flags to an assert in map_parse_fds
- Modified map_fd_by_name to keep an existing fd where possible
- Fixed requested access for map delete command in do_delete
- Changed requested access to RDONLY for inner map fd in do_create
- Changed requested access to RDONLY for iterator fd in do_pin
---

---
 tools/bpf/bpftool/btf.c           |  3 +-
 tools/bpf/bpftool/common.c        | 58 ++++++++++++++++++++++---------
 tools/bpf/bpftool/iter.c          |  2 +-
 tools/bpf/bpftool/link.c          |  2 +-
 tools/bpf/bpftool/main.h          | 13 ++++---
 tools/bpf/bpftool/map.c           | 56 +++++++++++++++++------------
 tools/bpf/bpftool/map_perf_ring.c |  3 +-
 tools/bpf/bpftool/prog.c          |  4 +--
 8 files changed, 91 insertions(+), 50 deletions(-)
tools/bpf/bpftool/btf.c +2 -1
index 6b14cbfa58aa..1ba27cb03348 100644
@@ -905,7 +905,8 @@ static int do_dump(int argc, char **argv)
 			return -1;
 		}
 
-		fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
+		fd = map_parse_fd_and_info(&argc, &argv, &info, &len,
+					   BPF_F_RDONLY);
 		if (fd < 0)
 			return -1;
 
tools/bpf/bpftool/common.c +42 -16

165 lines not on this page · raw message

tools/bpf/bpftool/iter.c +1 -1
index 5c39c2ed36a2..df5f0d1e07e8 100644
@@ -37,7 +37,7 @@ static int do_pin(int argc, char **argv)
 				return -1;
 			}
 
-			map_fd = map_parse_fd(&argc, &argv);
+			map_fd = map_parse_fd(&argc, &argv, BPF_F_RDONLY);
 			if (map_fd < 0)
 				return -1;
 
tools/bpf/bpftool/link.c +1 -1
index 03513ffffb79..a773e05d5ade 100644
@@ -117,7 +117,7 @@ static int link_parse_fd(int *argc, char ***argv)
 		path = **argv;
 		NEXT_ARGP();
 
-		return open_obj_pinned_any(path, BPF_OBJ_LINK);
+		return open_obj_pinned_any(path, BPF_OBJ_LINK, NULL);
 	}
 
 	p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
tools/bpf/bpftool/main.h +8 -5
index 9eb764fe4cc8..6db704fda5c0 100644
@@ -15,6 +15,7 @@
 
 #include <bpf/hashmap.h>
 #include <bpf/libbpf.h>
+#include <bpf/bpf.h>
 
 #include "json_writer.h"
 
@@ -140,8 +141,10 @@ void get_prog_full_name(const struct bpf_prog_info *prog_info, int prog_fd,
 int get_fd_type(int fd);
 const char *get_fd_type_name(enum bpf_obj_type type);
 char *get_fdinfo(int fd, const char *key);
-int open_obj_pinned(const char *path, bool quiet);
-int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type);
+int open_obj_pinned(const char *path, bool quiet,
+		    const struct bpf_obj_get_opts *opts);
+int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type,
+			const struct bpf_obj_get_opts *opts);
 int mount_bpffs_for_file(const char *file_name);
 int create_and_mount_bpffs_dir(const char *dir_name);
 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(int *, char ***));
@@ -167,10 +170,10 @@ int do_iter(int argc, char **argv) __weak;
 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
 int prog_parse_fd(int *argc, char ***argv);
 int prog_parse_fds(int *argc, char ***argv, int **fds);
-int map_parse_fd(int *argc, char ***argv);
-int map_parse_fds(int *argc, char ***argv, int **fds);
+int map_parse_fd(int *argc, char ***argv, __u32 open_flags);
+int map_parse_fds(int *argc, char ***argv, int **fds, __u32 open_flags);
 int map_parse_fd_and_info(int *argc, char ***argv, struct bpf_map_info *info,
-			  __u32 *info_len);
+			  __u32 *info_len, __u32 open_flags);
 
 struct bpf_prog_linfo;
 #if defined(HAVE_LLVM_SUPPORT) || defined(HAVE_LIBBFD_SUPPORT)
tools/bpf/bpftool/map.c +33 -23

192 lines not on this page · raw message

tools/bpf/bpftool/map_perf_ring.c +2 -1
index 552b4ca40c27..bcb767e2d673 100644
@@ -128,7 +128,8 @@ int do_event_pipe(int argc, char **argv)
 	int err, map_fd;
 
 	map_info_len = sizeof(map_info);
-	map_fd = map_parse_fd_and_info(&argc, &argv, &map_info, &map_info_len);
+	map_fd = map_parse_fd_and_info(&argc, &argv, &map_info, &map_info_len,
+				       0);
 	if (map_fd < 0)
 		return -1;
 
tools/bpf/bpftool/prog.c +2 -2
index 96eea8a67225..deeaa5c1ed7d 100644
@@ -1062,7 +1062,7 @@ static int parse_attach_detach_args(int argc, char **argv, int *progfd,
 	if (!REQ_ARGS(2))
 		return -EINVAL;
 
-	*mapfd = map_parse_fd(&argc, &argv);
+	*mapfd = map_parse_fd(&argc, &argv, 0);
 	if (*mapfd < 0)
 		return *mapfd;
 
@@ -1608,7 +1608,7 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 			}
 			NEXT_ARG();
 
-			fd = map_parse_fd(&argc, &argv);
+			fd = map_parse_fd(&argc, &argv, 0);
 			if (fd < 0)
 				goto err_free_reuse_maps;
 
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 2/2 selftests/bpf: Add test for bpftool access to read-only protected maps

Add selftest cases that validate bpftool's expected behavior when accessing maps protected from modification via security_bpf_map.

The test includes a BPF program attached to security_bpf_map with two maps:
- A protected map that only allows read-only access
- An unprotected map that allows full access

The test script attaches the BPF program to security_bpf_map and verifies that for the bpftool map command:
- Read access works on both maps
- Write access fails on the protected map
- Write access succeeds on the unprotected map
- These behaviors remain consistent when the maps are pinned

---
Changes in v2:
- fix for a test compilation error: "conflicting types for 'bpf_fentry_test1'" Changes in v3:
- Addressed review feedback
- Added tests for map iterator, map and map-of-maps creation, deletion
- Cleaned up excessive output logging
---

---
 tools/testing/selftests/bpf/Makefile          |   1 +
 .../selftests/bpf/progs/bpf_iter_map_elem.c   |  22 ++
 .../selftests/bpf/progs/security_bpf_map.c    |  69 ++++
 .../testing/selftests/bpf/test_bpftool_map.sh | 363 ++++++++++++++++++
 4 files changed, 455 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_map_elem.c
 create mode 100644 tools/testing/selftests/bpf/progs/security_bpf_map.c
 create mode 100755 tools/testing/selftests/bpf/test_bpftool_map.sh
tools/testing/selftests/bpf/Makefile +1 -0
index cf5ed3bee573..731a86407799 100644
@@ -109,6 +109,7 @@ TEST_PROGS := test_kmod.sh \
 	test_xdping.sh \
 	test_bpftool_build.sh \
 	test_bpftool.sh \
+	test_bpftool_map.sh \
 	test_bpftool_metadata.sh \
 	test_doc_build.sh \
 	test_xsk.sh \
tools/testing/selftests/bpf/progs/bpf_iter_map_elem.c new file +22 -0
new file mode 100644
index 000000000000..2f20485e0de3
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include "vmlinux.h"
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "GPL";
+
+__u32 value_sum = 0;
+
+SEC("iter/bpf_map_elem")
+int dump_bpf_map_values(struct bpf_iter__bpf_map_elem *ctx)
+{
+	__u32 value = 0;
+
+	if (ctx->value == (void *)0)
+		return 0;
+
+	bpf_probe_read_kernel(&value, sizeof(value), ctx->value);
+	value_sum += value;
+	return 0;
+}
tools/testing/selftests/bpf/progs/security_bpf_map.c new file +69 -0

69 lines not on this page · raw message

tools/testing/selftests/bpf/test_bpftool_map.sh new file +363 -0

363 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer Acked-by on v3 2/2
#2 lore raw

Re: [PATCH bpf-next v3 2/2] selftests/bpf: Add test for bpftool access to read-only protected maps

2025-06-12 08:18 UTC+1000 ~ Slava Imameev <[email protected]>

Quoted 7 lines

Add selftest cases that validate bpftool's expected behavior when accessing maps protected from modification via security_bpf_map.

The test includes a BPF program attached to security_bpf_map with two maps:
- A protected map that only allows read-only access
- An unprotected map that allows full access

Quoted 8 lines

The test script attaches the BPF program to security_bpf_map and verifies that for the bpftool map command:
- Read access works on both maps
- Write access fails on the protected map
- Write access succeeds on the unprotected map
- These behaviors remain consistent when the maps are pinned

Thank you!

maintainer
#3 lore raw
In reply to Slava Imameev

2025-06-12 08:18 UTC+1000 ~ Slava Imameev <[email protected]>

Quoted 49 lines

Modify several functions in tools/bpf/bpftool/common.c to allow specification of requested access for file descriptors, such as read-only access.

Update bpftool to request only read access for maps when write access is not required. This fixes errors when reading from maps that are protected from modification via security_bpf_map.

---
Changes in v2:
- fix for a test compilation error: "conflicting types for 'bpf_fentry_test1'" Changes in v3:
- Addressed review feedback
- Converted the check for flags to an assert in map_parse_fds
- Modified map_fd_by_name to keep an existing fd where possible
- Fixed requested access for map delete command in do_delete
- Changed requested access to RDONLY for inner map fd in do_create
- Changed requested access to RDONLY for iterator fd in do_pin
---

---
 tools/bpf/bpftool/btf.c           |  3 +-
 tools/bpf/bpftool/common.c        | 58 ++++++++++++++++++++++---------
 tools/bpf/bpftool/iter.c          |  2 +-
 tools/bpf/bpftool/link.c          |  2 +-
 tools/bpf/bpftool/main.h          | 13 ++++---
 tools/bpf/bpftool/map.c           | 56 +++++++++++++++++------------
 tools/bpf/bpftool/map_perf_ring.c |  3 +-
 tools/bpf/bpftool/prog.c          |  4 +--
 8 files changed, 91 insertions(+), 50 deletions(-)
tools/bpf/bpftool/btf.c +2 -1
index 6b14cbfa58aa..1ba27cb03348 100644
@@ -905,7 +905,8 @@ static int do_dump(int argc, char **argv)
 			return -1;
 		}
 
-		fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
+		fd = map_parse_fd_and_info(&argc, &argv, &info, &len,
+					   BPF_F_RDONLY);
 		if (fd < 0)
 			return -1;
 
tools/bpf/bpftool/common.c +0 -0
index ecfa790adc13..3bdc65112c0d 100644

[...]

tools/bpf/bpftool/common.c static int map_fd_by_name(char *name, int **fds) · line 1049

Quoted 10 lines
tools/bpf/bpftool/common.c +4 -1
@@ -1023,8 +1042,13 @@ static int map_fd_by_name(char *name, int **fds)
 	return -1;
 }
 
-int map_parse_fds(int *argc, char ***argv, int **fds)
+int map_parse_fds(int *argc, char ***argv, int **fds, __u32 open_flags)
 {
+	LIBBPF_OPTS(bpf_get_fd_by_id_opts, opts);
+
+	assert((open_flags & ~BPF_F_RDONLY) == 0);

Can you please "#include <assert.h>" at the top of the file? We don't need it in the kernel repo, because the header is included from tools/include/linux/kernel.h (from bpftool's main.h) if I remember correctly. But the GitHub mirror uses a stripped-down version of kernel.h which doesn't pull assert.h, so we need to include it explicitly - I just remembered when seeing your v3, sorry.

Looks good to me otherwise, thanks!
Quentin