GHSA-8g5g-8vmr-9782 - Pillow: Heap buffer overflow in ImagingJpegDecode
## Summary
`JpegImageFile.draft()` โ which `Image.thumbnail()` calls automatically โ requests 1/2, 1/4 or 1/8 scaled JPEG output and shrinks `self._size` and the tile extents to match, so `_setimage()` allocates a single-row decode buffer of `ceil(W/s)` samples. `ImagingJpegDecode()` sets `cinfo.scale_num`/`scale_denom`, calls `jpeg_start_decompress()`, and then reads scanlines into that buffer without ever comparing `cinfo.output_width`, `cinfo.output_height` or `cinfo.out_color_components` against `state->xsize`, `state->ysize` and `state->bits`.
libjpeg-turbo 3.x decodes lossless (SOF3) JPEG, but hardwires lossless streams to no scaling โ the requested `scale_denom` is silently discarded. It therefore returns full-width `W`-sample rows into a buffer sized for `ceil(W/s)`. Each `jpeg_read_scanlines()` call writes `(W - ceil(W/s)) x components` bytes past the end of the heap allocation, and the overflowing bytes are the decoded pixel values, which are byte-exact for an 8-bit grayscale lossless image.
The input is a standards-compliant JPEG, so `Image.open(..., formats=["JPEG"])` does not prevent it. Pillow's Python SOF parser already accepts marker `0xFFC3` ("SOF3", "Spatial lossless") at 8-bit precision, so the file opens normally as a `JPEG` / mode `L` image at its full declared size.
## Affected code
At `e41083f383c9cd3db95de52564cc0b6452313d4a` (`main`, 2026-08-29):
- `src/libImaging/JpegDecode.c:224-262` โ `ImagingJpegDecode()`; `jpeg_start_decompress()` at :240, `jpeg_read_scanlines()` at :252, with no geometry check between them.
- `src/decode.c:222-231` โ `_setimage()`; `state->buffer = calloc(1, state->bytes)` with `state->bytes = (state->bits * state->xsize + 7) / 8`.
- `src/PIL/JpegImagePlugin.py:428-464` โ `JpegImageFile.draft()`; computes `s` and shrinks `_size`, the tile extents and `decoderconfig` with no notion of lossless coding.
- `src/PIL/JpegImagePlugin.py:271` โ `0xFFC3: ("SOF3", "Spatial lossless", SOF)`.
```c
// src/libImaging/JpegDeco
CRITICAL
Severity & confidence
Source severitycritical
Platform severitycritical
Confidencemedium
Exploitationunknown
CVSS-
Affected
VendorsPillow
ProductsPillow
Reporting sources (1)
GitHub Security Advisories
Timeline & provenance
Published2026-10-20T20:40:07
Last updated2026-10-21T04:59:36
Ingested2026-10-20T20:52:57
FreshnessNEAR-REAL-TIME
Threat typeadvisory
Recommended action
Review the advisory and upgrade affected packages to the first patched version.
Next steps
- Identify affected systems in your environment.
- Apply the vendor patch or mitigation guidance.
- Review logs for indicators described in the advisory.
- Follow the affected vendor/product to track updates.
Confidence explanation
Reported by a single or secondary source; treat as indicative, verify before acting.
Source type: vendor ยท Original: GitHub Security Advisories