Sort Objects() in O(n log n) to avoid xref DoS#12
Merged
Conversation
Objects() insertion-sorted every xref entry (commented "small dataset"), but the entry count is attacker-controlled — a crafted xref stream with millions of entries made it O(n^2) and could hang for minutes. Use sort.Slice (O(n log n)); the ascending iteration order is unchanged. Test: TestObjectsIteratorSortedOrder.
e648984 to
4ad87a3
Compare
Collaborator
Author
|
That is the last issue I found in the codebase at all, the next ones will be just test coverage. |
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.
The one item from my own earlier review that slipped through (the external security review didn't flag it).
Reader.Objects()insertion-sorted every xref entry, with a comment assuming a "small dataset" — but the entry count is attacker-controlled. A crafted xref stream packs millions of entries into a few MB (decoded cap 16 MiB ÷ ~4 B/entry ≈ 4M), making the sort O(n²) → ~10¹³ comparisons → a minutes-long CPU hang (DoS). The fuzzer missed it because fuzz inputs are tiny, sonstays small.Fix:
sort.Slice(O(n log n)). Iteration order is unchanged (ascending by object number, then generation).TestObjectsIteratorSortedOrderguards that the sort still yields ascending order — the xref map's random iteration order means the sort is what produces it, so a wrong comparator would fail it. The complexity improvement itself is by construction.go test -race ./...passes;go vetandgofmtclean.