fix: use-after-realloc in addManyNumbers (#517)#522
Open
BAder82t wants to merge 1 commit into
Open
Conversation
ct_ptr is captured before the loop, but three4Two() can resize the NTL::Vec<Ctxt> it points into, invalidating the pointer. Later iterations then read freed memory on ct_ptr->getContext().BPL(). Cache the threshold (3 * BPL()) before the loop and drop ct_ptr. Closes homenc#517
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.
Summary
Fixes heap-buffer-overflow reported in #517.
In
addManyNumbers(src/binaryArith.cpp),ct_ptr = numbers.ptr2nonNull()is captured before the 3-for-2 while-loop. Inside the loop,three4Two()callsNTL::Vec<Ctxt>::SetLengthon the rowct_ptrpoints into. When the Vec grows past capacity, storage is reallocated andct_ptris left dangling. The next iteration reads freed memory onct_ptr->getContext().BPL()(Ctxt.h:1374), which AddressSanitizer catches as aheap-buffer-overflow READ of size 8.Fix
Cache
3 * BPL()before the loop (theContextobject is stable, soBPLis constant for the duration) and null outct_ptrso it cannot be dereferenced after that point.bootstrappableandeawere already captured as a value / stable reference and are unaffected.Reproduction
Parameters from #517 (
m=4095,mvec={7,5,9,13},gens={2341,3277,911},ords={6,4,6},p=2,bits=500,c=2) plus a 16-bitmultTwoNumbers, built with-fsanitize=address -fno-omit-frame-pointer -g:heap-buffer-overflowREAD of size 8 atCtxt.h:1374(Ctxt::getContext()), called fromaddManyNumbers(binaryArith.cpp:927) <-multTwoNumbers(binaryArith.cpp:1121).multTwoNumberscompletes successfully.Closes
#517