2ee7a7
From 5c79b9faae0f1dd67cc8288964c72c12e03884f8 Mon Sep 17 00:00:00 2001
2ee7a7
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
2ee7a7
Date: Fri, 15 Jun 2018 14:49:47 +0200
2ee7a7
Subject: [PATCH] Prevent from traversing symlinks and parent directories when
2ee7a7
 extracting
2ee7a7
MIME-Version: 1.0
2ee7a7
Content-Type: text/plain; charset=UTF-8
2ee7a7
Content-Transfer-Encoding: 8bit
2ee7a7
2ee7a7
If an attacker-supplied archive contains symbolic links and files that
2ee7a7
referes to the symbolic links in their path components, the user can
2ee7a7
be tricked into overwriting any arbitrary file.
2ee7a7
2ee7a7
The same issue is with archives whose members refer to a parent
2ee7a7
directory (..) in their path components.
2ee7a7
2ee7a7
This patch fixes it by aborting an extraction (extractTree(),
2ee7a7
extractMember(), extractMemberWithoutPaths()) in those cases by not
2ee7a7
traversing the dangerous paths and returning AZ_ERORR instead.
2ee7a7
2ee7a7
However, if a user supplies a local file name, the security checks are
2ee7a7
not performed. This is based on the assumption that a user knows
2ee7a7
what's on his local file system.
2ee7a7
2ee7a7
CVE-2018-10860
2ee7a7
https://bugzilla.redhat.com/show_bug.cgi?id=1591449
2ee7a7
2ee7a7
Signed-off-by: Petr Písař <ppisar@redhat.com>
2ee7a7
---
2ee7a7
 MANIFEST                               |   3 +
2ee7a7
 lib/Archive/Zip.pm                     |   8 ++
2ee7a7
 lib/Archive/Zip/Archive.pm             |  37 +++++++
2ee7a7
 t/25_traversal.t                       | 189 +++++++++++++++++++++++++++++++++
2ee7a7
 t/data/dotdot-from-unexistant-path.zip | Bin 0 -> 245 bytes
2ee7a7
 t/data/link-dir.zip                    | Bin 0 -> 260 bytes
2ee7a7
 t/data/link-samename.zip               | Bin 0 -> 257 bytes
2ee7a7
 7 files changed, 237 insertions(+)
2ee7a7
 create mode 100644 t/25_traversal.t
2ee7a7
 create mode 100644 t/data/dotdot-from-unexistant-path.zip
2ee7a7
 create mode 100644 t/data/link-dir.zip
2ee7a7
 create mode 100644 t/data/link-samename.zip
2ee7a7
2ee7a7
diff --git a/MANIFEST b/MANIFEST
2ee7a7
index 2e9655d..a1bd7d6 100644
2ee7a7
--- a/MANIFEST
2ee7a7
+++ b/MANIFEST
2ee7a7
@@ -59,6 +59,7 @@ t/21_zip64.t
2ee7a7
 t/22_deflated_dir.t
2ee7a7
 t/23_closed_handle.t
2ee7a7
 t/24_unicode_win32.t
2ee7a7
+t/25_traversal.t
2ee7a7
 t/badjpeg/expected.jpg
2ee7a7
 t/badjpeg/source.zip
2ee7a7
 t/common.pm
2ee7a7
@@ -68,6 +69,7 @@ t/data/crypcomp.zip
2ee7a7
 t/data/crypt.zip
2ee7a7
 t/data/def.zip
2ee7a7
 t/data/defstr.zip
2ee7a7
+t/data/dotdot-from-unexistant-path.zip
2ee7a7
 t/data/empty.zip
2ee7a7
 t/data/emptydef.zip
2ee7a7
 t/data/emptydefstr.zip
2ee7a7
@@ -75,6 +77,7 @@ t/data/emptystore.zip
2ee7a7
 t/data/emptystorestr.zip
2ee7a7
 t/data/good_github11.zip
2ee7a7
 t/data/jar.zip
2ee7a7
+t/data/link-dir.zip
2ee7a7
 t/data/linux.zip
2ee7a7
 t/data/mkzip.pl
2ee7a7
 t/data/perl.zip
