Address CVE-2023-39327, stop excessive loop errors - #1616
Conversation
|
From where does the hardcoded 100000 packet limit comes from ? Couldn't it be reached for large valid images ? |
It was a "big" number that seemed reasonable, but yes in theory it could be reached for very large images.... |
|
I doubt making this configurable would be meaningful to 95% + of users who have no idea of the internals of JPEG2000 and in particular what a packet is. As far as making it bigger, there is perhaps some correlation with other JPEG2000 parameters (image or probably tile size, number of resoutions, number of quality layers), but that should be carefully studied. More globally there are tons of ways where JPEG2000 parameters can be abused to create huge RAM or CPU consumption. Ideally we'd have some global strategy to deal with that, but I can't think of one |
This makes sense to me, hmmm we'll have to take a looks and see what might work. Ill convert this PR back to draft and take a look at what parameters we could measure and how to intake that data in a secure and effective manner. Thanks for taking a look @rouault! |
One way to deal with hugh RAM consumption is to provide API support for a custom allocator. If OpenJPEG had an interface similar to the one below, then opj_malloc(), opj_realloc(), opj_free() etc, could be implemented in terms of it. Naturally, OpenJPEG should provide a default allocator based on malloc(), realloc() and free() in case the program doesn't call opj_set_allocator() at all. The user and p_user pointers are just pointers for the custom allocator; in case it needs a handle to its state or other data structure. This way, if an program wants to restrict memory usage, it can implement its own allocator where malloc(), realloc(), etc. return NULL if a chosen memory threshold is reached. That threshold would be decided by the program, not by the OpenJPEG library itself. It wouldn't require messing with ulimit() and such. And the program can decide to limit memory usage related to J2K decoding but allow more (or less) memory to be dedicated to other matters. You have probably consider this before, but it would still be interesting to hear your thoughts on it in that case. :) |
Attempts to resolve #1472
Prevents excessive looping when consuming packets when errors are hit.
Validated with the steps below
Testing
create bigloop file to validate the issue
Baseline
This establishes that there is an issue and that it breaks when the loop is excessive
Create a docker container from amazon linux 2023
Test script - test_baseline.sh
Dockerfile
Run the container
see that there are many loop iterations and time out is hit
Fixed
Test scipt to validate fix - test_cve_fix.sh
Docker file
Run and build the tests
See that the timeout is not hit