]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.13] 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:45 +0000 (11:23 +0200)
committerGitHub <noreply@github.com>
Wed, 24 Jun 2026 09:23:45 +0000 (11:23 +0200)
commitb83961ade8bbd4b6d6a14a86b8324a3e4141362c
treec0a09e6aa7bd2785f15cfcc4b23e35655b361258
parent43c188d0d2128e1c6f11706c365b131608b707ca
[3.13] gh-151497: Avoid huge pre-allocation for oversized tarfile extended headers (GH-151498) (GH-151978)

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]