bpftool: Use appropriate permissions for map access
Subsystems (4)
PATCH v4 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 Changes in v4:
- Explicitly included assert.h in common.c
- Added read only access to map in build_btf_type_table
---
tools/bpf/bpftool/btf.c
--- tools/bpf/bpftool/btf.c | 8 +++-- tools/bpf/bpftool/common.c | 59 ++++++++++++++++++++++--------- 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, 96 insertions(+), 51 deletions(-)
tools/bpf/bpftool/btf.c +6 -2
@@ -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 ;
@@ -1118,10 +1119,13 @@ build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type, [BPF_OBJ_PROG] = "prog", [BPF_OBJ_MAP ] ="map" , }; +LIBBPF_OPTS (bpf_get_fd_by_id_opts, opts_ro);__u32 btf_id, id =0 ;int err;int fd; + opts_ro.open_flags =BPF_F_RDONLY ; +while (true) {switch (type) {case BPF_OBJ_PROG :
@@ -1151,7 +1155,7 @@ build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type, fd =bpf_prog_get_fd_by_id (id);break ;case BPF_OBJ_MAP : - fd =bpf_map_get_fd_by_id (id); + fd =bpf_map_get_fd_by_id_opts (id, &opts_ro);break ;default : err =-1 ;
tools/bpf/bpftool/common.c +43 -16
172 lines not on this page · raw message
tools/bpf/bpftool/iter.c +1 -1
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH v4 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
Changes in v4:
- Added a test for the btf list command to verify read only access to maps
- Added a test for map's btf data access with the btf dump command
---
--- 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 | 398 ++++++++++++++++++ 4 files changed, 490 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
@@ -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
@@ -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 +398 -0
398 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v4 2/2] selftests/bpf: Add test for bpftool access to read-only protected maps
2025-06-21 01: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_mapwith 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_mapand 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 pinnedSigned-off-by Slava Imameev <[email protected]>
2025-06-21 01:18 UTC+1000 ~ Slava Imameev <[email protected]>
Quoted 9 lines
Modify several functions in
tools/bpf/bpftool/common.cto 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.Signed-off-by Slava Imameev <[email protected]>
Thanks!
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Sat, 21 Jun 2025 01:18:11 +1000 you wrote:
Modify several functions in
tools/bpf/bpftool/common.cto 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.[...]
Here is the summary with links:
- [bpf-next,v4,1/2] bpftool: Use appropriate permissions for map access
https://git.kernel.org/bpf/bpf-next/c/d32179e8c258 - [bpf-next,v4,2/2] selftests/bpf: Add test for bpftool access to read-only protected maps https://git.kernel.org/bpf/bpf-next/c/f8b19aeca165
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html