Fix integer overflow in RKNPU implicit bias allocation#29249
Open
GopalakrishnanN wants to merge 6 commits into
Open
Fix integer overflow in RKNPU implicit bias allocation#29249GopalakrishnanN wants to merge 6 commits into
GopalakrishnanN wants to merge 6 commits into
Conversation
The RKNPU converter built implicit (all-zero) bias buffers with malloc(sizeof(T) * dim), where dim is an attacker-controlled uint32_t taken from a model weight's shape, and never checked the result before memset. On 32-bit RKNPU targets (size_t is 32-bit) a crafted dim such as 0x40000400 makes sizeof(float) * dim wrap to 4096 bytes while the tensor still advertises dim elements, yielding a heap buffer overflow when the bias is consumed. On 64-bit hosts a large dim instead produces a failing malloc whose NULL return was dereferenced by the following memset. Route all four bias sites (Conv, QLinearConv, DepthwiseConv, FC) through a single AllocZeroedBias helper that computes the byte count with SafeInt<size_t> (throws on overflow) and null-checks the allocation before zeroing.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the RKNPU execution provider’s ONNX-to-RKNN converter against attacker-controlled shape dimensions by preventing sizeof(T) * dim wraparound and by avoiding memset on a null allocation when creating implicit (all-zero) bias buffers.
Changes:
- Add a shared
AllocZeroedBiashelper that computes allocation sizes withSafeInt<size_t>and checksmallocresults before zeroing. - Route implicit-bias allocations in Conv/QLinearConv/DepthwiseConv/FC layer builders through the helper.
- Include ORT’s
safeint.hto use the standard overflow-checked arithmetic idiom.
added 2 commits
June 24, 2026 17:04
Address review feedback: AllocZeroedBias now reports element_size, count, and the computed byte count in both the SafeInt-overflow and malloc-failure error paths, so an oversized/malicious model or OOM is easier to diagnose.
Reject negative and out-of-range ONNX dimensions before converting them to the uint32_t shape representation used by the RKNPU converter and DDK. This prevents malicious int64 ONNX dimensions from silently truncating at model ingestion.
xadupre
reviewed
Jun 25, 2026
added 2 commits
June 25, 2026 14:23
Address review feedback by replacing the explicit invalid_argument throw in ToRknpuDim with ORT_ENFORCE and including core/common/common.h directly.
Keep the allocation overflow/OOM diagnostic context while replacing direct std::runtime_error throws in AllocZeroedBias with ORT_THROW and ORT_ENFORCE.
Address review feedback by replacing the raw try/catch in AllocZeroedBias with ORT_TRY/ORT_CATCH so the code remains portable to ORT_NO_EXCEPTIONS builds.
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.
Description
The RKNPU execution provider's ONNX converter allocates implicit (all-zero)
bias buffers with
malloc(sizeof(T) * dim)when aConv/Gemm/QLinearConvnode omits its bias input.dimcomes from a model weight'sshape (an attacker-controlled
uint32_tafter RKNPU shape conversion), and theallocation result was never checked before
memset.This PR hardens the converter in two places:
int64_tdimensions before narrowing them to the RKNPUuint32_tshape representation, rejecting negative or out-of-range values;AddLayerConvImpl,AddLayerQLinearConvImpl,AddLayerDepthwiseConvImpl,AddLayerFC) througha shared
AllocZeroedBiashelper that computes byte counts withSafeInt<size_t>and null-checksmallocbefore zeroing.Both overflow/OOM error paths include
element_size,count, and byte-countcontext where available.
Motivation and Context
A malicious ONNX model can provide dimensions that are not safe for the RKNPU
converter's 32-bit shape representation or for byte-size allocation arithmetic:
int64_t, while the RKNPU converter/DDK usesuint32_tshape values. Silently narrowing a large or negativeint64_tvalue can produce a misleading
uint32_tdimension.uint32_t,sizeof(T) * dimcanoverflow
size_ton 32-bit RKNPU targets. For example,dim = 0x40000400makes
sizeof(float) * dimwrap to 4096 bytes while the created tensorstill advertises
dimelements, causing a heap buffer overflow when the biasis consumed by the driver.
mallocwhose
NULLresult was previously dereferenced bymemset.SafeInt<size_t>is the ORT-standard idiom for memory-size arithmetic, and thenew dimension helper rejects invalid ONNX dimensions before they enter the
RKNPU
uint32_tshape model.Validation: the RKNPU EP requires the Rockchip DDK (
RKNPU_DDK_PATH) and anARM target, so it does not build on a typical x64 dev box and has no GPU-free CI
leg. The change was validated with
clang-format,git diff --check, and astandalone test against ORT's
SafeInt.hpp, confirming the guard throws on theoverflowing dimensions (
0x40000400,0xFFFFFFFF) while preserving normal andzero-dimension behavior.
Testing: No unit test is included because the RKNPU EP is not compiled in
any CI leg (it requires the proprietary Rockchip DDK and an ARM target), and the
ToRknpuDim/AllocZeroedBiashelpers have internal (file-static) linkage,so they are not reachable from the gtest suites. The overflow/validation logic
was instead exercised with a standalone test against ORT's
SafeInt.hppasnoted above.