2ee7a7
diff --git a/lib/Archive/Zip.pm b/lib/Archive/Zip.pm
2ee7a7
index ca82e31..907808b 100644
2ee7a7
--- a/lib/Archive/Zip.pm
2ee7a7
+++ b/lib/Archive/Zip.pm
2ee7a7
@@ -1145,6 +1145,9 @@ member is used as the name of the extracted file or
2ee7a7
 directory.
2ee7a7
 If you pass C<$extractedName>, it should be in the local file
2ee7a7
 system's format.
2ee7a7
+If you do not pass C<$extractedName> and the internal filename traverses
2ee7a7
+a parent directory or a symbolic link, the extraction will be aborted with
2ee7a7
+C<AC_ERROR> for security reason.
2ee7a7
 All necessary directories will be created. Returns C<AZ_OK>
2ee7a7
 on success.
2ee7a7
 
2ee7a7
@@ -1162,6 +1165,9 @@ extracted member (its paths will be deleted too). Otherwise,
2ee7a7
 the internal filename of the member (minus paths) is used as
2ee7a7
 the name of the extracted file or directory. Returns C<AZ_OK>
2ee7a7
 on success.
2ee7a7
+If you do not pass C<$extractedName> and the internal filename is equalled
2ee7a7
+to a local symbolic link, the extraction will be aborted with C<AC_ERROR> for
2ee7a7
+security reason.
2ee7a7
 
2ee7a7
 =item addMember( $member )
2ee7a7
 
2ee7a7
@@ -1609,6 +1615,8 @@ a/x to f:\d\e\x
2ee7a7
 
2ee7a7
 a/b/c to f:\d\e\b\c and ignore ax/d/e and d/e
2ee7a7
 
2ee7a7
+If the path to the extracted file traverses a parent directory or a symbolic
2ee7a7
+link, the extraction will be aborted with C<AC_ERROR> for security reason.
2ee7a7
 Returns an error code or AZ_OK if everything worked OK.
2ee7a7
 
2ee7a7
 =back
