diff --git a/Lib/tarfile.py b/Lib/tarfile.py index b052ca0dd407292..2aea7b91af3cc26 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -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. @@ -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: diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 1927ff227d5356f..5f392e841a0c045 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -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 diff --git a/Misc/NEWS.d/next/Library/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst b/Misc/NEWS.d/next/Library/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst new file mode 100644 index 000000000000000..ffff837d9f50988 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst @@ -0,0 +1,3 @@ +Prevent excessive memory allocation in :mod:`tarfile` by adding a limit +(:data:`!_MAX_PAX_RECORDS`) to the number of records parsed from a single +PAX extended header.