tail: allocate a large enough inotify event buffer

* src/tail.c (tail_forever_inotify): Size the buffer based on NAME_MAX
instead of the operands. Reduce the scope of some variables.
* tests/tail/inotify-long-name.sh: New file.
* tests/local.mk (all_tests): Add the new test.
* NEWS: Mention the bug fix. Adjust the reference in a previously fixed
bug to mention it was introduced with inotify support.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-S9L24ERC.
This commit is contained in:
Collin Funk committed 2026-10-30 17:59:19 -07:00
1 parent e10de8ddd0
commit 83545af2a8
4 files changed
+75 -10

No files matched your search

+6 -1
View File
@@ -129,7 +129,12 @@ GNU coreutils NEWS -*- outline -*-
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,
if events arrived faster than tail could process them.
[bug introduced in coreutils-7.5]
[bug introduced with inotify support added in coreutils-7.5]
'tail -F' no longer fails with "error reading inotify event" when a separate
file with a longer name is created, removed, or renamed in the directory
of a followed file.
[bug introduced with inotify support added in coreutils-7.5]
'timeout --signal=ALRM -k' again honors the -k option.
Similarly 'timeout --signal=CHLD' no longer reports "No child processes",
+7 -9
View File
@@ -1534,9 +1534,6 @@ tail_forever_inotify (int wd, struct File_spec *f, int n_files,
bool found_unwatchable_dir = false;
bool no_inotify_resources = false;
struct File_spec *prev_fspec;
idx_t evlen = 0;
char *evbuf;
idx_t evbuf_off = 0;
*parent_by_namep = NULL;
@@ -1569,10 +1566,6 @@ tail_forever_inotify (int wd, struct File_spec *f, int n_files,
{
if (!f[i].ignore)
{
idx_t fnlen = strlen (f[i].name);
if (evlen < fnlen)
evlen = fnlen;
f[i].wd = -1;
if (follow_mode == Follow_name)
@@ -1680,8 +1673,13 @@ tail_forever_inotify (int wd, struct File_spec *f, int n_files,
if (debug)
error (0, 0, "%s", _("using notification mode"));
evlen += sizeof (struct inotify_event) + 1;
evbuf = ximalloc (evlen);
idx_t evbuf_off = 0;
/* NAME_MAX covers that max length of all file systems
that might be mounted in the parent dir, and the define
should be available any where that inotify is. */
idx_t evlen = sizeof (struct inotify_event) + NAME_MAX + 1;
char *evbuf = ximalloc (evlen);
/* Wait for inotify events and handle them. Events on directories
ensure that watched files can be re-added when following by name.
+1
View File
@@ -207,6 +207,7 @@ all_tests = \
tests/tail/inotify-rotate.sh \
tests/tail/inotify-rotate-resources.sh \
tests/tail/inotify-dir-recreate.sh \
tests/tail/inotify-long-name.sh \
tests/tail/inotify-only-regular.sh \
tests/tail/inotify-overflow.sh \
tests/tail/quote-headers.sh \
+61
View File
@@ -0,0 +1,61 @@
#!/bin/sh
# Check that the inotify event buffers are allocated with a large enough size.
# Copyright (C) 2026 Free Software Foundation, Inc.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ tail
require_inotify_supported_
check_tail_output()
{
local delay="$1"
grep "$tail_re" out > /dev/null ||
{ sleep $delay; return 1; }
}
# Make sure we can create a long file name.
long_name=$(printf '%0255d' | tr '0' 'a') || framework_failure_
touch "$long_name" || skip_ 'cannot create long file name'
rm -f "$long_name" || framework_failure_
# Create an empty file to tail.
touch file || framework_failure_
cleanup_() { kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null; pid=; }
# Now tail it and make sure we are using notification mode.
timeout 60 tail --debug -F file >out 2>&1 & pid=$!
tail_re='tail: using notification mode' retry_delay_ check_tail_output .1 7 ||
{ cat out; skip_ 'tail did not switch to notification mode'; }
# Create a long file name and then write to the file being tailed.
# coreutils-9.12 and earlier only allocated enough memory to follow
# events for its operands. Creating a file name longer than that would
# cause it to fail and exit.
touch "$long_name" &&
echo abc >file || framework_failure_
tail_re='^abc$' retry_delay_ check_tail_output .1 7 || { cat out; fail=1; }
echo 1 >"$long_name" &&
echo def >>file || framework_failure_
tail_re='^def$' retry_delay_ check_tail_output .1 7 || { cat out; fail=1; }
mv "$long_name" new &&
echo ghi >>file || framework_failure_
tail_re='^ghi$' retry_delay_ check_tail_output .1 7 || { cat out; fail=1; }
Exit $fail