* src/chmod.c (mode_change): Accept a follow flag to restrict
the check to the symlink with -h.
* tests/chmod/c-option.sh: Add a test case.
* NEWS: Mention the fix.
---
NEWS | 5 +++++
src/chmod.c | 17 +++++++++--------
tests/chmod/c-option.sh | 8 ++++++++
3 files changed, 22 insertions(+), 8 deletions(-)
diff --git a/NEWS b/NEWS
index 202711bb1..62b2c5f2c 100644
--- a/NEWS
+++ b/NEWS
@@ -32,6 +32,11 @@ GNU coreutils NEWS -*-
outline -*-
Previously it used the target's mode.
[bug introduced with the -h option in coreutils-9.5]
+ 'chmod -hv' and 'chmod -hc' now check the symlink itself when verifying
+ set-ID or sticky bits on systems that support changing symlink permissions.
+ Previously they could misreport a change or warn about a dangling symlink.
+ [bug introduced with the -h option in coreutils-9.5]
+
'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.
If built on older systems with glibc < 2.39 these operations now retry
diff --git a/src/chmod.c b/src/chmod.c
index 788a73ff1..763c552ea 100644
--- a/src/chmod.c
+++ b/src/chmod.c
@@ -127,8 +127,8 @@ static struct option const long_options[] =
static bool
mode_changed (int dir_fd, char const *file, char const *file_full_name,
- mode_t old_mode, mode_t new_mode, bool ignore_missing,
- bool *file_missing)
+ 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))
@@ -138,7 +138,8 @@ mode_changed (int dir_fd, char const *file, char const
*file_full_name,
struct stat new_stats;
- if (fstatat (dir_fd, file, &new_stats, 0) != 0)
+ if (fstatat (dir_fd, file, &new_stats,
+ follow ? 0 : AT_SYMLINK_NOFOLLOW) != 0)
{
*file_missing = (ignore_missing
&& ignorable_traversal_errno (errno));
@@ -359,11 +360,11 @@ process_file (FTS *fts, FTSENT *ent)
return false;
}
+ bool follow_symlink = !!dereference;
+ if (dereference == -1) /* -H with/without -R, -P without -R. */
+ follow_symlink = ent->fts_level == FTS_ROOTLEVEL;
if (ch.status == CH_NOT_APPLIED)
{
- bool follow_symlink = !!dereference;
- if (dereference == -1) /* -H with/without -R, -P without -R. */
- follow_symlink = ent->fts_level == FTS_ROOTLEVEL;
int chmod_errno = adjust_file_mode (fts->fts_cwd_fd, file,
follow_symlink,
file_stats, &ch);
if (! chmod_errno)
@@ -402,8 +403,8 @@ process_file (FTS *fts, FTSENT *ent)
bool file_missing;
if (ch.status == CH_SUCCEEDED
&& !mode_changed (fts->fts_cwd_fd, file, file_full_name,
- ch.old_mode, ch.new_mode, ignore_missing,
- &file_missing))
+ ch.old_mode, ch.new_mode, follow_symlink,
+ ignore_missing, &file_missing))
{
if (file_missing)
return true;
diff --git a/tests/chmod/c-option.sh b/tests/chmod/c-option.sh
index f2d0bae61..b5e134d00 100755
--- a/tests/chmod/c-option.sh
+++ b/tests/chmod/c-option.sh
@@ -48,4 +48,12 @@ chmod g+s a/b
chmod -c -R g+w a 2>err
compare /dev/null err || fail=1
+# Verify special bits on the link itself, even when its target is missing.
+ln -s missing link || framework_failure_
+if chmod -h 1755 link 2>/dev/null; then
+ chmod -ch 1755 link >out 2>err || fail=1
+ compare /dev/null out || fail=1
+ compare /dev/null err || fail=1
+fi
+
Exit $fail
--
2.56.0