Repository navigation
fix: do not pre-size the xref table from the untrusted /Size - #98
Open
prostomarkeloff wants to merge 1 commit into
Open
prostomarkeloff wants to merge 1 commit into
prostomarkeloff wants to merge 1 commit into
Conversation
readXrefStream did `table := make([]xref, size)` where size is the /Size entry read straight from the xref-stream dictionary, with no bound. A ~130 byte PDF declaring a huge /Size forces a multi-gigabyte allocation (OOM; the fatal runtime out-of-memory is not caught by the recover() in NewReaderEncrypted). readXrefStreamData already grows the table on demand via ensureXrefLen, so the eager pre-size is unnecessary.
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.
Found with find-dup-defs using its converge pass, which flagged
readXrefStreamagainstreadXrefTable.read.goreadXrefStreampre-sizes the xref table from the attacker-controlled/Sizeentry:table := make([]xref, size), withsizeread unchecked from the xref-stream dictionary. A ~130-byte PDF whose xref stream declares a large/Size(e.g. 50,000,000) forces a multi-gigabyte allocation before any stream data is read. Therecover()inNewReaderEncryptedcatches themakeslice: len out of rangepanic for absurd sizes, but a large-yet-allocatable size drives a genuineruntime: out of memory, which is fatal and unrecoverable — a memory-exhaustion DoS from a tiny file.The twin
readXrefTabledoes not trust/Size: it grows its table incrementally viaensureXrefLenand only truncates with/Sizeat the end.readXrefStreamDatalikewise grows the slice viaensureXrefLen, so the eager pre-size inreadXrefStreamis unnecessary.Fix: drop the eager pre-size (
table := []xref(nil)); the table is grown on demand.Added a regression test asserting a 134-byte PDF with
/Size= 50,000,000 no longer allocates gigabytes.Disclosure: prepared with the help of an AI tool; I have reviewed the change and test and understand each line.