Repository navigation
gh-151497: Avoid huge pre-allocation for oversized tarfile extended headers (2.7) - #125
Merged
Merged
Conversation
…nded headers
tarfile reads a member's extended header (a GNU long name/link or a pax
header) with a single read sized by the header's size field:
buf = tarfile.fileobj.read(self._block(self.size))
The size is taken from the archive and is not validated, so a ~512-byte
crafted file can claim several gigabytes (or, via base-256 encoding, far
more) and make read() pre-allocate that much memory -- on open/iterate,
before any member is extracted.
Read the extended-header data in bounded chunks instead, so an oversized
or truncated header can no longer force a huge allocation. The bytes
returned for valid archives are unchanged.
Backport of upstream b83961a (3.13) /
da99711, as carried on 3.7.17.x
(ed14500), 3.9.25.x and 3.10.22.x.
2.7.18.x: tarfile.py is Latin-1, so the change was applied without
re-encoding the file. The tests use super(Class, self) and drop
@support.cpython_only, which 2.7's test_support doesn't have; the new
class is added to test_main()'s list.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ezequielp-activestate
approved these changes
Oct 30, 2026
ezequielp-activestate
left a comment
There was a problem hiding this comment.
Approving.
- The port. It matches the 3.7.17.x port (
ed14500d10) and upstream 3.13 (b83961ade8) line for line, apart from the 2.7 adaptations the description lists:super(Class, self)- no
@support.cpython_only - the
test_main()entry
- Read sites. Both size-driven reads, in
_proc_gnulongand_proc_pax, are covered. The other header reads in 2.7'starfile.pyare fixedBLOCKSIZEreads. - Encoding.
tarfile.pystays ISO-8859-1. - Tests. I put this branch's
Lib/teston stockpython:2.7images, both 64-bit and 32-bit (i386):- With this branch's
tarfile.py,test_tarfileruns 306 tests and passes, with 1 skipped. - With the
2.7tarfile.py, the three newExtendedHeaderMemoryTestcases fail on both builds.
- With this branch's
Merged
icanhasmath
added a commit
that referenced
this pull request
Oct 30, 2026
Bump PY_VERSION to 2.7.18.17 and move the pythongh-151497 NEWS entry (#125: tarfile no longer pre-allocates a huge buffer for an oversized GNU or pax extended header) into the 2.7.18.17 release notes. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backports pythongh-151497 to the 2.7 line, the fix ezequielp flagged in the #117 review. 3.7.17.8, 3.9.25.4 and 3.10.22.3 already ship it.
The problem
tarfilereads a GNU long name/link or a pax extended header with oneread()sized by the header's size field. That size comes from the archive and isn't checked, so a 512-byte crafted file can claim gigabytes and maketarfile.open()allocate them before any member is touched.The fix
_safe_read()reads the extended header in 1 MiB chunks and stops at end of file, so a bogus size can't force a big allocation. Valid archives return the same bytes as before. It's the upstream change (b83961a / da99711), as carried on 3.7.17.x in ed14500.2.7 adaptations:
tarfile.pyis Latin-1, and the edit keeps that encoding.super(Class, self)and drop@support.cpython_only, which 2.7'stest_supportdoesn't have.ExtendedHeaderMemoryTestis added totest_main().Testing
On a Linux 2.7.18.16 build:
ExtendedHeaderMemoryTestcases.read().ulimit -v 1500000, raisesMemoryErroron unpatched 2.7 andReadErrorwith this change.There's no GitHub CI on 2.7 PRs.
This goes into the next 2.7 release (2.7.18.17).