Fixes for heap overflow #1077
@gigony , @cdinea, @jakirkham : I don't think we should just hard-code these limits here.
This kMaxRasterBytes is much smaller than many real-world microscopy images (and the memory limits of typical workstations), so it would likely prevent valid use cases?
I am not sure if the 256 MB tile size limit is also problematic in practice, but may not want to also hard-code an arbitrary value.
The width and height here come from user-supplied values not potentially malicious file metadata so I don't think we need to check and reject here?
One option could be to have a bit larger default like 16 GB for kMaxRasterBytes but allow user-override via environment variable like CUCIM_MAX_RASTER_BYTES?
I commented on an issue with hard-coded size limits that may be too restrictive in practice.
I also had a local agent review and it pointed out one remaining related issue:
High: J2K validation still does not validate component dimensions used by the SYCC converters.
cpp/plugins/cucim.kit.cuslide/src/cuslide/jpeg2k/libopenjpeg.cpp:231 validates output bytes from component 0 only. But cpp/plugins/
cucim.kit.cuslide/src/cuslide/jpeg2k/color_conversion.cpp:36 and cpp/plugins/cucim.kit.cuslide/src/cuslide/jpeg2k/color_conversion.cpp:122
read Cb/Cr based on component 0 geometry and subsampling assumptions. A malformed codestream with non-null but undersized chroma components
can still drive out-of-bounds reads. Add per-component dimension checks for RGB, 4:4:4, 4:2:2, and 4:2:0 before conversion.
thank you @grlee77 - i pushed the changes in this commit to raise defaults to 2 GiB (tiles) and 16 GiB (rasters) - I think these should accommodate even large microscopy tiles and full-resolution region reads on typical workstations?
Also added environment variable overrides: CUCIM_MAX_TILE_BYTES and CUCIM_MAX_RASTER_BYTES so users with unusual image sizes or memory-constrained environments can adjust without recompiling
thank you for the feedback @grlee77 - updated this in this commit to use checked_mul3 (overflow detection only) instead of checked_raster_size (overflow + upper-bound limit),this way we still catch silent integer wraparound
I commented on an issue with hard-coded size limits that may be too restrictive in practice.
> I also had a local agent review and it pointed out one remaining related issue:
> > High: J2K validation still does not validate component dimensions used by the SYCC converters.
> cpp/plugins/cucim.kit.cuslide/src/cuslide/jpeg2k/libopenjpeg.cpp:231 validates output bytes from component 0 only. But cpp/plugins/
> cucim.kit.cuslide/src/cuslide/jpeg2k/color_conversion.cpp:36 and cpp/plugins/cucim.kit.cuslide/src/cuslide/jpeg2k/color_conversion.cpp:122
> read Cb/Cr based on component 0 geometry and subsampling assumptions. A malformed codestream with non-null but undersized chroma components
> can still drive out-of-bounds reads. Add per-component dimension checks for RGB, 4:4:4, 4:2:2, and 4:2:0 before conversion.
thank you for the feedback @grlee77 - addressed the agent review in the latest commit . Added per-component dimension validation in libopenjpeg.cpp that computes the minimum expected chroma size as ceil(luma_dim / subsampling_factor) using each component's dx/dy values, and rejects the image if any chroma plane is undersized
/ok to test fd0aea1
/ok to test d4a8128
/ok to test 5441fdcc55edbb3b46e9e93a75a06945e3d3db54
Thanks @cdinea, looks good to me (once CI passes)
Thanks @cdinea for handling the issues! It looks good to me! Please remove the 'draft' tag from the PR title now.
thank you for the (re)review and approval @grlee77 @gigony
Thanks Cristiana for working on this! Also thanks Gigon and Greg for reviewing 🙏
/ok to test 5441fdc
/merge
grlee77
cdinea
<!--
Thank you for contributing to cuCIM :)
Here are some guidelines to help the review process go smoothly.
1. Please write a description in this text box of the changes that are being
made.
2. Please ensure that you have written units tests for the changes made/features
added.
3. If you are closing an issue please use one of the automatic closing words as
noted here: https://help.github.com/articles/closing-issues-using-keywords/
4. If your pull request is not ready for review but you want to make use of the
continuous integration testing facilities please label it with
[WIP].5. If your pull request is ready to be reviewed without requiring additional
work on top of it, then remove the
[WIP]label (if present) and replaceit with
[REVIEW]. If assistance is required to complete the functionality,for example when the C/C++ code of a feature is complete but Python bindings
are still required, then add the label
[HELP-REQ]so that others can triageand assist. The additional changes then can be implemented on top of the
same PR. If the assistance is done by members of the rapidsAI team, then no
additional actions are required by the creator of the original PR for this,
otherwise the original author of the PR needs to give permission to the
person(s) assisting to commit to their personal fork of the project. If that
doesn't happen then a new PR based on the code of the original PR can be
opened by the person assisting, which then will be the PR that will be
merged.
6. Once all work has been done and review has taken place please do not add
features or make changes out of the scope of those requested by the reviewer
(doing this just add delays as already reviewed code ends up having to be
re-reviewed/it is hard to tell what is new etc!). Further, please do not
rebase your branch on main/force push/rewrite history, doing any of these
causes the context of any comments made by reviewers to be lost. If
conflicts occur against main they should be resolved by merging main
into the branch used for making the pull request.
Many thanks in advance for your cooperation!
-->