From 6ed44e12fb4f3b374de9ce2c35560fd60b43685f Mon Sep 17 00:00:00 2001 From: liuh Date: Wed, 8 May 2024 10:33:31 +0800 Subject: [PATCH] tail: avoid infloop with -c on /dev/zero --- ...ail-avoid-infloop-with-c-on-dev-zero.patch | 99 +++++++++++++++++++ coreutils.spec | 6 +- 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 backport-tail-avoid-infloop-with-c-on-dev-zero.patch diff --git a/backport-tail-avoid-infloop-with-c-on-dev-zero.patch b/backport-tail-avoid-infloop-with-c-on-dev-zero.patch new file mode 100644 index 0000000..5f630a5 --- /dev/null +++ b/backport-tail-avoid-infloop-with-c-on-dev-zero.patch @@ -0,0 +1,99 @@ +From fb543b6b82c1f3a20ff88f44cc3ed367bfe811b6 Mon Sep 17 00:00:00 2001 +From: Paul Eggert +Date: Fri, 19 Apr 2024 21:44:32 -0700 +Subject: [PATCH] tail: avoid infloop with -c on /dev/zero + +Problem reported by Ionut Nicula in: +https://bugs.gnu.org/70477 +* src/tail.c (tail_bytes): Do not loop forever on commands +like 'tail -c 4096 /dev/zero'. +* tests/tail/tail-c.sh: Test this fix. +--- + NEWS | 3 +++ + src/tail.c | 24 +++++++++++++++++++----- + tests/tail/tail-c.sh | 10 ++++++++++ + 3 files changed, 32 insertions(+), 5 deletions(-) + +diff --git a/NEWS b/NEWS +index 2d9e46b..8f36e33 100644 +--- a/NEWS ++++ b/NEWS +@@ -67,6 +67,9 @@ GNU coreutils NEWS -*- outline -*- + on x86 Linux kernels that disable XSAVE YMM. This was seen on Xen VMs. + [bug introduced in coreutils-9.0] + ++ 'tail -c 4096 /dev/zero' no longer loops forever. ++ [This bug was present in "the beginning".] ++ + ** Changes in behavior + + 'cp -v' and 'mv -v' will no longer output a message for each file skipped +diff --git a/src/tail.c b/src/tail.c +index f293551..d65d1ac 100644 +--- a/src/tail.c ++++ b/src/tail.c +@@ -732,7 +732,8 @@ free_lbuffers: + return ok; + } + +-/* Print the last N_BYTES characters from the end of pipe FD. ++/* Print the last N_BYTES characters from the end of FD. ++ Work even if the input is a pipe. + This is a stripped down version of pipe_lines. + Return true if successful. */ + +@@ -1833,15 +1834,28 @@ tail_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes, + { + off_t end_pos = -1; + off_t current_pos = -1; ++ bool copy_from_current_pos = false; + + if (! presume_input_pipe && n_bytes <= OFF_T_MAX) + { + if (usable_st_size (&stats)) +- end_pos = stats.st_size; +- else if ((current_pos = lseek (fd, -n_bytes, SEEK_END)) != -1) +- end_pos = current_pos + n_bytes; ++ { ++ /* Use st_size only if it's so large that this is ++ probably not a /proc or similar file, where st_size ++ is notional. */ ++ end_pos = stats.st_size; ++ off_t smallish_size = STP_BLKSIZE (&stats); ++ copy_from_current_pos = smallish_size < end_pos; ++ } ++ else ++ { ++ current_pos = lseek (fd, -n_bytes, SEEK_END); ++ copy_from_current_pos = current_pos != -1; ++ if (copy_from_current_pos) ++ end_pos = current_pos + n_bytes; ++ } + } +- if (end_pos <= (off_t) ST_BLKSIZE (stats)) ++ if (! copy_from_current_pos) + return pipe_bytes (pretty_filename, fd, n_bytes, read_pos); + if (current_pos == -1) + current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename); +diff --git a/tests/tail/tail-c.sh b/tests/tail/tail-c.sh +index cd92909..f968f6d 100755 +--- a/tests/tail/tail-c.sh ++++ b/tests/tail/tail-c.sh +@@ -35,4 +35,14 @@ printf '123456' | tail -c3 > out || fail=1 + printf '456' > exp || framework_failure_ + compare exp out || fail=1 + ++# Any part of /dev/zero should be valid for tail -c. ++head -c 4096 /dev/zero >exp || fail=1 ++tail -c 4096 /dev/zero >out || fail=1 ++compare exp out || fail=1 ++ ++# Any part of /dev/urandom, if it exists, should be valid for tail -c. ++if test -r /dev/urandom; then ++ timeout --verbose 1 tail -c 4096 /dev/urandom >/dev/null || fail=1 ++fi ++ + Exit $fail +-- +2.27.0 + diff --git a/coreutils.spec b/coreutils.spec index 2d0a0c2..75dd2f7 100644 --- a/coreutils.spec +++ b/coreutils.spec @@ -1,6 +1,6 @@ Name: coreutils Version: 9.4 -Release: 3 +Release: 4 License: GPLv3+ Summary: A set of basic GNU tools commonly used in shell scripts Url: https://www.gnu.org/software/coreutils/ @@ -24,6 +24,7 @@ Patch9: backport-coreutils-i18n.patch patch10: backport-CVE-2024-0684-split-do-not-shrink-hold-buffer.patch patch11: test-skip-overlay-filesystem-because-of-no-inotify_add_watch.patch patch12: fix-coredump-if-enable-systemd.patch +patch13: backport-tail-avoid-infloop-with-c-on-dev-zero.patch %ifarch sw_64 Patch9001: coreutils-9.0-sw.patch %endif @@ -155,6 +156,9 @@ fi %{_mandir}/man*/* %changelog +* Wed May 8 2024 liuh - 9.4-4 +- sync patch from community + * Fri Mar 15 2024 xueyamao - 9.4-3 - fix coredump if enable systemd -- Gitee