Collin Funk <[email protected]> writes: > Pádraig Brady <[email protected]> writes: > >> On 17/11/2026 02:55, Collin Funk wrote: >>> * 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. >> >> I think this may also be needed to handle the `cp --preserve=owner >> --parents` case? > > I was thinking about that today as well. Let me do some more testing.
I think my original patch is correct. I've attached a v2 patch with some additional tests. If you add your change, you'll see the sticky bit is missed when using --preserve=owner. Collin
>From 08b042185625dc97143e3f7c19b0fa38003b8c36 Mon Sep 17 00:00:00 2001 Message-ID: <08b042185625dc97143e3f7c19b0fa38003b8c36.1794970967.git.collin.fu...@gmail.com> From: Collin Funk <[email protected]> Date: Mon, 16 Nov 2026 18:51:51 -0800 Subject: [PATCH v2] cp: clear special mode bits on --parents when preserving ownership fails * src/cp.c (re_protect): Clear special mode bits when lchownat fails. * tests/cp/special-bits.sh: Add some test cases. * 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 | 52 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index d2f707c4b..216cec450 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..0c4973a92 100755 --- a/tests/cp/special-bits.sh +++ b/tests/cp/special-bits.sh @@ -48,4 +48,56 @@ 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 --recursive and --parents. +mkdir -p d/e && +touch d/e/f && +chmod a=rx,ug+s,+t d d/e || framework_failure_ +for opt in --recursive --parents; do + rm -rf dst dst2 && + mkdir -p dst dst2 && + chown $NON_ROOT_USERNAME dst2 || framework_failure_ + + src=d + test $opt = --parents && src=d/e/f + + cp -p $opt $src dst || fail=1 + set _ $(ls -ld d); shift; p1=$1 + set _ $(ls -ld dst/d); shift; p2=$1 + set _ $(ls -ld dst/d/e); shift; p3=$1 + test $p1 = $p2 || fail=1 + test $p1 = $p3 || fail=1 + + # coreutils-9.12 and earlier would not clear special mode bits + # on parent directories created by --parents. + chroot --skip-chdir --user=$NON_ROOT_USERNAME / env PATH="$PATH" \ + cp -p $opt $src dst2 || fail=1 + set _ $(ls -ld dst2/d); shift; p2=$1 + set _ $(ls -ld dst2/d/e); shift; p3=$1 + case $p2 in dr-xr-xr-x*) ;; *) fail=1 ;; esac + case $p3 in dr-xr-xr-x*) ;; *) fail=1 ;; esac +done + +# Test the behavior when --preserve=ownership is used. +for opt in --recursive --parents; do + rm -rf dst dst2 && + mkdir -p dst dst2 && + chown $NON_ROOT_USERNAME dst2 || framework_failure_ + + src=d + test $opt = --parents && src=d/e/f + + cp --preserve=ownership $opt $src dst || fail=1 + set _ $(ls -ld dst/d); shift; p2=$1 + set _ $(ls -ld dst/d/e); shift; p3=$1 + case $p2 in dr-xr-xr-t*) ;; *) fail=1 ;; esac + case $p3 in dr-xr-xr-t*) ;; *) fail=1 ;; esac + + chroot --skip-chdir --user=$NON_ROOT_USERNAME / env PATH="$PATH" \ + cp --preserve=ownership $opt $src dst2 || fail=1 + set _ $(ls -ld dst2/d); shift; p2=$1 + set _ $(ls -ld dst2/d/e); shift; p3=$1 + case $p2 in dr-xr-xr-t*) ;; *) fail=1 ;; esac + case $p3 in dr-xr-xr-t*) ;; *) fail=1 ;; esac +done + Exit $fail -- 2.56.1
