Skip to content

Commit

Permalink
bpftool: Fix readlink usage in get_fd_type
Browse files Browse the repository at this point in the history
The `readlink(path, buf, sizeof(buf))` call reads at most sizeof(buf)
bytes and *does not* append null-terminator to buf. With respect to
that, fix two pieces in get_fd_type:

1. Change the truncation check to contain sizeof(buf) rather than
   sizeof(path).
2. Append null-terminator to buf.

Reported by Coverity.

Signed-off-by: Viktor Malik <[email protected]>
Signed-off-by: Andrii Nakryiko <[email protected]>
Reviewed-by: Quentin Monnet <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
viktormalik authored and anakryiko committed Jan 30, 2025
1 parent c03320a commit 57e71f8
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tools/bpf/bpftool/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,11 @@ int get_fd_type(int fd)
p_err("can't read link type: %s", strerror(errno));
return -1;
}
if (n == sizeof(path)) {
if (n == sizeof(buf)) {
p_err("can't read link type: path too long!");
return -1;
}
buf[n] = '\0';

if (strstr(buf, "bpf-map"))
return BPF_OBJ_MAP;
Expand Down

0 comments on commit 57e71f8

Please sign in to comment.