df: don't open non-directory operands

* src/df.c (automount_stat_err): Use O_DIRECTORY when opening operands.
Fall back to stat if open fails with ENOTDIR.
* tests/df/open.sh: New file.
* tests/local.mk (all_tests): Add the new test case.
* NEWS: Mention the bug fix.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-Z5SPLRCM.
This commit is contained in:
Collin Funk committed 2026-11-13 18:27:05 -08:00
1 parent 394427e691
commit 3f8a5b5470
4 files changed
+64 -2

No files matched your search

+5
View File
@@ -89,6 +89,11 @@ GNU coreutils NEWS -*- outline -*-
oflag=seek_bytes, and when N is smaller than the output block size.
[bug introduced with oflag=seek_bytes in coreutils-8.16]
'df' no longer opens non-directory operands. Previously, it would open and
subsequently close them, which may lead to unintended behavior when the
operand is a device or a symbolic link to a device.
[bug introduced in coreutils-7.3]
'dircolors --csh' now escapes '!' characters in its output. Previously, csh
would perform history substitution on the output when evaluated, which would
fail or execute arbitrary commands depending on the contents of the database
+2 -2
View File
@@ -288,10 +288,10 @@ static struct option const long_options[] =
static int
automount_stat_err (char const *file, struct stat *st)
{
int fd = open (file, O_RDONLY | O_NOCTTY | O_NONBLOCK);
int fd = open (file, O_RDONLY | O_DIRECTORY | O_NOCTTY | O_NONBLOCK);
if (fd < 0)
{
if (errno == ENOENT || errno == ENOTDIR)
if (errno == ENOENT)
return errno;
return stat (file, st) == 0 ? 0 : errno;
}
+56
View File
@@ -0,0 +1,56 @@
#!/bin/sh
# Test that 'df' opens directory operands to trigger automounts.
# 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_ df
require_strace_ openat
getlimits_
trace_open ()
{
strace --quiet=all -o trace -e trace=open,openat "$@" >out 2>err
}
trace_open true || skip_ 'strace does not support the required options'
ln -s /dev/null dev_null &&
touch file || framework_failure_
# Make sure that non-directories don't get opened.
for file in file dev_null; do
trace_open df $file || fail=1
compare /dev/null err || fail=1
returns_ 1 grep '^open.*"'$file'",.* = [0-9]' trace || fail=1
done
# Make sure that directories get opened so that automounts get triggered.
mkdir dir || framework_failure_
trace_open df dir || fail=1
compare /dev/null err || fail=1
grep '^open.*"dir",.* = [0-9]' trace || fail=1
# Make sure the errors given for missing files are what we expect.
cat <<EOF >exp-err || framework_failure_
df: missing: $ENOENT
df: missing/f: $ENOENT
EOF
returns_ 1 df missing missing/f >out 2>err || fail=1
compare /dev/null out || fail=1
compare exp-err err || fail=1
Exit $fail
+1
View File
@@ -657,6 +657,7 @@ all_tests = \
tests/df/df-P.sh \
tests/df/df-output.sh \
tests/df/df-symlink.sh \
tests/df/open.sh \
tests/df/sync.sh \
tests/df/unreadable.sh \
tests/df/total-unprocessed.sh \