Skip to content

gh-151497: Avoid huge pre-allocation for oversized tarfile extended headers (2.7) - #125

Open
icanhasmath wants to merge 1 commit into
2.7from
2.7.18.x-tarfile-gh-151497
Open

icanhasmath wants to merge 1 commit into
2.7from
2.7.18.x-tarfile-gh-151497

Conversation

@icanhasmath

Copy link
Copy Markdown

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

tarfile reads a GNU long name/link or a pax extended header with one read() 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 make tarfile.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.py is Latin-1, and the edit keeps that encoding.
  • The tests use super(Class, self) and drop @support.cpython_only, which 2.7's test_support doesn't have.
  • ExtendedHeaderMemoryTest is added to test_main().

Testing

On a Linux 2.7.18.16 build:

  • test_tarfile: 306 tests OK (1 skipped), including the three new ExtendedHeaderMemoryTest cases.
  • Without the fix, all three new tests fail, each passing a 4294967296-byte size to a single read().
  • Real file: a 512-byte GNU long-name header claiming 3 GB, opened under ulimit -v 1500000, raises MemoryError on unpatched 2.7 and ReadError with this change.

There's no GitHub CI on 2.7 PRs.

This goes into the next 2.7 release (2.7.18.17).

…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant