bpftool: Use appropriate permissions for map access
Subsystems (4)
PATCH 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.
--- tools/bpf/bpftool/btf.c | 3 +- tools/bpf/bpftool/common.c | 57 ++++++++++++++++++++++--------- 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, 90 insertions(+), 50 deletions(-)
tools/bpf/bpftool/btf.c +2 -1
@@ -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 +41 -16
164 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,0 );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 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
--- tools/testing/selftests/bpf/Makefile | 1 + .../selftests/bpf/progs/security_bpf_map.c | 56 +++++ .../testing/selftests/bpf/test_bpftool_map.sh | 208 ++++++++++++++++++ 3 files changed, 265 insertions(+) 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/security_bpf_map.c new file +56 -0
56 lines not on this page · raw message
tools/testing/selftests/bpf/test_bpftool_map.sh new file +208 -0
208 lines not on this page · raw message
Signature · 1 line
2.34.1