From aa13c74665a61bcabcf670714e53b77d9b963c0c Mon Sep 17 00:00:00 2001 From: Jeffrey 'Alex' Clark Date: Fri, 18 Sep 2026 14:42:40 -0400 Subject: [PATCH 1/2] Validate FPX tile descriptor sizing before expansion Reject tile descriptor lengths below 16 bytes, cap descriptor reads to the tile grid, and apply the decompression-bomb size check before subimage descriptors are used. Add regression coverage for invalid descriptor lengths and oversized declared dimensions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Tests/test_file_fpx.py | 17 +++++++++++++++++ src/PIL/FpxImagePlugin.py | 13 ++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Tests/test_file_fpx.py b/Tests/test_file_fpx.py index 1887fce1be5..548842d8987 100644 --- a/Tests/test_file_fpx.py +++ b/Tests/test_file_fpx.py @@ -53,6 +53,23 @@ def test_invalid_tile_size() -> None: Image.open(BytesIO(data)) +def test_invalid_tile_descriptor_length() -> None: + with open("Tests/images/input_bw_one_band.fpx", "rb") as f: + data = f.read() + data = data[:4220] + b"\x01\x00\x00\x00" + data[4224:] + with pytest.raises(ValueError, match="Tile descriptor must be at least 16 bytes"): + Image.open(BytesIO(data)) + + +def test_decompression_bomb() -> None: + with open("Tests/images/input_bw_one_band.fpx", "rb") as f: + data = f.read() + # Set the width to 2**30 in the Image Contents property set. + data = data[:3220] + b"\x00\x00\x00\x40" + data[3224:] + with pytest.raises(Image.DecompressionBombError): + Image.open(BytesIO(data)) + + def test_invalid_number_of_bands() -> None: with pytest.raises(OSError, match="Invalid number of bands"): with Image.open("Tests/images/input_bw_five_bands.fpx"): diff --git a/src/PIL/FpxImagePlugin.py b/src/PIL/FpxImagePlugin.py index 6da46c17bee..9344b90d750 100644 --- a/src/PIL/FpxImagePlugin.py +++ b/src/PIL/FpxImagePlugin.py @@ -87,6 +87,8 @@ def _open_index(self, index: int = 1) -> None: assert isinstance(prop[0x1000002], int) assert isinstance(prop[0x1000003], int) self._size = prop[0x1000002], prop[0x1000003] + # The size bounds the number of tile descriptors read in _open_subimage. + Image._decompression_bomb_check(self._size) size = max(self.size) i = 1 @@ -142,7 +144,7 @@ def _open_subimage(self, index: int = 1, subimage: int = 0) -> None: s = fp.read(36) size = i32(s, 4), i32(s, 8) - # tilecount = i32(s, 12) + tilecount = i32(s, 12) xtile, ytile = i32(s, 16), i32(s, 20) if xtile != 64 or ytile != 64: msg = "Tile must be 64 pixels by 64 pixels" @@ -151,14 +153,19 @@ def _open_subimage(self, index: int = 1, subimage: int = 0) -> None: # channels = i32(s, 24) offset = i32(s, 28) length = i32(s, 32) + if length < 16: + # offset, size, compression type and compression subtype + msg = "Tile descriptor must be at least 16 bytes" + raise ValueError(msg) if size != self.size: msg = "subimage mismatch" raise OSError(msg) - # get tile descriptors + # get tile descriptors, at most one per tile + xtiles, ytiles = (size[0] + xtile - 1) // xtile, (size[1] + ytile - 1) // ytile fp.seek(28 + offset) - s = fp.read(i32(s, 12) * length) + s = fp.read(min(tilecount, xtiles * ytiles) * length) x = y = 0 xsize, ysize = size From f01dd69972cbf584b26bdcb089b360b7cdadf571 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 23 Nov 2026 17:27:20 +1100 Subject: [PATCH 2/2] Update error message --- Tests/test_file_fpx.py | 28 ++++++++++++++++------------ src/PIL/FpxImagePlugin.py | 11 ++++++----- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/Tests/test_file_fpx.py b/Tests/test_file_fpx.py index 548842d8987..4f5cdd484d3 100644 --- a/Tests/test_file_fpx.py +++ b/Tests/test_file_fpx.py @@ -47,27 +47,31 @@ def test_invalid_file() -> None: def test_invalid_tile_size() -> None: with open("Tests/images/input_bw_one_band.fpx", "rb") as f: - data = f.read() - data = data[:4204] + b"\x00" * 8 + data[4212:] + data = bytearray(f.read()) + data[4204:4212] = b"\x00" * 8 + b = BytesIO(data) with pytest.raises(ValueError, match="Tile must be 64 pixels by 64 pixels"): - Image.open(BytesIO(data)) + Image.open(b) -def test_invalid_tile_descriptor_length() -> None: +def test_invalid_tile_header_entry_length() -> None: with open("Tests/images/input_bw_one_band.fpx", "rb") as f: - data = f.read() - data = data[:4220] + b"\x01\x00\x00\x00" + data[4224:] - with pytest.raises(ValueError, match="Tile descriptor must be at least 16 bytes"): - Image.open(BytesIO(data)) + data = bytearray(f.read()) + data[4220:4224] = b"\x01\x00\x00\x00" + b = BytesIO(data) + with pytest.raises( + ValueError, match="Tile header entry length must be at least 16" + ): + Image.open(b) def test_decompression_bomb() -> None: with open("Tests/images/input_bw_one_band.fpx", "rb") as f: - data = f.read() - # Set the width to 2**30 in the Image Contents property set. - data = data[:3220] + b"\x00\x00\x00\x40" + data[3224:] + data = bytearray(f.read()) + data[3220:3224] = b"\x00\x00\x00\x40" + b = BytesIO(data) with pytest.raises(Image.DecompressionBombError): - Image.open(BytesIO(data)) + Image.open(b) def test_invalid_number_of_bands() -> None: diff --git a/src/PIL/FpxImagePlugin.py b/src/PIL/FpxImagePlugin.py index 9344b90d750..43ca8e25ed2 100644 --- a/src/PIL/FpxImagePlugin.py +++ b/src/PIL/FpxImagePlugin.py @@ -87,7 +87,7 @@ def _open_index(self, index: int = 1) -> None: assert isinstance(prop[0x1000002], int) assert isinstance(prop[0x1000003], int) self._size = prop[0x1000002], prop[0x1000003] - # The size bounds the number of tile descriptors read in _open_subimage. + # The size bounds the number of tile descriptors read in _open_subimage() Image._decompression_bomb_check(self._size) size = max(self.size) @@ -154,17 +154,18 @@ def _open_subimage(self, index: int = 1, subimage: int = 0) -> None: offset = i32(s, 28) length = i32(s, 32) if length < 16: - # offset, size, compression type and compression subtype - msg = "Tile descriptor must be at least 16 bytes" + # 16 bytes are required + # for the tile offset, tile size, compression type and compression subtype + msg = "Tile header entry length must be at least 16" raise ValueError(msg) if size != self.size: msg = "subimage mismatch" raise OSError(msg) - # get tile descriptors, at most one per tile - xtiles, ytiles = (size[0] + xtile - 1) // xtile, (size[1] + ytile - 1) // ytile + # get tile header entries fp.seek(28 + offset) + xtiles, ytiles = (size[0] + xtile - 1) // xtile, (size[1] + ytile - 1) // ytile s = fp.read(min(tilecount, xtiles * ytiles) * length) x = y = 0