This could be considered a security fix in the edge case
where in a user writeable directory with a root owned file,
a user could rename a setuid file in place, and rename back.
If a root chmod is operating in parallel it could read say setuid
from the first file, and then set that on the replaced root file.

* src/chmod.c (adjust_file_mode): A new function to apply changes
based on an opened descriptor where supported, thus avoiding
races between reading and writing of permissions.
* tests/chmod/rename.sh: Add a new test using strace to induce the race.
* tests/local.mk: Reference the new test.
* NEWS: Mention the bug fix.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-H9P7Y138.
---
 NEWS                  |  5 ++++
 src/chmod.c           | 51 ++++++++++++++++++++++++++++++++-----
 tests/chmod/rename.sh | 58 +++++++++++++++++++++++++++++++++++++++++++
 tests/local.mk        |  1 +
 4 files changed, 109 insertions(+), 6 deletions(-)
 create mode 100755 tests/chmod/rename.sh

diff --git a/NEWS b/NEWS
index 868d96d85..202711bb1 100644
--- a/NEWS
+++ b/NEWS
@@ -39,6 +39,11 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   these operations now diagnose the unsupported changes and exit with failure.
   [bug introduced in coreutils-9.5]
 
+  'chmod' now computes and applies permissions to the same file,
+  even if it is renamed during the operation. If built on older systems
+  with glibc < 2.39, this guarantee relies on /proc being available.
+  [This bug was present in "the beginning".]
+
   'chmod -R --dereference --preserve-root' no longer silently skips the
   entry following a symbolic link to '/' encountered during traversal.
   [bug introduced in coreutils-9.5]
diff --git a/src/chmod.c b/src/chmod.c
index a182b5f82..788a73ff1 100644
--- a/src/chmod.c
+++ b/src/chmod.c
@@ -208,6 +208,48 @@ describe_change (char const *file, struct change_status 
const *ch)
   printf (fmt, quoted_file, old_m, &old_perms[1], m, &perms[1]);
 }
 
+/* Compute and apply FILE's new mode on the same object when possible.
+   ST is the status from FTS.  Without descriptor support, -Rh can use
+   the referent's mode to compute the symlink's new mode if FTS followed
+   the link.  This matters on systems that can change symlink permissions.
+   Return 0 on success, or an errno value.  */
+static int
+adjust_file_mode (int dirfd, char const *file, bool follow,
+                  struct stat const *st, struct change_status *ch)
+{
+#if defined O_PATH && defined AT_EMPTY_PATH && HAVE_WORKING_O_NOFOLLOW
+  int fd = openat (dirfd, file, O_PATH | O_CLOEXEC | (follow ? 0 : 
O_NOFOLLOW));
+  struct stat fd_st;
+  int stat_errno = (fd < 0 || fstat (fd, &fd_st) < 0) ? errno : 0;
+  if (! stat_errno)
+    st = &fd_st;
+#endif
+
+  /* Note without descriptor support, -Rh can use the target's mode
+     to compute the symlink's new mode if FTS followed the link.
+     Given this only matters on older systems, and then only on systems
+     that can change the permissions of a symlink, we ignore for now.  */
+
+  ch->old_mode = st->st_mode;
+  ch->new_mode = mode_adjust (ch->old_mode, S_ISDIR (ch->old_mode),
+                              umask_value, change, NULL);
+
+#if defined O_PATH && defined AT_EMPTY_PATH && HAVE_WORKING_O_NOFOLLOW
+  int err = stat_errno;
+  if (! err && fchmodat (fd, "", ch->new_mode,
+                         AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW) < 0)
+    err = errno;
+  if (0 <= fd)
+    close (fd);
+  if (stat_errno || (err != EINVAL && err != ENOSYS && err != ENOENT))
+    return err;
+#endif
+
+  /* Older systems may require a second lookup of the name.  */
+  return (fchmodat (dirfd, file, ch->new_mode,
+                    follow ? 0 : AT_SYMLINK_NOFOLLOW) < 0 ? errno : 0);
+}
+
 /* Change the mode of FILE.
    Return true if successful.  This function is called
    once for every file system object that fts encounters.  */
