Fix heap-buffer-overflow in HNSW visited bitmap - #83
Merged
Conversation
The visited bitmap was sized from TableStats::getTableCard(), an estimated (possibly stale) cardinality, and indexed without bounds checks by graph neighbor offsets in oneHopSearch and related paths. When the estimate was smaller than the actual max node offset this caused an ASan heap-buffer-overflow READ in VisitedState::contains (LadybugDB/ladybug#940). - Size the bitmap from the exact NodeTable::getNumTotalRows() instead of the estimated table cardinality. - Make VisitedState bounds-safe and auto-growing: contains() returns false for out-of-range offsets, add() grows geometrically while preserving existing marks. - Proactively resize the bitmap to the current row count in initLayerSearchState to cover growth between query init and search.
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.
Fixes LadybugDB/ladybug#940.
ASan reported a heap-buffer-overflow READ in
VisitedState::contains(viaOnDiskHNSWIndex::oneHopSearch): the visited bitmap was sized fromTableStats::getTableCard(), an estimated (possibly stale) cardinality, and then indexed without bounds checks by graph neighbor offsets.Changes:
initQueryHNSWSharedStatenow sizes the bitmap from the exactNodeTable::getNumTotalRows()instead of the estimated table cardinality.VisitedStateis now bounds-safe and auto-growing:contains()returns false for out-of-range offsets,add()grows the bitmap geometrically while preserving existing marks.initLayerSearchStateproactively resizes the bitmap to the current row count, covering table growth between query init and search.