// (or fail, if it points to dir/nonexistent location/etc).
// This is strange, but POSIX-correct.
// coreutils cp has --remove-destination to override this...
+//
+// NB: we have special code which still allows for "cp file /dev/node"
+// to work POSIX-ly (the only realistic case where it makes sense)
#define DO_POSIX_CP 0 /* 1 - POSIX behavior, 0 - safe behavior */
// 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) {
if (S_ISDIR(source_stat.st_mode)) {
DIR *dp;
- const char *existing_name;
+ const char *tp;
struct dirent *d;
mode_t saved_umask = 0;
}
/* Did we ever create source ourself before? */
- existing_name = is_in_ino_dev_hashtable(&source_stat);
- if (existing_name) {
+ tp = is_in_ino_dev_hashtable(&source_stat);
+ if (tp) {
/* We did! it's a recursion! man the lifeboats... */
bb_error_msg("recursion detected, omitting directory '%s'",
- existing_name);
+ source);
return -1;
}
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 (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) {
- char *link_target;
+ 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;
link_target = is_in_ino_dev_hashtable(&source_stat);
if (link_target) {
if (link(link_target, dest) < 0) {
if (src_fd < 0)
return -1;
-#if DO_POSIX_CP /* POSIX way (a security problem versus symlink attacks!): */
- dst_fd = open(dest, (flags & FILEUTILS_INTERACTIVE)
- ? O_WRONLY|O_CREAT|O_EXCL
- : O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
-#else /* safe way: */
- dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
-#endif
+ /* 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 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_ISLNK(dest_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, 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... */