Skip to contentSkip to conversation

perf header: Ensure read strings are '\0' terminated

PATCH In review Ian Rogers 3 participants 4 messages Apr 14 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 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
index f30e48eb3fc3..fa4f6d773874 100644
@@ -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
#1 lore raw
Quoted 24 lines · On Tue, 14 Apr 2026 13:57:25 -0700 Ian Rogers <[email protected]> wrote:

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 +3 -0
index f30e48eb3fc3..fa4f6d773874 100644
@@ -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);
#2 lore raw
In reply to Ian Rogers
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. --

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
#3 lore raw
In reply to Ian Rogers
Quoted 3 lines · On Tue, 14 Apr 2026 13:57:25 -0700 Ian Rogers <[email protected]> wrote:

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.

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
---
 tools/perf/util/header.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/util/header.c +7 -1
index f30e48eb3fc3..fa4f6d773874 100644
@@ -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);