perf header: Ensure read strings are '\0' terminated
Ian Rogers
reviewerPATCH 1/1 perf header: Ensure read strings are '\0' terminated
Sashiko reviews were complaining do_read_string didn't necessarily ensure strings were correctly terminated. Add checking for this and if a string isn't correctly terminated return NULL.
--- tools/perf/util/header.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/util/header.c +7 -1
@@ -269,6 +269,9 @@ static char *do_read_string(struct feat_fd *ff)if (do_read_u32 (ff, &len))return NULL ; +if (len ==0 ) +return NULL ; + buf =malloc (len);if (!buf)return NULL ;
@@ -279,7 +282,10 @@ static char *do_read_string(struct feat_fd *ff)* thus the actual strlen of buf * may be less than len */ -return buf; +for (int i = (int )len -1 ; i >=0 ; i--) { +if (buf[i] =='\0' ) +return buf; + } }free (buf);
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
Quoted 24 lines · On Tue, 14 Apr 2026 13:57:25 -0700 Ian Rogers <[email protected]> wrote:
Sashiko reviews were complaining
do_read_stringdidn't necessarily ensure strings were correctly terminated. Add checking for this and if a string isn't correctly terminated return NULL.Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/header.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)tools/perf/util/header.c +3
-0@@ -269,6 +269,9 @@ static char *do_read_string(struct feat_fd *ff)if (do_read_u32 (ff, &len))return NULL ; +if (len ==0 ) +return NULL ; + buf =malloc (len);if (!buf)return NULL ;
tools/perf/util/header.c static char *do_read_string(struct feat_fd *ff) · line 288
Quoted 8 lines
tools/perf/util/header.c +4
-1@@ -279,7 +282,10 @@ static char *do_read_string(struct feat_fd *ff)* thus the actual strlen of buf * may be less than len */ -return buf; +for (int i = (int )len -1 ; i >=0 ; i--) { +if (buf[i] =='\0' ) +return buf; + }
Would be a lot simpler to malloc(len + 1) and set buf[len] = 0.
david
Quoted 3 lines
tools/perf/util/header.c +0
-0}free (buf);
[email protected]
AI reviewSashiko AI review found 1 potential issue(s): - [Medium] Casting the unsigned 32-bit length to a signed `int` causes integer underflow and logic failures for large lengths. --
Sashiko AI review found 1 potential issue(s):
- [Medium] Casting the unsigned 32-bit length to a signed int causes integer underflow and logic failures for large lengths. --
commit 684fd164d75fdb623faa938c77daf911d959b273
Author: Ian Rogers <[email protected]>
perf header: Ensure read strings are '\0' terminated
This commit adds a check to do_read_string() to ensure that strings read from perf data are correctly '\0' terminated. If the string is not correctly terminated or if the length is zero, it returns NULL.
Quoted 3 lines
tools/perf/util/header.c +0
-0
[ ... ]
tools/perf/util/header.c static char *do_read_string(struct feat_fd *ff) · line 275
Quoted 6 lines
tools/perf/util/header.c +1
-1@@ -269,7 +272,10 @@ static char *do_read_string(struct feat_fd *ff)* thus the actual strlen of buf * may be less than len */ -return buf; +for (int i = (int )len -1 ; i >=0 ; i--) {
Could this explicit cast of the unsigned 32-bit length to a signed int cause integer underflow and logic failures for large lengths?
Since do_read_u32() reads the 32-bit unsigned integer len directly from the perf data file, a length greater than 2GB (e.g., 0x80000001) would result in a negative number when cast to int.
Would this cause the loop condition i >= 0 to evaluate to false immediately, skipping the null-terminator check and rejecting the string by returning NULL?
Additionally, if len is exactly 0x80000000, would subtracting 1 cause signed integer underflow?
Quoted 6 lines
tools/perf/util/header.c +3
-0+if (buf[i] =='\0' ) +return buf; + } }free (buf);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 3 lines · On Tue, 14 Apr 2026 13:57:25 -0700 Ian Rogers <[email protected]> wrote:
Sashiko reviews were complaining
do_read_stringdidn't necessarily ensure strings were correctly terminated. Add checking for this and if a string isn't correctly terminated return NULL.
Actually I'd be just as worried about corrupt data giving insane lengths (eg if it processes some ascii text).
For that you want a better error than just 'return NULL'.
David
Quoted 32 lines
Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/header.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)tools/perf/util/header.c +7
-1@@ -269,6 +269,9 @@ static char *do_read_string(struct feat_fd *ff)if (do_read_u32 (ff, &len))return NULL ; +if (len ==0 ) +return NULL ; + buf =malloc (len);if (!buf)return NULL ;@@ -279,7 +282,10 @@ static char *do_read_string(struct feat_fd *ff)* thus the actual strlen of buf * may be less than len */ -return buf; +for (int i = (int )len -1 ; i >=0 ; i--) { +if (buf[i] =='\0' ) +return buf; + } }free (buf);