[git commit] cp: fix -i for POSIX mode. Closes 9106

Denys Vlasenko vda.linux at googlemail.com
Sat Aug 13 21:23:48 UTC 2016


commit: https://git.busybox.net/busybox/commit/?id=98c50f93fe494be5547188813c367299db80dbc5
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 libbb/copy_file.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/libbb/copy_file.c b/libbb/copy_file.c
index 7801b58..23c0f83 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)
 		if (!S_ISREG(source_stat.st_mode))
 			new_mode = 0666;
 
-		// POSIX way is a security problem versus (sym)link attacks
-		if (!ENABLE_FEATURE_NON_POSIX_CP) {
-			dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, new_mode);
-		} else { /* safe way: */
+		if (ENABLE_FEATURE_NON_POSIX_CP || (flags & FILEUTILS_INTERACTIVE)) {
+			/*
+			 * O_CREAT|O_EXCL: require that file did not exist before creation
+			 */
 			dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode);
+		} else { /* POSIX, and not "cp -i" */
+			/*
+			 * O_CREAT|O_TRUNC: create, or truncate (security problem versus (sym)link attacks)
+			 */
+			dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, new_mode);
 		}
 		if (dst_fd == -1) {
 			ovr = ask_and_unlink(dest, flags);


More information about the busybox-cvs mailing list