Skip to content

Sort Objects() in O(n log n) to avoid xref DoS#12

Merged
pgundlach merged 1 commit into
mainfrom
claude/objects-sort
Jun 22, 2026
Merged

Sort Objects() in O(n log n) to avoid xref DoS#12
pgundlach merged 1 commit into
mainfrom
claude/objects-sort

Conversation

@fank

@fank fank commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

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, so n stays small.

Fix: sort.Slice (O(n log n)). Iteration order is unchanged (ascending by object number, then generation).

TestObjectsIteratorSortedOrder guards 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 vet and gofmt clean.

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.
@fank fank force-pushed the claude/objects-sort branch from e648984 to 4ad87a3 Compare June 22, 2026 19:16
@fank

fank commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator Author

That is the last issue I found in the codebase at all, the next ones will be just test coverage.

@fank fank marked this pull request as ready for review June 22, 2026 19:17
@fank fank requested a review from pgundlach June 22, 2026 19:17
@pgundlach pgundlach merged commit e41591e into main Jun 22, 2026
1 check passed
@pgundlach pgundlach deleted the claude/objects-sort branch June 22, 2026 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants