Subject: bug#65599: closed (Re: bug#65599: mv and cp give a pointless warning when moving/copying a directory)

From: [email-masked] (GNU bug Tracking System)
Date: Sat, 02 Sep 2023 20:45:02 +0000

Your bug report

#65599: mv and cp give a pointless warning when moving/copying a directory

which was filed against the coreutils package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
65599: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=65599
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#65599: mv and cp give a pointless warning when moving/copying a directory
From: Paul Eggert <[email-masked]>
To: Bruno Haible <[email-masked]>
Cc: [email-masked]
Date: Sat, 2 Sep 2023 13:44:11 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

On 2023-09-02 04:18, Bruno Haible wrote:
> chmod fails with EACCES, hence per POSIX
> <https://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html>
> it's buggy.

Thanks, I reported the bug with chmod, chown, etc. to the linux-cifs 
mailing list, here:

https://lore.kernel.org/linux-cifs/[email-masked]/T/#u

I did some testing with fsetxattr and found that it has a similar issue 
even with ext4; for example, setxattr("/", "a", "b", 1, 0) fails with 
EACCES. This is not a POSIX violation, since POSIX lacks setxattr. 
However it's bad form. And it means coreutils needs the attached 
workaround not just for CIFS, but also for ext4 in some (rare) cases.

I installed the attached workaround on coreutils and am closing the bug 
report. Thanks again for mentioning it.

From 9cd52bd9993163c2ef8b3d62b757c573fb5320df Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sat, 2 Sep 2023 13:27:45 -0700
Subject: [PATCH] cp,mv,install: fix chmod on Linux CIFS

This bug occurs only when temporarily setting the mode to the
intersection of old and new modes when changing ownership.
* src/copy.c (owner_failure_ok): Treat EACCES like EPERM.
---
 NEWS       | 6 +++---
 src/copy.c | 3 ++-
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/NEWS b/NEWS
index 6801832f7..2adea1e11 100644
--- a/NEWS
+++ b/NEWS
@@ -4,9 +4,9 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 ** Bug fixes
 
-  cp, mv, install no longer issue spurious "failed to preserve ownership"
-  diagnostics when copying to GNU/Linux CIFS filesystems.  They do
-  this by working around a Linux CIFS bug.
+  cp, mv, and install no longer issue spurious diagnostics like "failed
+  to preserve ownership" when copying to GNU/Linux CIFS file systems.
+  They do this by working around some Linux CIFS bugs.
 
   numfmt options like --suffix no longer have an arbitrary 127-byte limit.
   [bug introduced with numfmt in coreutils-8.21]
diff --git a/src/copy.c b/src/copy.c
index 0b3de04f3..9f1f3e85a 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -3468,7 +3468,8 @@ chown_failure_ok (struct cp_options const *x)
 static bool
 owner_failure_ok (struct cp_options const *x)
 {
-  return ((errno == EPERM || errno == EINVAL) && !x->owner_privileges);
+  return ((errno == EPERM || errno == EINVAL || errno == EACCES)
+          && !x->owner_privileges);
 }
 
 /* Return the user's umask, caching the result.
-- 
2.39.2

Subject: mv and cp give a pointless warning when moving/copying a directory
From: Bruno Haible <[email-masked]>
To: [email-masked]
Date: Tue, 29 Aug 2023 20:20:02 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

When I move a directory from an ext4 file system to a CIFS v1 file system,
I get a diagnostic
  mv: failed to preserve ownership for '...': Permission denied
although the owner and group of the directory after and before the move
are the same. So, there is actually nothing to warn about.

$ mv --version
mv (GNU coreutils) 9.3.147-d553ab
Copyright (C) 2023 Free Software Foundation, Inc.
...

$ mkdir dir1
$ echo foo > dir1/file1
$ ls -ld dir1
drwxrwxr-x 2 bruno bruno 4096 Aug 26 19:38 dir1
$ strace -o /tmp/mv.log mv dir1 /media/nas/bruno/dir1
mv: failed to preserve ownership for '/media/nas/bruno/dir1': Permission denied
$ echo $?
0
$ ls -ld /media/nas/bruno/dir1
drwxrwxr-x 2 bruno bruno 0 Aug 26 19:38 /media/nas/bruno/dir1

Find attached the strace log file. The essential line is
fchownat(AT_FDCWD, "/media/nas/bruno/dir1", 1000, 1000, AT_SYMLINK_NOFOLLOW) = -1 EACCES (Permission denied)

People say that "chown only works for root anyway" [1], but it's certainly
safer to try the chown nevertheless, because
  - the current user might not be root but might have non-standard capabilities.
  - the target file system may be reporting failure but may do the requested
    action nevertheless,
  - the target file system might map uids (think of NFS or CIFS [2]).

However, the diagnostic should be omitted if the 'mv' program can verify
that the uid and gid of the directory on the target file system is the same
as on the source file system.

Likewise for the 'cp' program:

$ cp --version
cp (GNU coreutils) 9.3.147-d553ab
...

Written by Torbjorn Granlund, David MacKenzie, and Jim Meyering.
$ mkdir dir2
$ echo foo > dir2/file2
$ ls -ld dir2
drwxrwxr-x 2 bruno bruno 4096 Aug 29 20:15 dir2
$ strace -o /tmp/cp.log cp dir2 /media/nas/bruno/dir2
$ strace -o /tmp/cp.log cp -a dir2 /media/nas/bruno/dir2
cp: failed to preserve ownership for '/media/nas/bruno/dir2': Permission denied
$ ls -ld /media/nas/bruno/dir2
drwx------ 2 bruno bruno 0 Aug 29 20:15 /media/nas/bruno/dir2

Here, in cp.log, the essential line is:
fchownat(AT_FDCWD, "/media/nas/bruno/dir2", 1000, 1000, AT_SYMLINK_NOFOLLOW) = -1 EACCES (Permission denied)

Bruno

[1] https://lists.gnu.org/archive/html/emacs-devel/2009-10/msg00094.html
[2] https://linux.die.net/man/8/mount.cifs