Fix copyfile() not preserving source mode with restrictive umask

copyfile() passes the source mode to open(2), which applies the umask,
so a 0644 file copied with umask 0027 ends up 0640.

Apply the source mode with fchmod() after creating the destination.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-09-13 18:53:40 +02:00
1 parent e874608b76
commit b69d8a6013
3 files changed
+34 -1

No files matched your search

+8
View File
@@ -208,12 +208,20 @@ ssize_t copyfile(const char *src, const char *dst, int len, int opt)
goto exit;
}
/* open(2) applies umask, we want the source file mode */
if (fchmod(out, st.st_mode & 07777)) {
saved_errno = errno;
size = -1;
goto close_exit;
}
size = do_copy(in, out, num, buffer, BUFSIZ);
if (size < 0)
saved_errno = errno;
else if (keep_mtim)
set_mtime(in, out);
close_exit:
close(out);
close(in);