]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.15] 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:21:41 +0000 (11:21 +0200)
committerGitHub <noreply@github.com>
Wed, 24 Jun 2026 09:21:41 +0000 (11:21 +0200)
commit72554b378e58955740ebce9861a4699ba8761eb8
treef259fe59e003fca073a3c4e5a9c969c5fbc76ae3
parentf84299087fcdee0ca962db49a7a4a91461d54f18
[3.15] gh-151497: Avoid huge pre-allocation for oversized tarfile extended headers (GH-151498) (GH-151977)

gh-151497: Avoid huge pre-allocation for oversized tarfile extended headers (GH-151498)

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]