mirror of
https://git.savannah.gnu.org/git/coreutils.git
synced 2026-11-17 18:02:09 +03:00
pr: avoid a heap overflow with a very wide line number
With -n and truncated lines, add_line_number() added NUMBER_WIDTH to INPUT_POSITION without checking for overflow. A tab at the start of a line followed by a line number width near INT_MAX would wrap INPUT_POSITION negative, and with -e the next tab would then be expanded by more than the tab width, writing past the end of CLUMP_BUFF. E.g., printf '\t\t\n' | pr -t -e -W2147483647 -n:2147483643 wrote 12 bytes into an 8 byte buffer. * src/pr.c (add_line_number): Use ckd_add when advancing INPUT_POSITION. (init_parameters): Likewise when computing NUMBER_WIDTH, which overflowed with -n:2147483647. * tests/pr/options.sh: Add test cases. * NEWS: Mention the bug fix. Link: https://github.com/coreutils/coreutils/pull/458
This commit is contained in:
1 parent
57d6cc458b
commit
3b77964c3c
3 files changed
+33
-7
No files matched your search
@@ -201,6 +201,12 @@ GNU coreutils NEWS -*- outline -*-
|
||||
would loop infinitely instead of printing an empty page.
|
||||
[This bug was present in "the beginning".]
|
||||
|
||||
'pr -n' now exits gracefully when the line number width overflows internal
|
||||
accounting limits. Previously, with -e and a very wide -n, the input
|
||||
position could wrap negative and a following tab would be expanded past
|
||||
the end of a heap buffer.
|
||||
[This bug was present in "the beginning".]
|
||||
|
||||
'realpath' without -s, or 'readlink' with -e,-f,-m no longer
|
||||
loop infinitely when resolving a symbolic link which
|
||||
resolves to a path beginning with the symbolic link itself.
|
||||
|
||||
@@ -1276,11 +1276,12 @@ init_parameters (int number_of_files)
|
||||
+ TAB_WIDTH (chars_per_input_tab, chars_per_number); */
|
||||
|
||||
/* Estimate chars_per_text without any margin and keep it constant. */
|
||||
if (number_separator == '\t')
|
||||
number_width = (chars_per_number
|
||||
+ TAB_WIDTH (chars_per_default_tab, chars_per_number));
|
||||
else
|
||||
number_width = chars_per_number + 1;
|
||||
int separator_width = (number_separator == '\t'
|
||||
? TAB_WIDTH (chars_per_default_tab,
|
||||
chars_per_number)
|
||||
: 1);
|
||||
if (ckd_add (&number_width, chars_per_number, separator_width))
|
||||
integer_overflow ();
|
||||
|
||||
/* The number is part of the column width unless we are
|
||||
printing files in parallel. */
|
||||
@@ -2074,8 +2075,9 @@ add_line_number (COLUMN *p)
|
||||
output_position);
|
||||
}
|
||||
|
||||
if (truncate_lines && !parallel_files)
|
||||
input_position += number_width;
|
||||
if (truncate_lines && !parallel_files
|
||||
&& ckd_add (&input_position, input_position, number_width))
|
||||
integer_overflow ();
|
||||
}
|
||||
|
||||
/* Print (or store) padding until the current horizontal position
|
||||
|
||||
@@ -57,6 +57,13 @@ printf '%s\n' "pr: '-e' extra characters or $INV in the argument: '=-1'" \
|
||||
>exp || framework_failure_
|
||||
compare exp err || fail=1
|
||||
|
||||
# Ensure the line number width plus its separator does not overflow
|
||||
for n in -n$INT_MAX -n:$INT_MAX; do
|
||||
returns_ 1 pr $n 2>err </dev/null || fail=1
|
||||
printf '%s\n' "pr: integer overflow" > exp || framework_failure_
|
||||
compare exp err || fail=1
|
||||
done
|
||||
|
||||
# Ensure we exit gracefully upon internal overflow limits
|
||||
# Tag as expensive as it uses little mem, but about 10s on a 2020 class machine.
|
||||
if { test "$RUN_VERY_EXPENSIVE_TESTS" = yes ||
|
||||
@@ -68,4 +75,15 @@ if { test "$RUN_VERY_EXPENSIVE_TESTS" = yes ||
|
||||
compare exp err || fail=1
|
||||
fi
|
||||
|
||||
# Ensure a line number after a tab doesn't overflow the input position,
|
||||
# which would expand the next tab past the end of its buffer.
|
||||
# Tag as very expensive as it uses about 2GiB of memory and output.
|
||||
if test "$RUN_VERY_EXPENSIVE_TESTS" = yes &&
|
||||
test "$INT_MAX" = 2147483647; then # restrict to usual 32 bit limits
|
||||
printf '\t\t\n' |
|
||||
returns_ 1 pr -t -e -W$INT_MAX -n:$(($INT_MAX - 4)) 2>err >/dev/null || fail=1
|
||||
printf '%s\n' "pr: integer overflow" > exp || framework_failure_
|
||||
compare exp err || fail=1
|
||||
fi
|
||||
|
||||
Exit $fail
|
||||
Reference in new issue
Block a user