Repository navigation
gh-151497: Avoid huge pre-allocation for oversized tarfile extended headers (2.7) - #125
Open
icanhasmath wants to merge 1 commit into
Open
icanhasmath wants to merge 1 commit into
icanhasmath wants to merge 1 commit into
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>
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).