bpf: check bpf_map/bpf_program fd validity
Mykyta Yatsenko [email protected]
PATCH 1/1 bpf: check bpf_map/bpf_program fd validity
From: Mykyta Yatsenko <[email protected]>
libbpf creates bpf_program/bpf_map structs for each program/map that user defines, but it allows to disable creating/loading those objects in kernel, in that case they won't have associated file descriptor (fd < 0). Such functionality is used for backward compatibility with some older kernels.
Nothing prevents users from passing these maps or programs with no kernel counterpart to libbpf APIs. This change introduces explicit checks for kernel objects existence, aiming to improve visibility of those edge cases and provide meaningful warnings to users.
--- tools/lib/bpf/libbpf.c | 56 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 5 deletions(-)
tools/lib/bpf/libbpf.c +51 -5
@@ -8572,6 +8572,12 @@ int bpf_map__pin(struct bpf_map *map, const char *path)return libbpf_err (-EINVAL ); } +if (map->fd <0 ) { +pr_warn ("map '%s': can't pin BPF map without FD (was it created?)\n" , +bpf_map__name (map)); +return libbpf_err (-EINVAL ); + } +if (map->pin_path) {if (path &&strcmp (path, map->pin_path)) { pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
@@ -10316,6 +10322,11 @@ static int validate_map_op(const struct bpf_map *map, size_t key_sz,return -EINVAL ; } +if (map->fd <0 ) { +pr_warn ("map '%s': can't use BPF map without FD (was it created?)\n" , map->name); +return -EINVAL ; + } +if (!check_value_sz)return 0 ;
@@ -10428,8 +10439,15 @@ long libbpf_get_error(const void *ptr)int bpf_link__update_program (struct bpf_link *link,struct bpf_program *prog) {int ret; +int prog_fd =bpf_program__fd (prog); - ret =bpf_link_update (bpf_link__fd (link),bpf_program__fd (prog),NULL ); +if (prog_fd <0 ) { +pr_warn ("prog '%s': can't use BPF program without FD (was it created?)\n" , + prog->name); +return libbpf_err (-EINVAL ); + } + + ret =bpf_link_update (bpf_link__fd (link), prog_fd,NULL );return libbpf_err_errno (ret); }
@@ -11347,6 +11365,13 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,if (!OPTS_VALID (opts, bpf_kprobe_multi_opts))return libbpf_err_ptr (-EINVAL ); + prog_fd =bpf_program__fd (prog); +if (prog_fd <0 ) { +pr_warn ("prog '%s': can't attach BPF program without FD (was it created?)\n" , + prog->name); +return libbpf_err_ptr (-EINVAL ); + } + syms =OPTS_GET (opts, syms, false); addrs =OPTS_GET (opts, addrs, false); cnt =OPTS_GET (opts, cnt, false);
@@ -11387,7 +11412,6 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, } link->detach = &bpf_link__detach_fd; - prog_fd =bpf_program__fd (prog); link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_KPROBE_MULTI , &lopts);if (link_fd <0 ) { err = -errno;
@@ -11770,6 +11794,13 @@ bpf_program__attach_uprobe_multi(const struct bpf_program *prog,if (!OPTS_VALID (opts, bpf_uprobe_multi_opts))return libbpf_err_ptr (-EINVAL ); + prog_fd =bpf_program__fd (prog); +if (prog_fd <0 ) { +pr_warn ("prog '%s': can't attach BPF program without FD (was it created?)\n" , + prog->name); +return libbpf_err_ptr (-EINVAL ); + } + syms =OPTS_GET (opts, syms,NULL ); offsets =OPTS_GET (opts, offsets,NULL ); ref_ctr_offsets =OPTS_GET (opts, ref_ctr_offsets,NULL );
@@ -11845,7 +11876,6 @@ bpf_program__attach_uprobe_multi(const struct bpf_program *prog, } link->detach = &bpf_link__detach_fd; - prog_fd =bpf_program__fd (prog); link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &lopts);if (link_fd <0 ) { err = -errno;
@@ -12671,6 +12701,12 @@ struct bpf_link *bpf_program__attach(const struct bpf_program *prog)if (!prog->sec_def || !prog->sec_def->prog_attach_fn)return libbpf_err_ptr (-EOPNOTSUPP ); +if (bpf_program__fd (prog) <0 ) { +pr_warn ("prog '%s': can't attach BPF program w/o FD (did you load it?)\n" , + prog->name); +return libbpf_err_ptr (-EINVAL ); + } + err = prog->sec_def->prog_attach_fn (prog, prog->sec_def->cookie, &link);if (err)return libbpf_err_ptr (err);
@@ -12711,9 +12747,14 @@ struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)__u32 zero =0 ;int err, fd; -if (!bpf_map__is_struct_ops (map) || map->fd ==-1 ) +if (!bpf_map__is_struct_ops (map))return libbpf_err_ptr (-EINVAL ); +if (map->fd <0 ) { +pr_warn ("map '%s': can't attach BPF map w/o FD (did you load it?)\n" , map->name); +return libbpf_err_ptr (-EINVAL ); + } + link =calloc (1 ,sizeof (*link));if (!link)return libbpf_err_ptr (-EINVAL );
@@ -12760,8 +12801,13 @@ int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map)__u32 zero =0 ;int err; -if (!bpf_map__is_struct_ops (map) || !map_is_created (map)) +if (!bpf_map__is_struct_ops (map)) +return -EINVAL ; + +if (map->fd <0 ) { +pr_warn ("map '%s': can't use BPF map w/o FD (did you load it?)\n" , map->name);return -EINVAL ; + } st_ops_link = container_of(link, struct bpf_link_struct_ops, link);/* Ensure the type of a link is correct */
Signature · 1 line
2.42.0
Quoted 1 line · Mykyta Yatsenko
We generally use "libbpf: " prefix for patch subject if changes are related to libbpf code base. I fixed it up while applying.
Quoted 13 lines
From: Mykyta Yatsenko <[email protected]>
libbpf creates bpf_program/bpf_map structs for each program/map that user defines, but it allows to disable creating/loading those objects in kernel, in that case they won't have associated file descriptor (fd < 0). Such functionality is used for backward compatibility with some older kernels.
Nothing prevents users from passing these maps or programs with no kernel counterpart to libbpf APIs. This change introduces explicit checks for kernel objects existence, aiming to improve visibility of those edge cases and provide meaningful warnings to users.
Quoted 5 lines
Signed-off-by Mykyta Yatsenko <[email protected]>--- tools/lib/bpf/libbpf.c | 56 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 5 deletions(-)
See nits below. I fixed all that up while applying, as it was just some text adjustment, so it didn't seem worth another version. Thanks for the patch, it will help users!
Quoted 30 lines
tools/lib/bpf/libbpf.c +11
-0@@ -8572,6 +8572,12 @@ int bpf_map__pin(struct bpf_map *map, const char *path)return libbpf_err (-EINVAL ); } +if (map->fd <0 ) { +pr_warn ("map '%s': can't pin BPF map without FD (was it created?)\n" , +bpf_map__name (map)); +return libbpf_err (-EINVAL ); + } +if (map->pin_path) {if (path &&strcmp (path, map->pin_path)) { pr_warn("map '%s' already has pin path '%s' different from '%s'\n",@@ -10316,6 +10322,11 @@ static int validate_map_op(const struct bpf_map *map, size_t key_sz,return -EINVAL ; } +if (map->fd <0 ) { +pr_warn ("map '%s': can't use BPF map without FD (was it created?)\n" , map->name); +return -EINVAL ; + } +if (!check_value_sz)return 0 ;
tools/lib/bpf/libbpf.c long libbpf_get_error(const void *ptr) · line 10445
Quoted 8 lines
tools/lib/bpf/libbpf.c +3
-1@@ -10428,8 +10439,15 @@ long libbpf_get_error(const void *ptr)int bpf_link__update_program (struct bpf_link *link,struct bpf_program *prog) {int ret; +int prog_fd =bpf_program__fd (prog); - ret =bpf_link_update (bpf_link__fd (link),bpf_program__fd (prog),NULL ); +if (prog_fd <0 ) { + pr_warn("prog '%s': can't use BPF program without FD (was it created?)\n",
maps are created, programs are loaded, so "was it loaded?"?
Quoted 9 lines
tools/lib/bpf/libbpf.c +5
-0+ prog->name); +return libbpf_err (-EINVAL ); + } + + ret =bpf_link_update (bpf_link__fd (link), prog_fd,NULL );return libbpf_err_errno (ret); }
tools/lib/bpf/libbpf.c bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, · line 11370
Quoted 6 lines
tools/lib/bpf/libbpf.c +3
-0@@ -11347,6 +11365,13 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,if (!OPTS_VALID (opts, bpf_kprobe_multi_opts))return libbpf_err_ptr (-EINVAL ); + prog_fd =bpf_program__fd (prog); +if (prog_fd <0 ) { + pr_warn("prog '%s': can't attach BPF program without FD (was it created?)\n",
ditto
Quoted 16 lines
tools/lib/bpf/libbpf.c +4
-1+ prog->name); +return libbpf_err_ptr (-EINVAL ); + } + syms =OPTS_GET (opts, syms, false); addrs =OPTS_GET (opts, addrs, false); cnt =OPTS_GET (opts, cnt, false);@@ -11387,7 +11412,6 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, } link->detach = &bpf_link__detach_fd; - prog_fd =bpf_program__fd (prog); link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_KPROBE_MULTI , &lopts);if (link_fd <0 ) { err = -errno;
tools/lib/bpf/libbpf.c bpf_program__attach_uprobe_multi(const struct bpf_program *prog, · line 11799
Quoted 6 lines
tools/lib/bpf/libbpf.c +3
-0@@ -11770,6 +11794,13 @@ bpf_program__attach_uprobe_multi(const struct bpf_program *prog,if (!OPTS_VALID (opts, bpf_uprobe_multi_opts))return libbpf_err_ptr (-EINVAL ); + prog_fd =bpf_program__fd (prog); +if (prog_fd <0 ) { + pr_warn("prog '%s': can't attach BPF program without FD (was it created?)\n",
loaded
Quoted 16 lines
tools/lib/bpf/libbpf.c +4
-1+ prog->name); +return libbpf_err_ptr (-EINVAL ); + } + syms =OPTS_GET (opts, syms,NULL ); offsets =OPTS_GET (opts, offsets,NULL ); ref_ctr_offsets =OPTS_GET (opts, ref_ctr_offsets,NULL );@@ -11845,7 +11876,6 @@ bpf_program__attach_uprobe_multi(const struct bpf_program *prog, } link->detach = &bpf_link__detach_fd; - prog_fd =bpf_program__fd (prog); link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &lopts);if (link_fd <0 ) { err = -errno;
tools/lib/bpf/libbpf.c struct bpf_link *bpf_program__attach(const struct bpf_program *prog) · line 12705
Quoted 5 lines
tools/lib/bpf/libbpf.c +2
-0@@ -12671,6 +12701,12 @@ struct bpf_link *bpf_program__attach(const struct bpf_program *prog)if (!prog->sec_def || !prog->sec_def->prog_attach_fn)return libbpf_err_ptr (-EOPNOTSUPP ); +if (bpf_program__fd (prog) <0 ) { + pr_warn("prog '%s': can't attach BPF program w/o FD (did you load it?)\n",
I normalized w/o to without
Quoted 8 lines
tools/lib/bpf/libbpf.c +4
-0+ prog->name); +return libbpf_err_ptr (-EINVAL ); + } + err = prog->sec_def->prog_attach_fn (prog, prog->sec_def->cookie, &link);if (err)return libbpf_err_ptr (err);
tools/lib/bpf/libbpf.c struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map) · line 12754
Quoted 9 lines
tools/lib/bpf/libbpf.c +3
-1@@ -12711,9 +12747,14 @@ struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)__u32 zero =0 ;int err, fd; -if (!bpf_map__is_struct_ops (map) || map->fd ==-1 ) +if (!bpf_map__is_struct_ops (map))return libbpf_err_ptr (-EINVAL ); +if (map->fd <0 ) { +pr_warn ("map '%s': can't attach BPF map w/o FD (did you load it?)\n" , map->name);
without, "was it created?"
Quoted 7 lines
tools/lib/bpf/libbpf.c +3
-0+return libbpf_err_ptr (-EINVAL ); + } + link =calloc (1 ,sizeof (*link));if (!link)return libbpf_err_ptr (-EINVAL );
tools/lib/bpf/libbpf.c int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map) · line 12808
Quoted 9 lines
tools/lib/bpf/libbpf.c +5
-1@@ -12760,8 +12801,13 @@ int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map)__u32 zero =0 ;int err; -if (!bpf_map__is_struct_ops (map) || !map_is_created (map)) +if (!bpf_map__is_struct_ops (map)) +return -EINVAL ; + +if (map->fd <0 ) { +pr_warn ("map '%s': can't use BPF map w/o FD (did you load it?)\n" , map->name);
same about create vs load
Quoted 8 lines
tools/lib/bpf/libbpf.c +1
-1return -EINVAL ; + } st_ops_link = container_of(link, struct bpf_link_struct_ops, link);/* Ensure the type of a link is correct */ --2.42.0
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 9 lines · On Mon, 18 Mar 2024 13:18:08 +0000 you wrote:
From: Mykyta Yatsenko <[email protected]>
libbpf creates bpf_program/bpf_map structs for each program/map that user defines, but it allows to disable creating/loading those objects in kernel, in that case they won't have associated file descriptor (fd < 0). Such functionality is used for backward compatibility with some older kernels.
[...]
Here is the summary with links:
- [bpf-next] bpf: check bpf_map/bpf_program fd validity
https://git.kernel.org/bpf/bpf-next/c/7b30c296af65
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html