]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.5] bpo-39017: Avoid infinite loop in the tarfile module (GH-21454) (#21489)
authorPetr Viktorin <encukou@gmail.com>
Thu, 16 Jul 2020 19:48:01 +0000 (21:48 +0200)
committerGitHub <noreply@github.com>
Thu, 16 Jul 2020 19:48:01 +0000 (12:48 -0700)
Avoid infinite loop when reading specially crafted TAR files using the tarfile module
(CVE-2019-20907).
(cherry picked from commit 5a8d121a1f3ef5ad7c105ee378cc79a3eac0c7d4)

Co-authored-by: Rishi <rishi_devan@mail.com>
Lib/tarfile.py
Lib/test/recursion.tar [new file with mode: 0644]
Lib/test/test_tarfile.py
Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst [new file with mode: 0644]

index 999a99b978fced11befb850c3025c12a27064c99..8015fae7a63e54af67a525dc403b0695c306a677 100755 (executable)
@@ -1221,6 +1221,8 @@ class TarInfo(object):
 
             length, keyword = match.groups()
             length = int(length)
+            if length == 0:
+                raise InvalidHeaderError("invalid header")
             value = buf[match.end(2) + 1:match.start(1) + length - 1]
 
             # Normally, we could just use "utf-8" as the encoding and "strict"
diff --git a/Lib/test/recursion.tar b/Lib/test/recursion.tar
new file mode 100644 (file)
index 0000000..b823725
Binary files /dev/null and b/Lib/test/recursion.tar differ
index 1efb841bb22dc2f0922a606a00c0d396fa5df3cc..2438fd3abf2ee939c18b8c13a19fddf319cfbed6 100644 (file)
@@ -394,6 +394,13 @@ class CommonReadTest(ReadTest):
                 with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
                     tar.extractfile(t).read()
 
+    def test_length_zero_header(self):
+        # bpo-39017 (CVE-2019-20907): reading a zero-length header should fail
+        # with an exception
+        with self.assertRaisesRegex(tarfile.ReadError, "file could not be opened successfully"):
+            with tarfile.open(support.findfile('recursion.tar')) as tar:
+                pass
+
 class MiscReadTestBase(CommonReadTest):
     def requires_name_attribute(self):
         pass
diff --git a/Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst b/Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst
new file mode 100644 (file)
index 0000000..ad26676
--- /dev/null
@@ -0,0 +1 @@
+Avoid infinite loop when reading specially crafted TAR files using the tarfile module (CVE-2019-20907).