// for POSIX mode to give reasonable error message
static int ask_and_unlink(const char *dest, int flags)
{
+ int e = errno;
#if DO_POSIX_CP
if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
// Either it exists, or the *path* doesnt exist
return 0; // not allowed to overwrite
}
if (unlink(dest) < 0) {
- bb_perror_msg("cannot remove '%s'", dest);
+#if ENABLE_FEATURE_VERBOSE_CP_MESSAGE
+ if (e == errno && e == ENOENT) {
+ /* e == ENOTDIR is similar: path has non-dir component,
+ * but in this case we don't even reach copy_file() */
+ bb_error_msg("cannot create '%s': Path does not exist", dest);
+ return -1; // error
+ }
+#endif
+ errno = e;
+ bb_perror_msg("cannot create '%s'", dest);
return -1; // error
}
return 1; // ok (to try again)
* 0 copy is made or user answered "no" in interactive mode
* (failures to preserve mode/owner/times are not reported in exit code)
*/
-int copy_file(const char *source, const char *dest, int flags)
+int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
{
/* This is a recursive function, try to minimize stack usage */
/* NB: each struct stat is ~100 bytes */
signed char dest_exists = 0;
signed char ovr;
+/* Inverse of cp -d ("cp without -d") */
#define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
bb_error_msg("target '%s' is not a directory", dest);
return -1;
}
+ /* race here: user can substitute a symlink between
+ * this check and actual creation of files inside dest */
} else {
mode_t mode;
saved_umask = umask(0);
return 0;
}
- if (S_ISREG(source_stat.st_mode)
- /* DEREF uses stat, which never returns S_ISLNK() == true. */
+ if (/* "cp thing1 thing2" without -R: just open and read() from thing1 */
+ !(flags & FILEUTILS_RECUR)
+ /* "cp [-opts] regular_file thing2" */
+ || S_ISREG(source_stat.st_mode)
+ /* DEREF uses stat, which never returns S_ISLNK() == true.
+ * So the below is never true: */
/* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
) {
int src_fd;
int dst_fd;
+ mode_t new_mode;
+
+ if (!FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) {
+ /* "cp -d symlink dst": create a link */
+ goto dont_cat;
+ }
if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) {
const char *link_target;
if (src_fd < 0)
return -1;
+ /* Do not try to open with weird mode fields */
+ new_mode = source_stat.st_mode;
+ if (!S_ISREG(source_stat.st_mode))
+ new_mode = 0666;
+
/* POSIX way is a security problem versus symlink attacks,
- * we do it only for dest's which are device nodes,
- * and only for non-recursive, non-interactive cp. NB: it is still racy
- * for "cp file /home/bad_user/device_node" case
- * (user can rm device_node and create link to /etc/passwd) */
+ * we do it only for non-symlinks, and only for non-recursive,
+ * non-interactive cp. NB: it is still racy
+ * for "cp file /home/bad_user/file" case
+ * (user can rm file and create a link to /etc/passwd) */
if (DO_POSIX_CP
- || (dest_exists && !(flags & (FILEUTILS_RECUR|FILEUTILS_INTERACTIVE))
- && (S_ISBLK(dest_stat.st_mode) || S_ISCHR(dest_stat.st_mode)))
+ || (dest_exists
+ && !(flags & (FILEUTILS_RECUR|FILEUTILS_INTERACTIVE))
+ && !S_ISLNK(dest_stat.st_mode))
) {
- dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
+ dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, new_mode);
} else /* safe way: */
- dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
+ dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode);
if (dst_fd == -1) {
ovr = ask_and_unlink(dest, flags);
if (ovr <= 0) {
return ovr;
}
/* It shouldn't exist. If it exists, do not open (symlink attack?) */
- dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
+ dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode);
if (dst_fd < 0) {
close(src_fd);
return -1;
}
#if ENABLE_SELINUX
- if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT)
- || (flags & FILEUTILS_SET_SECURITY_CONTEXT))
+ if ((flags & (FILEUTILS_PRESERVE_SECURITY_CONTEXT|FILEUTILS_SET_SECURITY_CONTEXT))
&& is_selinux_enabled() > 0
) {
security_context_t con;
}
/* ...but read size is already checked by bb_copyfd_eof */
close(src_fd);
+ /* "cp /dev/something new_file" should not
+ * copy mode of /dev/something */
+ if (!S_ISREG(source_stat.st_mode))
+ return retval;
goto preserve_mode_ugid_time;
}
+ dont_cat:
/* Source is a symlink or a special file */
/* We are lazy here, a bit lax with races... */