PT-2026-120317 · Pypi · Pillow

Published

2026-10-20

·

Updated

2026-10-20

CVSS v4.0

8.7

High

VectorAV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:N/VA:N/SC:N/SI:N/SA:N
============================================================================
TiffDecode returns uninitialized memory as pixels for short JPEG strips
Location: src/libImaging/TiffDecode.c:554 in decodeStrip()
Commit:  a707c6b21c313880d0771e54965d142dd42358b6
============================================================================
Pillow hands back leftover process memory as image pixels when it opens a small crafted TIFF file. Anyone who can get an application to open their image, and can see something produced from its pixels (a thumbnail, a preview, a converted copy), can read heap memory that the process had previously used and freed. This works in the default configuration: opening and loading the 478-byte file succeeds with no error or warning, and every row below the first eight holds stale memory instead of picture content.
The bug is in decodeStrip() in src/libImaging/TiffDecode.c, and decodeTile() has the same shape. The function realloc()s state->buffer to TIFFStripSize() bytes without initialising it and calls TIFFReadEncodedStrip(). Whenever that call does not return -1, it copies all RowsPerStrip rows of the buffer into the image. libtiff's JPEG codec only warns when the JPEG embedded in a strip has fewer rows than the strip, and Pillow silences libtiff warnings. The codec decodes the rows that exist and still returns the full strip size, so the untouched remainder is copied out as pixels. libtiff's Group 4 fax codec behaves the same way for a stream that ends early.
The attached reproducer builds a 256×256 TIFF whose single strip or tile is a 256×8 JPEG. Before loading it, the script frees buffers filled with b"LEAK". It then finds 15,872 copies of LEAK in rows 8–255 of the decoded image (the same freed chunk was reused in 500 of 500 runs). Under glibc's MALLOC PERTURB , those rows are entirely the allocator's fill byte. A well-formed control file decodes correctly in both cases.
This is a read of uninitialised heap memory, not memory corruption. Each strip can disclose up to width × RowsPerStrip bytes, and what comes back depends on the allocator's state at the time. The strip path was introduced in e91b851fdc1c ("Add reading old-JPEG compressed TIFFs", first released in Pillow 6.0.0). The tile path has had the same pattern since f0436a4ddc95 (Pillow 5.3.0).

Details

