Blame 00351-avoid-infinite-loop-in-the-tarfile-module.patch

c8bbc30
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
c8bbc30
From: "Miss Islington (bot)"
c8bbc30
 <31488909+miss-islington@users.noreply.github.com>
c8bbc30
Date: Wed, 15 Jul 2020 05:35:08 -0700
c8bbc30
Subject: [PATCH] 00351: Avoid infinite loop in the tarfile module
c8bbc30
c8bbc30
Avoid infinite loop when reading specially crafted TAR files using the tarfile module
c8bbc30
(CVE-2019-20907).
c8bbc30
Fixed upstream: https://bugs.python.org/issue39017
c8bbc30
---
c8bbc30
 Lib/tarfile.py                                    |   2 ++
c8bbc30
 Lib/test/recursion.tar                            | Bin 0 -> 516 bytes
c8bbc30
 Lib/test/test_tarfile.py                          |   7 +++++++
c8bbc30
 .../2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst      |   1 +
c8bbc30
 4 files changed, 10 insertions(+)
c8bbc30
 create mode 100644 Lib/test/recursion.tar
c8bbc30
 create mode 100644 Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst
c8bbc30
c8bbc30
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
c8bbc30
index 3b596cbf49..3be5188c8b 100755
c8bbc30
--- a/Lib/tarfile.py
c8bbc30
+++ b/Lib/tarfile.py
c8bbc30
@@ -1233,6 +1233,8 @@ class TarInfo(object):
c8bbc30
 
c8bbc30
             length, keyword = match.groups()
c8bbc30
             length = int(length)
c8bbc30
+            if length == 0:
c8bbc30
+                raise InvalidHeaderError("invalid header")
c8bbc30
             value = buf[match.end(2) + 1:match.start(1) + length - 1]
c8bbc30
 
c8bbc30
             # Normally, we could just use "utf-8" as the encoding and "strict"
c8bbc30
diff --git a/Lib/test/recursion.tar b/Lib/test/recursion.tar
c8bbc30
new file mode 100644
c8bbc30
index 0000000000000000000000000000000000000000..b8237251964983f54ed1966297e887636cd0c5f4
c8bbc30
GIT binary patch
c8bbc30
literal 516
c8bbc30
zcmYdFPRz+kEn=W0Fn}74P8%Xw3X=l~85kIuo0>8xq$A1Gm}!7)KUsFc41m#O8A5+e
c8bbc30
I1_}|j06>QaCIA2c
c8bbc30
c8bbc30
literal 0
c8bbc30
HcmV?d00001
c8bbc30
c8bbc30
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
c8bbc30
index 5e4d75ecfc..9133d60e49 100644
c8bbc30
--- a/Lib/test/test_tarfile.py
c8bbc30
+++ b/Lib/test/test_tarfile.py
c8bbc30
@@ -395,6 +395,13 @@ class CommonReadTest(ReadTest):
c8bbc30
                 with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
c8bbc30
                     tar.extractfile(t).read()
c8bbc30
 
c8bbc30
+    def test_length_zero_header(self):
c8bbc30
+        # bpo-39017 (CVE-2019-20907): reading a zero-length header should fail
c8bbc30
+        # with an exception
c8bbc30
+        with self.assertRaisesRegex(tarfile.ReadError, "file could not be opened successfully"):
c8bbc30
+            with tarfile.open(support.findfile('recursion.tar')) as tar:
c8bbc30
+                pass
c8bbc30
+
c8bbc30
 class MiscReadTestBase(CommonReadTest):
c8bbc30
     def requires_name_attribute(self):
c8bbc30
         pass
c8bbc30
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
c8bbc30
new file mode 100644
c8bbc30
index 0000000000..ad26676f8b
c8bbc30
--- /dev/null
c8bbc30
+++ b/Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst
c8bbc30
@@ -0,0 +1 @@
c8bbc30
+Avoid infinite loop when reading specially crafted TAR files using the tarfile module (CVE-2019-20907).