git: 78a69f98b104 - stable/14 - top: fix off-by-one in screenbuf
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 19 Nov 2026 17:10:15 UTC
The branch stable/14 has been updated by novel:
URL: https://cgit.FreeBSD.org/src/commit/?id=78a69f98b10425c946fa67ed4b916c78e33d6852
commit 78a69f98b10425c946fa67ed4b916c78e33d6852
Author: Roman Bogorodskiy <novel@FreeBSD.org>
AuthorDate: 2026-11-15 09:19:44 +0000
Commit: Roman Bogorodskiy <novel@FreeBSD.org>
CommitDate: 2026-11-19 17:08:05 +0000
top: fix off-by-one in screenbuf
There is a heap overflow for screenbuf originating from
the top import to the tree. The i_process()/u_process() functions
write a terminating NUL one byte past each screenbuf line slot
when the line fills the slot, which is a 1-byte heap overflow on the last slot.
The display_resize() function allocates screen_width bytes per line.
i_process() and u_process() cut lines to screen_width characters and
then stpcpy() them into the slot, which writes screen_width + 1 bytes.
line_update() can do the same with *old = ch. For inner slots,
the NUL lands in byte 0 of the next slot. This can corrupt the saved
contents used when updating the next line. For the last slot, it writes
one byte past the end of screenbuf.
Fix by allocating (screen_width + 1) bytes per line and updating the
line offsets to fit the terminating NUL.
Reviewed by: kib
Reported by: llm-postcr
MFC after: 3 days
Differential Revision: https://reviews.freebsd.org/D61593
(cherry picked from commit 5233aaccdce59abfb80312cdbe94495516fd211f)
---
usr.bin/top/display.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/usr.bin/top/display.c b/usr.bin/top/display.c
index 32b8b12798dc..85105853a169 100644
--- a/usr.bin/top/display.c
+++ b/usr.bin/top/display.c
@@ -57,7 +57,7 @@ static int lmpid = 0;
static int last_hi = 0; /* used in u_process and u_endscreen */
static int lastline = 0;
-#define lineindex(l) ((l)*screen_width)
+#define lineindex(l) ((l)*(screen_width + 1))
/* things initialized by display_init and used thruout */
@@ -140,8 +140,8 @@ display_resize(void)
if (lines < 0)
lines = 0;
- /* now, allocate space for the screen buffer */
- screenbuf = calloc(lines, screen_width);
+ /* allocate space for each line, including its terminating NUL */
+ screenbuf = calloc(lines, screen_width + 1);
if (screenbuf == NULL)
{
/* oops! */