tools/bpf/bpftool: fix buffer handling in get_fd_type()
PATCH 1/1 tools/bpf/bpftool: fix buffer handling in get_fd_type()
The current check "if (n == sizeof(buf))" is incorrect for detecting buffer overflow from readlink(). When readlink() fills the entire buffer, it returns sizeof(buf) but does not null-terminate the string, leading to potential buffer overrun in subsequent string operations.
Fix by changing the condition to "n >= sizeof(buf)" to properly detect when the buffer is completely filled, ensuring space is reserved for null termination.
--- tools/bpf/bpftool/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/common.c +1 -1
@@ -464,7 +464,7 @@ int get_fd_type(int fd)p_err ("can't read link type: %s" ,strerror (errno));return -1 ; } -if (n ==sizeof (buf)) { +if (n >=sizeof (buf)) {p_err ("can't read link type: path too long!" );return -1 ; }
Signature · 1 line
2.34.1
Quentin Monnet
maintainerQuoted 19 lines · Kaushlendra Kumar
The current check "if (n == sizeof(buf))" is incorrect for detecting buffer overflow from
readlink(). Whenreadlink()fills the entire buffer, it returns sizeof(buf) but does not null-terminate the string, leading to potential buffer overrun in subsequent string operations.Fix by changing the condition to "n >= sizeof(buf)" to properly detect when the buffer is completely filled, ensuring space is reserved for null termination.
Signed-off-by Kaushlendra Kumar <[email protected]>--- tools/bpf/bpftool/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/bpf/bpftool/common.c +0
-0
tools/bpf/bpftool/common.c int get_fd_type(int fd) · line 470
Quoted 8 lines
tools/bpf/bpftool/common.c +1
-1@@ -464,7 +464,7 @@ int get_fd_type(int fd)p_err ("can't read link type: %s" ,strerror (errno));return -1 ; } -if (n ==sizeof (buf)) { +if (n >=sizeof (buf)) {p_err ("can't read link type: path too long!" );return -1 ; }
Hi and thanks, but I don't understand the change. On success, readlink() returns the number of bytes placed in the buffer, which is at most sizeof(buf) in our case, given that it silently truncates the string if the buffer is too small. So we can never have "n > sizeof(buf)" here? The current code looks correct to me.
Did you actually hit the buffer overflow you describe?
Quentin
Quoted 7 lines
Hi and thanks, but I don't understand the change. On success,
readlink()returns the number of bytes placed in the buffer, which is at most sizeof(buf) in our case, given that it silently truncates the string if the buffer is too small. So we can never have "n > sizeof(buf)" here? The current code looks correct to me.Did you actually hit the buffer overflow you describe?
Hi Quentin,
Thank you for the review. You're absolutely right. I apologize for the confusion in my commit message.
Looking at the code more carefully, I realize the current check is actually correct for the overflow detection. The real issue I was trying to address
is that when n == sizeof(buf), there's no space for null termination. Which is already taken care.
BR,
Kaushlendra