# Pillow: Heap buffer overflow saving TIFF with libtiff when tags override image size

Pillow's libtiff TIFF encoder trusts ImageWidth/SamplesPerPixel tags from EXIF or source IFD, so libtiff reads or writes past the image-sized row buffer, leaking heap or crashing.

- Payout: Research
- Severity: high
- Program: Pillow
- Found by: carlini
- Disclosed: 2026-11-12
- CVE: CVE-2026-128067
- Write-up quality: 9/10
- Original: https://github.com/advisories/GHSA-xcvg-w56q-r89m
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-xcvg-w56q-r89m
- Tags: area:oss domain:oss impact:data-leak impact:dos kind:research platform:ghsa sev:high tech:python technique:chained technique:source-review vuln:dos vuln:info-disclosure vuln:memory-corruption

**Target:** Pillow TiffImagePlugin libtiff save path (ImagingLibTiffEncode / TIFFWriteScanline)
**Root cause:** TiffImagePlugin._save only pins BitsPerSample in the encoder attribute dict; ImageWidth, ImageLength, PlanarConfiguration and SamplesPerPixel from exif=/tiffinfo=/source IFD overwrite the image geometry, so libtiff's TIFFScanlineSize exceeds the row buffer allocated from the real image, and nothing compares the two before TIFFWriteScanline.
**Steps:**
1. Build a 64x4 mode L JPEG with EXIF IFD0 `{277: 3, 262: 2}` (SamplesPerPixel=3, Photometric=RGB).
2. Run the metadata-preserving conversion `im = Image.open(uploaded_jpeg); im.save(out, "TIFF", exif=im.getexif(), compression="tiff_lzw")`.
3. Re-open the output: it is a 64x4 RGB TIFF where each 192-byte row has 64 image bytes followed by 128 bytes of heap (pointers visible).
4. For the crash variant use EXIF `{256: 1<<27}` (ImageWidth=2^27); libtiff reads 128 MiB from the 64-byte buffer and the process dies with SIGSEGV.
5. For the write variant save `Image.new("RGB", (64, 4)).save(b, "TIFF", compression="tiff_lzw", tiffinfo={274: 6, 317: 2})` (Orientation=6, Predictor=2), reopen (auto-transposed to 4x64, tag_v2 still 64x4), then `im.save(io.BytesIO(), "TIFF", exif=im.getexif())`; PredictorEncodeRow rewrites 180 heap bytes past the 12-byte row buffer on each of 64 rows, glibc aborts with `free(): invalid next size (fast)`.
6. Source-IFD route: craft a <2 KB palette PX TIFF with `Compression=LZW, Predictor=2, SamplesPerPixel=2, BitsPerSample=(8,8), ExtraSamples=(0,)`; a bare `Image.open(f).save(out, "TIFF")` inherits compression/predictor and aborts.
**How found:** Code reading of TiffImagePlugin._save's libtiff branch: noticed only BitsPerSample is pinned in `atts` while ImageWidth/ImageLength/PlanarConfiguration are set from im.size before the exif/tiffinfo merge loop overwrites them, and SamplesPerPixel is only set for multi-band modes; then traced that ImagingLibTiffEncode passes an image-sized row buffer to TIFFWriteScanline with no check against TIFFScanlineSize, and that Predictor=2 turns the over-read into an in-place over-write.
**Exploitation:** An attacker uploads an image to an application that converts to compressed TIFF while carrying metadata (`exif=im.getexif()`, `exif=im.info["exif"]`, `tiffinfo=`) or simply re-saves a crafted palette TIFF. A small SamplesPerPixel with matching Photometric yields a valid RGB TIFF embedding (spp-1)*width bytes of process heap per row (pointers, allocator metadata, ASLR defeat) readable with any TIFF decoder; a large ImageWidth causes SIGSEGV; ImageWidth/ImageLength larger than the (transposed or thumbnailed) image plus Predictor=2 writes past a small calloc block on every row, corrupting the heap and aborting the worker. Written values are byte-wise differences of existing heap, so extent and repetition are attacker-controlled but not the data.
**Escalation:** Chaining the leak (heap pointers, ASLR defeat) with the write variant (heap corruption via Predictor=2) is implied but full code execution is not demonstrated; observed impact is info leak, SIGSEGV and SIGABRT of the worker.
**Insight:** When a library hands a buffer to a C dependency that sizes its own reads from metadata tags, check which tags the wrapper pins versus which can be overwritten by caller- or file-supplied metadata; also check whether a predictor/differencing codec rewrites the scanline in place, converting an over-read into an over-write. Stale geometry from auto-transpose or thumbnail() is a reliable source of mismatched tags.

## Proof of concept (copied exactly from the original report)

**EXIF SamplesPerPixel=3 heap over-read reproducer**

```
from PIL import Image; import io
b = io.BytesIO()
Image.new("RGB", (64, 4)).save(b, "TIFF", compression="tiff_lzw", tiffinfo={274: 6, 317: 2})  # Orientation=6, Predictor=2
im = Image.open(io.BytesIO(b.getvalue())); im.load()   # auto-transposed to 4x64; tag_v2/getexif() still say 64x4
im.save(io.BytesIO(), "TIFF", exif=im.getexif())       # or tiffinfo=im.tag_v2
```

**Metadata-preserving JPEG to TIFF conversion**

```
im = Image.open(uploaded_jpeg)                                   # 64x4, mode "L"
im.save(out, "TIFF", exif=im.getexif(), compression="tiff_lzw")
```

**Programmatic tiffinfo geometry override save**

```
Image.new("RGB", (128, 32)).save(fp, "TIFF", compression="tiff_lzw", tiffinfo={256: 1024, 257: 128, 317: 2})
```

