You can get ENOTSUP due to the system not supporting chmod(symlink),
but you can also get ENOTSUP on any file type if the system needs
/proc to emulate AT_SYMLINK_NOFOLLOW and /proc is not available.
This emulation is used before Linux 6.6 (2023).

* src/chmod.c (process_file): Check we're operating on a symlink
if we get ENOTSUP, and only ignore the error in that case.
* tests/chmod/symlink-enotsup.sh: Add a new test.
* tests/local.mk: Reference the new test.
* NEWS: Mention the bug fix.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-PSCKLNL1.
---
 NEWS                           |  5 ++++
 src/chmod.c                    | 20 +++++++++++--
 tests/chmod/symlink-enotsup.sh | 54 ++++++++++++++++++++++++++++++++++
 tests/local.mk                 |  1 +
 4 files changed, 77 insertions(+), 3 deletions(-)
 create mode 100755 tests/chmod/symlink-enotsup.sh

diff --git a/NEWS b/NEWS
index b288a6bf5..3f4d4184d 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,11 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   when only a partial context is specified.
   [bug introduced with the chcon program in coreutils-6.9.90]
 
+  'chmod -R' and 'chmod -h' no longer silently skip permission changes on
+  files and directories on Linux kernels before 6.6 when /proc is unavailable.
+  These operations now diagnose the unsupported changes and exit with failure.
+  [bug introduced in coreutils-9.5]
+
   '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 58310157f..5a7b41c2b 100644
--- a/src/chmod.c
+++ b/src/chmod.c
@@ -27,6 +27,7 @@
 #include "filemode.h"
 #include "fts-missing.h"
 #include "ignore-value.h"
+#include "issymlinkat.h"
 #include "modechange.h"
 #include "quote.h"
 #include "root-dev-ino.h"
@@ -329,13 +330,26 @@ process_file (FTS *fts, FTSENT *ent)
         ch.status = CH_SUCCEEDED;
       else
         {
+          int chmod_errno = errno;
           if (ignore_missing
-              && ignorable_traversal_errno (errno))
+              && ignorable_traversal_errno (chmod_errno))
             return true;
-          if (! is_ENOTSUP (errno))
+          /* Ignore unsupported changes only for symlinks themselves.
+             Older systems (without /proc) may return ENOTSUP for non symlinks.
+             FTS may have supplied referent metadata, so check again.  */
+          bool unsupported_symlink = false;
+          if (is_ENOTSUP (chmod_errno) && ! follow_symlink)
+            {
+              int is_symlink = issymlinkat (fts->fts_cwd_fd, file);
+              if (is_symlink < 0 && ignore_missing
+                  && ignorable_traversal_errno (errno))
+                return true;
+              unsupported_symlink = is_symlink == 1;
+            }
+          if (! unsupported_symlink)
             {
               if (! force_silent)
-                error (0, errno, _("changing permissions of %s"),
+                error (0, chmod_errno, _("changing permissions of %s"),
                        quoteaf (file_full_name));
 
               ch.status = CH_FAILED;
diff --git a/tests/chmod/symlink-enotsup.sh b/tests/chmod/symlink-enotsup.sh
new file mode 100755
index 000000000..b3b93e770
--- /dev/null
+++ b/tests/chmod/symlink-enotsup.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+# Ignore ENOTSUP only when changing a symlink itself.
+
+# 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
+require_strace_ chmod
+getlimits_
+
+# Match all chmod variants, including fchmodat2 when supported.
+inject='/.*chmod:error=EOPNOTSUPP'
+strace --quiet=all -o /dev/null -e inject="$inject" true 2>/dev/null \
+  || skip_ 'strace does not support the required injection options'
+
+touch file || framework_failure_
+ln -s file link || framework_failure_
+
+for deref in --dereference --no-dereference; do
+  for file in file link; do
+    strace --quiet=all -o trace -e inject="$inject" \
+      chmod "$deref" u+w "$file" >out 2>err
+    status=$?
+    grep ' (INJECTED)' trace > /dev/null \
+      || skip_ 'strace did not inject the requested failure'
+
+    # Only unsupported changes to the symlink itself may be ignored.
+    if test "$deref:$file" = --no-dereference:link; then
+      test "$status" = 0 || fail=1
+      compare /dev/null err || fail=1
+    else
+      test "$status" = 1 || fail=1
+      echo "chmod: changing permissions of '$file': $ENOTSUP" >exp \
+        || framework_failure_
+      compare exp err || fail=1
+    fi
+    compare /dev/null out || fail=1
+  done
+done
+
+Exit $fail
diff --git a/tests/local.mk b/tests/local.mk
index 60574262e..d905ad76e 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -586,6 +586,7 @@ all_tests =                                 \
   tests/chmod/thru-dangling.sh                 \
   tests/chmod/umask-x.sh                       \
   tests/chmod/usage.sh                         \
+  tests/chmod/symlink-enotsup.sh                       \
   tests/chmod/symlinks.sh                      \
   tests/chown/cannot-access.sh                 \
   tests/chown/deref.sh                         \
-- 
2.56.0


Reply via email to