2ee7a7
diff --git a/lib/Archive/Zip/Archive.pm b/lib/Archive/Zip/Archive.pm
2ee7a7
index 48f0d1a..b0d3e46 100644
2ee7a7
--- a/lib/Archive/Zip/Archive.pm
2ee7a7
+++ b/lib/Archive/Zip/Archive.pm
2ee7a7
@@ -185,6 +185,8 @@ sub extractMember {
2ee7a7
         $dirName = File::Spec->catpath($volumeName, $dirName, '');
2ee7a7
     } else {
2ee7a7
         $name = $member->fileName();
2ee7a7
+        if ((my $ret = _extractionNameIsSafe($name))
2ee7a7
+            != AZ_OK) { return $ret; }
2ee7a7
         ($dirName = $name) =~ s{[^/]*$}{};
2ee7a7
         $dirName = Archive::Zip::_asLocalName($dirName);
2ee7a7
         $name    = Archive::Zip::_asLocalName($name);
2ee7a7
@@ -218,6 +220,8 @@ sub extractMemberWithoutPaths {
2ee7a7
     unless ($name) {
2ee7a7
         $name = $member->fileName();
2ee7a7
         $name =~ s{.*/}{};    # strip off directories, if any
2ee7a7
+        if ((my $ret = _extractionNameIsSafe($name))
2ee7a7
+            != AZ_OK) { return $ret; }
2ee7a7
         $name = Archive::Zip::_asLocalName($name);
2ee7a7
     }
2ee7a7
     my $rc = $member->extractToFileNamed($name, @_);
2ee7a7
@@ -827,6 +831,37 @@ sub addTreeMatching {
2ee7a7
     return $self->addTree($root, $dest, $matcher, $compressionLevel);
2ee7a7
 }
2ee7a7
 
2ee7a7
+# Check if one of the components of a path to the file or the file name
2ee7a7
+# itself is an already existing symbolic link. If yes then return an
2ee7a7
+# error. Continuing and writing to a file traversing a link posseses
2ee7a7
+# a security threat, especially if the link was extracted from an
2ee7a7
+# attacker-supplied archive. This would allow writing to an arbitrary
2ee7a7
+# file. The same applies when using ".." to escape from a working
2ee7a7
+# directory. <https://bugzilla.redhat.com/show_bug.cgi?id=1591449>
2ee7a7
+sub _extractionNameIsSafe {
2ee7a7
+    my $name = shift;
2ee7a7
+    my ($volume, $directories) = File::Spec->splitpath($name, 1);
2ee7a7
+    my @directories = File::Spec->splitdir($directories);
2ee7a7
+    if (grep '..' eq $_, @directories) {
2ee7a7
+        return _error(
2ee7a7
+            "Could not extract $name safely: a parent directory is used");
2ee7a7
+    }
2ee7a7
+    my @path;
2ee7a7
+    my $path;
2ee7a7
+    for my $directory (@directories) {
2ee7a7
+        push @path, $directory;
2ee7a7
+        $path = File::Spec->catpath($volume, File::Spec->catdir(@path), '');
2ee7a7
+        if (-l $path) {
2ee7a7
+            return _error(
2ee7a7
+                "Could not extract $name safely: $path is an existing symbolic link");
2ee7a7
+        }
2ee7a7
+        if (!-e $path) {
2ee7a7
+            last;
2ee7a7
+        }
2ee7a7
+    }
2ee7a7
+    return AZ_OK;
2ee7a7
+}
2ee7a7
+
2ee7a7
 # $zip->extractTree( $root, $dest [, $volume] );
2ee7a7
 #
2ee7a7
 # $root and $dest are Unix-style.
2ee7a7
@@ -861,6 +896,8 @@ sub extractTree {
2ee7a7
         $fileName =~ s{$pattern}{$dest};       # in Unix format
2ee7a7
                                                # convert to platform format:
2ee7a7
         $fileName = Archive::Zip::_asLocalName($fileName, $volume);
2ee7a7
+        if ((my $ret = _extractionNameIsSafe($fileName))
2ee7a7
+            != AZ_OK) { return $ret; }
2ee7a7
         my $status = $member->extractToFileNamed($fileName);
2ee7a7
         return $status if $status != AZ_OK;
2ee7a7
     }
2ee7a7
diff --git a/t/25_traversal.t b/t/25_traversal.t
2ee7a7
new file mode 100644
2ee7a7
index 0000000..d03dede
2ee7a7
--- /dev/null
2ee7a7
+++ b/t/25_traversal.t
2ee7a7
@@ -0,0 +1,189 @@
2ee7a7
+use strict;
2ee7a7
+use warnings;
2ee7a7
+
2ee7a7
+use Archive::Zip qw( :ERROR_CODES );
2ee7a7
+use File::Spec;
2ee7a7
+use File::Path;
2ee7a7
+use lib 't';
2ee7a7
+use common;
2ee7a7
+
2ee7a7
+use Test::More tests => 41;
2ee7a7
+
2ee7a7
+# These tests check for CVE-2018-10860 vulnerabilities.
2ee7a7
+# If an archive contains a symlink and then a file that traverses that symlink,
2ee7a7
+# extracting the archive tree could write into an abitrary file selected by
2ee7a7
+# the symlink value.
2ee7a7
+# Another issue is if an archive contains a file whose path component refers
2ee7a7
+# to a parent direcotory. Then extracting that file could write into a file
2ee7a7
+# out of current working directory subtree.
2ee7a7
+# These tests check extracting of these files is refuses and that they are
2ee7a7
+# indeed not created.
2ee7a7
+
2ee7a7
+# Suppress croaking errors, the tests produce some.
2ee7a7
+Archive::Zip::setErrorHandler(sub {});
2ee7a7
+my ($existed, $ret, $zip, $allowed_file, $forbidden_file);
2ee7a7
+
2ee7a7
+# Change working directory to a temporary directory because some tested
2ee7a7
+# functions operarates there and we need prepared symlinks there.
2ee7a7
+my @data_path = (File::Spec->splitdir(File::Spec->rel2abs('.')), 't', 'data');
2ee7a7
+ok(chdir TESTDIR, "Working directory changed");
2ee7a7
+
2ee7a7
+# Case 1:
2ee7a7
+#   link-dir -> /tmp
2ee7a7
+#   link-dir/gotcha-linkdir
2ee7a7
+# writes into /tmp/gotcha-linkdir file.
2ee7a7
+SKIP: {
2ee7a7
+    # Symlink tests make sense only if a file system supports them.
2ee7a7
+    my $link = 'trylink';
2ee7a7
+    $ret = eval { symlink('.', $link)};
2ee7a7
+    skip 'Symbolic links are not supported', 12 if $@;
2ee7a7
+    unlink $link;
2ee7a7
+
2ee7a7
+    # Extracting an archive tree must fail
2ee7a7
+    $zip = Archive::Zip->new();
2ee7a7
+    isa_ok($zip, 'Archive::Zip');
2ee7a7
+    is($zip->read(File::Spec->catfile(@data_path, 'link-dir.zip')), AZ_OK,
2ee7a7
+        'Archive read');
2ee7a7
+    $existed = -e File::Spec->catfile('', 'tmp', 'gotcha-linkdir');
2ee7a7
+    $ret = eval { $zip->extractTree() };
2ee7a7
+    is($ret, AZ_ERROR, 'Tree extraction aborted');
2ee7a7
+    SKIP: {
2ee7a7
+        skip 'A canary file existed before the test', 1 if $existed;
2ee7a7
+        ok(! -e File::Spec->catfile('link-dir', 'gotcha-linkdir'),
2ee7a7
+            'A file was not created in a symlinked directory');
2ee7a7
+    }
2ee7a7
+    ok(unlink(File::Spec->catfile('link-dir')), 'link-dir removed');
2ee7a7
+
2ee7a7
+    # The same applies to extracting an archive member without an explicit
2ee7a7
+    # local file name. It must abort.
2ee7a7
+    $link = 'link-dir';
2ee7a7
+    ok(symlink('.', $link), 'A symlink to a directory created');
2ee7a7
+    $forbidden_file = File::Spec->catfile($link, 'gotcha-linkdir');
2ee7a7
+    $existed = -e $forbidden_file;
2ee7a7
+    $ret = eval { $zip->extractMember('link-dir/gotcha-linkdir') };
2ee7a7
+    is($ret, AZ_ERROR, 'Member extraction without a local name aborted');
2ee7a7
+    SKIP: {
2ee7a7
+        skip 'A canary file existed before the test', 1 if $existed;
2ee7a7
+        ok(! -e $forbidden_file,
2ee7a7
+            'A file was not created in a symlinked directory');
2ee7a7
+    }
2ee7a7
+
2ee7a7
+    # But allow extracting an archive member into a supplied file name
2ee7a7
+    $allowed_file = File::Spec->catfile($link, 'file');
2ee7a7
+    $ret = eval { $zip->extractMember('link-dir/gotcha-linkdir', $allowed_file) };
2ee7a7
+    is($ret, AZ_OK, 'Member extraction passed');
2ee7a7
+    ok(-e $allowed_file, 'File created');
2ee7a7
+    ok(unlink($allowed_file), 'File removed');
2ee7a7
+    ok(unlink($link), 'A symlink to a directory removed');
2ee7a7
+}
2ee7a7
+
2ee7a7
+# Case 2:
2ee7a7
+#   unexisting/../../../../../tmp/gotcha-dotdot-unexistingpath
2ee7a7
+# writes into ../../../../tmp/gotcha-dotdot-unexistingpath, that is
2ee7a7
+# /tmp/gotcha-dotdot-unexistingpath file if CWD is not deeper than
2ee7a7
+# 4 directories.
2ee7a7
+$zip = Archive::Zip->new();
2ee7a7
+isa_ok($zip, 'Archive::Zip');
2ee7a7
+is($zip->read(File::Spec->catfile(@data_path,
2ee7a7
+            'dotdot-from-unexistant-path.zip')), AZ_OK, 'Archive read');
2ee7a7
+$forbidden_file = File::Spec->catfile('..', '..', '..', '..', 'tmp',
2ee7a7
+    'gotcha-dotdot-unexistingpath');
2ee7a7
+$existed = -e $forbidden_file;
2ee7a7
+$ret = eval { $zip->extractTree() };
2ee7a7
+is($ret, AZ_ERROR, 'Tree extraction aborted');
2ee7a7
+SKIP: {
2ee7a7
+    skip 'A canary file existed before the test', 1 if $existed;
2ee7a7
+    ok(! -e $forbidden_file, 'A file was not created in a parent directory');
2ee7a7
+}
2ee7a7
+
2ee7a7
+# The same applies to extracting an archive member without an explicit local
2ee7a7
+# file name. It must abort.
2ee7a7
+$existed = -e $forbidden_file;
2ee7a7
+$ret = eval { $zip->extractMember(
2ee7a7
+        'unexisting/../../../../../tmp/gotcha-dotdot-unexistingpath',
2ee7a7
+    ) };
2ee7a7
+is($ret, AZ_ERROR, 'Member extraction without a local name aborted');
2ee7a7
+SKIP: {
2ee7a7
+    skip 'A canary file existed before the test', 1 if $existed;
2ee7a7
+    ok(! -e $forbidden_file, 'A file was not created in a parent directory');
2ee7a7
+}
2ee7a7
+
2ee7a7
+# But allow extracting an archive member into a supplied file name
2ee7a7
+ok(mkdir('directory'), 'Directory created');
2ee7a7
+$allowed_file = File::Spec->catfile('directory', '..', 'file');
2ee7a7
+$ret = eval { $zip->extractMember(
2ee7a7
+        'unexisting/../../../../../tmp/gotcha-dotdot-unexistingpath',
2ee7a7
+        $allowed_file
2ee7a7
+    ) };
2ee7a7
+is($ret, AZ_OK, 'Member extraction passed');
2ee7a7
+ok(-e $allowed_file, 'File created');
2ee7a7
+ok(unlink($allowed_file), 'File removed');
2ee7a7
+
2ee7a7
+# Case 3:
2ee7a7
+#   link-file -> /tmp/gotcha-samename
2ee7a7
+#   link-file
2ee7a7
+# writes into /tmp/gotcha-samename. It must abort. (Or replace the symlink in
2ee7a7
+# more relaxed mode in the future.)
2ee7a7
+$zip = Archive::Zip->new();
2ee7a7
+isa_ok($zip, 'Archive::Zip');
2ee7a7
+is($zip->read(File::Spec->catfile(@data_path, 'link-samename.zip')), AZ_OK,
2ee7a7
+    'Archive read');
2ee7a7
+$existed = -e File::Spec->catfile('', 'tmp', 'gotcha-samename');
2ee7a7
+$ret = eval { $zip->extractTree() };
2ee7a7
+is($ret, AZ_ERROR, 'Tree extraction aborted');
2ee7a7
+SKIP: {
2ee7a7
+    skip 'A canary file existed before the test', 1 if $existed;
2ee7a7
+    ok(! -e File::Spec->catfile('', 'tmp', 'gotcha-samename'),
2ee7a7
+        'A file was not created through a symlinked file');
2ee7a7
+}
2ee7a7
+ok(unlink(File::Spec->catfile('link-file')), 'link-file removed');
2ee7a7
+
2ee7a7
+# The same applies to extracting an archive member using extractMember()
2ee7a7
+# without an explicit local file name. It must abort.
2ee7a7
+my $link = 'link-file';
2ee7a7
+my $target = 'target';
2ee7a7
+ok(symlink($target, $link), 'A symlink to a file created');
2ee7a7
+$forbidden_file = File::Spec->catfile($target);
2ee7a7
+$existed = -e $forbidden_file;
2ee7a7
+# Select a member by order due to same file names.
2ee7a7
+my $member = ${[$zip->members]}[1];
2ee7a7
+ok($member, 'A member to extract selected');
2ee7a7
+$ret = eval { $zip->extractMember($member) };
2ee7a7
+is($ret, AZ_ERROR,
2ee7a7
+    'Member extraction using extractMember() without a local name aborted');
2ee7a7
+SKIP: {
2ee7a7
+    skip 'A canary file existed before the test', 1 if $existed;
2ee7a7
+    ok(! -e $forbidden_file,
2ee7a7
+        'A symlinked target file was not created');
2ee7a7
+}
2ee7a7
+
2ee7a7
+# But allow extracting an archive member using extractMember() into a supplied
2ee7a7
+# file name.
2ee7a7
+$allowed_file = $target;
2ee7a7
+$ret = eval { $zip->extractMember($member, $allowed_file) };
2ee7a7
+is($ret, AZ_OK, 'Member extraction using extractMember() passed');
2ee7a7
+ok(-e $allowed_file, 'File created');
2ee7a7
+ok(unlink($allowed_file), 'File removed');
2ee7a7
+
2ee7a7
+# The same applies to extracting an archive member using
2ee7a7
+# extractMemberWithoutPaths() without an explicit local file name.
2ee7a7
+# It must abort.
2ee7a7
+$existed = -e $forbidden_file;
2ee7a7
+# Select a member by order due to same file names.
2ee7a7
+$ret = eval { $zip->extractMemberWithoutPaths($member) };
2ee7a7
+is($ret, AZ_ERROR,
2ee7a7
+    'Member extraction using extractMemberWithoutPaths() without a local name aborted');
2ee7a7
+SKIP: {
2ee7a7
+    skip 'A canary file existed before the test', 1 if $existed;
2ee7a7
+    ok(! -e $forbidden_file,
2ee7a7
+        'A symlinked target file was not created');
2ee7a7
+}
2ee7a7
+
2ee7a7
+# But allow extracting an archive member using extractMemberWithoutPaths()
2ee7a7
+# into a supplied file name.
2ee7a7
+$allowed_file = $target;
2ee7a7
+$ret = eval { $zip->extractMemberWithoutPaths($member, $allowed_file) };
2ee7a7
+is($ret, AZ_OK, 'Member extraction using extractMemberWithoutPaths() passed');
2ee7a7
+ok(-e $allowed_file, 'File created');
2ee7a7
+ok(unlink($allowed_file), 'File removed');
2ee7a7
+ok(unlink($link), 'A symlink to a file removed');
2ee7a7
diff --git a/t/data/dotdot-from-unexistant-path.zip b/t/data/dotdot-from-unexistant-path.zip
2ee7a7
new file mode 100644
2ee7a7
index 0000000000000000000000000000000000000000..faaa5bb95c4310ad3dfa8ea7bbad6850da3f2095
2ee7a7
GIT binary patch
2ee7a7
literal 245
2ee7a7
zcmWIWW@Zs#0D%jBS9~Vyb&-_^vO(Aih)eTQD>92qGV{{)_4H6sNp69DdVWcAMxt&?
2ee7a7
zehCoiBGeWnmSjNWtQ7S06v{J8G87Q93LxnKZ$>5&X51D7?FNHwjUWo48O04iClPW+
2ee7a7
TfHx}}$OJ|p%mC8mAPxfn{*XXp
2ee7a7
2ee7a7
literal 0
2ee7a7
HcmV?d00001
2ee7a7
2ee7a7
diff --git a/t/data/link-dir.zip b/t/data/link-dir.zip
2ee7a7
new file mode 100644
2ee7a7
index 0000000000000000000000000000000000000000..99fbb437ec0bd694b8122cdb1ce8221a3da2e453
2ee7a7
GIT binary patch
2ee7a7
literal 260
2ee7a7
zcmWIWW@Zs#0D
2ee7a7
z`6bC2iMk-2K#dTdLRn^_0+6Qw66Ff;W@Hj!#%(FkG%)zT5JbV8fUXPO0T4Y54BHyD
2ee7a7
ZkaX#zIw!!Jl?|kj2?(o!bTNp-004oiIM)CG
2ee7a7
2ee7a7
literal 0
2ee7a7
HcmV?d00001
2ee7a7
2ee7a7
diff --git a/t/data/link-samename.zip b/t/data/link-samename.zip
2ee7a7
new file mode 100644
2ee7a7
index 0000000000000000000000000000000000000000..e9036c0348f5fb9536cb7ee0f2c09b9ef595a12c
2ee7a7
GIT binary patch
2ee7a7
literal 257
2ee7a7
zcmWIWW@Zs#00HsHOFm_vR
2ee7a7
zfTHz8cF##^pcW8D(F)O}P?njf0Me-o(wd?GGMOvDn~_O`8MpO7qrl+*LJ$Ra47xUS
2ee7a7
bt09^g7`8Q9qiSPi14%IfVIGjK1#uVvx^_1y
2ee7a7
2ee7a7
literal 0
2ee7a7
HcmV?d00001
2ee7a7
2ee7a7
-- 
2ee7a7
2.14.4
2ee7a7