* src/tac.c (tac_seekable): Keep track of the offset where the search
for the separator started in G_BUFFER. Use it when setting MATCH_START.
* src/tac.pl ($limits, $INITIAL_READSIZE): New variables.
(@Tests): Add test cases that would previously fail.
* NEWS: Mention the bug fix.
Discovered by Anthropic's OSS Scanner, as bug ANT-2026-W4REGHHY.
Discovered by SecBuddyF (with Hy4-preview) of Tencent KeenSecurityLab.
---
NEWS | 5 +++++
src/tac.c | 11 +++++++++--
tests/tac/tac.pl | 23 +++++++++++++++++++++++
3 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/NEWS b/NEWS
index d2f707c4b..e4cd6d10d 100644
--- a/NEWS
+++ b/NEWS
@@ -305,6 +305,11 @@ GNU coreutils NEWS -*-
outline -*-
it would have failed if the control flag encoding was different.
[This bug was present in "the beginning".]
+ 'tac -s SEP' with a multi-byte SEP no longer reads past the end of an
internal
+ buffer. Previously, it could result in incorrect output or a crash when
given
+ multiple inputs.
+ [This bug was present in "the beginning".]
+
'tail --follow' no longer ignores an overflow of the inotify event queue,
and now reverts to polling in that case. Previously tail could fail
to notice new data, or that a file followed by name was replaced,
diff --git a/src/tac.c b/src/tac.c
index e39b26b8e..84ec6c6c6 100644
--- a/src/tac.c
+++ b/src/tac.c
@@ -254,6 +254,9 @@ tac_seekable (int input_fd, char const *file, off_t
file_pos)
if (sentinel_length)
match_start -= match_length1;
+ /* Offset from G_BUFFER at which the last non-regexp search began. */
+ idx_t search_offset = 0;
+
while (true)
{
/* Search backward from 'match_start' - 1 to 'G_buffer' for a match
@@ -288,6 +291,8 @@ tac_seekable (int input_fd, char const *file, off_t
file_pos)
}
else
{
+ search_offset = match_start - G_buffer;
+
/* 'match_length' is constant for non-regexp boundaries. */
while (*--match_start != first_char
|| (match_length1 && !STREQ_LEN (match_start + 1, separator1,
@@ -341,9 +346,11 @@ tac_seekable (int input_fd, char const *file, off_t
file_pos)
The source and destination regions probably overlap. */
memmove (G_buffer + read_size, G_buffer, saved_record_size);
past_end = G_buffer + read_size + saved_record_size;
- /* For non-regexp searches, avoid unnecessary scanning. */
+ /* For non-regexp searches, avoid unnecessary scanning.
+ Keep our position before pending data, so a match doesn't
+ extend beyond PAST_END or overlap with the previous match. */
if (sentinel_length)
- match_start = G_buffer + read_size;
+ match_start = G_buffer + read_size + MIN (0, search_offset);
else
match_start = past_end;
diff --git a/tests/tac/tac.pl b/tests/tac/tac.pl
index 99d2d0d50..cd97ab10d 100755
--- a/tests/tac/tac.pl
+++ b/tests/tac/tac.pl
@@ -27,6 +27,10 @@ my $bad_dir = 'no/such/dir';
# This must be longer than 16KiB to trigger the double free in coreutils-8.5.
my $long_line = 'o' x (16 * 1024 + 1);
+# Keep in sync with the definition from src/tac.c.
+my $limits = getlimits ();
+my $INITIAL_READSIZE = $limits->{BUFSIZ};
+
my @Tests =
(
['segfault', '-r', {IN=>"a\n"}, {IN=>"b\n"}, {OUT=>"a\nb\n"}],
@@ -82,6 +86,25 @@ my @Tests =
# coreutils-8.5's tac would double-free its primary buffer.
['double-free', {IN=>$long_line}, {OUT=>$long_line}],
+
+ # coreutils-9.13 and earlier would crash due to a heap over-read.
+ ['buf-sep-1', '-s abc',
+ do { my $sep_x = 'x' x $INITIAL_READSIZE;
+ my $sep_y = 'x' x ($INITIAL_READSIZE - 1);
+ ({IN=>$sep_x . '?c'},
+ {IN=>$sep_y . 'ab'},
+ {OUT=>$sep_x . '?c' . $sep_y . 'ab'}); }],
+
+ # coreutils-9.13 and earlier would give invalid output.
+ ['buf-sep-2', '-b -s ab',
+ do { my $sep_x1 = 'x' x ($INITIAL_READSIZE - 1);
+ my $sep_x2 = 'x' x ($INITIAL_READSIZE - 2);
+ ({IN=>$sep_x1 . 'a' . 'ab' . $sep_x2 . 'byy'},
+ {OUT=>'ab' . $sep_x2 . 'byy' . $sep_x1 . 'a'}); }],
+ ['buf-sep-3', '-s aa',
+ do { my $sep_x = 'x' x ($INITIAL_READSIZE - 1);
+ ({IN=>$sep_x . 'aaa'},
+ {OUT=>$sep_x . 'aaa'}); }],
);
@Tests = triple_test \@Tests;
--
2.56.1