coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] cat: fix out-of-bounds write with -n and many lines


From: Collin Funk
Subject: [PATCH] cat: fix out-of-bounds write with -n and many lines
Date: Wed, 18 Nov 2026 19:25:53 -0800

I haven't included a test in this patch, since triggering the bug
takes minutes without ASAN or Valgrind in use. Without those, you are
unlikely to even notice the bug. With them enabled, reproducing it
will be even slower.

-- 8< --

The equation used to calculate the size of this allocation was incorrect
since "the beginning", but an extra page was allocated until
b973d2d44 (maint: simplify memory alignment, 2022-01-27). Therefore, you
couldn't run into the out-of-bounds write until coreutils-9.1.

* src/cat.c (main): Allocate space for another line number.
* NEWS: Mention the bug fix.
---
 NEWS      |  4 ++++
 src/cat.c | 12 ++++++++----
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/NEWS b/NEWS
index d03316785..2e33e7041 100644
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,10 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   'basenc --z85 --ignore-garbage' now correctly ignores NUL bytes.
   [bug introduced with the basenc program in coreutils-8.31]
 
+  'cat -n' with -v, -e, -t, or -A no longer writes beyond the end of its
+  internal buffer when processing more than 10 billion lines.
+  [bug introduced in coreutils-9.1]
+
   'chcon' now emits the entire file name in error messages when setting the
   security context fails instead of the basename.
   [bug introduced with the chcon program in coreutils-6.9.90]
diff --git a/src/cat.c b/src/cat.c
index 1f342cb48..af48c85c8 100644
--- a/src/cat.c
+++ b/src/cat.c
@@ -954,7 +954,7 @@ main (int argc, char **argv)
                                    page_size, insize + 1);
 
           /* Why are
-             (OUTSIZE - 1 + INSIZE * 4 + LINE_COUNTER_BUF_LEN)
+             (OUTSIZE - 1 + INSIZE * 4 + LINE_COUNTER_BUF_LEN * 2)
              bytes allocated for the output buffer?
 
              A test whether output needs to be written is done when the input
@@ -968,8 +968,12 @@ main (int argc, char **argv)
              newline, a line number may be written (according to the given
              options) as the first thing in the output buffer. (Done after the
              new input is read, but before processing of the input begins.)
-             A line number requires seldom more than LINE_COUNTER_BUF_LEN
-             positions.
+
+             If the current buffered input starts with an empty line, its line
+             number, a '$' if -E is in use, and a new line are written, then 
the
+             number of the subsequent line, and then the (INSIZE - 1) remaining
+             buffered characters.  Therefore, we may have two line numbers
+             pending, each requiring (LINE_COUNTER_BUF_LEN - 1) bytes.
 
              Align the output buffer to a page size boundary, for efficiency
              on some paging implementations.  */
@@ -977,7 +981,7 @@ main (int argc, char **argv)
           idx_t bufsize;
           if (ckd_mul (&bufsize, insize, 4)
               || ckd_add (&bufsize, bufsize, outsize)
-              || ckd_add (&bufsize, bufsize, LINE_COUNTER_BUF_LEN - 1))
+              || ckd_add (&bufsize, bufsize, LINE_COUNTER_BUF_LEN * 2 - 1))
             xalloc_die ();
           outbuf = ensure_buf_size (outbuf, &outbuf_alloc,
                                     page_size, bufsize);
-- 
2.56.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]