From 6bcd9525e743959ef50717e734bbf4ca61f17cee Mon Sep 17 00:00:00 2001 From: Abhijeet Jha Date: Sun, 15 Nov 2026 09:19:15 +0530 Subject: [PATCH 1/2] 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. --- Lib/tarfile.py | 7 +++ Lib/test/test_tarfile.py | 50 +++++++++++++++++++ ...-11-15-09-15-00.gh-issue-161955.aBcDeF.rst | 3 ++ 3 files changed, 60 insertions(+) create mode 100644 Misc/NEWS.d/next/Security/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst 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/Security/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst b/Misc/NEWS.d/next/Security/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst new file mode 100644 index 000000000000000..f15a68d801863b4 --- /dev/null +++ b/Misc/NEWS.d/next/Security/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst @@ -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. From 0c99ba3a0115be025e82ee6eccc483885ed60cd9 Mon Sep 17 00:00:00 2001 From: Abhijeet Jha Date: Mon, 16 Nov 2026 21:41:50 +0530 Subject: [PATCH 2/2] gh-161955: Move NEWS entry to Library section Relocate the NEWS entry from Security to Library following core developer clarification that untrusted archive resource limits are classified as standard library bug/hardening improvements rather than security vulnerabilities. --- .../Library/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst | 3 +++ .../Security/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst delete mode 100644 Misc/NEWS.d/next/Security/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst 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. diff --git a/Misc/NEWS.d/next/Security/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst b/Misc/NEWS.d/next/Security/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst deleted file mode 100644 index f15a68d801863b4..000000000000000 --- a/Misc/NEWS.d/next/Security/2026-11-15-09-15-00.gh-issue-161955.aBcDeF.rst +++ /dev/null @@ -1,3 +0,0 @@ -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.