Skip to content

Commit c554795

Browse files
[3.8] bpo-39017: Avoid infinite loop in the tarfile module (GH-21454) (GH-21483)
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> Automerge-Triggered-By: @encukou
1 parent b4cd77d commit c554795

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
@@ -1241,6 +1241,8 @@ def _proc_pax(self, tarfile):
12411241

12421242
length, keyword = match.groups()
12431243
length = int(length)
1244+
if length == 0:
1245+
raise InvalidHeaderError("invalid header")
12441246
value = buf[match.end(2) + 1:match.start(1) + length - 1]
12451247

12461248
# 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
@@ -397,6 +397,13 @@ def test_premature_end_of_archive(self):
397397
with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
398398
tar.extractfile(t).read()
399399

400+
def test_length_zero_header(self):
401+
# bpo-39017 (CVE-2019-20907): reading a zero-length header should fail
402+
# with an exception
403+
with self.assertRaisesRegex(tarfile.ReadError, "file could not be opened successfully"):
404+
with tarfile.open(support.findfile('recursion.tar')) as tar:
405+
pass
406+
400407
class MiscReadTestBase(CommonReadTest):
401408
def requires_name_attribute(self):
402409
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)