c8515551f18276b763f44df8fa7ccc6145b519d9
[feed/packages.git] /
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
6 (GH-21454) (GH-21484)
7
8 Avoid infinite loop when reading specially crafted TAR files using the tarfile module
9 (CVE-2019-20907).
10 (cherry picked from commit 5a8d121a1f3ef5ad7c105ee378cc79a3eac0c7d4)
11
12 Co-authored-by: Rishi <rishi_devan@mail.com>
13 ---
14 Lib/tarfile.py | 2 ++
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
21
22 diff --git a/Lib/tarfile.py b/Lib/tarfile.py
23 index 3b596cbf49d27..3be5188c8b0a2 100755
24 --- a/Lib/tarfile.py
25 +++ b/Lib/tarfile.py
26 @@ -1233,6 +1233,8 @@ def _proc_pax(self, tarfile):
27
28 length, keyword = match.groups()
29 length = int(length)
30 + if length == 0:
31 + raise InvalidHeaderError("invalid header")
32 value = buf[match.end(2) + 1:match.start(1) + length - 1]
33
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()
42
43 + def test_length_zero_header(self):
44 + # bpo-39017 (CVE-2019-20907): reading a zero-length header should fail
45 + # with an exception
46 + with self.assertRaisesRegex(tarfile.ReadError, "file could not be opened successfully"):
47 + with tarfile.open(support.findfile('recursion.tar')) as tar:
48 + pass
49 +
50 class MiscReadTestBase(CommonReadTest):
51 def requires_name_attribute(self):
52 pass
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
54 new file mode 100644
55 index 0000000000000..ad26676f8b856
56 --- /dev/null
57 +++ b/Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst
58 @@ -0,0 +1 @@
59 +Avoid infinite loop when reading specially crafted TAR files using the tarfile module (CVE-2019-20907).