tools/bpftool: Fix PID fetching with a lot of results
authorAndrii Nakryiko <andrii@kernel.org>
Fri, 4 Dec 2020 23:20:01 +0000 (15:20 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Tue, 8 Dec 2020 15:45:20 +0000 (16:45 +0100)
In case of having so many PID results that they don't fit into a singe page
(4096) bytes, bpftool will erroneously conclude that it got corrupted data due
to 4096 not being a multiple of struct pid_iter_entry, so the last entry will
be partially truncated. Fix this by sizing the buffer to fit exactly N entries
with no truncation in the middle of record.

Fixes: d53dee3fe013 ("tools/bpftool: Show info for processes holding BPF map/prog/link/btf FDs")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20201204232002.3589803-1-andrii@kernel.org
tools/bpf/bpftool/pids.c

index df7d8ec..477e55d 100644 (file)
@@ -89,9 +89,9 @@ libbpf_print_none(__maybe_unused enum libbpf_print_level level,
 
 int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type)
 {
-       char buf[4096];
-       struct pid_iter_bpf *skel;
        struct pid_iter_entry *e;
+       char buf[4096 / sizeof(*e) * sizeof(*e)];
+       struct pid_iter_bpf *skel;
        int err, ret, fd = -1, i;
        libbpf_print_fn_t default_print;