Skip to content

Commit b69d441

Browse files
Martin KaFai LauAlexei Starovoitov
authored andcommitted
bpftool: Fix cgroup command to only show cgroup bpf programs
The netkit program is not a cgroup bpf program and should not be shown in the output of the "bpftool cgroup show" command. However, if the netkit device happens to have ifindex 3, the "bpftool cgroup show" command will output the netkit bpf program as well: > ip -d link show dev nk1 3: nk1@if2: ... link/ether ... netkit mode ... > bpftool net show tc: nk1(3) netkit/peer tw_ns_nk2phy prog_id 469447 > bpftool cgroup show /sys/fs/cgroup/... ID AttachType AttachFlags Name ... ... ... 469447 netkit_peer tw_ns_nk2phy The reason is that the target_fd (which is the cgroup_fd here) and the target_ifindex are in a union in the uapi/linux/bpf.h. The bpftool iterates all values in "enum bpf_attach_type" which includes non cgroup attach types like netkit. The cgroup_fd is usually 3 here, so the bug is triggered when the netkit ifindex just happens to be 3 as well. The bpftool's cgroup.c already has a list of cgroup-only attach type defined in "cgroup_attach_types[]". This patch fixes it by iterating over "cgroup_attach_types[]" instead of "__MAX_BPF_ATTACH_TYPE". Cc: Quentin Monnet <[email protected]> Reported-by: Takshak Chahande <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 43745d1 commit b69d441

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

tools/bpf/bpftool/cgroup.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,11 @@ static int show_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
318318

319319
static int do_show(int argc, char **argv)
320320
{
321-
enum bpf_attach_type type;
322321
int has_attached_progs;
323322
const char *path;
324323
int cgroup_fd;
325324
int ret = -1;
325+
unsigned int i;
326326

327327
query_flags = 0;
328328

@@ -370,14 +370,14 @@ static int do_show(int argc, char **argv)
370370
"AttachFlags", "Name");
371371

372372
btf_vmlinux = libbpf_find_kernel_btf();
373-
for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
373+
for (i = 0; i < ARRAY_SIZE(cgroup_attach_types); i++) {
374374
/*
375375
* Not all attach types may be supported, so it's expected,
376376
* that some requests will fail.
377377
* If we were able to get the show for at least one
378378
* attach type, let's return 0.
379379
*/
380-
if (show_bpf_progs(cgroup_fd, type, 0) == 0)
380+
if (show_bpf_progs(cgroup_fd, cgroup_attach_types[i], 0) == 0)
381381
ret = 0;
382382
}
383383

@@ -400,9 +400,9 @@ static int do_show(int argc, char **argv)
400400
static int do_show_tree_fn(const char *fpath, const struct stat *sb,
401401
int typeflag, struct FTW *ftw)
402402
{
403-
enum bpf_attach_type type;
404403
int has_attached_progs;
405404
int cgroup_fd;
405+
unsigned int i;
406406

407407
if (typeflag != FTW_D)
408408
return 0;
@@ -434,8 +434,8 @@ static int do_show_tree_fn(const char *fpath, const struct stat *sb,
434434
}
435435

436436
btf_vmlinux = libbpf_find_kernel_btf();
437-
for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++)
438-
show_bpf_progs(cgroup_fd, type, ftw->level);
437+
for (i = 0; i < ARRAY_SIZE(cgroup_attach_types); i++)
438+
show_bpf_progs(cgroup_fd, cgroup_attach_types[i], ftw->level);
439439

440440
if (errno == EINVAL)
441441
/* Last attach type does not support query.

0 commit comments

Comments
 (0)