bound MCOS pointer-array walks to the allocated size#322
Open
naruto-lgtm wants to merge 1 commit into
Open
Conversation
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.
Repro: read a v5 file whose MCOS subsystem encodes a cell/object whose dimensions imply more elements than the stored
matvar_t *array holds (test_mat readvarormatdump -don such a file).Cause:
ResolveNestedMCOSderives the child count from the product ofmatvar->dims(timesnum_fieldsfor the struct/object branch) and walkscells[i]/fields[i]over it, but the dimensions come from subsystem metadata and are not tied to what was actually allocated inmatvar->data. When they disagree the loop reads past the pointer array and dereferences the out-of-bounds pointer (ASan heap-buffer-overflow READ at mcos.c:1603 and :1624).ParseSubsystem5has the same trust issue on the top-level FileWrapper__ array: it dereferencescells[0]right afterMat_MulDims, ahead of thencells >= 3/5checks, so a zero- or short-length cell array is indexed out of bounds. The v7.3 pathParseSubsystem73already validates the cell count before indexing.Fix: clamp both walks in
ResolveNestedMCOStonbytes / sizeof(matvar_t *), and reject a FileWrapper cell array whose element count exceeds its allocation before using it. For well-formed files the dimensions and the allocation agree, so nothing is clamped and output is unchanged (verified against the committed MCOS dump results).