]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.14] gh-151497: Avoid huge pre-allocation for oversized tarfile extended headers...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 24 Jun 2026 09:23:36 +0000 (11:23 +0200)
committerGitHub <noreply@github.com>
Wed, 24 Jun 2026 09:23:36 +0000 (11:23 +0200)
commit2cf26d05553ae9f69382eca47ff59e31a11eb2e8
treee77d46586f0d0ee25a68505ff6c4faf0380a2003
parent59ff73aa77be7802ee5a0345692088f526162588
[3.14] gh-151497: Avoid huge pre-allocation for oversized tarfile extended headers (GH-151498) (GH-151979)

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 extraction filter runs.

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.
(cherry picked from commit da99711d37dba3413af05207ea8b12cb06041c0f)

Co-authored-by: Shardul Deshpande <iamsharduld@users.noreply.github.com>
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS.d/next/Library/2026-06-15-15-32-36.gh-issue-151497.1cfmSV.rst [new file with mode: 0644]