Thai Duong <[email protected]> writes:

> Thanks, will do!

And thanks to you for the report! It was reported by Anthropic's OSS
scanner as well, which is interesting. I'll reference both reports in
the commit message.

I've attached a patch that should fix the issue. However, I'm going to
hold off on pushing until I write some additional tests tomorrow, since
the changes mean chown/chmod can be called from two separate places, one
of which is shared with 'cp' and 'mv'.

Avoiding this race, and some other ones around this area, means using
reflinks less on macOS, which I am not super happy about. As far as I
know, Linux is the only operating system with the ability to reflink
from two file descriptors. The macOS API is marginally better than
Solaris since you can reflink from a source file descriptor, but you
still need to open the destination afterward, which may be replaced with
something else.

Collin

>From 89798592ecc2c482df091c7fb20d93264f680b17 Mon Sep 17 00:00:00 2001
Message-ID: <89798592ecc2c482df091c7fb20d93264f680b17.1794641499.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Fri, 13 Nov 2026 23:12:41 -0800
Subject: [PATCH] install: don't follow symlinks when setting the mode

* src/copy.h (struct cp_options): Add the owner and group fields.
* src/cp.c (cp_option_init): Initialize them to -1.
* src/mv.c (cp_option_init): Likewise.
* src/install.c (cp_option_init): Likewise.
(change_attributes): Use lchmodat instead of chmodat. Only set the
attributes here if 'strip' is used.
(change_timestamps): Use AT_SYMLINK_NOFOLLOW when calling utimensat.
(main): If --strip is not in use, have copy set the mode and ownership
for us with the file descriptor it has open.
* src/copy.c (copy_reg): Don't use fclonefileat if we have to set the
mode, since it would require a subsequent open. If the owner and/or
group are given in the options, set the ownership.
(copy_internal): Omit permissions if set_mod in the options is set.
* NEWS: Mention the bug fix.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-HJARXJNW.
Discovered by Calif.io, as bug ANT-2026-J82F5KHS.
---
 NEWS          |  7 +++++++
 src/copy.c    | 21 ++++++++++++++++-----
 src/copy.h    |  6 ++++++
 src/cp.c      |  2 ++
 src/install.c | 28 ++++++++++++++++++++++++----
 src/mv.c      |  2 ++
 6 files changed, 57 insertions(+), 9 deletions(-)

diff --git a/NEWS b/NEWS
index d890729f4..0dd2ed427 100644
--- a/NEWS
+++ b/NEWS
@@ -129,6 +129,13 @@ GNU coreutils NEWS                                    -*- outline -*-
   path components for an install file are replaced with symlinks.
   [bug introduced in coreutils-6.12]
 
+  'install' no longer follows symbolic links in the last component of
+  destination files when setting their permissions.  Previously, a newly
+  created file could be replaced with a symbolic link by the owner of
+  its parent directory to change the permissions of the file it
+  referenced.
+  [This bug was present in "the beginning".]
+
   'ls' no longer omits ACL indicators or capability coloring for subsequent
   files on the same file system after an unsupported security context
   lookup on a file without an ACL.
