Skip to content

Commit 756fff3

Browse files
wiredfoolradarhere
authored andcommitted
Fix Memory DOS in Icns, Ico and Blp Image Plugins
Some container plugins that could contain images of other formats, such as the ICNS format, did not properly check the reported size of the contained image. These images could cause arbitrariliy large memory allocations. This is fixed for all locations where individual *ImageFile classes are created without going through the usual Image.open method.
1 parent 886ad5a commit 756fff3

File tree

5 files changed

+10
-0
lines changed

5 files changed

+10
-0
lines changed
Binary file not shown.

Tests/test_file_icns.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,9 @@ def test_not_an_icns_file():
139139
with io.BytesIO(b"invalid\n") as fp:
140140
with pytest.raises(SyntaxError):
141141
IcnsImagePlugin.IcnsFile(fp)
142+
143+
144+
def test_icns_decompression_bomb():
145+
with pytest.raises(Image.DecompressionBombError):
146+
im = Image.open('Tests/images/oom-8ed3316a4109213ca96fb8a256a0bfefdece1461.icns')
147+
im.load()

src/PIL/BlpImagePlugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ def _decode_jpeg_stream(self):
353353
data = jpeg_header + data
354354
data = BytesIO(data)
355355
image = JpegImageFile(data)
356+
Image._decompression_bomb_check(image.size)
356357
self.tile = image.tile # :/
357358
self.fd = image.fp
358359
self.mode = image.mode

src/PIL/IcnsImagePlugin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def read_png_or_jpeg2000(fobj, start_length, size):
105105
if sig[:8] == b"\x89PNG\x0d\x0a\x1a\x0a":
106106
fobj.seek(start)
107107
im = PngImagePlugin.PngImageFile(fobj)
108+
Image._decompression_bomb_check(im.size)
108109
return {"RGBA": im}
109110
elif (
110111
sig[:4] == b"\xff\x4f\xff\x51"
@@ -121,6 +122,7 @@ def read_png_or_jpeg2000(fobj, start_length, size):
121122
jp2kstream = fobj.read(length)
122123
f = io.BytesIO(jp2kstream)
123124
im = Jpeg2KImagePlugin.Jpeg2KImageFile(f)
125+
Image._decompression_bomb_check(im.size)
124126
if im.mode != "RGBA":
125127
im = im.convert("RGBA")
126128
return {"RGBA": im}

src/PIL/IcoImagePlugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def frame(self, idx):
178178
if data[:8] == PngImagePlugin._MAGIC:
179179
# png frame
180180
im = PngImagePlugin.PngImageFile(self.buf)
181+
Image._decompression_bomb_check(im.size)
181182
else:
182183
# XOR + AND mask bmp frame
183184
im = BmpImagePlugin.DibImageFile(self.buf)

0 commit comments

Comments
 (0)