6cfad1
From ae65651eab053fc6dc4590dbb863a268215c1fc5 Mon Sep 17 00:00:00 2001
6cfad1
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
6cfad1
Date: Fri, 8 Jun 2018 11:45:40 +0100
6cfad1
Subject: [PATCH] [PATCH] Remove existing files before overwriting them
6cfad1
6cfad1
Archive should extract only the latest same-named entry.
6cfad1
Extracted regular file should not be writtent into existing block
6cfad1
device (or any other one).
6cfad1
6cfad1
https://rt.cpan.org/Ticket/Display.html?id=125523
6cfad1
6cfad1
Signed-off-by: Chris 'BinGOs' Williams <chris@bingosnet.co.uk>
6cfad1
---
6cfad1
 lib/Archive/Tar.pm | 14 ++++++++++++++
6cfad1
 1 file changed, 14 insertions(+)
6cfad1
6cfad1
diff --git a/lib/Archive/Tar.pm b/lib/Archive/Tar.pm
6cfad1
index 6244369..a83975f 100644
6cfad1
--- a/lib/Archive/Tar.pm
6cfad1
+++ b/lib/Archive/Tar.pm
6cfad1
@@ -842,6 +842,20 @@ sub _extract_file {
6cfad1
         return;
6cfad1
     }
6cfad1
 
6cfad1
+    ### If a file system already contains a block device with the same name as
6cfad1
+    ### the being extracted regular file, we would write the file's content
6cfad1
+    ### to the block device. So remove the existing file (block device) now.
6cfad1
+    ### If an archive contains multiple same-named entries, the last one
6cfad1
+    ### should replace the previous ones. So remove the old file now.
6cfad1
+    ### If the old entry is a symlink to a file outside of the CWD, the new
6cfad1
+    ### entry would create a file there. This is CVE-2018-12015
6cfad1
+    ### <https://rt.cpan.org/Ticket/Display.html?id=125523>.
6cfad1
+    if (-l $full || -e _) {
6cfad1
+	if (!unlink $full) {
6cfad1
+	    $self->_error( qq[Could not remove old file '$full': $!] );
6cfad1
+	    return;
6cfad1
+	}
6cfad1
+    }
6cfad1
     if( length $entry->type && $entry->is_file ) {
6cfad1
         my $fh = IO::File->new;
6cfad1
         $fh->open( '>' . $full ) or (