Skip to content

Commit 0c220da

Browse files
gh-161955: Limit number of records parsed from PAX headers in tarfile
A crafted tar archive containing a massive PAX extended header with minimal-length records (e.g. "5 a=\n") could force tarfile to allocate tens of millions of records into memory, triggering an Out-Of-Memory (OOM) crash or severe denial of service. Add _MAX_PAX_RECORDS = 10_000 to limit the number of records parsed from a single PAX header in TarInfo._proc_pax(), raising InvalidHeaderError if the limit is exceeded.
1 parent 60eca2d commit 0c220da

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎Lib/tarfile.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ def copyfileobj(src, dst, length=None, exception=OSError, bufsize=None):
265265
# (gh-151497).
266266
_EXTHEADER_READ_CHUNK = 1024 * 1024 # 1 MiB
267267

268+
# Maximum number of records parsed from a single pax extended header.
269+
# A crafted header containing millions of minimal records (e.g. "5 a=\n")
270+
# can cause excessive memory allocation and Denial of Service (gh-161955).
271+
_MAX_PAX_RECORDS = 10_000
272+
268273
def _safe_read(fileobj, size):
269274
"""Read up to *size* bytes from *fileobj* in bounded chunks.
270275
@@ -1553,6 +1558,8 @@ def _proc_pax(self, tarfile):
15531558
encoding = None
15541559
raw_headers = []
15551560
while len(buf) > pos and buf[pos] != 0x00:
1561+
if len(raw_headers) >= _MAX_PAX_RECORDS:
1562+
raise InvalidHeaderError("too many pax records")
15561563
if not (match := _header_length_prefix_re.match(buf, pos)):
15571564
raise InvalidHeaderError("invalid header")
15581565
try:

‎Lib/test/test_tarfile.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,56 @@ def test_pax_header_bad_formats(self):
17321732
with self.assertRaisesRegex(tarfile.ReadError, r"method tar: ReadError\('invalid header'\)"):
17331733
tarfile.open(tmpname, encoding="iso8859-1")
17341734

1735+
def test_pax_header_too_many_records(self):
1736+
# gh-161955: A crafted PAX header with an excessive number of records
1737+
# must raise InvalidHeaderError to prevent memory exhaustion (DoS).
1738+
record = b"5 a=\n"
1739+
num_records = tarfile._MAX_PAX_RECORDS + 1
1740+
payload = record * num_records
1741+
tarinfo = tarfile.TarInfo("././@PaxHeader")
1742+
tarinfo.type = tarfile.XHDTYPE
1743+
tarinfo.size = len(payload)
1744+
buf = tarinfo.tobuf(format=tarfile.GNU_FORMAT)
1745+
buf += payload + b"\0" * (-len(payload) % tarfile.BLOCKSIZE)
1746+
buf += tarfile.TarInfo("test.txt").tobuf(format=tarfile.GNU_FORMAT)
1747+
buf += b"\0" * (tarfile.BLOCKSIZE * 2)
1748+
1749+
with self.assertRaisesRegex(tarfile.ReadError, "too many pax records"):
1750+
with tarfile.open(fileobj=io.BytesIO(buf), mode="r:") as tar:
1751+
tar.getmembers()
1752+
1753+
def test_pax_header_record_limit_boundary(self):
1754+
# Test boundary behavior using a small patched _MAX_PAX_RECORDS
1755+
record = b"5 a=\n"
1756+
with unittest.mock.patch.object(tarfile, "_MAX_PAX_RECORDS", 5):
1757+
# Exactly 5 records should succeed
1758+
payload = record * 5
1759+
tarinfo = tarfile.TarInfo("././@PaxHeader")
1760+
tarinfo.type = tarfile.XHDTYPE
1761+
tarinfo.size = len(payload)
1762+
buf = tarinfo.tobuf(format=tarfile.GNU_FORMAT)
1763+
buf += payload + b"\0" * (-len(payload) % tarfile.BLOCKSIZE)
1764+
normal = tarfile.TarInfo("test.txt")
1765+
buf += normal.tobuf(format=tarfile.GNU_FORMAT)
1766+
buf += b"\0" * (tarfile.BLOCKSIZE * 2)
1767+
1768+
with tarfile.open(fileobj=io.BytesIO(buf), mode="r:") as tar:
1769+
members = tar.getmembers()
1770+
self.assertEqual(len(members), 1)
1771+
self.assertEqual(members[0].name, "test.txt")
1772+
1773+
# 6 records should exceed limit and raise ReadError
1774+
payload = record * 6
1775+
tarinfo.size = len(payload)
1776+
buf = tarinfo.tobuf(format=tarfile.GNU_FORMAT)
1777+
buf += payload + b"\0" * (-len(payload) % tarfile.BLOCKSIZE)
1778+
buf += normal.tobuf(format=tarfile.GNU_FORMAT)
1779+
buf += b"\0" * (tarfile.BLOCKSIZE * 2)
1780+
1781+
with self.assertRaisesRegex(tarfile.ReadError, "too many pax records"):
1782+
with tarfile.open(fileobj=io.BytesIO(buf), mode="r:") as tar:
1783+
tar.getmembers()
1784+
17351785

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

0 commit comments

Comments
 (0)