[CVE-2019-20907] bpo-39017: Avoid infinite loop in the tarfile module (GH-21454) 68/258468/1
authorRishi <rishi_devan@mail.com>
Wed, 15 Jul 2020 11:51:00 +0000 (13:51 +0200)
committerDongHun Kwak <dh0128.kwak@samsung.com>
Tue, 18 May 2021 05:02:38 +0000 (14:02 +0900)
Avoid infinite loop when reading specially crafted TAR files using the tarfile module
(CVE-2019-20907).

Change-Id: If7420b14bb50a40afa39b933801a7d1cd2a85afe
Signed-off-by: DongHun Kwak <dh0128.kwak@samsung.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 edd31e96fb4694ddb1ea3c9e9285f930a59ba0e0..a17d2a7d80bc02d1957b6998a7a954284ecc12ed 100755 (executable)
@@ -1233,6 +1233,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 5e4d75ecfce1af384c47417343d3e3224389ee36..9133d60e49be14aaccae865fdf4665637cc5bc70 100644 (file)
@@ -395,6 +395,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).