X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=libbb%2Fcopy_file.c;h=d804eccfaee8009cf55f6957233f6685b9f1e1f9;hb=defc1ea34074e7882724c460260d307cdf981a70;hp=86449f27c853b0b654939a07d0243479e4ce1db6;hpb=fa05074eeed1a6a25393d4b466c7512112f39104;p=oweals%2Fbusybox.git diff --git a/libbb/copy_file.c b/libbb/copy_file.c index 86449f27c..d804eccfa 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c @@ -29,6 +29,7 @@ // 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 @@ -50,7 +51,16 @@ static int ask_and_unlink(const char *dest, int flags) 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) @@ -61,7 +71,7 @@ static int ask_and_unlink(const char *dest, int flags) * 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 */ @@ -71,6 +81,7 @@ int copy_file(const char *source, const char *dest, int flags) 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) { @@ -141,6 +152,8 @@ int copy_file(const char *source, const char *dest, int flags) 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); @@ -217,12 +230,22 @@ int copy_file(const char *source, const char *dest, int flags) 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; @@ -246,18 +269,24 @@ int copy_file(const char *source, const char *dest, int flags) 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) { @@ -265,7 +294,7 @@ int copy_file(const char *source, const char *dest, int flags) 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; @@ -273,8 +302,7 @@ int copy_file(const char *source, const char *dest, int flags) } #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; @@ -301,8 +329,13 @@ int copy_file(const char *source, const char *dest, int flags) } /* ...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... */