diff --git a/src/copy.c b/src/copy.c
index 3760b558b..8ee3a890e 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -852,8 +852,10 @@ copy_reg (char const *src_name, char const *dst_name,
          Use CLONE_NOFOLLOW to avoid security issues that could occur
          if writing through dangling symlinks.  Although the circa
          2023 macOS documentation doesn't say so, CLONE_NOFOLLOW
-         affects the destination file too.  */
-      if (data_copy_required && x->reflink_mode
+         affects the destination file too.  Also, avoid using
+         fclonefileat when we need to set the mode of the file, to
+         avoid the race when using chown and/or chmod.  */
+      if (data_copy_required && x->reflink_mode && ! x->set_mode
           && (CLONE_NOOWNERCOPY || x->preserve_ownership))
         {
           /* Try fclonefileat so long as it won't create the
@@ -863,7 +865,6 @@ copy_reg (char const *src_name, char const *dst_name,
           mode_t cloned_mode = src_mode & cloned_mode_bits;
           mode_t desired_mode
             = (x->preserve_mode ? src_mode & CHMOD_MODE_BITS
-               : x->set_mode ? x->mode
                : ((x->explicit_no_preserve_mode ? MODE_RW_UGO : dst_mode)
                   & ~ cached_umask ()));
           if (! (cloned_mode & ~desired_mode))
@@ -1098,7 +1099,17 @@ set_dest_mode:
     }
   else if (x->set_mode)
     {
-      if (xset_acl (dst_name, dest_desc, x->mode) != 0)
+      /* Set the ownership with chown before chmod since on some systems
+         chown clears the set[ug]id bits.  */
+      if (! (x->owner == (uid_t) -1 && x->group == (gid_t) -1)
+          && fchown_or_lchown (dest_desc, dst_dirfd, dst_relname,
+                               x->owner, x->group) < 0)
+        {
+          error (0, errno, _("cannot change ownership of %s"),
+                 quoteaf (dst_name));
+          return_val = false;
+        }
+      else if (xset_acl (dst_name, dest_desc, x->mode) != 0)
         return_val = false;
     }
   else if (x->explicit_no_preserve_mode && *new_dst)
@@ -2294,7 +2305,7 @@ skip:
   dst_mode_bits = (x->set_mode ? x->mode : src_mode) & CHMOD_MODE_BITS;
   omitted_permissions =
     (dst_mode_bits
-     & (x->preserve_ownership ? S_IRWXG | S_IRWXO
+     & (x->preserve_ownership || x->set_mode ? S_IRWXG | S_IRWXO
         : S_ISDIR (src_mode) ? S_IWGRP | S_IWOTH
         : 0));
 
diff --git a/src/copy.h b/src/copy.h
index ead096683..9f25a611e 100644
--- a/src/copy.h
+++ b/src/copy.h
@@ -131,6 +131,12 @@ struct cp_options
      if SET_MODE is nonzero.  */
   mode_t mode;
 
+  /* Set the ownership of the destination file to OWNER:GROUP if
+     SET_MODE is true.  Either or both values can be set to -1 to
+     leave that ID unchanged.  */
+  uid_t owner;
+  gid_t group;
+
   /* If true, copy all files except directories (and, if not dereferencing
      them, symbolic links) as if they were regular files.  */
   bool copy_as_regular;
diff --git a/src/cp.c b/src/cp.c
index 1ba3a4fa7..587a28e10 100644
--- a/src/cp.c
+++ b/src/cp.c
@@ -914,6 +914,8 @@ cp_option_init (struct cp_options *x)
   x->symbolic_link = false;
   x->set_mode = false;
   x->mode = 0;
+  x->owner = (uid_t) -1;
+  x->group = (uid_t) -1;
 
   /* Not used.  */
   x->stdin_tty = false;
diff --git a/src/install.c b/src/install.c
index 289a9d528..5883da583 100644
--- a/src/install.c
+++ b/src/install.c
@@ -287,6 +287,12 @@ cp_option_init (struct cp_options *x)
      would fail.  */
   x->set_mode = true;
   x->mode = S_IRUSR | S_IWUSR;
+
+  /* May be overridden by --owner.  */
+  x->owner = (uid_t) -1;
+  /* May be overridden by --group.  */
+  x->group = (gid_t) -1;
+
   x->stdin_tty = false;
 
   x->open_dangling_dest_symlink = false;
@@ -452,10 +458,14 @@ change_attributes (char const *name, int dirfd, char const *relname)
      If the file doesn't end up with the group they asked for, they'll
      want to know.  */
 
-  if (! (owner_id == (uid_t) -1 && group_id == (gid_t) -1)
-      && lchownat (dirfd, relname, owner_id, group_id) != 0)
+  /* If we aren't using 'strip', then the copy function set the
+     ownership and permissions for us.  */
+  if (! strip_files)
+    ok = true;
+  else if (! (owner_id == (uid_t) -1 && group_id == (gid_t) -1)
+           && lchownat (dirfd, relname, owner_id, group_id) != 0)
     error (0, errno, _("cannot change ownership of %s"), quoteaf (name));
-  else if (chmodat (dirfd, relname, mode) != 0)
+  else if (lchmodat (dirfd, relname, mode) != 0)
     error (0, errno, _("cannot change permissions of %s"), quoteaf (name));
   else
     ok = true;
@@ -476,7 +486,7 @@ change_timestamps (struct stat const *src_sb, char const *dest,
   struct timespec timespec[2] = { get_stat_atime (src_sb),
                                   get_stat_mtime (src_sb) };
 
-  if (utimensat (dirfd, relname, timespec, 0))
+  if (utimensat (dirfd, relname, timespec, AT_SYMLINK_NOFOLLOW) < 0)
     {
       error (0, errno, _("cannot set timestamps for %s"), quoteaf (dest));
       return false;
@@ -1080,8 +1090,18 @@ main (int argc, char **argv)
     error (0, 0, _("the --compare (-C) option is ignored when you"
                    " specify a mode with non-permission bits"));
 
+  /* Initialize OWNER_ID and GROUP_ID from the given names.  */
   get_ids ();
 
+  /* If we are not stripping the file, set up the copy options so that
+     the copy function sets the mode and ownership using file descriptors.  */
+  if (! strip_files)
+    {
+      x.mode = mode;
+      x.owner = owner_id;
+      x.group = group_id;
+    }
+
   int exit_status = EXIT_SUCCESS;
   if (dir_arg)
     exit_status = savewd_process_files (n_files, file, process_dir, &x);
diff --git a/src/mv.c b/src/mv.c
index 7f0d4f8c3..aa4d32953 100644
--- a/src/mv.c
+++ b/src/mv.c
@@ -151,6 +151,8 @@ cp_option_init (struct cp_options *x)
   x->symbolic_link = false;
   x->set_mode = false;
   x->mode = 0;
+  x->owner = (uid_t) -1;
+  x->group = (gid_t) -1;
   x->stdin_tty = isatty (STDIN_FILENO);
 
   x->open_dangling_dest_symlink = false;
-- 
2.56.1

Reply via email to