@@ -319,18 +361,15 @@ process_file (FTS *fts, FTSENT *ent)
 
   if (ch.status == CH_NOT_APPLIED)
     {
-      ch.old_mode = file_stats->st_mode;
-      ch.new_mode = mode_adjust (ch.old_mode, S_ISDIR (ch.old_mode) != 0,
-                                 umask_value, change, NULL);
       bool follow_symlink = !!dereference;
       if (dereference == -1) /* -H with/without -R, -P without -R.  */
         follow_symlink = ent->fts_level == FTS_ROOTLEVEL;
-      if (fchmodat (fts->fts_cwd_fd, file, ch.new_mode,
-                    follow_symlink ? 0 : AT_SYMLINK_NOFOLLOW) == 0)
+      int chmod_errno = adjust_file_mode (fts->fts_cwd_fd, file, 
follow_symlink,
+                                          file_stats, &ch);
+      if (! chmod_errno)
         ch.status = CH_SUCCEEDED;
       else
         {
-          int chmod_errno = errno;
           if (ignore_missing
               && ignorable_traversal_errno (chmod_errno))
             return true;
diff --git a/tests/chmod/rename.sh b/tests/chmod/rename.sh
new file mode 100755
index 000000000..4edbff18a
--- /dev/null
+++ b/tests/chmod/rename.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+# Check that chmod finishes on the file it opened if the name goes away.
+
+# 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_ chmod timeout
+require_strace_ openat
+
+touch file && chmod 600 file || framework_failure_
+# Older kernels or libraries can require a second lookup of the name.
+strace --quiet=all -o probe -e trace=fchmodat2 chmod -h 600 file \
+  2>/dev/null || skip_ 'cannot trace fchmodat2'
+grep 'fchmodat2(.* = 0' probe >/dev/null || skip_ 'fchmodat2 unavailable'
+grep 'AT_EMPTY_PATH.* = -1' probe >/dev/null && skip_ 'no empty-path chmod'
+
+inject=$(get_open_syscalls_):signal=SIGSTOP:when=1
+strace --quiet=all -o /dev/null -P file -e inject=$inject true 2>/dev/null \
+  || skip_ 'strace does not support the required injection options'
+
+tracer=
+cleanup_()
+{
+  if test -n "$tracer" && kill -0 "$tracer" 2>/dev/null; then
+    test ! -s pid || kill -KILL "$(cat pid)" 2>/dev/null
+    kill "$tracer" 2>/dev/null
+    wait "$tracer" 2>/dev/null
+  fi
+}
+
+# Stop after opening file.  Its new name is the only one left when chmod
+# resumes, and the descriptor must suffice to read and change its mode.
+timeout -k 1 60 strace --quiet=all -o trace -P file -e inject=$inject \
+  $SHELL -c 'echo $$ >pid; exec chmod g+r file' 2>err & tracer=$!
+stopped_() { sleep "$1"; grep 'stopped by SIGSTOP' trace >/dev/null; }
+retry_delay_ stopped_ .1 6 || { cat trace; Exit 1; }
+
+mv file moved || framework_failure_
+kill -CONT "$(cat pid)" || framework_failure_
+wait "$tracer" || { cat err; fail=1; }
+tracer=
+rm -f pid
+test "$(stat -c %a moved)" = 640 || fail=1
+
+Exit $fail
diff --git a/tests/local.mk b/tests/local.mk
index 42ea050c3..876c37f49 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -584,6 +584,7 @@ all_tests =                                 \
   tests/chmod/only-op.sh                       \
   tests/chmod/partial-fail.sh                  \
   tests/chmod/reference-order.sh               \
+  tests/chmod/rename.sh                        \
   tests/chmod/setgid.sh                                \
   tests/chmod/silent.sh                                \
   tests/chmod/thru-dangling.sh                 \
-- 
2.56.0


Reply via email to