[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] cp: clear special mode bits on --parents when preserving ownersh
|
From: |
Collin Funk |
|
Subject: |
[PATCH] cp: clear special mode bits on --parents when preserving ownership fails |
|
Date: |
Mon, 16 Nov 2026 18:55:39 -0800 |
* src/cp.c (re_protect): Clear special mode bits when lchownat fails.
* tests/cp/special-bits.sh: Add a test case.
* NEWS: Mention the bug fix.
Discovered by Anthropic's OSS Scanner, as bug ANT-2026-5A56RQA9.
---
NEWS | 4 ++++
src/cp.c | 8 +++++++-
tests/cp/special-bits.sh | 18 ++++++++++++++++++
3 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/NEWS b/NEWS
index 58850e489..53cce2d73 100644
--- a/NEWS
+++ b/NEWS
@@ -69,6 +69,10 @@ GNU coreutils NEWS -*-
outline -*-
it creates and their sources.
[This bug was present in "the beginning".]
+ 'cp -p --parents' now clears special mode bits on the parent directories it
+ creates when their ownership cannot be preserved, as is done for 'cp -p -R'.
+ [This bug was present in "the beginning".]
+
'cp', 'mv', and 'install' no longer loop indefinitely when creating a
numbered backup in an unreadable directory if the guessed backup name
already exists. They now diagnose the failure and leave the files intact.
diff --git a/src/cp.c b/src/cp.c
index 587a28e10..1af5f78cf 100644
--- a/src/cp.c
+++ b/src/cp.c
@@ -404,6 +404,8 @@ re_protect (char const *const_dst_name, char const
*dst_src_name,
}
}
+ mode_t src_mode = p->st.st_mode;
+
if (x->preserve_ownership)
{
if (lchownat (dst_dirfd, relname, p->st.st_uid, p->st.st_gid)
@@ -418,12 +420,16 @@ re_protect (char const *const_dst_name, char const
*dst_src_name,
/* Failing to preserve ownership is OK. Still, try to preserve
the group, but ignore the possible error. */
ignore_value (lchownat (dst_dirfd, relname, -1, p->st.st_gid));
+
+ /* Clear special mode bits, as is done in copy_internal
+ and copy_reg. */
+ src_mode &= ~ (S_ISUID | S_ISGID | S_ISVTX);
}
}
if (x->preserve_mode)
{
- if (xcopy_acl (src_name, -1, dst_name, -1, p->st.st_mode) != 0)
+ if (xcopy_acl (src_name, -1, dst_name, -1, src_mode) != 0)
return false;
}
else if (p->restore_mode)
diff --git a/tests/cp/special-bits.sh b/tests/cp/special-bits.sh
index 947a07a05..e8bd993a2 100755
--- a/tests/cp/special-bits.sh
+++ b/tests/cp/special-bits.sh
@@ -48,4 +48,22 @@ set _ $(ls -l c); shift; p1=$1
set _ $(ls -l c2); shift; p2=$1
test $p1 = $p2 && fail=1
+# Likewise for parent directories created by --parents.
+mkdir -p d/e dst dst2 &&
+touch d/e/f &&
+chmod a=rx,ug+s,+t d &&
+chown $NON_ROOT_USERNAME dst2 || framework_failure_
+
+cp -p --parents d/e/f dst || fail=1
+set _ $(ls -ld d); shift; p1=$1
+set _ $(ls -ld dst/d); shift; p2=$1
+test $p1 = $p2 || fail=1
+
+# coreutils-9.12 and earlier would not clear special mode bits when
+# chown failed.
+chroot --skip-chdir --user=$NON_ROOT_USERNAME / env PATH="$PATH" \
+ cp -p --parents d/e/f dst2 || fail=1
+set _ $(ls -ld dst2/d); shift; p2=$1
+case $p2 in dr-xr-xr-x*) ;; *) fail=1;; esac
+
Exit $fail
--
2.56.1
- [PATCH] cp: clear special mode bits on --parents when preserving ownership fails,
Collin Funk <=