Skip to content

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

Merged
icanhasmath merged 1 commit into
2.7from
2.7.18.x-tarfile-gh-151497
Oct 30, 2026
Merged

icanhasmath merged 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>

@ezequielp-activestate ezequielp-activestate left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_gnulong and _proc_pax, are covered. The other header reads in 2.7's tarfile.py are fixed BLOCKSIZE reads.
  • Encoding. tarfile.py stays ISO-8859-1.
  • Tests. I put this branch's Lib/test on stock python:2.7 images, both 64-bit and 32-bit (i386):
    • With this branch's tarfile.py, test_tarfile runs 306 tests and passes, with 1 skipped.
    • With the 2.7 tarfile.py, the three new ExtendedHeaderMemoryTest cases fail on both builds.

@icanhasmath
icanhasmath merged commit ce988ae into 2.7 Oct 30, 2026
@icanhasmath icanhasmath mentioned this pull request Oct 30, 2026
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>
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.

2 participants