Repository navigation
video: fix out-of-bounds read in DISOpticalFlow with patch_size larger than the border #20185 🤖🤖🤖 - #29600
Closed
uditjainstjis wants to merge 1 commit into
Closed
video: fix out-of-bounds read in DISOpticalFlow with patch_size larger than the border #20185 🤖🤖🤖#29600uditjainstjis wants to merge 1 commit into
uditjainstjis wants to merge 1 commit into
Conversation
DISOpticalFlowImpl pads I1 of every pyramid level with a fixed 16 pixel border
(border_size), while PatchInverseSearch clamps the position of a patch to
i_lower_limit = border_size - patch_size + 1
i_upper_limit = border_size + h - 1
so a patch is allowed to hang up to patch_size - 1 pixels outside of the image.
computeSSD()/computeSSDMeanNorm() then read patch_size + 1 rows and columns
starting at that position, which stays inside the padded buffer only while
patch_size <= border_size. With a bigger patch, e.g. the patch_size = 25 of the
report, the clamped position itself is outside of the buffer (the lower limit
becomes -8) and the SSD loop reads out of bounds:
ERROR: AddressSanitizer: heap-buffer-overflow READ of size 1
#0 cv::computeSSDMeanNorm(...) dis_flow.cpp:755
opencv#1 cv::DISOpticalFlowImpl::PatchInverseSearch_ParBody::operator()(...)
dis_flow.cpp:894
opencv#5 cv::DISOpticalFlowImpl::calc(...) dis_flow.cpp:1501
0 bytes after 29184-byte region <- I1s_ext of the level, 152x192
which is the same overflow that was reported for this issue.
Pad by max(16, patch_size) instead, so that both search limits stay inside
I1s_ext for any patch size: the lowest position becomes 1 and the highest read
row becomes border_size + h - 1 + patch_size = h + 2*border_size - 1, the last
row of the buffer. The default patch sizes (8 and 12) are below 16, so the
padding, the memory consumption and the results of the default configurations
are unchanged.
Added a regression test that pushes the search against those limits with a
patch of 25 and 64 pixels; without the fix it triggers the overflow above
(verified with AddressSanitizer).
Fixes opencv#20185
uditjainstjis
force-pushed
the
fix-20185-dis-border-size
branch
from
July 25, 2026 09:09
634d3f0 to
f43237e
Compare
Contributor
|
The patch is not efficient: |
5 tasks done
asmorkalov
pushed a commit
that referenced
this pull request
Sep 21, 2026
video: fix DISOpticalFlow heap-buffer-overflow with patch_size > border_size - #29715 Fixes #20185. ### What `DISOpticalFlowImpl` pads `I1` of every pyramid level with a **fixed 16 pixel border** (`border_size`), while `PatchInverseSearch` clamps a patch's sample position to: ```cpp float i_lower_limit = bsz - psz + 1.0f; // bsz = border_size, psz = patch_size float i_upper_limit = bsz + dis->h - 1.0f; ``` i.e. a patch may be placed up to `patch_size - 1` pixels outside of the image. `computeSSD()`/`computeSSDMeanNorm()` then read `patch_size` (+1 for bilinear interpolation) rows/columns starting at that clamped position, which stays inside the padded buffer only while `patch_size <= border_size`. `patch_size` is user-settable via `setPatchSize()` with no upper bound relative to the hardcoded `border_size` -- so `setPatchSize()` past 16, or a temporal-candidate flow large enough to push the clamped search against its limit, reads past the end of `I1s_ext` (confirmed via AddressSanitizer: heap-buffer-overflow in `computeSSDMeanNorm`). ### Fix Size the border to whichever is larger, exactly once, at the top of `prepareBuffers()` (where every `I1s_ext[i]` already gets freshly created/padded on every `calc()` call, so this doesn't need any additional cache-invalidation logic): ```cpp border_size = max(16, patch_size); ``` The needed inequality (`border_size >= patch_size`) is independent of image size, so this is sufficient at every pyramid level uniformly. `ocl_prepareBuffers()` does not need the equivalent change: `calc()`'s `CV_OCL_RUN` gate only takes the OpenCL path when `patch_size == 8` exactly, which is always within the hardcoded border, so that path can never reach this overflow. ### On the prior attempt A prior attempt at this exact fix (#29600) was closed by @asmorkalov, who ran its own added regression test and got the same ASan heap-buffer-overflow again ("the patch is not efficient"). I want to be upfront about this rather than just re-submitting the same-looking diff: **I could not reproduce that failure.** I reproduced the original overflow on unfixed 5.x (same crash site / allocator stack as both the original issue report and #29600's own ASan trace), then applied the same one-line fix and ran #29600's own added test 20x plus a new 40-trial randomized stress test (`patch_size` 9-48 -- spanning above and below `border_size=16` -- varying image size, `patch_stride`, flow magnitude up to 120px, and a same-instance second `calc()` call to exercise buffer re-use across a patch-size change) under AddressSanitizer, with zero failures. I don't have a confirmed explanation for the discrepancy -- possibly a stale incremental build on the original attempt's end, since I hit and had to work around exactly that kind of false "no work to do" ninja build-cache issue myself while setting up this reproduction. Flagging this openly rather than asserting certainty either way -- happy to dig further if CI or review surfaces a case this doesn't cover. ### Testing Verified locally with a dedicated ASan-instrumented Debug build (`-fsanitize=address`, separate from the Release build used to verify the rest of today's changes), specifically because an out-of-bounds read like this often just silently returns garbage in a Release build instead of crashing: - Confirmed the crash reproduces on unfixed 5.x, and is gone with the fix, under both `regression_20185_patch_larger_than_border` (from #29600, included here) and a new `regression_20185_stress` test (40 randomized trials) -- both passing cleanly under ASan. - Re-verified both tests pass in the standard Release configuration too. - Full `opencv_test_video` suite (both ASan and Release): no failures attributable to this change; everything else failing needs `opencv_extra` test data (tracking/ECC/optical-flow reference videos/images) not configured in this scoped build. ### PR checklist - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on code under GPL or another incompatible license. - [x] The PR is proposed to the proper branch (5.x). - [x] Accuracy tests included (see above). - [x] No public API/behavior change, so no documentation or sample updates needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #20185.
Problem
DISOpticalFlowImplpadsI1of every pyramid level with a fixed 16 pixel border (border_size), whilePatchInverseSearchclamps the position of a patch toi.e. a patch may hang up to
patch_size - 1pixels outside of the image.computeSSD()/computeSSDMeanNorm()then readpatch_size + 1rows and columns starting at that position, which stays inside the padded buffer only whilepatch_size <= border_size.With a larger patch — the reporter used
setPatchSize(25)— the clamped position itself is outside of the buffer (the lower limit becomes16 - 25 + 1 = -8, and the upper limit lets the last read row reachh + 2*bsz + 8), so the SSD loop reads out of bounds. This is the overflow @asmorkalov reported in the issue; reproduced locally on 4.x:The limits exist precisely to bound arbitrary flow values, so this is hit whenever the flow (estimated or passed in as the flow of the previous frame pair) pushes a patch more than
border_sizeoutside of the image — which is why the report depends on the image pair, the direction and the patch size.Fix
Pad by
max(16, patch_size). Both limits then stay insideI1s_extfor any patch size: the lowest position becomes1and the highest read row becomesborder_size + h - 1 + patch_size = h + 2*border_size - 1, i.e. the last row of the buffer.border_sizeis set inprepareBuffers()/ocl_prepareBuffers(), which run on everycalc()afterautoSelectPatchSizeAndScales(), so asetPatchSize()between twocalc()calls is picked up and the padded buffers are re-created. The default patch sizes (8 and 12) are below 16, so the padding, the memory consumption and the results of the default configurations are unchanged.Testing
Added
DenseOpticalFlow_DIS.regression_20185_patch_larger_than_border, which drives the patch search against the limits: onecalc()with the preset patch size (8, fits the border) and one withsetPatchSize(25)after it, so the re-creation of the padded buffers is covered too.videotests still pass againstopencv_extratest data, also under ASan (DenseOpticalFlow_DIS.*accuracy andMultithreadReproducibilityincluded; the unrelatedVideo_OpticalFlowPyrLK.submatfails on the unmodified 4.x here as well).Pull Request Readiness Checklist
4.x)