-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decompression bomb protection #515
Comments
I suspect that a nearly empty image that only included size metadata and enough plausible data to start the decoder would trigger a DOS. Essentially, Image.new allocates the storage before passing it off to the decoder to fill. And that's solely based on the size and the number of bands. There's now protection in the BMP decoder against that, but the limit is set rather high, 2GP or something like that. I can see adding a limit to the allocator that would raise an exception if a memory limit was exceeded. I'm not 100% sure that I want to enable it by default -- I suspect that the scientific imaging community would be the ones that would bump up against it in the course of ordinary work. |
Big jpegs aren't even a problem if you are using thumbnail(). But we did test our backend with image like Carina Nebula http://hubblesite.org/gallery/album/entire/pr2007016a/warn/ edit: another thing to note is that
Should solve this bomb problems just fine. |
Interesting. The issue still exists for other operations though right? And for non-JPEG plugins, unless the author has specifically taken steps to avoid it? How comes thumbnail works but resize seems to load the whole image in memory? Tricks in the JPEG image plugin?
Yes I didn't specifically note it, as the doc states sufficiently clearly that
It "does", if you know about the issue in the first place. Hence my suggestion to add a warning on the subject in Pillow's documentation, similar to the standard library warning about entity-based attacks on XML parsers. I think that'd be a baseline solution, though I also think protecting users by default (by adding an overridable check directly in |
JPEG thumbnails are a special case because of the way that JPEG compression works. It's a spatial frequency approach, rather than a raster line compression. If you cut off the frequency low enough, you don't have to read most of the data. |
Where is the PR for this issue, assuming one exists? |
Anyone have any interest in working on this for 2.5.0? |
For starters, how about something along the lines of this?
If the image is larger than this, throw an This test code:
Outputs:
|
See #674. |
This would break existing code. I'm all in for an optional parameter, but we can't just throw a new Exception now. Nokia lumia pictures are already bigger than 36M. |
That would be no improvement over the current situation. |
It would be, you could block giant images if you want to. As far as I understand these, we are talking about simple two colored png images. Which will take a lot of memory if you load them, but that isn't really a bug and the arbitary low limit of 36M would break existing code for no urgent reason. IMHO API changes as intrusive as this should only happen for a very good reason. |
My thought process, which is in short bits at the moment.
So, I can see a couple of options.
|
Which you can already do "by hand" if you know and remember the problem exists. Having to opt-in via an optional parameter isn't an improvement over having to opt-in via an assert or a one-line conditional.
A bug's in the eye of the beholder, decompression bombs allow DOSing services using PIL unless the developer was aware of the problem (which is not documented anywhere near
An issue here being the ability to disable the warning without breaking previous pillow versions. |
Warnings can be ignored or turned into exceptions at the interpreter level, so they're safe to add from the POV of breaking existing code. |
True. |
Thanks for all the comments. So how about this?
|
if so, see updated #674. Warning text and limit need checking. This test code:
Outputs:
|
For cell phones, the biggest resolutions are Nokia's Lumia 808 and 1020 with 41.3MP sensors and 38.2MP maximum resolution (in 4:3 aspect ratio, 33.6MP in 16:9) (note: the default resolution for the 1020 is 5MP). For SLR, looking at dpreview.com they're in the 24~36Mp range, except for a few medium-format $10k and more 50MP cameras (the Phase One IQ2 at ~$40k, the Hasselblad H5D-50c at ~$30k and the Pentax 645Z announced at $8500) |
The default for the Nokia 808 (which isn't a Lumia) is also 5 MP. |
I'd lean toward setting the default warning level rather high, perhaps on the level of 1/4-1/2 gig of memory. It's big enough that anyone intentionally using a larger image would likely know that they're doing something that might require reading the documentation, and it's small enough that it's not going to exhaust most servers (in the event that you get only one of them, anyway). Alternately, one could assume that it's a big enough image to be annoying and it's small enough to process in my default dev vms. |
PR #674 updated with tests, further feedback welcome. TODO:
|
@masklinn @wiredfool @d-schmidt @xmo-odoo Any comments on the current state of PR #674? |
@masklinn @wiredfool @d-schmidt @xmo-odoo @hugovk FYI deadline approaching 🐎 |
@aclark4life commented on #674, that seemed smarter. |
#674 has now been merged into master. Documentation was suggested earlier. How about something like this? Feel free to completely rewrite it.
And where should this go in the docs? |
A warning box in the |
Can we close this? |
@aclark4life Let's keep it open until the docs are updated. I'll make a PR. |
@aclark4life Please can you check the text is ok and the markup is correct in #738? |
OK, docs updated. Let's fix them up as needed later. I'll close this now. Thanks for all the input! |
**Bug fixes:** * The `resize_data.py`, `find_bad.py`, & `vis.py` scripts now support gigapixel images. Hopefully the Pillow/PIL `DecompressionBombWarning` warning no longer shows up. python-pillow/Pillow#515, https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.open You can manually change `Image.MAX_IMAGE_PIXELS = 1000000000` to `Image.MAX_IMAGE_PIXELS = None` if you're still running into issues. * The `vis.py` script now checks that you specified the required number of image dimensions. **Improvements:** * General README improvements for color correlation matrices & color decorrelation.
"zip bombs" are a somewhat know threat, but it also applies to images and can't be protected against by checking the filesystem size of the data:
This means it's possible to DOS e.g. a web application performing image resizing by sending one of these bombs. As far as I can tell the protection possibilities are limited:
Image.open
could be augmented with e.g. amaximum_pixels
parameter raising an error in caseimage.h * image.w
goes above the specified limit to make this easier for usersThe text was updated successfully, but these errors were encountered: