diff options
-rw-r--r-- | libbb/copy_file.c | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/libbb/copy_file.c b/libbb/copy_file.c index 7801b58c7..23c0f8320 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c | |||
@@ -296,11 +296,16 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) | |||
296 | if (!S_ISREG(source_stat.st_mode)) | 296 | if (!S_ISREG(source_stat.st_mode)) |
297 | new_mode = 0666; | 297 | new_mode = 0666; |
298 | 298 | ||
299 | // POSIX way is a security problem versus (sym)link attacks | 299 | if (ENABLE_FEATURE_NON_POSIX_CP || (flags & FILEUTILS_INTERACTIVE)) { |
300 | if (!ENABLE_FEATURE_NON_POSIX_CP) { | 300 | /* |
301 | dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, new_mode); | 301 | * O_CREAT|O_EXCL: require that file did not exist before creation |
302 | } else { /* safe way: */ | 302 | */ |
303 | dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode); | 303 | dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode); |
304 | } else { /* POSIX, and not "cp -i" */ | ||
305 | /* | ||
306 | * O_CREAT|O_TRUNC: create, or truncate (security problem versus (sym)link attacks) | ||
307 | */ | ||
308 | dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, new_mode); | ||
304 | } | 309 | } |
305 | if (dst_fd == -1) { | 310 | if (dst_fd == -1) { |
306 | ovr = ask_and_unlink(dest, flags); | 311 | ovr = ask_and_unlink(dest, flags); |