CVE-2020-15908 (published 2020-07-23T04:15Z)

tar/TarFileReader.cpp in Cauldron cbang (aka C-Bang or C!) before 1.6.0 allows Directory Traversal during extraction from a TAR archive.

Severity: MEDIUM
CVSS v2 base score: 5.0 (AV:N/AC:L/Au:N/C:P/I:N/A:N)
CVSS v3 base score: 7.5, HIGH (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N)

Weakness information:
cwe_id: CWE-22
cwe_name: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
extended_description: 
            Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
         
         

Fix information:
repo_url: https://github.com/CauldronDevelopmentLLC/cbang
commit_details: repo_url: https://github.com/CauldronDevelopmentLLC/cbang
author: Joseph Coffland
author_date: 2020-07-19 12:43:49-07:00
msg: Don't allow extraction of tar files outside of the target directory, added tar tests
repository: repo_url: https://github.com/CauldronDevelopmentLLC/cbang
repo_name: CauldronDevelopmentLLC/cbang
description: C! (cbang) is a library for cross-platform C++ development.
repo_language: C++
owner: CauldronDevelopmentLLC
file_changes: filename: TarFileReader.cpp
change_type: ModificationType.MODIFY
diff: @@ -101,11 +101,26 @@ std::string TarFileReader::extract(const string &_path) {
   if (!hasMore()) THROW("No more tar files");
 
   string path = _path;
-  if (SystemUtilities::isDirectory(path)) path += "/" + getFilename();
+  if (SystemUtilities::isDirectory(path)) {
+    path += "/" + getFilename();
+
+    // Check that path is under the target directory
+    string a = SystemUtilities::getCanonicalPath(_path);
+    string b = SystemUtilities::getCanonicalPath(path);
+    if (!String::startsWith(b, a))
+      THROW("Tar path points outside of the extraction directory: " << path);
+  }
 
   LOG_DEBUG(5, "Extracting: " << path);
 
-  return extract(*SystemUtilities::oopen(path));
+  switch (getType()) {
+  case NORMAL_FILE: case CONTIGUOUS_FILE:
+    return extract(*SystemUtilities::oopen(path));
+  case DIRECTORY: SystemUtilities::ensureDirectory(path); break;
+  default: THROW("Unsupported tar file type " << getType());
+  }
+
+  return getFilename();
 }
 
 

programming_language: C++
filename: .gitignore
change_type: ModificationType.ADD
diff: @@ -0,0 +1 @@
+/path

programming_language: SQL
filename: args
change_type: ModificationType.ADD
diff: @@ -0,0 +1 @@
+--extract test.tar

programming_language: SQL
filename: return
change_type: ModificationType.ADD
diff: @@ -0,0 +1 @@
+1

programming_language: SQL
filename: stderr
change_type: ModificationType.ADD
diff: @@ -0,0 +1 @@
+Tar path points outside of the extraction directory: ./../hello.txt
\ No newline at end of file

programming_language: Markdown
filename: args
change_type: ModificationType.ADD
diff: @@ -0,0 +1 @@
+--extract test.tar.bz2

programming_language: Batchfile
filename: return
change_type: ModificationType.ADD
diff: @@ -0,0 +1 @@
+0

programming_language: SQL
filename: stdout
change_type: ModificationType.ADD
diff: @@ -0,0 +1 @@
+hello.txt

programming_language: Batchfile
filename: args
change_type: ModificationType.ADD
diff: @@ -0,0 +1 @@
+--extract test.tar

programming_language: SQL
filename: return
change_type: ModificationType.ADD
diff: @@ -0,0 +1 @@
+0

programming_language: SQL
filename: stdout
change_type: ModificationType.ADD
diff: @@ -0,0 +1 @@
+hello.txt

programming_language: Batchfile
filename: SConscript
change_type: ModificationType.ADD
diff: @@ -0,0 +1,8 @@
+Import('*')
+
+# Local includes
+env.Append(CPPPATH = ['#'])
+
+prog = env.Program('tar', 'tar.cpp')
+
+Return('prog')

programming_language: Python
filename: tar.cpp
change_type: ModificationType.ADD
diff: @@ -0,0 +1,61 @@
+/******************************************************************************\
+
+          This file is part of the C! library.  A.K.A the cbang library.
+
+                Copyright (c) 2003-2019, Cauldron Development LLC
+                   Copyright (c) 2003-2017, Stanford University
+                               All rights reserved.
+
+         The C! library is free software: you can redistribute it and/or
+        modify it under the terms of the GNU Lesser General Public License
+       as published by the Free Software Foundation, either version 2.1 of
+               the License, or (at your option) any later version.
+
+        The C! library is distributed in the hope that it will be useful,
+          but WITHOUT ANY WARRANTY; without even the implied warranty of
+        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+                 Lesser General Public License for more details.
+
+         You should have received a copy of the GNU Lesser General Public
+                 License along with the C! library.  If not, see
+                         <http://www.gnu.org/licenses/>.
+
+        In addition, BSD licensing may be granted on a case by case basis
+        by written permission from at least one of the copyright holders.
+           You may request written permission by emailing the authors.
+
+                  For information regarding this software email:
+                                 Joseph Coffland
+                          joseph@cauldrondevelopment.com
+
+\******************************************************************************/
+
+#include <cbang/tar/TarFileReader.h>
+#include <cbang/Catch.h>
+
+#include <iostream>
+
+using namespace cb;
+using namespace std;
+
+
+int main(int argc, char *argv[]) {
+  try {
+    for (int i = 1; i < argc; i++) {
+      string arg = argv[i];
+
+      if (arg == "--extract" && i < argc - 1) {
+        TarFileReader reader(argv[++i]);
+
+        while (reader.hasMore())
+          cout << reader.extract() << endl;
+
+      } else THROWS("Invalid arg '" << arg << "'");
+    }
+
+    return 0;
+
+  } catch (const Exception &e) {cerr << e.getMessage();}
+
+  return 1;
+}

programming_language: C++
filename: test.json
change_type: ModificationType.ADD
diff: @@ -0,0 +1,3 @@
+{
+  "command": "%(suite-dir)s/tar"
+}

programming_language: Batchfile