1 From c55479556db015f48fc8bbca17f64d3e65598559 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:30:53 -0700
5 Subject: [PATCH] [3.8] 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)
13 Co-authored-by: Rishi <rishi_devan@mail.com>
15 Automerge-Triggered-By: @encukou
18 Lib/test/recursion.tar | Bin 0 -> 516 bytes
19 Lib/test/test_tarfile.py | 7 +++++++
20 .../2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst | 1 +
21 4 files changed, 10 insertions(+)
22 create mode 100644 Lib/test/recursion.tar
23 create mode 100644 Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst
25 diff --git a/Lib/tarfile.py b/Lib/tarfile.py
26 index d31b9cbb51d65..7a69e1b1aa544 100755
29 @@ -1241,6 +1241,8 @@ def _proc_pax(self, tarfile):
31 length, keyword = match.groups()
34 + raise InvalidHeaderError("invalid header")
35 value = buf[match.end(2) + 1:match.start(1) + length - 1]
37 # Normally, we could just use "utf-8" as the encoding and "strict"
38 diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
39 index 15324a4e48819..b512168d6ea87 100644
40 --- a/Lib/test/test_tarfile.py
41 +++ b/Lib/test/test_tarfile.py
42 @@ -397,6 +397,13 @@ def test_premature_end_of_archive(self):
43 with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
44 tar.extractfile(t).read()
46 + def test_length_zero_header(self):
47 + # bpo-39017 (CVE-2019-20907): reading a zero-length header should fail
49 + with self.assertRaisesRegex(tarfile.ReadError, "file could not be opened successfully"):
50 + with tarfile.open(support.findfile('recursion.tar')) as tar:
53 class MiscReadTestBase(CommonReadTest):
54 def requires_name_attribute(self):
56 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
58 index 0000000000000..ad26676f8b856
60 +++ b/Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst
62 +Avoid infinite loop when reading specially crafted TAR files using the tarfile module (CVE-2019-20907).