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.
This commit is contained in:
Collin Funk committed 2026-11-14 15:10:37 -08:00
1 parent a80621e6aa
commit 35492ca115
6 files changed
+57 -9

No files matched your search

+7
View File
@@ -140,6 +140,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.
+16 -5
View File
@@ -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));
+6
View File
@@ -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;
+2
View File
@@ -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;
+24 -4
View File
@@ -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);
+2
View File
@@ -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;