* src/chmod.c (mode_changed): Update mode with what was
read back, and pass to...
(describe_change): report the accurate mode.
* tests/chmod/report-setgid.sh: Add a new root test.
* tests/local.mk: Reference the new test.
* NEWS: Mention the bug fix.
---
NEWS | 3 ++-
src/chmod.c | 24 ++++++++++++---------
tests/chmod/report-setgid.sh | 42 ++++++++++++++++++++++++++++++++++++
tests/local.mk | 1 +
4 files changed, 59 insertions(+), 11 deletions(-)
create mode 100755 tests/chmod/report-setgid.sh
diff --git a/NEWS b/NEWS
index b60b12ec0..1e56ffb0a 100644
--- a/NEWS
+++ b/NEWS
@@ -51,7 +51,8 @@ GNU coreutils NEWS -*-
outline -*-
'chmod -v' and 'chmod -c' now more robustly verify set-ID and sticky bits.
Previously, renaming the file could cause an incorrect or spurious
diagnostic.
- [bug introduced in fileutils-4.0.33]
+ Also if these requested bits are ignored, an inaccurate mode was diagnosed.
+ [This bug was present in "the beginning".]
'chmod -R --preserve-root' now checks the opened file against '/' before
changing permissions through its descriptor. Previously a concurrent
diff --git a/src/chmod.c b/src/chmod.c
index 0c18752c6..6aececb77 100644
--- a/src/chmod.c
+++ b/src/chmod.c
@@ -123,15 +123,16 @@ static struct option const long_options[] =
};
/* Return true if the chmodable permission bits of FILE changed.
- FD, if nonnegative, is the descriptor used to set NEW_MODE. */
+ FD, if nonnegative, is the descriptor used to set *NEW_MODE.
+ Update *NEW_MODE if the resulting mode is read back. */
static bool
mode_changed (int fd, int dir_fd, char const *file, char const *file_full_name,
- mode_t old_mode, mode_t new_mode, bool follow,
+ mode_t old_mode, mode_t *new_mode, bool follow,
bool ignore_missing, bool *file_missing)
{
*file_missing = false;
- if (new_mode & (S_ISUID | S_ISGID | S_ISVTX))
+ if (*new_mode & (S_ISUID | S_ISGID | S_ISVTX))
{
/* The new mode contains unusual bits that the call to chmod may
have silently cleared. Check whether they actually changed. */
@@ -150,17 +151,18 @@ mode_changed (int fd, int dir_fd, char const *file, char
const *file_full_name,
return false;
}
- new_mode = new_stats.st_mode;
+ *new_mode = new_stats.st_mode;
}
- return ((old_mode ^ new_mode) & CHMOD_MODE_BITS) != 0;
+ return ((old_mode ^ *new_mode) & CHMOD_MODE_BITS) != 0;
}
/* Tell the user how/if the MODE of FILE has been changed.
CH describes what (if anything) has happened. */
static void
-describe_change (char const *file, struct change_status const *ch)
+describe_change (char const *file, struct change_status const *ch,
+ mode_t new_mode)
{
char perms[12]; /* "-rwxrwxrwx" ls-style modes. */
char old_perms[12];
@@ -184,9 +186,9 @@ describe_change (char const *file, struct change_status
const *ch)
unsigned long int
old_m = ch->old_mode & CHMOD_MODE_BITS,
- m = ch->new_mode & CHMOD_MODE_BITS;
+ m = new_mode & CHMOD_MODE_BITS;
- strmode (ch->new_mode, perms);
+ strmode (new_mode, perms);
perms[10] = '\0'; /* Remove trailing space. */
strmode (ch->old_mode, old_perms);
@@ -387,6 +389,7 @@ process_file (FTS *fts, FTSENT *ent)
break;
}
+ mode_t reported_mode = 0;
if (ch.status == CH_NOT_APPLIED)
{
bool follow_symlink = !!dereference;
@@ -395,6 +398,7 @@ process_file (FTS *fts, FTSENT *ent)
int fd;
int chmod_errno = adjust_file_mode (fts->fts_cwd_fd, file,
follow_symlink,
ignore_missing, file_stats, &ch,
&fd);
+ reported_mode = ch.new_mode;
if (chmod_errno == ROOT_PRESERVED)
{
ROOT_DEV_INO_WARN (file_full_name);
@@ -412,7 +416,7 @@ process_file (FTS *fts, FTSENT *ent)
ch.status = CH_SUCCEEDED;
if (verbosity != V_off
&& !mode_changed (fd, fts->fts_cwd_fd, file, file_full_name,
- ch.old_mode, ch.new_mode, follow_symlink,
+ ch.old_mode, &reported_mode, follow_symlink,
ignore_missing, &file_missing))
ch.status = CH_NO_CHANGE_REQUESTED;
if (0 <= fd)
@@ -434,7 +438,7 @@ process_file (FTS *fts, FTSENT *ent)
if (verbosity == V_high
|| (verbosity == V_changes_only && ch.status == CH_SUCCEEDED))
- describe_change (file_full_name, &ch);
+ describe_change (file_full_name, &ch, reported_mode);
if (CH_NO_CHANGE_REQUESTED <= ch.status && diagnose_surprises)
{
diff --git a/tests/chmod/report-setgid.sh b/tests/chmod/report-setgid.sh
new file mode 100755
index 000000000..8556b4635
--- /dev/null
+++ b/tests/chmod/report-setgid.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+# Verify chmod's output when the kernel silently clears the set-group-ID bit.
+
+# 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_root_
+
+# Let the owner change the directory mode without belonging to its group.
+chmod go+x . && mkdir d && chown "$NON_ROOT_USERNAME:+0" d &&
+ chmod =755 d || framework_failure_
+as_owner_()
+{
+ chroot --skip-chdir --userspec="$NON_ROOT_USERNAME:+1" --groups='' / "$@"
+}
+
+as_owner_ chmod g+s d && test ! -g d ||
+ skip_ 'chmod g+s must was not ignored'
+
+as_owner_ chmod -v g+s d >out || fail=1
+echo "mode of 'd' retained as 0755 (rwxr-xr-x)" >exp
+compare exp out || fail=1
+
+as_owner_ chmod -c g+ws d >out || fail=1
+echo "mode of 'd' changed from 0755 (rwxr-xr-x) to 0775 (rwxrwxr-x)" >exp
+compare exp out || fail=1
+
+Exit $fail
diff --git a/tests/local.mk b/tests/local.mk
index 876c37f49..5fe17818a 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -107,6 +107,7 @@ EXTRA_DIST += \
$(pr_data)
all_root_tests = \
+ tests/chmod/report-setgid.sh \
tests/chown/basic.sh \
tests/chgrp/from.sh \
tests/cp/cp-a-selinux.sh \
--
2.56.0