fix: prevent array out-of-bounds write in segv function.Add boundary … #74
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.
For details, please see: scipy/scipy#23973 .
segv Memory Corruption Analysis
1. Root Cause of Memory Corruption
There is an array out-of-bounds write in the segv function in specfun.h source code at lines 6058-6061.
The length of the
egarray isn - m + 1, which comes from thesegvfunction call in _specfun.pyx to the C++ function. In thesegvfunction in specfun.h, the index ofegcan exactly reachn - m + 1, causing an out-of-bounds write.2. Test Code
The variables and functions required to reproduce the vulnerability in specfun.h and _specfun.pyx have been written into a cpp file.
After compiling with g++ and running, you can see successful calculations but also memory-related error messages.
Modify the code at lines 6058-6061 to add debug information when the length exceeds
n - m + 1.After compiling with g++ and running, it can be observed that the array length is 3, but the index also reaches 3, exceeding the bounds by 1 unit.