Skip to content

Validate Conv bias tensor size against output channel count to prevent OOB read - #29464

Closed
Chi Lo (chilo-ms) wants to merge 2 commits into
mainfrom
chilo-ms/conv-bias-size-validation
Closed

Chi Lo (chilo-ms) wants to merge 2 commits into
mainfrom
chilo-ms/conv-bias-size-validation

Conversation

@chilo-ms

Copy link
Copy Markdown
Contributor

Description

The  Conv  operator reads  M  bias values ( M  = output channels from  W ) without checking that the bias tensor actually has  M  elements. A model with  W  declaring 64 output channels and a 1-element bias causes the kernel to read 63 floats past the bias tensor into adjacent heap. Because all-initializer  Conv  nodes are constant-folded at load, the OOB read fires inside  InferenceSession()  before any explicit  Run() .

Changes

•  conv.cc : Add bias shape validation ( 1D, size == M ) in both  Conv::Compute  and  Conv::Compute , before any bias access.
•  conv_op_test.cc : Add  Conv2D_BiasSizeMismatch  test (W with M=4 output channels, B with 1 element → expects failure).

Motivation and Context

…t OOB read

The Conv operator reads M bias values (M = number of output channels from
the weight tensor) without checking that the bias tensor actually has M
elements. A model with W declaring 64 output channels and a 1-element bias
causes the kernel to read 63 floats past the bias tensor into adjacent heap.
Because all-initializer Conv nodes are constant-folded at load, the read
fires inside InferenceSession() before any explicit Run().

Add a bias shape check (1D with size == M) in both Conv<T>::Compute and
Conv<float>::Compute, before any bias access. Add a test that supplies a
mismatched bias and expects failure.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds CPU Conv bias-shape validation to prevent out-of-bounds reads.

Changes:

  • Validates that bias is 1D and matches the output-channel count.
  • Adds a regression test for undersized bias tensors.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
onnxruntime/core/providers/cpu/nn/conv.cc Validates bias shape in generic and float Conv kernels.
onnxruntime/test/providers/cpu/nn/conv_op_test.cc Tests rejection of a mismatched bias size.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Resolve the Conv validation conflicts in favor of the equivalent upstream implementation merged in #32160, including its additional invalid-rank regression coverage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 1d814472-441f-441d-bd46-931956efc1cd
@chilo-ms

Copy link
Copy Markdown
Contributor Author

Resolved the conflicts by merging current main in eab23c5. The resulting branch has no remaining diff because #32160 independently merged the same Conv bias size/rank validation and regression coverage, so this PR is now superseded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants