All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fix buffer overflow in dump_dmesg_structured and dump_record
@ 2026-11-12 13:10 litenglong
  2026-12-01 20:04 ` Simon Horman
  0 siblings, 1 reply; 2+ messages in thread
From: litenglong @ 2026-11-12 13:10 UTC (permalink / raw)
  To: kexec; +Cc: horms, 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.

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@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



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01 20:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12 13:10 [PATCH] Fix buffer overflow in dump_dmesg_structured and dump_record litenglong
2026-12-01 20:04 ` Simon Horman

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.