Commit 0053f7d3 authored by Viktor Malik's avatar Viktor Malik Committed by Alexei Starovoitov
Browse files

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.

Signed-off-by: default avatarViktor Malik <vmalik@redhat.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Reviewed-by: default avatarQuentin Monnet <qmo@kernel.org>
Link: https://lore.kernel.org/bpf/20250129071857.75182-1-vmalik@redhat.com


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent cb3ade56
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -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;