Fix out of bounds read issue in cross.entropy.cc#27568
Merged
Conversation
baijumeswani
previously approved these changes
Mar 5, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds bounds checking for label tensor values in SparseSoftmaxCrossEntropy::Compute to prevent out-of-bounds memory reads. Without this fix, a malicious ONNX model could embed arbitrary int64 label values that would be used directly as array indices into the log-probability buffer (which has size N×D), allowing heap memory reads beyond that buffer.
Changes:
- A validation loop over all
nlabel values is added after the softmax computation to return an error if any label falls outside[0, D) - The check uses
ORT_RETURN_IFwith a descriptive message that includes the offending label value and its index
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
baijumeswani
approved these changes
Mar 16, 2026
This was referenced Apr 25, 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.
Description
Add bounds checking for label tensor values in
SparseSoftmaxCrossEntropy::Computeto prevent out-of-bounds memory reads.The
SparseSoftmaxCrossEntropyoperator useslabel_data[i](int64_t) directly as an array index into the log-probability buffer without validating that the value falls within[0, D)whereDis the number of classes. A malicious ONNX model can embed arbitrary label values in a model initializer, causing the operator to read heap memory beyond the log-probability buffer.Affected expressions in
cross_entropy.cc:Existing shape validation confirms label and logit dimensions are compatible, but never validates label values against the class dimension.
Fix
Added a validation loop before the loss computation that returns an error status if any label value is outside
[0, D):