Skip to contentSkip to conversation

bpftool: Fix readlink usage in get_fd_type

PATCH Applied Viktor Malik 3 participants 3 messages Jan 29, 2025 – Jan 30, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpftool: Fix readlink usage in get_fd_type

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.

---
 tools/bpf/bpftool/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/bpf/bpftool/common.c +2 -1
index 9b75639434b8..0a764426d935 100644
@@ -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;
Signature · 1 line
2.48.1
maintainer Reviewed-by on v1 1/1
#1 lore raw

2025-01-29 08:18 UTC+0100 ~ Viktor Malik <[email protected]>

Quoted 11 lines

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.

Nice, thank you!

Fixes 71bb428fe2c1 ("tools: bpf: add bpftool")
#2 lore raw
In reply to Viktor Malik
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 9 lines · On Wed, 29 Jan 2025 08:18:57 +0100 you wrote:

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.

[...]

Here is the summary with links:
- [bpf-next] bpftool: Fix readlink usage in get_fd_type

    https://git.kernel.org/bpf/bpf-next/c/57e71f8c8923

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html