Skip to contentSkip to conversation

perf header: Validate build_id filename length to prevent buffer overflow

PATCH Superseded SeungJu Cheon 2 participants 2 messages Feb 6 – Apr 1 perf events

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf header: Validate build_id filename length to prevent buffer overflow

The build_id parsing functions calculate a filename length from the event header size and read directly into a stack buffer of PATH_MAX bytes without bounds checking. A malformed perf.data file with a crafted header.size can cause the length to be negative or exceed PATH_MAX, resulting in a stack buffer overflow.

Add bounds checking for the filename length in both perf_header__read_build_ids() and the ABI quirk variant.

---
 tools/perf/util/header.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/perf/util/header.c +6 -0
index f5cad377c99e..208ccaa62399 100644
@@ -2335,6 +2335,9 @@ static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
 			perf_event_header__bswap(&old_bev.header);
 
 		len = old_bev.header.size - sizeof(old_bev);
+		if (len < 0 || len >= PATH_MAX)
+			return -1;
+
 		if (readn(input, filename, len) != len)
 			return -1;
 
@@ -2377,6 +2380,9 @@ static int perf_header__read_build_ids(struct perf_header *header,
 			perf_event_header__bswap(&bev.header);
 
 		len = bev.header.size - sizeof(bev);
+		if (len < 0 || len >= PATH_MAX)
+			goto out;
+
 		if (readn(input, filename, len) != len)
 			goto out;
 		/*
Signature · 1 line
2.52.0
reviewer
#1 lore raw
Quoted 9 lines · SeungJu Cheon

The build_id parsing functions calculate a filename length from the event header size and read directly into a stack buffer of PATH_MAX bytes without bounds checking. A malformed perf.data file with a crafted header.size can cause the length to be negative or exceed PATH_MAX, resulting in a stack buffer overflow.

Add bounds checking for the filename length in both perf_header__read_build_ids() and the ABI quirk variant.

Lgtm, could we print errors when these conditions occur?

Thanks,
Ian

Quoted 32 lines
---
 tools/perf/util/header.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/perf/util/header.c +6 -1
index f5cad377c99e..208ccaa62399 100644
@@ -2335,6 +2335,9 @@ static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
                        perf_event_header__bswap(&old_bev.header);
 
                len = old_bev.header.size - sizeof(old_bev);
+               if (len < 0 || len >= PATH_MAX)
+                       return -1;
+
                if (readn(input, filename, len) != len)
                        return -1;
 
@@ -2377,6 +2380,9 @@ static int perf_header__read_build_ids(struct perf_header *header,
                        perf_event_header__bswap(&bev.header);
 
                len = bev.header.size - sizeof(bev);
+               if (len < 0 || len >= PATH_MAX)
+                       goto out;
+
                if (readn(input, filename, len) != len)
                        goto out;
                /*
--

2.52.0