tools/bpf/bpftool: Reset vmlinux BTF after map dump
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/2 tools/bpf/bpftool: Reset vmlinux BTF after map dump
map dump frees the global btf_vmlinux object.
In batch mode, a later map dump can reuse the stale pointer.
Reset the global pointer after freeing it.
--- tools/bpf/bpftool/map.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/bpf/bpftool/map.c +7 -1
@@ -790,6 +790,12 @@ static int maps_have_btf(int *fds, int nb_fds)static struct btf *btf_vmlinux; +static void free_btf_vmlinux (void ) +{ +btf__free (btf_vmlinux); + btf_vmlinux =NULL ; +} +static int get_map_kv_btf (const struct bpf_map_info *info,struct btf **btf) {int err =0 ;
@@ -958,7 +964,7 @@ static int do_dump(int argc, char **argv)close (fds[i]); exit_free:free (fds); -btf__free (btf_vmlinux); +free_btf_vmlinux ();return err; }
Signature · 1 line
2.51.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Incomplete Fix: print_key_value() frees the global btf_vmlinux object but leaves the pointer dangling, leading to a Use-After-Free in batch mode.
- [High] Severe memory leak in cgroup tree traversal and batch execution.
--
commit c154c8297093a9827de6d90793d321cdcca31123
Author: Yichong Chen <[email protected]>
tools/bpf/bpftool: Reset vmlinux BTF after map dump
map dump frees the global btf_vmlinux object.
In batch mode, a later map dump can reuse the stale pointer.
Reset the global pointer after freeing it.
Quoted 17 lines
tools/bpf/bpftool/map.c +6
-0@@ -790,6 +790,12 @@ static int maps_have_btf(int *fds, int nb_fds)static struct btf *btf_vmlinux; +static void free_btf_vmlinux (void ) +{ +btf__free (btf_vmlinux); + btf_vmlinux =NULL ; +} +static int get_map_kv_btf (const struct bpf_map_info *info,struct btf **btf) {int err =0 ;
[ ... ]
tools/bpf/bpftool/map.c static int do_dump(int argc, char **argv) · line 969
Quoted 8 lines
tools/bpf/bpftool/map.c +1
-1@@ -958,7 +964,7 @@ static int do_dump(int argc, char **argv)close (fds[i]); exit_free:free (fds); -btf__free (btf_vmlinux); +free_btf_vmlinux ();return err; }
[Severity: Critical]
This is a pre-existing issue, but does this commit leave an incomplete fix?
Looking at print_key_value() in map.c, it fetches the BTF object using get_map_kv_btf(), which aliases the local btf pointer to the global btf_vmlinux cache:
static int get_map_kv_btf(...) {
...
*btf = btf_vmlinux;
...
}print_key_value() then unconditionally frees it:
static void print_key_value(...) {
...
btf__free(btf);
}Does this destroy the global BTF object but leave the static btf_vmlinux pointer non-NULL? In batch mode, if a subsequent command executes, could get_map_kv_btf() return this dangling pointer and trigger a use-after-free when accessing map data?
[Severity: High]
This isn't a bug introduced by this patch, but while reviewing the use of btf_vmlinux, does this code leak the BTF object in cgroup.c?
In do_show_tree_fn(), which is invoked by nftw() for every directory in the cgroup tree:
static int do_show_tree_fn(...) {
...
btf_vmlinux = libbpf_find_kernel_btf();
for (i = 0; i < ARRAY_SIZE(cgroup_attach_types); i++)
show_bpf_progs(cgroup_fd, cgroup_attach_types[i], ftw->level);
...
}Could this unconditionally overwrite the static btf_vmlinux variable without freeing the previous allocation? On systems with many cgroups, wouldn't traversing the tree leak memory and potentially trigger the OOM killer?
Does do_show() in cgroup.c leak the BTF object across batch commands in the same way, since it also fails to free the allocation?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 2/2 tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands
In reply to a message not in the archivestruct_ops frees the global btf_vmlinux object.
In batch mode, a later struct_ops command can reuse stale state.
Reset the BTF pointer and cached map info state.
--- tools/bpf/bpftool/struct_ops.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
tools/bpf/bpftool/struct_ops.c +10 -1
@@ -21,6 +21,15 @@ static __u32 map_info_alloc_len;static struct btf *btf_vmlinux;static __s32 map_info_type_id; +static void free_btf_vmlinux (void ) +{ +btf__free (btf_vmlinux); + btf_vmlinux =NULL ; + map_info_type =NULL ; + map_info_alloc_len =0 ; + map_info_type_id =0 ; +} +struct res {unsigned int nr_maps;unsigned int nr_errs;
@@ -642,7 +651,7 @@ int do_struct_ops(int argc, char **argv) err =cmd_select (cmds, argc, argv, do_help); -btf__free (btf_vmlinux); +free_btf_vmlinux ();return err; }
Signature · 1 line
2.51.0
[email protected]
AI reviewRe: [PATCH 2/2] tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Use-After-Free of btf_vmlinux in map.c during batch execution. --
commit 39dc823c4a9209628a9e626b6a80fe537d15b79e
Author: Yichong Chen <[email protected]>
tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands
This commit fixes a stale state reuse issue in batch mode by correctly resetting the BTF pointer and cached map info state after struct_ops commands.
Quoted 4 lines
tools/bpf/bpftool/struct_ops.c +0
-0
[ ... ]
tools/bpf/bpftool/struct_ops.c int do_struct_ops(int argc, char **argv) · line 654
Quoted 6 lines
tools/bpf/bpftool/struct_ops.c +1
-1@@ -642,7 +651,7 @@ int do_struct_ops(int argc, char **argv) err =cmd_select (cmds, argc, argv, do_help); -btf__free (btf_vmlinux); +free_btf_vmlinux ();
[Severity: High]
This is a pre-existing issue, but does a similar use-after-free exist in tools/bpf/bpftool/map.c during batch execution?
Looking at map.c, get_map_kv_btf() can return the static btf_vmlinux pointer without taking a reference:
tools/bpf/bpftool/map.c:get_map_kv_btf() {
...
*btf = btf_vmlinux;
}Then print_key_value() frees it unconditionally:
tools/bpf/bpftool/map.c:print_key_value() {
...
btf__free(btf);
}Since it doesn't NULL the static btf_vmlinux pointer (unlike what this patch does for struct_ops.c), could a subsequent map command in batch mode bypass the (!btf_vmlinux) initialization check and reuse the freed pointer?
Quoted 3 lines
tools/bpf/bpftool/struct_ops.c +0
-0return err; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2