Repository navigation
Fix integer overflow in fully-connected-nc.c bias/kernel buffer sizing - #10935
Open
prasanna8585 wants to merge 2 commits into
Open
prasanna8585 wants to merge 2 commits into
prasanna8585 wants to merge 2 commits into
Conversation
output_channels * sizeof(xnn_float16) (xnn_create_fully_connected_nc_f16, xnn_create_fully_connected_nc_pf16) and input_channels * output_channels * sizeof(float) / output_channels * sizeof(float) (xnn_create_fully_connected_nc_f32_f16, xnn_create_fully_connected_nc_pf32_f16) were used directly as allocation sizes and write-loop bounds with no overflow check, before any other parameter validation runs. On 32-bit size_t targets this allows a caller-supplied channel count to wrap the allocation size to a few bytes while the loop still writes/reads the real, unwrapped count: heap-buffer-overflow write, plus an out-of-bounds read of the caller-supplied bias array in the f16-bias case. Guard all four sites with the existing xnn_safe_mul() helper, matching the pattern already used for the main packed-weights buffer allocation elsewhere in this file, returning xnn_status_invalid_parameter on overflow instead of allocating an undersized buffer.
Contributor
Author
|
Hi Team, is there any update require from my side on this ? |
Contributor
|
Nope, waiting for the internal approval. |
Aelphy
approved these changes
Aug 14, 2026
Contributor
Author
|
Hi @Aelphy , Just following up to check if this is ready to be merged. |
Contributor
Author
|
Hi @Aelphy , is there any update on this to get this merged? |
Contributor
Author
|
Hi @Aelphy , Just following up to check if this is ready to be merged. |
Contributor
|
Could you please rebase. |
Contributor
Author
|
Rebased and resolved the conflicts, but flagging something first: this looks like it may have been independently fixed upstream in 4a1dd2d (via #11362), which applies the same xnn_safe_mul guard to every site this PR touches, plus unit tests. Happy to leave this open or close it as superseded your call. |
fbarchard
approved these changes
Oct 17, 2026
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.
Summary
Four sibling functions in
src/operators/fully-connected-nc.ccompute an allocation size by multiplying caller-suppliedoutput_channels(and, in two cases,input_channels * output_channels) by an element size, with no overflow check, and use the same unvalidated value as a write-loop bound immediately after — before any of this file's normal parameter validation (theoutput_channels == 0/ stride checks increate_fully_connected_nc) has run:xnn_create_fully_connected_nc_f16—output_channels * sizeof(xnn_float16)xnn_create_fully_connected_nc_pf16— samexnn_create_fully_connected_nc_f32_f16—input_channels * output_channels * sizeof(float), andoutput_channels * sizeof(float)for the bias bufferxnn_create_fully_connected_nc_pf32_f16— sameOn a 32-bit
size_ttarget (XNNPACK supports ARM32 and WASM32 — both real, shipping deployment targets), a channel count just above2^31(or2^30for the two-factor kernel-buffer case) wraps the multiplication to a small value. The allocation then succeeds at a tiny size while the subsequent loop still iterates the real, unwrapped channel count — a heap-buffer-overflow write. In thef16bias-conversion path, the same loop also reads that many elements from the caller'sbiasarray, which is only sized for the real (intended) channel count — an out-of-bounds read as well.This is a narrow, specific gap: the main packed-weights buffer allocation a few hundred lines later in this same file is already correctly guarded with
xnn_safe_mul()(seecreate_fully_connected_nc, packed_weights_size). This PR applies that exact same, already-established pattern to the four sites above.Fix
Each site now computes the allocation size via
xnn_safe_mul()first and returnsxnn_status_invalid_parameter(consistent with this file's other validation-failure paths) if it would overflow, before doing any allocation or touching caller memory.Verification
master.gcc -fsyntax-only -Wall -Wextraclean against the real repo headers, no new warnings introduced.xnn_safe_mul(fromsrc/xnnpack/math.h) against the patched logic: the previously-overflowing input is now rejected withxnn_status_invalid_parameterbefore any allocation, and a legitimate smalloutput_channelsstill succeeds unchanged.