Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ def copyfileobj(src, dst, length=None, exception=OSError, bufsize=None):
# (gh-151497).
_EXTHEADER_READ_CHUNK = 1024 * 1024 # 1 MiB

# Maximum number of records parsed from a single pax extended header.
# A crafted header containing millions of minimal records (e.g. "5 a=\n")
# can cause excessive memory allocation and Denial of Service (gh-161955).
_MAX_PAX_RECORDS = 10_000

def _safe_read(fileobj, size):
"""Read up to *size* bytes from *fileobj* in bounded chunks.

Expand Down Expand Up @@ -1553,6 +1558,8 @@ def _proc_pax(self, tarfile):
encoding = None
raw_headers = []
while len(buf) > pos and buf[pos] != 0x00:
if len(raw_headers) >= _MAX_PAX_RECORDS:
raise InvalidHeaderError("too many pax records")
if not (match := _header_length_prefix_re.match(buf, pos)):
raise InvalidHeaderError("invalid header")
try:
Expand Down
50 changes: 50 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,56 @@ def test_pax_header_bad_formats(self):
with self.assertRaisesRegex(tarfile.ReadError, r"method tar: ReadError\('invalid header'\)"):
tarfile.open(tmpname, encoding="iso8859-1")

def test_pax_header_too_many_records(self):
# gh-161955: A crafted PAX header with an excessive number of records
# must raise InvalidHeaderError to prevent memory exhaustion (DoS).
record = b"5 a=\n"
num_records = tarfile._MAX_PAX_RECORDS + 1
payload = record * num_records
tarinfo = tarfile.TarInfo("././@PaxHeader")
tarinfo.type = tarfile.XHDTYPE
tarinfo.size = len(payload)
buf = tarinfo.tobuf(format=tarfile.GNU_FORMAT)
buf += payload + b"\0" * (-len(payload) % tarfile.BLOCKSIZE)
buf += tarfile.TarInfo("test.txt").tobuf(format=tarfile.GNU_FORMAT)
buf += b"\0" * (tarfile.BLOCKSIZE * 2)

with self.assertRaisesRegex(tarfile.ReadError, "too many pax records"):
with tarfile.open(fileobj=io.BytesIO(buf), mode="r:") as tar:
tar.getmembers()

def test_pax_header_record_limit_boundary(self):
# Test boundary behavior using a small patched _MAX_PAX_RECORDS
record = b"5 a=\n"
with unittest.mock.patch.object(tarfile, "_MAX_PAX_RECORDS", 5):
# Exactly 5 records should succeed
payload = record * 5
tarinfo = tarfile.TarInfo("././@PaxHeader")
tarinfo.type = tarfile.XHDTYPE
tarinfo.size = len(payload)
buf = tarinfo.tobuf(format=tarfile.GNU_FORMAT)
buf += payload + b"\0" * (-len(payload) % tarfile.BLOCKSIZE)
normal = tarfile.TarInfo("test.txt")
buf += normal.tobuf(format=tarfile.GNU_FORMAT)
buf += b"\0" * (tarfile.BLOCKSIZE * 2)

with tarfile.open(fileobj=io.BytesIO(buf), mode="r:") as tar:
members = tar.getmembers()
self.assertEqual(len(members), 1)
self.assertEqual(members[0].name, "test.txt")

# 6 records should exceed limit and raise ReadError
payload = record * 6
tarinfo.size = len(payload)
buf = tarinfo.tobuf(format=tarfile.GNU_FORMAT)
buf += payload + b"\0" * (-len(payload) % tarfile.BLOCKSIZE)
buf += normal.tobuf(format=tarfile.GNU_FORMAT)
buf += b"\0" * (tarfile.BLOCKSIZE * 2)

with self.assertRaisesRegex(tarfile.ReadError, "too many pax records"):
with tarfile.open(fileobj=io.BytesIO(buf), mode="r:") as tar:
tar.getmembers()


class WriteTestBase(TarTest):
# Put all write tests in here that are supposed to be tested
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix Denial of Service (DoS) and memory exhaustion (OOM) vulnerability in
:mod:`tarfile` by adding a limit (:data:`!_MAX_PAX_RECORDS`) to the number of
records parsed from a single PAX extended header.
Loading