HIGH · CVSS 4.0 EPSS 0.5% · 38th percentile CWE-787

CVE-2026-128067: Pillow: Heap buffer overflow saving TIFF with libtiff when tags override image size

Published 2026-11-12 · Updated 2026-11-27 · Also known as BIT-pillow-2026-128067, GHSA-xcvg-w56q-r89m

Summary

Pillow contains a heap buffer overflow vulnerability in its libtiff encoder that occurs when TIFF metadata tags conflict with actual image dimensions. Developers using Pillow to save TIFF images with custom compression or metadata should upgrade to version 13.0.0 or later.

Who is affected

This flaw affects applications that process and re-save images using Pillow's libtiff interface, particularly those that propagate EXIF or TIFF metadata from untrusted source files. When these metadata tags specify dimensions or configurations larger than the actual image buffer, the encoder performs out-of-bounds reads or writes, leading to potential information disclosure (memory leakage) or application crashes (SIGSEGV/SIGABRT).

Exploitability

The vulnerability is considered high severity due to the lack of access restrictions and ease of exploitation via crafted image files. While the EPSS score of 0.005 suggests a low current probability of exploitation, the CVSS 4.0 vector indicates that an attacker does not require special privileges or complex conditions to trigger a crash or information leak.

Remediation

How to check your project

Automated analysis of the published advisory data, generated 2026-11-27. Not human-reviewed; verify against the official advisory. How this works.

Affected packages

EcosystemPackageAffected versionsFixed inUpgrade
PyPI pillow >= 0, < 13.0.0 13.0.0 python -m pip install 'pillow>=13.0.0'

CVSS 4.0 vector

CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:H/VI:L/VA:H/SC:N/SI:N/SA:N

Attack vectorNetwork
Attack complexityLow
Attack requirementsPresent
Privileges requiredNone
User interactionNone
Confidentiality (vulnerable system)High
Integrity (vulnerable system)Low
Availability (vulnerable system)High
Confidentiality (subsequent systems)None
Integrity (subsequent systems)None
Availability (subsequent systems)None

Base score is not computed for CVSS 4.0 vectors; severity is taken from the advisory.

FAQ

Does this vulnerability require the attacker to have user privileges?

No, the CVSS vector indicates that the attack is network-exploitable with no special privileges (PR:N) and no user interaction required (UI:N).

Is this vulnerability actively exploited in the wild?

There is no record of this vulnerability being listed in the CISA Known Exploited Vulnerabilities (KEV) catalog.

What specifically triggers the heap overflow?

The overflow is triggered when metadata tags like ImageWidth, ImageLength, or SamplesPerPixel provided during an image save operation do not match the internal buffer size calculated by Pillow, causing the libtiff encoder to read or write beyond allocated memory.

