Skip to contentSkip to conversation

Fix buffer overflow in dump_dmesg_structured and dump_record

PATCH Applied litenglong 2 participants 2 messages Nov 12 – Dec 1

Subsystems (2)
lore
Share & tools
lore

litenglong

author
#0 lore raw

PATCH 1/1 Fix buffer overflow in dump_dmesg_structured and dump_record

Both functions accumulate output across multiple dmesg records into a fixed-size stack buffer out_buf[OUT_BUF_SIZE] (4096 bytes) via sprintf with a cumulative offset that is never reset across loop iterations.

The hex-byte escape loops already have a post-write flush guard at each character boundary (len >= OUT_BUF_SIZE - 64), which was present in the original upstream code. However, two write sites in each function lacked any bounds check:

dump_dmesg_structured:
- timestamp prefix sprintf: len += sprintf(out_buf + len, "[%5llu.%06llu] ", ...)
- trailing newline: out_buf[len++] = '\n'

dump_record:
- timestamp prefix sprintf: offset += sprintf(out_buf + offset, "[%5llu.%06llu] ", ...)
- trailing newline at out:: out_buf[offset++] = '\n'

On a long log record, the cumulative offset can approach OUT_BUF_SIZE. Without a guard before these writes, the timestamp sprintf (up to ~30 bytes) or the newline byte can write past out_buf[OUT_BUF_SIZE - 1], causing a stack buffer overflow.

Fix: add a flush-and-reset guard before each of the four unprotected write sites, using the same threshold (OUT_BUF_SIZE - 64) as the existing loop guards. When the buffer is nearly full, the accumulated output is emitted via the handler callback and the offset resets to zero, so subsequent writes start from the beginning of the buffer.

The 64-byte margin is sufficient: the timestamp prefix is at most ~30 bytes, and the newline is a single byte. The loop guards continue to protect the character-by-character escape writes as before.

---
 util_lib/elf_info.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
util_lib/elf_info.c +22 -0
index cfbbea3..17588d7 100644
@@ -888,6 +888,12 @@ static void dump_dmesg_structured(int fd, void (*handler)(char*, unsigned int))
 		imaxdiv_sec = imaxdiv(ts_nsec, 1000000000);
 		imaxdiv_usec = imaxdiv(imaxdiv_sec.rem, 1000);
 
+		if (len >= OUT_BUF_SIZE - 64) {
+			if (handler)
+				handler(out_buf, len);
+			len = 0;
+		}
+
 		len += sprintf(out_buf + len, "[%5llu.%06llu] ",
 			(long long unsigned int)imaxdiv_sec.quot,
 			(long long unsigned int)imaxdiv_usec.quot);
@@ -922,6 +928,11 @@ static void dump_dmesg_structured(int fd, void (*handler)(char*, unsigned int))
 			}
 		}
 
+		if (len >= OUT_BUF_SIZE - 1) {
+			if (handler)
+				handler(out_buf, len);
+			len = 0;
+		}
 		out_buf[len++] = '\n';
 		free(msg);
 		/*
@@ -1124,6 +1135,12 @@ static void dump_record(int fd, struct prb_map *m, unsigned long id,
 	imaxdiv_sec = imaxdiv(ts_nsec, 1000000000);
 	imaxdiv_usec = imaxdiv(imaxdiv_sec.rem, 1000);
 
+	if (offset >= OUT_BUF_SIZE - 64) {
+		if (handler)
+			handler(out_buf, offset);
+		offset = 0;
+	}
+
 	offset += sprintf(out_buf + offset, "[%5llu.%06llu] ",
 		(long long unsigned int)imaxdiv_sec.quot,
 		(long long unsigned int)imaxdiv_usec.quot);
@@ -1169,6 +1186,11 @@ static void dump_record(int fd, struct prb_map *m, unsigned long id,
 		}
 	}
 out:
+	if (offset >= OUT_BUF_SIZE - 1) {
+		if (handler)
+			handler(out_buf, offset);
+		offset = 0;
+	}
 	out_buf[offset++] = '\n';
 
 	if (offset && handler)
Signature · 1 line
2.25.1
Applied
#1 lore raw
Quoted 28 lines · litenglong

Both functions accumulate output across multiple dmesg records into a fixed-size stack buffer out_buf[OUT_BUF_SIZE] (4096 bytes) via sprintf with a cumulative offset that is never reset across loop iterations.

The hex-byte escape loops already have a post-write flush guard at each character boundary (len >= OUT_BUF_SIZE - 64), which was present in the original upstream code. However, two write sites in each function lacked any bounds check:

dump_dmesg_structured:
- timestamp prefix sprintf: len += sprintf(out_buf + len, "[%5llu.%06llu] ", ...)
- trailing newline: out_buf[len++] = '\n'

dump_record:
- timestamp prefix sprintf: offset += sprintf(out_buf + offset, "[%5llu.%06llu] ", ...)
- trailing newline at out:: out_buf[offset++] = '\n'

On a long log record, the cumulative offset can approach OUT_BUF_SIZE. Without a guard before these writes, the timestamp sprintf (up to ~30 bytes) or the newline byte can write past out_buf[OUT_BUF_SIZE - 1], causing a stack buffer overflow.

Fix: add a flush-and-reset guard before each of the four unprotected write sites, using the same threshold (OUT_BUF_SIZE - 64) as the existing loop guards. When the buffer is nearly full, the accumulated output is emitted via the handler callback and the offset resets to zero, so subsequent writes start from the beginning of the buffer.

Quoted 5 lines

The 64-byte margin is sufficient: the timestamp prefix is at most ~30 bytes, and the newline is a single byte. The loop guards continue to protect the character-by-character escape writes as before.

Thanks, applied.

- Fix buffer overflow in dump_dmesg_structured and dump_record
https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=57fc3341e3e0