e954eb685e5b7d072fdd9ac76e47c17f63861d90
[openwrt-packages.git] /
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
6  (GH-21454) (GH-21483)
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
13 Co-authored-by: Rishi <rishi_devan@mail.com>
14
15 Automerge-Triggered-By: @encukou
16 ---
17  Lib/tarfile.py                                    |   2 ++
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
24
25 diff --git a/Lib/tarfile.py b/Lib/tarfile.py
26 index d31b9cbb51d65..7a69e1b1aa544 100755
27 --- a/Lib/tarfile.py
28 +++ b/Lib/tarfile.py
29 @@ -1241,6 +1241,8 @@ def _proc_pax(self, tarfile):
30  
31              length, keyword = match.groups()
32              length = int(length)
33 +            if length == 0:
34 +                raise InvalidHeaderError("invalid header")
35              value = buf[match.end(2) + 1:match.start(1) + length - 1]
36  
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()
45  
46 +    def test_length_zero_header(self):
47 +        # bpo-39017 (CVE-2019-20907): reading a zero-length header should fail
48 +        # with an exception
49 +        with self.assertRaisesRegex(tarfile.ReadError, "file could not be opened successfully"):
50 +            with tarfile.open(support.findfile('recursion.tar')) as tar:
51 +                pass
52 +
53  class MiscReadTestBase(CommonReadTest):
54      def requires_name_attribute(self):
55          pass
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
57 new file mode 100644
58 index 0000000000000..ad26676f8b856
59 --- /dev/null
60 +++ b/Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst
61 @@ -0,0 +1 @@
62 +Avoid infinite loop when reading specially crafted TAR files using the tarfile module (CVE-2019-20907).
git clone https://git.99rst.org/PROJECT