Original advisory description
``` ============================================================================ ANT-2026-CS7N494P [high] CWE-787 memory-safety Heap buffer overflow saving TIFF with libtiff when tags override image size Location: src/libImaging/TiffDecode.c:1035 in ImagingLibTiffEncode() Commit: a707c6b21c313880d0771e54965d142dd42358b6 ============================================================================ ``` When Pillow saves an image as a compressed TIFF, size information taken from the image's metadata can make the encoder read past the end of a heap buffer. Someone who uploads an image can use this to get the server process's memory, including pointers, copied into the saved file, or to crash the process. When the metadata also enables TIFF "Predictor=2" compression, the encoder writes past the buffer instead, corrupting the heap and aborting the process. This affects applications that write TIFF through libtiff (any `compression=` other than raw) and either carry the source metadata across with `exif=im.getexif()` / `tiffinfo=`, or simply re-save a crafted palette TIFF with `Image.open(f).save(out, "TIFF")`, which inherits its compression and predictor from the upload. In `TiffImagePlugin._save`'s libtiff branch, only `BitsPerSample` is pinned to the real image when the tag list for the encoder is built (`src/PIL/TiffImagePlugin.py:1955`). `ImageWidth`, `ImageLength` and `PlanarConfiguration` are set from `im.size` but are then overwritten by the `exif=`/`tiffinfo=` merge loop at `:1768-1772`. `SamplesPerPixel` is never set for single-band modes, so a value from the caller's tags or from the source TIFF's own IFD reaches `TIFFSetField`. `ImagingLibTiffEncode` then passes a row buffer sized from the actual image (`src/encode.c:291`) to `TIFFWriteScanline` (`src/libImaging/TiffDecode.c:1035`) with no check against `TIFFScanlineSize()`, and libtiff consumes a full scanline of the tag-described width from it. The attached reproducer demonstrates the read case. A 64x4 greyscale JPEG whose EXIF says `SamplesPerPixel=3, Photometric=RGB` is converted into a valid RGB TIFF in which 128 bytes of each row are live heap, including pointers that change from run to run. The same JPEG with EXIF `ImageWidth=2**27` kills the process with SIGSEGV. For the write case, the values written are byte-wise differences of existing heap contents rather than attacker-chosen data, and nothing beyond the observed glibc abort is shown. The `tiffinfo=` route dates back to Pillow 2.3.0 (`dbf47837ce99`), and the `exif=` route to 8.4.0 (`c0f619384cbc`). ## Details When an image is saved as TIFF through the libtiff encoder (any `compression=` other than raw, or `TiffImagePlugin.WRITE_LIBTIFF`), libtiff sizes its scanline from the `ImageWidth` / `SamplesPerPixel` / `BitsPerSample` / `PlanarConfiguration` tags it was handed, Pillow sizes the encoder row buffer from the real image, and nothing compares the two. In `TiffImagePlugin._save`'s libtiff branch only `BitsPerSample` is pinned in the attribute dict given to the encoder (`atts`, `src/PIL/TiffImagePlugin.py:1955-1982`); the other geometry tags can come from caller- or attacker-supplied data: - `ImageWidth` (256), `ImageLength` (257) and `PlanarConfiguration` (284) are set from `im.size` *before* the `tiffinfo=` / `exif=` merge loop at `:1768-1772`, so they are overwritten by it - by the EXIF of an uploaded JPEG, or by stale geometry in `im.getexif()` / `im.tag_v2` of a TIFF that Pillow auto-transposed on load (Orientation tag) or that the application `thumbnail()`ed. - `SamplesPerPixel` (277) is only put in the IFD for multi-band modes (`:1847-1850`), so for single-band images (`L`, `P`, `I;16`, `1`, …) a value from `exif=` / `tiffinfo=`, or from the *source TIFF's own IFD* via `supplied_tags` (`im.tag_v2`, `:1782`), is forwarded to libtiff. A palette TIFF with `SamplesPerPixel=2, BitsPerSample=(8,8), ExtraSamples=(0,)` opens as mode `P` (rawmode `PX`, extra sample dropped), and a bare `im.save(out, "TIFF")` - no save arguments; `compression` and `Predictor` are inherited from the source - already makes libtiff process `2*width` bytes per row from a `width`-byte buffer. `ImagingLibTiffEncode` then hands the image-sized row buffer to `TIFFWriteScanline`, and libtiff reads one scanline of the *tag-described* geometry out of a buffer holding one row of the *actual* image. With `Predictor = 2` (also forwarded from `tiffinfo=` / `im.tag_v2`) libtiff's horizontal differencing first rewrites the whole scanline **in place**, so the over-read becomes an out-of-bounds heap **write** on every row. The tags do not have to be supplied deliberately by the application: `im.getexif()` of an uploaded JPEG returns whatever IFD0 tags the file carries, and the common metadata-preserving conversion ```python im = Image.open(uploaded_jpeg) # 64x4, mode "L" im.save(out, "TIFF", exif=im.getexif(), compression="tiff_lzw") ``` behaves as follows on the stock build at this commit: - EXIF IFD0 `{277: 3, 262: 2}` (SamplesPerPixel=3, Photometric=RGB): the save succeeds and `out` is a 64x4 **RGB** TIFF. Re-opening it, the first 64 bytes of every 192-byte row are the image (0x11 fill); the remaining 128 bytes are heap that followed the 64-byte row buffer - e.g. `... 807a97e6527f0000 ... f03632e5527f0000 40832ee5527f0000` (x86-64 heap pointers), different on every run. - EXIF IFD0 `{256: 1<<27}` (ImageWidth=2^27): the process dies with SIGSEGV; gdb shows `TIFFWriteScanline (libtiff.so.6) ← ImagingLibTiffEncode (src/libImaging/TiffDecode.c:1035) ← _encode (src/encode.c:139)`. - valgrind on the first case: `Invalid read of size 1 … TIFFWriteScanline … by ImagingLibTiffEncode … Address is 0 bytes after a block of size 64 alloc'd at calloc by _setimage`. - Control: the same JPEG with an EXIF holding only `Software` produces a 64x4 `L` TIFF whose 256 pixel bytes are all 0x11. The write variant needs nothing more than a well-formed 222-byte TIFF and the `exif=im.getexif()` idiom the project's own tests use (`Tests/test_file_tiff.py::test_exif`, `test_modify_exif`); the interpreter dies with glibc's `free(): invalid next size (fast)` (SIGABRT): ```python 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 ``` Here libtiff processes 192-byte rows (64 px × 3 samples) from the 12-byte buffer `_setimage` allocated for a 4-pixel RGB row, and `PredictorEncodeRow` rewrites the 180 heap bytes that follow it on each of 64 rows; valgrind on a `-g` build reports `Invalid write of size 1 … TIFFWriteScanline … by ImagingLibTiffEncode (TiffDecode.c:1035) … Address is 0 bytes after a block of size 12 alloc'd … by _setimage (encode.c:293)`. `im.thumbnail((8, 8))` before the save instead of the Orientation tag reaches the same state, as does the purely programmatic `Image.new("RGB", (128, 32)).save(fp, "TIFF", compression="tiff_lzw", tiffinfo={256: 1024, 257: 128, 317: 2})` (SIGSEGV). For the source-IFD `SamplesPerPixel` route, a crafted <2 KB palette `PX` TIFF with `Compression = LZW, Predictor = 2` kills the stock build on a bare `Image.open(f).save(out, "TIFF")`; with a predictor-less codec the same re-save returns normally with `width` bytes of heap per row encoded into the output. ### Walk-through `_save` seeds the output IFD from the image and then copies the caller's tags over it: ```python # src/PIL/TiffImagePlugin.py 1746 libtiff = WRITE_LIBTIFF or compression != "raw" 1749 ifd[PLANAR_CONFIGURATION] = 1 1751 ifd[IMAGEWIDTH] = im.size[0] 1752 ifd[IMAGELENGTH] = im.size[1] … 1757 elif "exif" in encoderinfo: 1758 info = encoderinfo["exif"] # Image.Exif, or bytes → Exif … 1768 for key in info: … 1772 ifd[key] = info.get(key) # 256/257/277/262/284/317 … overwrite the above ``` The geometry is only partially re-asserted afterwards: `BITSPERSAMPLE` is always set, but `SAMPLESPERPIXEL` only for multi-band modes and `IMAGEWIDTH`/`IMAGELENGTH` never: ```python 1847 if bits != (1,): 1848 ifd[BITSPERSAMPLE] = bits 1849 if len(bits) != 1: 1850 ifd[SAMPLESPERPIXEL] = len(bits) # not reached for L, P, I, F, I;16 … 1856 if PHOTOMETRIC_INTERPRETATION not in ifd: # supplied value kept ``` In the libtiff branch every IFD entry that is not block-listed - and, after the IFD, every entry of `supplied_tags` (the source file's `im.tag_v2`) not already present - becomes a `TIFFSetField` argument. The only tag pinned to the image's real value is `BITSPERSAMPLE`: ```python 1955 atts: dict[int, Any] = {BITSPERSAMPLE: bits[0]} 1959 for tag, value in itertools.chain(ifd.items(), supplied_tags.items()): … 1976 if tag not in atts and tag not in blocklist: … atts[tag] = value 2002 encoder = Image._getencoder(im.mode, "libtiff", a, encoderconfig) 2003 encoder.setimage(im.im, (0, 0, *im.size)) ``` For the SamplesPerPixel=3 example the tag list passed to the encoder is `[(256, 64), (257, 4), (258, 8), (259, 5), (262, 2), (277, 3), (278, 4), (284, 1)]`; for the ImageWidth example it is `[(256, 134217728), (257, 4), (258, 8), …]`. 256 and 277 are in `core_tags` (`src/encode.c:694`), so `PyImaging_LibTiffEncoderNew` forwards them straight to `TIFFSetField`. For a single-band image `SAMPLESPERPIXEL` is absent from `ifd`, so the loop takes tag 277 from `supplied_tags` - the source TIFF's IFD - when one is present; the `PLANAR_CONFIGURATION == 2` adjustment at `:1783-1788` already compensates for this for separate-planes sources, the contiguous `PX` layout is the remaining case. `encoder.setimage` sizes the packer's row buffer from the real image - 64 bytes for a 64-pixel `L` row: ```c /* src/encode.c, _setimage */ 287 if (state->bits > 0) { 291 state->bytes = (state->bits * state->xsize + 7) / 8; 293 state->buffer = (UINT8 *)calloc(1, state->bytes); ``` and `ImagingLibTiffEncode` packs one image row into it and gives the pointer to libtiff with no length: ```c /* src/libImaging/TiffDecode.c */ 1027 while (state->y < state->ysize) { 1028 state->shuffle(state->buffer, (UINT8 *)im->image[state->y + state->yoff] + …, state->xsize); 1035 if (TIFFWriteScanline(tiff, (tdata_t)(state->buffer), (uint32_t)state->y, 0) == -1) { ``` `TIFFWriteScanline` consumes `TIFFScanlineSize()` bytes, i.e. `ImageWidth * SamplesPerPixel * BitsPerSample / 8` as set through the tags. Nothing between `_save` and line 1035 compares that with `state->bytes`. With SamplesPerPixel=3 libtiff reads 192 bytes from the 64-byte block and LZW-encodes the trailing 128 heap bytes into the strip; with ImageWidth=2^27 it reads 128 MiB starting at the 64-byte block and faults. With `Predictor = 2`, libtiff's `PredictorEncodeRow` first runs horizontal differencing in place over the whole scanline (`bp[i] -= bp[i-stride]`), i.e. it rewrites the heap bytes that follow the block before encoding them; libtiff grows `ImageLength` as rows beyond the declared count are written, so every row of the real image goes through. A `PlanarConfiguration = 2` carried over from a planar source reaches libtiff by the same route; there libtiff's scanline (one sample plane) is shorter than Pillow's row, so the result is an undecodable file rather than an out-of-bounds access. The non-libtiff path (`compression` absent/raw) is not affected: it goes through `ImagingRawEncode`, which treats the IFD-derived stride as padding and either zero-fills or fails with `IMAGING_CODEC_CONFIG` (`src/libImaging/RawEncode.c:34-37,57-59`). Overriding `IMAGEWIDTH`/`IMAGELENGTH` through `tiffinfo` on that path is an intended feature (`Tests/test_file_tiff.py::test_tiff_chunks`). The `exif=` route was introduced in `c0f619384cbc` ("Added "exif" keyword argument to save Image.Exif instance", 2021-07-04), first released in 8.4.0. The same override through `tiffinfo=` is older: it dates back to `dbf47837ce99` ("Add arbitrary tags to tiff images using an ImageFileDirectory", 2013-10-02, first in 2.3.0). The stale-geometry-from-the-file-itself form dates from `1b70a4c6b515` ("Use TIFF orientation", 2019-09-13, first in 6.2.0), which added the transpose on load. ## Impact Affected are applications that write TIFF output from user-supplied images through libtiff. Through the source-IFD `SamplesPerPixel` route nothing more is needed than `Image.open(f).save(out, "TIFF")` (format normalisation) on a crafted palette TIFF: `Compression`, `SamplesPerPixel` and `Predictor` are all inherited from the upload, so the uploader chooses between heap corruption / worker abort (`Predictor = 2`) and a normally-returning save whose output carries `width` bytes of the worker's heap per row. The `exif=` / `tiffinfo=` routes cover applications that convert to compressed TIFF while carrying the metadata across - `im.save(..., "TIFF", compression="tiff_lzw"|"tiff_adobe_deflate"|"packbits"|"jpeg"|…, exif=im.getexif())`, `exif=im.info["exif"]`, or `tiffinfo=` built from the source file's tags. Neither option is a default, but both are ordinary settings for archival/print/scan pipelines and the idiom used in Pillow's own tests. The uploader then chooses the outcome through a few EXIF shorts: a small `SamplesPerPixel` (with a matching `Photometric` so the result is a valid RGB/CMYK TIFF) returns `(spp-1) × width` bytes of process heap per image row inside the produced file - for a 4000-pixel-wide upload that is 8 KB of heap per row, megabytes per file, readable with any TIFF decoder and containing allocator metadata, pointers (ASLR defeat) and whatever other requests left in freed memory; a large `ImageWidth` kills the worker with SIGSEGV; `ImageWidth`/`ImageLength` larger than the (transposed or thumbnailed) image together with `Predictor = 2` writes `(declared_width − real_width) × bytes_per_pixel` bytes past a small fresh `calloc` block on every row - the values written are byte-wise differences of the existing heap contents, so the attacker controls extent and repetition but not directly the data; the observed result is heap corruption and abort of the whole worker, exploitation beyond that is not demonstrated. Because `ImageWidth`/`ImageLength` are routinely present in IFD0 of camera and scanner output, a benign photo that is resized before such a save also over-reads (its EXIF still describes the original width), so the crash variant can occur without an attacker. ## Reproducing `repro` is a single Python script that uses the installed Pillow and only its public API; no rebuild is needed. It runs in well under a second. It builds the attacker's input in memory: a 64x4 JPEG in mode `L` with every pixel set to 0x11, plus a chosen set of EXIF IFD0 tags. It then runs the metadata-preserving conversion an application would do: ```python im = Image.open(io.BytesIO(jpeg)) im.save(out, "TIFF", exif=im.getexif(), compression="tiff_lzw") ``` Finally it re-opens the TIFF that comes out. There are three cases: - **control**: the EXIF holds only `Software`. The output is a 64x4 `L` TIFF whose 256 bytes are all 0x11. The verdict can only be `BUG` if this holds. - **leak**: the EXIF holds `SamplesPerPixel=3, PhotometricInterpretation=RGB`. libtiff now treats each scanline as 192 bytes, but the row buffer only holds 64 bytes. The save still succeeds and the output is a 64x4 **`RGB`** TIFF. In each 192-byte row, the first 64 bytes are the image (0x11). The other 128 bytes are whatever sat in the heap after the 64-byte row buffer. - The script hex-dumps row 0's extra bytes. They include x86-64 pointers such as `…e06c297e677f0000`, which change from run to run because of ASLR. - It also counts the extra bytes that are neither 0x11 nor 0x00, since those could not have come from the image or from zero padding. On the stock build this is a few hundred of the 512 bytes (200-310 in our runs). - **crash**: the EXIF holds `ImageWidth=2**27`. The script re-runs itself as a child process (`repro crash`) with core dumps disabled. libtiff tries to read 128 MiB out of the 64-byte buffer, and the child dies with SIGSEGV (exit code -11). Expected output on the stock build (the heap bytes and the count vary): ``` control: L (64, 4), all bytes 0x11: True leak: RGB (64, 4) first 64 bytes of each row are the image (0x11): True row 0, bytes 64..192 (past the row buffer): 0000000000000000310000000000000090e5b50669550000e06c297e677f0000 0000000001007900007b8804d80c0d8830000000000000001007000000000000 0100000000000000801a897e677f0000240300000000000030444a7d677f0000 4095487d677f0000885f897e677f000003000001010000000000000000000000 220 of 512 bytes past the row buffers are neither 0x11 nor 0x00 crash: child exit code -11 (SIGSEGV) BUG ``` The last line is `BUG` if the control is clean and the leak case produced foreign bytes, the crash case died with SIGSEGV/SIGBUS, or both. Otherwise it is `OK`. With either attached patch applied and rebuilt, the script prints: ``` control: L (64, 4), all bytes 0x11: True leak: output is not a readable image (no over-read) crash: child exit code 0 (no signal) OK ``` In the leak case, the fixed build writes the image's real single sample per pixel but keeps the supplied `Photometric=RGB`. Pillow will not open a TIFF like that. The out-of-bounds write variant is the three-line `Orientation=6, Predictor=2` snippet in Details (SIGABRT, `free(): invalid next size (fast)` on the stock build); for a file:line stack run it under valgrind on a `-g` build of the extensions. ## Suggested fix Two alternative patches are attached. Patch 2 is the Python-only subset of patch 1; patch 1 also hardens the C encoder. **Attached patch 1** (`src/PIL/TiffImagePlugin.py`, `src/libImaging/TiffDecode.c`) does two independent things: 1. `src/PIL/TiffImagePlugin.py:1955` - the libtiff branch already pre-seeds `atts` with `BITSPERSAMPLE` so that no supplied tag can replace it. The patch extends that to the other tags that determine libtiff's scanline size, taken from the image being saved: ```diff - atts: dict[int, Any] = {BITSPERSAMPLE: bits[0]} + atts: dict[int, Any] = { + BITSPERSAMPLE: bits[0], + SAMPLESPERPIXEL: len(bits), + IMAGEWIDTH: im.size[0], + IMAGELENGTH: im.size[1], + PLANAR_CONFIGURATION: 1, + } ``` Doing it here rather than in the IFD built at 1749-1772 leaves the raw-writer behaviour exercised by `test_tiff_chunks` untouched, and because the merge loop visits `ifd` and `supplied_tags` only for tags not already in `atts`, it closes the `exif=`, `tiffinfo=` and source-IFD (`im.tag_v2`) routes alike. A supplied `PHOTOMETRIC_INTERPRETATION` still reaches the file (so EXIF `{277:3, 262:2}` now yields a 1-sample "RGB" TIFF that Pillow refuses to open); whether to force it to the mode's value is a format decision that no longer affects memory safety. For the re-saved `PX` palette case it may also be worth dropping the source's `EXTRASAMPLES` from `supplied_tags` (meaningless once the extra channel was discarded on load), as is already done for the planar case at `:1783-1788`, so the output is a self-consistent 1-sample palette file. 2. `src/libImaging/TiffDecode.c`, `ImagingLibTiffEncode` - before the scanline loop, it refuses any configuration in which libtiff would read more than the packer produced, so the encoder stays safe regardless of where tags come from: ```c if (TIFFScanlineSize64(tiff) > (uint64_t)state->bytes) { state->errcode = IMAGING_CODEC_CONFIG; ImagingLibTiffEncodeCleanup(state);

References

Source records: GHSA-xcvg-w56q-r89m (OSV.dev) · EPSS by FIRST, 2026-11-27.