mirror of
https://github.com/opencv/opencv.git
synced 2026-11-29 12:57:05 -06:00
dnn: bound per-channel w_scale to bias length in onnx parseQConv 🤖🤖🤖 - #30395 OpenCV Extra: https://github.com/opencv/opencv_extra/pull/1445 `QLinearConv` import folds a constant int32 bias to float in `parseQConv` (`modules/dnn/src/onnx/onnx_importer2.cpp`): each bias element is multiplied by its per-channel weight scale and written into `bias_float`, which is sized from the bias tensor. The fold loop runs over the length of `w_scale`, but `w_scale` and the bias are independent inputs of the model, so a 1-D `w_scale` longer than the bias writes past `bias_float`. It is reached from `readNetFromONNX` on any `.onnx` carrying such a `QLinearConv`, and the scalar `x_scale` read is likewise unguarded against an empty tensor. ASan on the default engine loading a model whose bias has one element and whose per-channel `w_scale` has 64: ``` ==ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 4 #0 ONNXImporter2::parseQConv(...) #1 ONNXImporter2::parseNode(...) #2 ONNXImporter2::parseGraph(...) #5 readNetFromONNX2(...) #6 readNetFromONNX(...) 0x... is located 0 bytes after 4-byte region allocated by Mat::convertTo in parseQConv ``` Require the per-channel `w_scale` to hold one entry per bias element before the loop, the per-axis quantization invariant that pairs one scale and one bias with each output channel, and check the scalar `x_scale` is non-empty before the read. A well-formed model is unaffected; a mismatched one now throws from the importer and the net loads empty instead of overrunning the buffer. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [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 a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake