perf trace-event: Fix buffer overflow in read_string()
PATCH 1/1 perf trace-event: Fix buffer overflow in read_string()
read_string() writes into buf[BUFSIZ] one byte at a time without checking 'size' against the buffer bound before each write. A string longer than BUFSIZ in the input overflows the stack buffer.
Add a bounds check before each write to prevent overflow. On overflow the function returns NULL, matching its other error paths.
--- tools/perf/util/trace-event-read.c | 5 +++++ 1 file changed, 5 insertions(+)
tools/perf/util/trace-event-read.c +5 -0
@@ -127,6 +127,11 @@ static char *read_string(void) } } +if (size >= (int )sizeof (buf) -1 ) { +pr_debug ("string too long (max %zu bytes)" ,sizeof (buf) -1 ); + goto out; + } + buf[size++] = c;if (!c)
Signature · 1 line
2.47.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but integer truncation when passing a 64-bit size to a 32-bit do_read parameter leads to out-of-bounds reads or massive uninitialized memory parsing. --
--- Patch [1]: [PATCH] perf trace-event: Fix buffer overflow in read_string() --- commit fd2c84f48dc9adf4455bf50d97942d8f87743ab3
Author: Tanushree Shah <[email protected]>
perf trace-event: Fix buffer overflow in read_string()
This patch adds a bounds check to read_string() before writing to the buffer. It ensures that the size does not exceed the bounds of buf, preventing a buffer overflow when processing trace events.
Quoted 4 lines
tools/perf/util/trace-event-read.c +0
-0
[ ... ]
tools/perf/util/trace-event-read.c static char *read_string(void) · line 137
Quoted 12 lines
tools/perf/util/trace-event-read.c +5
-0@@ -127,6 +127,11 @@ static char *read_string(void) } } +if (size >= (int )sizeof (buf) -1 ) { +pr_debug ("string too long (max %zu bytes)" ,sizeof (buf) -1 ); + goto out; + } + buf[size++] = c;if (!c)
[Severity: High]
This isn't a bug introduced by this patch, but integer truncation when passing a 64-bit size to a 32-bit do_read() parameter can lead to out-of-bounds reads.
In tools/perf/util/trace-event-read.c, do_read() expects a 32-bit integer:
static int do_read(void *data, int size)
{
...
}However, read_ftrace_file() passes an unsigned long long size parameter into it:
static int read_ftrace_file(struct tep_handle *pevent, unsigned long long size)
{
...
buf = malloc(size);
if (buf == NULL) {
...
}
ret = do_read(buf, size);
if (ret < 0) {
...
}
ret = parse_ftrace_file(pevent, buf, size);
...
}If a maliciously crafted trace file provides a size like 0x100000005, malloc(size) on 32-bit architectures truncates and allocates only 5 bytes. The size parameter in parse_ftrace_file() remains the full 64-bit value, which would result in a massive out-of-bounds read.
On 64-bit architectures, do_read() truncates the size and reads only 5 bytes, causing parse_ftrace_file() to process up to 4GB of mostly uninitialized memory.
Does this code need to update do_read() to use size_t or ssize_t to properly handle the size parameter?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Thanks for the review. Yes, this is a pre-existing issue. I will address this and send a V2 patch series.
Quoted 77 lines · On 22/07/26 17:20, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but integer truncation when passing a 64-bit size to a 32-bitdo_readparameter leads to out-of-bounds reads or massive uninitialized memory parsing. ----- Patch [1]: [PATCH] perf trace-event: Fix buffer overflow in
read_string()--- commitfd2c84f48dc9adf4455bf50d97942d8f87743ab3
Author: Tanushree Shah <[email protected]>perf trace-event: Fix buffer overflow in
read_string()This patch adds a bounds check to
read_string()before writing to the buffer. It ensures that the size does not exceed the bounds of buf, preventing a buffer overflow when processing trace events.Quoted 4 lines
tools/perf/util/trace-event-read.c +0
-0[ ... ]
tools/perf/util/trace-event-read.c static char *read_string(void) · line 137
Quoted 12 lines
tools/perf/util/trace-event-read.c +5
-0@@ -127,6 +127,11 @@ static char *read_string(void) } } +if (size >= (int )sizeof (buf) -1 ) { +pr_debug ("string too long (max %zu bytes)" ,sizeof (buf) -1 ); + goto out; + } + buf[size++] = c;if (!c)[Severity: High]
This isn't a bug introduced by this patch, but integer truncation when passing a 64-bit size to a 32-bitdo_read()parameter can lead to out-of-bounds reads.In
tools/perf/util/trace-event-read.c,do_read()expects a 32-bit integer:static int
do_read(void *data, int size){ ... }However,
read_ftrace_file()passes an unsigned long long size parameter into it:static int
read_ftrace_file(struct tep_handle *pevent, unsigned long long size){ ... buf = malloc(size); if (buf == NULL) { ... } ret = do_read(buf, size); if (ret < 0) { ... } ret = parse_ftrace_file(pevent, buf, size); ... }If a maliciously crafted trace file provides a size like
0x100000005, malloc(size) on 32-bit architectures truncates and allocates only 5 bytes. The size parameter inparse_ftrace_file()remains the full 64-bit value, which would result in a massive out-of-bounds read.On 64-bit architectures,
do_read()truncates the size and reads only 5 bytes, causingparse_ftrace_file()to process up to 4GB of mostly uninitialized memory.Does this code need to update
do_read()to usesize_torssize_tto properly handle the size parameter?