← Back to Pull requests

Fixes for heap overflow #1077

Merged
cdinea merged 12 files (438 lines) on May 04, 2026 18:31 UTC
View on GitHub
cdinea cdinea commented on Apr 27, 2026 04:37 UTC
Author

<!--

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 replace
it 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 triage
and 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!

-->

Diff: 438 lines (+371 / -67) across 12 files Reviewer: grlee77 Review duration: 10914m
All Vouch review confidence checks passed
Residual risk 0.05 = Change Risk (0.56) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.56 · 438 lines diff across 12 files
0.56
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.89 · 5 substantive comment(s)
0.89
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
90%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.364
Real PR Conversation Timeline
19 items
copy-pr-bot[bot] copy-pr-bot[bot] commented on Apr 27, 2026 04:37 UTC
Bot

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

grlee77 grlee77 commented on code on Apr 29, 2026 18:31 UTC
Code Review
cpp/include/cucim/util/checked_math.h:1
@@ -0,0 +1,62 @@
+/*
+ * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+#ifndef CUCIM_UTIL_CHECKED_MATH_H
+#define CUCIM_UTIL_CHECKED_MATH_H
+
+#include <cstddef>
+#include <cstdint>
+#include <stdexcept>
+#include <type_traits>
+
+namespace cucim::util
+{
+
+constexpr size_t kMaxTileBytes = 256ULL * 1024 * 1024; // 256 MiB
+constexpr size_t kMaxRasterBytes = 1ULL * 1024 * 1024 * 1024; // 1 GiB

@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.

grlee77 grlee77 commented on code on Apr 29, 2026 18:37 UTC
Code Review
cpp/plugins/cucim.kit.cuslide/src/cuslide/tiff/ifd.cpp:1
@@ -147,7 +148,8 @@ bool IFD::read(const TIFF* tiff,
     int32_t n_ch = samples_per_pixel_; // number of channels
     int ndim = 3;
 
-    size_t raster_size = w * h * samples_per_pixel_;
+    size_t raster_size = cucim::util::checked_raster_size(

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?

grlee77 grlee77 commented on code on Apr 29, 2026 18:38 UTC
Code Review
cpp/include/cucim/util/checked_math.h:1
@@ -0,0 +1,62 @@
+/*
+ * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+#ifndef CUCIM_UTIL_CHECKED_MATH_H
+#define CUCIM_UTIL_CHECKED_MATH_H
+
+#include <cstddef>
+#include <cstdint>
+#include <stdexcept>
+#include <type_traits>
+
+namespace cucim::util
+{
+
+constexpr size_t kMaxTileBytes = 256ULL * 1024 * 1024; // 256 MiB
+constexpr size_t kMaxRasterBytes = 1ULL * 1024 * 1024 * 1024; // 1 GiB

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?

grlee77 grlee77 commented on Apr 29, 2026 18:41 UTC
Reviewer

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.

cdinea cdinea commented on code on Apr 30, 2026 14:41 UTC
Code Review
cpp/include/cucim/util/checked_math.h:1
@@ -0,0 +1,62 @@
+/*
+ * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+#ifndef CUCIM_UTIL_CHECKED_MATH_H
+#define CUCIM_UTIL_CHECKED_MATH_H
+
+#include <cstddef>
+#include <cstdint>
+#include <stdexcept>
+#include <type_traits>
+
+namespace cucim::util
+{
+
+constexpr size_t kMaxTileBytes = 256ULL * 1024 * 1024; // 256 MiB
+constexpr size_t kMaxRasterBytes = 1ULL * 1024 * 1024 * 1024; // 1 GiB

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

cdinea cdinea commented on code on Apr 30, 2026 14:46 UTC
Code Review
cpp/plugins/cucim.kit.cuslide/src/cuslide/tiff/ifd.cpp:1
@@ -147,7 +148,8 @@ bool IFD::read(const TIFF* tiff,
     int32_t n_ch = samples_per_pixel_; // number of channels
     int ndim = 3;
 
-    size_t raster_size = w * h * samples_per_pixel_;
+    size_t raster_size = cucim::util::checked_raster_size(

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

cdinea cdinea commented on Apr 30, 2026 15:23 UTC
Author

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

cdinea cdinea commented on Apr 30, 2026 15:27 UTC
Author
cdinea cdinea commented on Apr 30, 2026 16:17 UTC
Author

/ok to test fd0aea1

cdinea cdinea commented on Apr 30, 2026 16:48 UTC
Author

/ok to test d4a8128

jakirkham jakirkham commented on May 01, 2026 20:30 UTC
Contributor

/ok to test 5441fdcc55edbb3b46e9e93a75a06945e3d3db54

grlee77 grlee77 approved these changes on May 01, 2026 20:43 UTC
Approved Reviewer

Thanks @cdinea, looks good to me (once CI passes)

gigony gigony approved these changes on May 01, 2026 20:53 UTC
Approved Reviewer

Thanks @cdinea for handling the issues! It looks good to me! Please remove the 'draft' tag from the PR title now.

cdinea cdinea commented on May 01, 2026 20:57 UTC
Author

thank you for the (re)review and approval @grlee77 @gigony

jakirkham jakirkham approved these changes on May 01, 2026 21:00 UTC
Approved Reviewer

Thanks Cristiana for working on this! Also thanks Gigon and Greg for reviewing 🙏

grlee77 grlee77 commented on May 03, 2026 15:59 UTC
Contributor

/ok to test 5441fdc

jakirkham jakirkham commented on May 04, 2026 18:31 UTC
Contributor

/merge

Reviewers
Assignees
cdinea cdinea
Labels
risk: low confidence: 90.6% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 55.5% (MEDIUM) · Higher = larger blast radius
55.5%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 90.6% · Lower = rushed or shallow review
90.6%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 5.2% · Safe to merge
5.2%
Repository