[PATCH] Fix buffer overflow in dump_dmesg_structured and dump_record

litenglong litenglong at kylinos.cn
Thu Nov 12 05:10:23 PST 2026


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.

Signed-off-by: litenglong <litenglong at kylinos.cn>
---
 util_lib/elf_info.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/util_lib/elf_info.c b/util_lib/elf_info.c
index cfbbea3..17588d7 100644
--- a/util_lib/elf_info.c
+++ b/util_lib/elf_info.c
@@ -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)
-- 
2.25.1




More information about the kexec mailing list