src/libImaging/TiffDecode.c decodes compressed TIFFs through libtiff into a strip (or tile) buffer that it realloc()s to TIFFStripSize() bytes and never initialises, and then copies all RowsPerStrip rows of that buffer into the image whenever TIFFReadEncodedStrip() does not return -1. libtiff's JPEG codec does not fail when the JPEG stream inside a strip has fewer rows than the strip: it decodes the rows that exist and returns the full strip size. The rows it did not write are whatever the heap held before, and Pillow returns them as pixels.
A 478-byte file shows it: a classic little-endian TIFF, 256×256, BitsPerSample=8, Compression=7, PhotometricInterpretation=1, RowsPerStrip=256, one strip whose data is a baseline JPEG of 256×8 pixels (as written by Image.new("L",(256,8),200).save(…,"JPEG")). Image.open(BytesIO(data)).load() succeeds with default settings (compressed TIFFs always take the libtiff path). Rows 0–7 of the result are the JPEG content (value 200); the remaining 63488 bytes of im.tobytes() are heap garbage: run under glibc's MALLOC PERTURB =1 they are all 0xFE, under MALLOC PERTURB =170 all 0x55 (the fresh-malloc fill byte), and in a process that has just freed buffers containing b"LEAK" they contain 15872 copies of LEAK.
Feeding the same file to libtiff 4.5.0 directly confirms the library side: TIFFReadEncodedStrip(t, 0, buf, 65536) prints JPEGPreDecode: Warning, Improper JPEG strip/tile size, expected 256x256, got 256x8., returns 65536, and leaves 63488 of the 65536 buffer bytes untouched. Pillow installs TIFFSetWarningHandler(NULL) (TiffDecode.c:682), so the warning is not visible either.
The strip path:
c
// src/libImaging/TiffDecode.c:552
  /* realloc to fit whole strip */
  /* malloc check above */
  new data = realloc(state->buffer, state->bytes);   // state->bytes = TIFFStripSize()
  …
  state->buffer = new data;

  for (; state->y < state->ysize; state->y += rows per strip) {
    …
      if (TIFFReadEncodedStrip(
          tiff,
          TIFFComputeStrip(tiff, state->y, plane),
          (tdata t)state->buffer,
          strip size
        ) == -1) {
        … return -1;
      }
      …
      // iterate over each row in the strip and stuff data into image
      for (strip row = 0;
         strip row < MIN((INT32)rows per strip, state->ysize - state->y);
         strip row++) {
        …
        shuffler(
          (UINT8 *)im->image[state->y + state->yoff + strip row] +
            state->xoff * im->pixelsize,
          state->buffer + strip row * row byte size,
          state->xsize
        );
state->buffer arrives here as the one-row calloc made by decode.c:230 ((bits*xsize+7)/8 = 256 bytes for the example). The realloc to 65536 bytes hands back a different, uninitialised chunk; nothing zeroes it. TIFFReadEncodedStrip() returns the nominal strip size on success, not the number of rows produced, and the only check is == -1, so the row loop unconditionally copies 256 rows × 256 bytes into im->image (which itself is zeroed by core.new, so the garbage provably comes from state->buffer). decodeTile() has the same shape (TiffDecode.c:448 realloc to TIFFTileSize(), then tile length rows copied after each TIFFReadTile() at 460–488).
For a well-formed file the JPEG inside the strip is 256×256, libtiff fills all 65536 bytes and the copy is correct. The malicious file differs only in the SOF height of the embedded JPEG; libtiff treats "JPEG smaller than strip" as a warning, clamps the row count to the JPEG height, and reports success.
JPEG is not the only libtiff codec that succeeds on a short strip. Fax4Decode (Compression=4) returns success for a T.6 stream that ends early: a 133-byte 512×256 1-bit TIFF whose strip is the Group 4 stream of a 512×16 image loads without error, libtiff writes 17 rows (1088 bytes) of the 16384-byte buffer (Fax4Decode: Warning, Premature EOL at line 16, return value 16384) and the other 15296 bytes are again heap contents. LZW, Deflate, PackBits, Group 3 and CCITT RLE were checked and do return -1 on short data, so Pillow raises OSError for those.
The strip path was introduced in e91b851fdc1c ("Add reading old-JPEG compressed TIFFs", 2018-12-08), first released in Pillow 6.0.0, which replaced row-by-row TIFFReadScanline() (where libtiff errors on the first missing row) with the whole-strip realloc() and copy. The tile path has had the same shape since it was added in f0436a4ddc95 (2018-07-02, Pillow 5.3.0).

Impact

Any application that opens attacker-supplied images with Pillow in the default configuration and returns something derived from the pixels (thumbnail, preview, format conversion, OCR text) discloses freed heap memory of the worker process to the uploader: everything below the rows the embedded JPEG/G4 stream actually encodes, up to ImageWidth × RowsPerStrip × SamplesPerPixel bytes per strip and tens of MB per image within the default MAX IMAGE PIXELS. load() raises nothing, so the application has no signal that the image is malformed. What comes back depends on allocator state; in a long-running web worker it is typically recently freed request bodies, other users' decoded image buffers and Python object contents. There is no memory corruption; this is a read of uninitialised (previously used) heap memory, analogous in effect to an out-of-bounds read that lands in pixels.

Reproducing

repro is a short Python script that runs against the installed Pillow build with default settings: no rebuild, no sanitizer, no files written. It runs twice, once normally and once re-executing itself under glibc's MALLOC PERTURB =170, and the last line it prints is BUG or OK.
Input. tiff(jpeg rows, tiled) builds a little-endian TIFF in memory. The header declares a 256×256 8-bit greyscale image with Compression=7 (JPEG), either as one strip (RowsPerStrip=256) or as one 256×256 tile (TileWidth/TileLength). The strip or tile is a baseline JPEG of 256×jpeg rows pixels, all value 200, written by Pillow's own JPEG encoder. For each layout the script builds two files:
  • Control: jpeg rows=256. This is a well-formed file, and libtiff fills the whole 64 KiB strip/tile.
  • Crafted: jpeg rows=8, 478 bytes (strip) or 490 bytes (tile). The only difference is the embedded JPEG's height. libtiff decodes 8 rows (2048 bytes), warns Improper JPEG strip/tile size (Pillow silences the warning) and returns the full strip/tile size as success.
Steps. Each file is opened (the script asserts it takes the libtiff decoder). Just before im.load() the script allocates and frees six 64 KiB bytearrays filled with b"LEAK", which leaves freed heap chunks about the size of the buffer that decodeStrip()/ decodeTile() is about to realloc(). For rows 8–255 it then counts copies of LEAK and bytes equal to 0x55 (glibc's fresh-malloc fill under MALLOC PERTURB =170). The files contain no LEAK bytes (the script asserts this), so any copy in the image came from freed heap memory. It reports BUG if a crafted file shows any LEAK in the normal pass, or rows 8–255 entirely 0x55 in the perturbed pass.
Expected output on this tree:
[normal ] strip well-formed (1222 B) rows 8-255:   0 x b'LEAK',   0/63488 == 0x55, head=b'xc8xc8xc8xc8xc8xc8xc8xc8'
[normal ] strip crafted   ( 478 B) rows 8-255: 15872 x b'LEAK',   0/63488 == 0x55, head=b'LEAKLEAK'
[normal ] tiled well-formed (1234 B) rows 8-255:   0 x b'LEAK',   0/63488 == 0x55, head=b'xc8xc8xc8xc8xc8xc8xc8xc8'
[normal ] tiled crafted   ( 490 B) rows 8-255: 15872 x b'LEAK',   0/63488 == 0x55, head=b'LEAKLEAK'
[perturb] strip well-formed (1222 B) rows 8-255:   0 x b'LEAK',   0/63488 == 0x55, head=b'xc8xc8xc8xc8xc8xc8xc8xc8'
[perturb] strip crafted   ( 478 B) rows 8-255:   0 x b'LEAK', 63488/63488 == 0x55, head=b'UUUUUUUU'
[perturb] tiled well-formed (1234 B) rows 8-255:   0 x b'LEAK',   0/63488 == 0x55, head=b'xc8xc8xc8xc8xc8xc8xc8xc8'
[perturb] tiled crafted   ( 490 B) rows 8-255:   0 x b'LEAK', 63488/63488 == 0x55, head=b'UUUUUUUU'
BUG
im.load() succeeds for every file. For the crafted files rows 8–255 are the uninitialised strip/tile buffer, copied as-is into the image. With the attached patch applied, rows 8–255 of the crafted files come back as x00 bytes, with no LEAK and no 0x55, and the script ends with OK.
The LEAK count depends on where glibc places the buffer (the same freed chunk was reused in 500 of 500 runs here); the MALLOC PERTURB pass does not. The script covers the JPEG codec only. The Group 4 variant in the Details section (Compression=4, a T.6 stream that ends early) goes through the same decodeStrip() copy, and the same fix covers it.

Suggested fix

The attached patch zeroes state->buffer before each libtiff read in both loops of src/libImaging/TiffDecode.c — before TIFFReadTile() in decodeTile() and before TIFFReadEncodedStrip() in decodeStrip() — so rows a codec does not produce are deterministic zeros rather than stale heap:
diff
--- a/src/libImaging/TiffDecode.c
+++ b/src/libImaging/TiffDecode.c
@@ decodeTile
       for (x = state->xoff; x < state->xsize; x += tile width) {
+        memset(state->buffer, 0, state->bytes);
         if (TIFFReadTile(tiff, (tdata t)state->buffer, x, y, 0, plane) == -1) {
@@ decodeStrip
     for (plane = 0; plane < planes; plane++) {
       ImagingShuffler shuffler = unpackers[plane];
+      memset(state->buffer, 0, state->bytes);
       if (TIFFReadEncodedStrip(
A memset per strip/tile (rather than one calloc after the size is known) is needed because the same buffer is reused for every strip, and a short last strip would otherwise expose the previous strip's rows — harmless for disclosure, but the per-read memset keeps the behaviour uniform at the cost of one extra pass over the buffer. Promoting libtiff warnings to errors via a warning handler is not a good alternative: both codecs only warn (Improper JPEG strip/tile size, Premature EOL), but so do many legitimate files (unknown tags, bad tag counts), and the return value of TIFFReadEncodedStrip() carries no row count to check against. Initialising the buffer covers every codec. decodeAsRGBA() needs no change: TIFFRGBAImageGet() fills its whole raster, and JPEG files do not reach it (JPEGCOLORMODE RGB sends them down the strip path).
Valid files decode exactly as before; rows a codec does not produce are now black instead of garbage, and nothing that previously loaded now fails. The patch also adds test jpeg short strip to Tests/test file libtiff.py, parametrised over strip and tile, which builds the 256×8-JPEG-in-256×256 file and asserts rows 8–255 are zero. It is a regression guard for the fixed code rather than a reliable detector of the bug: in a fresh pytest process the unfixed strip variant can pass because the new heap chunk happens to be zero (the tiled variant fails before the fix). With the patch, Tests/test file libtiff.py and Tests/test file tiff.py pass and the reproducer prints OK.

The patch

Zero the libtiff strip/tile buffer before each TIFFReadEncodedStrip/TIFFReadTile in TiffDecode.c so short strips do not expose uninitialised heap
Applies to src/libImaging/TiffDecode.c at the commit above.
diff
diff --git a/Tests/test file libtiff.py b/Tests/test file libtiff.py
index 3d804ea4a..67bc96329 100644
--- a/Tests/test file libtiff.py
+++ b/Tests/test file libtiff.py
@@ -1162,6 +1162,45 @@ class TestFileLibTiff(LibTiffTestCase):
       im.load()
     assert im.size == (950, 975)
 
+  @pytest.mark.parametrize("tiled", (False, True))
+  def test jpeg short strip(self, tiled: bool) -> None:
+    # The JPEG data in the strip/tile only contains 8 of the 256 rows that
+    # RowsPerStrip/TileLength declare. libtiff decodes the rows that exist
+    # and still reports success, so the remaining rows must be cleared
+    # rather than left as uninitialised memory.
+    jpeg = io.BytesIO()
+    Image.new("L", (256, 8), 200).save(jpeg, "JPEG")
+    jpeg data = jpeg.getvalue()
+
+    ifd = TiffImagePlugin.ImageFileDirectory v2()
+    ifd[256] = 256 # ImageWidth
+    ifd[257] = 256 # ImageLength
+    ifd[258] = 8 # BitsPerSample
+    ifd[259] = 7 # Compression: JPEG
+    ifd[262] = 1 # PhotometricInterpretation: BlackIsZero
+    ifd[277] = 1 # SamplesPerPixel
+    if tiled:
+      ifd[322] = 256 # TileWidth
+      ifd[323] = 256 # TileLength
+      ifd[324] = 0 # TileOffsets
+      ifd[325] = len(jpeg data) # TileByteCounts
+      # Point the tile data at the end of the IFD
+      ifd[324] = 8 + len(ifd.tobytes(8))
+    else:
+      ifd[278] = 256 # RowsPerStrip
+      ifd[STRIPOFFSETS] = 0 # Updated by tobytes() to follow the IFD
+      ifd[279] = len(jpeg data) # StripByteCounts
+
+    b = io.BytesIO()
+    ifd.save(b)
+    b.write(jpeg data)
+
+    with Image.open(b) as im:
+      assert im.tile[0][0] == "libtiff"
+      im.load()
+      assert im.crop((0, 0, 256, 8)).getextrema() == (200, 200)
+      assert im.crop((0, 8, 256, 256)).getextrema() == (0, 0)
+
   def test orientation(self) -> None:
     with Image.open("Tests/images/g4 orientation 1.tif") as base im:
       for i in range(2, 9):
diff --git a/src/libImaging/TiffDecode.c b/src/libImaging/TiffDecode.c
index 8a57c523d..d1d6bfbbb 100644
--- a/src/libImaging/TiffDecode.c
+++ b/src/libImaging/TiffDecode.c
@@ -457,6 +457,11 @@ decodeTile(
     for (plane = 0; plane < planes; plane++) {
       ImagingShuffler shuffler = unpackers[plane];
       for (x = state->xoff; x < state->xsize; x += tile width) {
+        // Some codecs (e.g. JPEG, Group 4) succeed on a tile that decodes
+        // to fewer rows than the tile length, leaving the rest of the
+        // buffer untouched. Clear it so that uninitialised memory is not
+        // copied into the image.
+        memset(state->buffer, 0, state->bytes);
         if (TIFFReadTile(tiff, (tdata t)state->buffer, x, y, 0, plane) == -1) {
           TRACE(("Decode Error, Tile at %dx%d
", x, y));
           state->errcode = IMAGING CODEC BROKEN;
@@ -563,6 +568,11 @@ decodeStrip(
     int plane;
     for (plane = 0; plane < planes; plane++) {
       ImagingShuffler shuffler = unpackers[plane];
+      // Some codecs (e.g. JPEG, Group 4) succeed on a strip that decodes
+      // to fewer rows than RowsPerStrip, leaving the rest of the buffer
+      // untouched. Clear it so that uninitialised memory is not copied
+      // into the image.
+      memset(state->buffer, 0, state->bytes);
       if (TIFFReadEncodedStrip(
           tiff,
           TIFFComputeStrip(tiff, state->y, plane),

The reproducer

Reproducer — repro, 2,688 bytes (click to open)
Its last line is BUG when the issue triggers and OK when it does not. Re-run on a clean build of that commit it printed BUG.
python
#!/usr/bin/env python3
# Pillow TiffDecode.c decodeStrip/ decodeTile: strip/tile buffer is realloc()ed, never
# initialised, and copied whole into the image even when libtiff's JPEG codec decoded
# fewer rows than RowsPerStrip/TileLength. Two independent signals:
# pass 1 (normal heap): seed freed 64 KiB chunks with b"LEAK" just before load() and
#     count copies of LEAK in the decoded pixels (file contains no such bytes);
# pass 2 (same script re-run under glibc MALLOC PERTURB =170): fresh malloc memory is
#     0x55, so rows that come back all-0x55 are provably uninitialised heap.
import gc, io, os, struct, subprocess, sys
from PIL import Image
W = H = 256; ROWS = 8
perturbed = os.environ.get("MALLOC PERTURB ") == "170"
if not perturbed:
  print("Pillow", Image. version , Image.core. file )

def tiff(jpeg rows, tiled):
  j = io.BytesIO(); Image.new("L", (W, jpeg rows), 200).save(j, "JPEG"); j = j.getvalue()
  tags = [(256,3,1,W), (257,3,1,H), (258,3,1,8), (259,3,1,7), (262,3,1,1), (277,3,1,1)]
  tags += ([(322,3,1,W), (323,3,1,H), (324,4,1,0), (325,4,1,len(j))] if tiled else
       [(273,4,1,0), (278,3,1,H), (279,4,1,len(j))])
  tags.sort(); off = 8 + 2 + 12*len(tags) + 4
  tags = [(t,ty,c, off if t in (273,324) else v) for t,ty,c,v in tags]
  ifd = struct.pack("<H", len(tags)) + b"".join(struct.pack("<HHLL", *t) for t in tags)
  return b"II*0" + struct.pack("<L", 8) + ifd + struct.pack("<L", 0) + j

bug = False
for tiled in (False, True):
  for jpeg rows, label in ((H, "well-formed"), (ROWS, "crafted")):
    data = tiff(jpeg rows, tiled); assert b"LEAK" not in data
    im = Image.open(io.BytesIO(data)); assert im.tile[0][0] == "libtiff"
    junk = [bytearray(b"LEAK" * (W*H//4)) for  in range(6)]; del junk; gc.collect()
    im.load()                  # no exception for either file
    below = im.tobytes()[W*ROWS:]
    n55, nleak = below.count(0x55), below.count(b"LEAK")
    print(f"[{'perturb' if perturbed else 'normal '}] {'tiled' if tiled else 'strip'} "
       f"{label:11s} ({len(data):4d} B) rows {ROWS}-{H-1}: {nleak:5d} x b'LEAK', "
       f"{n55:5d}/{len(below)} == 0x55, head={below[:8]!r}")
    if jpeg rows == ROWS and (nleak > 0 or (perturbed and n55 == len(below))):
      bug = True
if not perturbed:
  r = subprocess.run([sys.executable, sys.argv[0]], env={**os.environ, "MALLOC PERTURB ": "170"},
            capture output=True, text=True)
  lines = r.stdout.rstrip().split("
")
  print("
".join(lines[:-1])); sys.stderr.write(r.stderr)
  bug = bug or lines[-1] == "BUG"
print("BUG" if bug else "OK")

Fix

Use of Uninitialized Resource

Found an issue in the description? Have something to add? Feel free to write us 👾

Weakness Enumeration

Related Identifiers

GHSA-Q45J-58J2-45RG

Affected Products

Pillow