In tac_seekable, after the read-ahead memmove that shifts the pending

record to make room for newly read data, match_start was set to
G_buffer + read_size without checking whether the pending record
(saved_record_size) was at least match_length - 1 bytes long.  When
saved_record_size < match_length - 1 (which can happen when processing
a second or later file operand whose final chunk is shorter than the
separator), the backward STREQ_LEN scan can read past past_end into
leftover bytes from a previous file. &nbsp;If those leftover bytes happen
to match the separator, a spurious match is accepted whose match_end
exceeds past_end, causing output() to underflow bytes_to_add and
read past the G_buffer allocation.


Fix this by capping match_start at past_end - match_length1, so the
STREQ_LEN scan never reads past past_end. &nbsp;When saved_record_size &gt;=
match_length - 1 (the common case), the cap is a no-op and the
existing optimization is preserved.


* src/tac.c (tac_seekable): Cap match_start at past_end - match_length1
after the read-ahead memmove.
* tests/tac/tac.pl: Add regression test 'oob-multibyte-sep'.


Reported at https://bugs.gnu.org/82117.
Signed-off-by: Haolin Xue <[email protected]&gt;
---
&nbsp;src/tac.c &nbsp; &nbsp; &nbsp; &nbsp;| 10 ++++++++--
&nbsp;tests/tac/tac.pl | &nbsp;9 +++++++++
&nbsp;2 files changed, 17 insertions(+), 2 deletions(-)


diff --git a/src/tac.c b/src/tac.c
index 1845791ab..e7d731231 100644
--- a/src/tac.c
+++ b/src/tac.c
@@ -341,9 +341,15 @@ tac_seekable (int input_fd, char const *file, off_t 
file_pos)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The source and destination 
regions probably overlap. &nbsp;*/
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memmove (G_buffer + read_size, 
G_buffer, saved_record_size);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;past_end = G_buffer + read_size + 
saved_record_size;
- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* For non-regexp searches, avoid 
unnecessary scanning. */
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* For non-regexp searches, avoid 
unnecessary scanning.
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Also cap match_start so that the 
backward STREQ_LEN scan
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cannot read past past_end: a match 
starting near the
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boundary with the pending record 
would otherwise read
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; beyond the valid data when 
saved_record_size is less
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; than match_length - 1. &nbsp;*/
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (sentinel_length)
- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;match_start = G_buffer + read_size;
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;match_start = MIN (G_buffer + 
read_size,
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; past_end - match_length1);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;match_start = past_end;
&nbsp;
diff --git a/tests/tac/tac.pl b/tests/tac/tac.pl
index 99d2d0d50..5a9022191 100755
--- a/tests/tac/tac.pl
+++ b/tests/tac/tac.pl
@@ -82,6 +82,15 @@ my @Tests =
&nbsp;
&nbsp; &nbsp;# coreutils-8.5's tac would double-free its primary buffer.
&nbsp; &nbsp;['double-free', {IN=&gt;$long_line}, {OUT=&gt;$long_line}],
+
+ &nbsp;# bug#82117: with a multi-byte separator (length &gt;= 3) and at least
+ &nbsp;# two file operands, tac could read past the buffer. &nbsp;When the 
second
+ &nbsp;# file's pending record (after the read-ahead memmove) is shorter than
+ &nbsp;# match_length - 1, the backward STREQ_LEN scan reads past past_end,
+ &nbsp;# and a spurious match causes output() to underflow bytes_to_add.
+ &nbsp;['oob-multibyte-sep', '-s XYX',
+ &nbsp; {IN=&gt;'A' x 8193 . 'X'}, {IN=&gt;'A' x 8191 . 'XY'},
+ &nbsp; {OUT=&gt;'A' x 8193 . 'X' . 'A' x 8191 . 'XY'}],
&nbsp;);
&nbsp;
&nbsp;@Tests = triple_test \@Tests;
-- 
2.43.0

Reply via email to