BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next] bpftool: Fix readlink usage in get_fd_type
@ 2025-01-29  7:18 Viktor Malik
  2025-01-29 23:15 ` Quentin Monnet
  2025-01-30  1:00 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 3+ messages in thread
From: Viktor Malik @ 2025-01-29  7:18 UTC (permalink / raw)
  To: bpf
  Cc: Quentin Monnet, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
	Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev,
	Hao Luo, Jiri Olsa, Viktor Malik

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 <vmalik@redhat.com>
---
 tools/bpf/bpftool/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c
index 9b75639434b8..0a764426d935 100644
--- a/tools/bpf/bpftool/common.c
+++ b/tools/bpf/bpftool/common.c
@@ -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;
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] bpftool: Fix readlink usage in get_fd_type
  2025-01-29  7:18 [PATCH bpf-next] bpftool: Fix readlink usage in get_fd_type Viktor Malik
@ 2025-01-29 23:15 ` Quentin Monnet
  2025-01-30  1:00 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: Quentin Monnet @ 2025-01-29 23:15 UTC (permalink / raw)
  To: Viktor Malik, bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa

2025-01-29 08:18 UTC+0100 ~ Viktor Malik <vmalik@redhat.com>
> 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 <vmalik@redhat.com>

Nice, thank you!

Fixes: 71bb428fe2c1 ("tools: bpf: add bpftool")
Reviewed-by: Quentin Monnet <qmo@kernel.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] bpftool: Fix readlink usage in get_fd_type
  2025-01-29  7:18 [PATCH bpf-next] bpftool: Fix readlink usage in get_fd_type Viktor Malik
  2025-01-29 23:15 ` Quentin Monnet
@ 2025-01-30  1:00 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-01-30  1:00 UTC (permalink / raw)
  To: Viktor Malik
  Cc: bpf, qmo, ast, daniel, andrii, martin.lau, eddyz87, song,
	yonghong.song, john.fastabend, kpsingh, sdf, haoluo, jolsa

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

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!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-01-30  1:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-29  7:18 [PATCH bpf-next] bpftool: Fix readlink usage in get_fd_type Viktor Malik
2025-01-29 23:15 ` Quentin Monnet
2025-01-30  1:00 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox