GH-50077: [C++][IPC] Avoid int64 overflow in ReadSparseCSXIndex#50038
Open
jmestwa-coder wants to merge 1 commit into
Open
GH-50077: [C++][IPC] Avoid int64 overflow in ReadSparseCSXIndex#50038jmestwa-coder wants to merge 1 commit into
jmestwa-coder wants to merge 1 commit into
Conversation
Member
|
Author
|
Done. Opened #50077 and retitled the PR to reference it, and restored the PR template in the description. |
|
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the C++ IPC sparse tensor reader by preventing signed int64_t overflow when validating CSX (CSR/CSC) index buffer sizes in ReadSparseCSXIndex, closing an out-of-bounds read vector when parsing crafted IPC messages.
Changes:
- Use
internal::MultiplyWithOverflowto computeindices/indptrminimum byte sizes safely. - Use
internal::AddWithOverflowto compute(shape[axis] + 1)safely forindptrlength derivation. - Return
Status::Invalidwhen overflow is detected during size validation.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -2336,26 +2337,40 @@ Result<std::shared_ptr<SparseIndex>> ReadSparseCSXIndex( | |||
| /*allow_short_read=*/false)); | |||
|
|
|||
| std::vector<int64_t> indices_shape({non_zero_length}); | |||
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.
Rationale for this change
ReadSparseCSXIndexvalidates the SparseTensorindices/indptrbuffer sizes against the claimed shape usingint64products (non_zero_length * byte_widthand(shape[axis] + 1) * byte_width). Both inputs come unchecked from the flatbuffer viaGetSparseTensorMetadata, so a value nearINT64_MAXoverflows the signed product (UBSan-confirmed), wraps to a small value, and the size guard passes. The indexTensoris then built over a buffer smaller than its shape, enabling an out-of-bounds read.What changes are included in this PR?
Compute the
indices/indptrbyte counts (and theshape[axis] + 1term) withMultiplyWithOverflow/AddWithOverflow, the same checked helpers already used for this size math intensor.cc, and returnStatus::Invalidwhen the computation overflows.Are these changes tested?
Covered by the existing sparse tensor IPC round-trip tests; the change only adds an overflow guard on the existing validation path.
Are there any user-facing changes?
No.