Subject: Re: bug#50784: coreutils 9.0 sometimes gets spurious failures in chmod -R

From: Pádraig Brady <[email-masked]>
Date: Fri, 24 Sep 2021 21:02:56 +0100

On 24/09/2021 20:29, Pádraig Brady wrote:
> On 24/09/2021 19:21, Daniel Schepler wrote:
>> After I upgraded my system (locally built) to coreutils 9.0, I noticed
>> that my lightly modified sbuild started getting build failures when it
>> tries to run "chmod -R g+w /<<BUILDDIR>>" (as root) in the chroot.
>> I'm not sure exactly what combination of files triggers the error, but
>> I'm able to reproduce it roughly as follows:
>>
>> cd /tmp
>> tar -x -f .../coreutils-9.0.tar.xz
>> cd coreutils-9.0
>> mkdir obj-x86_64-linux-gnu
>> cd obj-x86_64-linux-gnu
>> ../configure --prefix=/usr
>> make -j8
>> chmod -R u+w /tmp/coreutils-9.0/obj-x86_64-linux-gnu
>> echo $?
>>
>> In the last chmod command, there are no failures reported; and even if
>> I run it under strace, I see no hints of any failed system calls other
>> than one in access("/etc/ld.so.preload", R_OK) at startup -- and
>> certainly no failures in the fchmodat() system calls.  Yet the exit
>> code from the process is 1.
>>
>> I wasn't seeing any such errors under sbuild using coreutils 8.32.
> 
> Drats. I think it's determining ignoring symlinks as an error.
> Could you try with the following untested diff:
> 
> diff --git a/src/chmod.c b/src/chmod.c
> index 37b04f500..9ab60898d 100644
> --- a/src/chmod.c
> +++ b/src/chmod.c
> @@ -322,6 +322,9 @@ process_file (FTS *fts, FTSENT *ent)
>      if ( ! recurse)
>        fts_set (fts, ent, FTS_SKIP);
> 
> +  if (ch.status == CH_NOT_APPLIED)  /* For symlinks.  */
> +    ch.status = CH_NO_CHANGE_REQUESTED;
> +
>      return CH_NO_CHANGE_REQUESTED <= ch.status;
>    }

Tested equivalent patch is attached.
>From 4342a8d24f4eaafbeb9f918973bf531517179e48 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email-masked]>
Date: Fri, 24 Sep 2021 20:57:41 +0100
Subject: [PATCH] chmod: fix exit status when ignoring symlinks

* src/chmod.c: Reorder enum so CH_NOT_APPLIED
can be treated as a non error.
* tests/chmod/ignore-symlink.sh: A new test.
* tests/local.mk: Reference the new test.
Fixes https://bugs.gnu.org/50784
---
 src/chmod.c                   |  4 ++--
 tests/chmod/ignore-symlink.sh | 31 +++++++++++++++++++++++++++++++
 tests/local.mk                |  1 +
 3 files changed, 34 insertions(+), 2 deletions(-)
 create mode 100755 tests/chmod/ignore-symlink.sh

diff --git a/src/chmod.c b/src/chmod.c
index 37b04f500..57ac47f33 100644
--- a/src/chmod.c
+++ b/src/chmod.c
@@ -44,8 +44,8 @@ struct change_status
   enum
     {
       CH_NO_STAT,
-      CH_NOT_APPLIED,
       CH_FAILED,
+      CH_NOT_APPLIED,
       CH_NO_CHANGE_REQUESTED,
       CH_SUCCEEDED
     }
@@ -322,7 +322,7 @@ process_file (FTS *fts, FTSENT *ent)
   if ( ! recurse)
     fts_set (fts, ent, FTS_SKIP);
 
-  return CH_NO_CHANGE_REQUESTED <= ch.status;
+  return CH_NOT_APPLIED <= ch.status;
 }
 
 /* Recursively change the modes of the specified FILES (the last entry
diff --git a/tests/chmod/ignore-symlink.sh b/tests/chmod/ignore-symlink.sh
new file mode 100755
index 000000000..5ce3de816
--- /dev/null
+++ b/tests/chmod/ignore-symlink.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+# Test for proper exit code of chmod on a processed symlink.
+
+# Copyright (C) 2021 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
+
+mkdir dir || framework_failure_
+touch dir/f || framework_failure_
+ln -s f dir/l || framework_failure_
+
+# This operation ignores symlinks but should succeed.
+chmod u+w -R dir 2> out || fail=1
+
+compare /dev/null out || fail=1
+
+Exit $fail
diff --git a/tests/local.mk b/tests/local.mk
index 228d0e368..b5b893fb7 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -456,6 +456,7 @@ all_tests =					\
   tests/chmod/c-option.sh			\
   tests/chmod/equal-x.sh			\
   tests/chmod/equals.sh				\
+  tests/chmod/ignore-symlink.sh			\
   tests/chmod/inaccessible.sh			\
   tests/chmod/octal.sh				\
   tests/chmod/setgid.sh				\
-- 
2.26.2