Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpftool: Remove BPF_OBJ_NAME_LEN restriction when looking up bpf program by name

bpftool was limiting the length of names to BPF_OBJ_NAME_LEN in prog_parse
fds.

Since commit b662000aff84 ("bpftool: Adding support for BTF program names")
we can get the full program name from BTF.

This patch removes the restriction of name length when running `bpftool
prog show name ${name}`.

Test:
Tested against some internal program names that were longer than
`BPF_OBJ_NAME_LEN`, here a redacted example of what was ran to test.

# previous behaviour
$ sudo bpftool prog show name some_long_program_name
Error: can't parse name
# with the patch
$ sudo ./bpftool prog show name some_long_program_name
123456789: tracing name some_long_program_name tag taghexa gpl ....
...
...
...
# too long
sudo ./bpftool prog show name $(python3 -c 'print("A"*128)')
Error: can't parse name
# not too long but no match
$ sudo ./bpftool prog show name $(python3 -c 'print("A"*127)')

Signed-off-by: Manu Bretelle <chantr4@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Tested-by: Jiri Olsa <jolsa@kernel.org>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20220801132409.4147849-1-chantr4@gmail.com

authored by

Manu Bretelle and committed by
Andrii Nakryiko
d55dfe58 3045f42a

+12 -3
+12 -3
tools/bpf/bpftool/common.c
··· 722 722 723 723 static int prog_fd_by_nametag(void *nametag, int **fds, bool tag) 724 724 { 725 + char prog_name[MAX_PROG_FULL_NAME]; 725 726 unsigned int id = 0; 726 727 int fd, nb_fds = 0; 727 728 void *tmp; ··· 755 754 goto err_close_fd; 756 755 } 757 756 758 - if ((tag && memcmp(nametag, info.tag, BPF_TAG_SIZE)) || 759 - (!tag && strncmp(nametag, info.name, BPF_OBJ_NAME_LEN))) { 757 + if (tag && memcmp(nametag, info.tag, BPF_TAG_SIZE)) { 760 758 close(fd); 761 759 continue; 760 + } 761 + 762 + if (!tag) { 763 + get_prog_full_name(&info, fd, prog_name, 764 + sizeof(prog_name)); 765 + if (strncmp(nametag, prog_name, sizeof(prog_name))) { 766 + close(fd); 767 + continue; 768 + } 762 769 } 763 770 764 771 if (nb_fds > 0) { ··· 829 820 NEXT_ARGP(); 830 821 831 822 name = **argv; 832 - if (strlen(name) > BPF_OBJ_NAME_LEN - 1) { 823 + if (strlen(name) > MAX_PROG_FULL_NAME - 1) { 833 824 p_err("can't parse name"); 834 825 return -1; 835 826 }