From fe6b3b0f02153601687d02bb5586f955fef1cb2f Mon Sep 17 00:00:00 2001 From: Jeffrey 'Alex' Clark Date: Fri, 20 Nov 2026 15:57:29 -0500 Subject: [PATCH 1/2] Limit TIFF RGBA decoding buffer to image height When decoding through TIFFRGBAImage, the buffer was sized from RowsPerStrip or TileLength, even if that exceeded the image height. Co-Authored-By: Claude Opus 5.5 --- Tests/test_file_libtiff.py | 15 +++++++++++++++ src/libImaging/TiffDecode.c | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 8ec89965929..013407ae101 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -1482,6 +1482,21 @@ def test_realloc_overflow(self, monkeypatch: pytest.MonkeyPatch) -> None: with pytest.raises(OSError, match="decoder error -9"): im.load() + def test_rgba_rows_per_strip_greater_than_height(self) -> None: + # 1000x1 Deflate YCbCr strip with RowsPerStrip of 1,000,000 + data = ( + b"SUkqAAgAAAALAAABBAABAAAA6AMAAAEBBAABAAAAAQAAAAIBAwADAAAAkgAA" + b"AAMBAwABAAAACAAAAAYBAwABAAAABgAAABEBBAABAAAAmAAAABUBAwABAAAA" + b"AwAAABYBBAABAAAAQEIPABcBBAABAAAAGgAAABwBAwABAAAAAQAAABICAwAC" + b"AAAAAQABAAAAAAAIAAgACAB42u3BMQEAAADCoOqLbg0PoAAAgHcDDP7cTA==" + ) + with Image.open(io.BytesIO(base64.b64decode(data))) as im: + assert im.tag_v2[TiffImagePlugin.ROWSPERSTRIP] == 1_000_000 + + # The decoding buffer is limited to the image height + im.load() + assert im.getpixel((0, 0)) == (128, 128, 128) + def test_unpacker_row_wider_than_libtiff_row(self) -> None: for data in ( # 16x16 8-bit greyscale tile, with a PhotometricInterpretation tag that diff --git a/src/libImaging/TiffDecode.c b/src/libImaging/TiffDecode.c index 137ea0052e4..ba9fe10685c 100644 --- a/src/libImaging/TiffDecode.c +++ b/src/libImaging/TiffDecode.c @@ -248,7 +248,8 @@ _decodeAsRGBA(Imaging im, ImagingCodecState state, TIFF *tiff) { ret = TIFFGetFieldDefaulted(tiff, TIFFTAG_ROWSPERSTRIP, &rows_per_block); } - if (ret != 1 || rows_per_block == (UINT32)(-1)) { + if (ret != 1 || rows_per_block > (UINT32)state->ysize) { + // No more than the image height is needed for the buffer rows_per_block = state->ysize; } From 146c2167176788540d665dd4dd1a0a1d6bad9614 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 21 Nov 2026 11:25:51 +1100 Subject: [PATCH 2/2] Lint fix --- Tests/test_file_libtiff.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 013407ae101..a99134431d1 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -1491,6 +1491,7 @@ def test_rgba_rows_per_strip_greater_than_height(self) -> None: b"AAAAAQABAAAAAAAIAAgACAB42u3BMQEAAADCoOqLbg0PoAAAgHcDDP7cTA==" ) with Image.open(io.BytesIO(base64.b64decode(data))) as im: + assert isinstance(im, TiffImagePlugin.TiffImageFile) assert im.tag_v2[TiffImagePlugin.ROWSPERSTRIP] == 1_000_000 # The decoding buffer is limited to the image height