1 From 79c6b602efc9a906c8496f3d5f4d54c54b48fa06 Mon Sep 17 00:00:00 2001
2 From: "Miss Islington (bot)"
3 <31488909+miss-islington@users.noreply.github.com>
4 Date: Wed, 15 Jul 2020 05:35:08 -0700
5 Subject: [PATCH] bpo-39017: Avoid infinite loop in the tarfile module
8 Avoid infinite loop when reading specially crafted TAR files using the tarfile module
10 (cherry picked from commit 5a8d121a1f3ef5ad7c105ee378cc79a3eac0c7d4)
12 Co-authored-by: Rishi <rishi_devan@mail.com>
15 Lib/test/recursion.tar | Bin 0 -> 516 bytes
16 Lib/test/test_tarfile.py | 7 +++++++
17 .../2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst | 1 +
18 4 files changed, 10 insertions(+)
19 create mode 100644 Lib/test/recursion.tar
20 create mode 100644 Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst
22 diff --git a/Lib/tarfile.py b/Lib/tarfile.py
23 index 3b596cbf49d27..3be5188c8b0a2 100755
26 @@ -1233,6 +1233,8 @@ def _proc_pax(self, tarfile):
28 length, keyword = match.groups()
31 + raise InvalidHeaderError("invalid header")
32 value = buf[match.end(2) + 1:match.start(1) + length - 1]
34 # Normally, we could just use "utf-8" as the encoding and "strict"
35 diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
36 index 5e4d75ecfce1a..9133d60e49be1 100644
37 --- a/Lib/test/test_tarfile.py
38 +++ b/Lib/test/test_tarfile.py
39 @@ -395,6 +395,13 @@ def test_premature_end_of_archive(self):
40 with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
41 tar.extractfile(t).read()
43 + def test_length_zero_header(self):
44 + # bpo-39017 (CVE-2019-20907): reading a zero-length header should fail
46 + with self.assertRaisesRegex(tarfile.ReadError, "file could not be opened successfully"):
47 + with tarfile.open(support.findfile('recursion.tar')) as tar:
50 class MiscReadTestBase(CommonReadTest):
51 def requires_name_attribute(self):
53 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
55 index 0000000000000..ad26676f8b856
57 +++ b/Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst
59 +Avoid infinite loop when reading specially crafted TAR files using the tarfile module (CVE-2019-20907).