Skip to content

Commit 47a2955

Browse files
bpo-39017: Avoid infinite loop in the tarfile module (GH-21454) (#21485)
Avoid infinite loop when reading specially crafted TAR files using the tarfile module (CVE-2019-20907). (cherry picked from commit 5a8d121) Co-authored-by: Rishi <rishi_devan@mail.com>
1 parent 6463cf0 commit 47a2955

File tree

4 files changed

+10
-0
lines changed

4 files changed

+10
-0
lines changed

Lib/tarfile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,8 @@ def _proc_pax(self, tarfile):
12311231

12321232
length, keyword = match.groups()
12331233
length = int(length)
1234+
if length == 0:
1235+
raise InvalidHeaderError("invalid header")
12341236
value = buf[match.end(2) + 1:match.start(1) + length - 1]
12351237

12361238
# Normally, we could just use "utf-8" as the encoding and "strict"

Lib/test/recursion.tar

516 Bytes
Binary file not shown.

Lib/test/test_tarfile.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,13 @@ def test_premature_end_of_archive(self):
395395
with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
396396
tar.extractfile(t).read()
397397

398+
def test_length_zero_header(self):
399+
# bpo-39017 (CVE-2019-20907): reading a zero-length header should fail
400+
# with an exception
401+
with self.assertRaisesRegex(tarfile.ReadError, "file could not be opened successfully"):
402+
with tarfile.open(support.findfile('recursion.tar')) as tar:
403+
pass
404+
398405
class MiscReadTestBase(CommonReadTest):
399406
def requires_name_attribute(self):
400407
pass
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid infinite loop when reading specially crafted TAR files using the tarfile module (CVE-2019-20907).

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy