Skip to content

webpmeta: bound ICC profile allocation by stream contents - #19

Open
ChrisJr404 wants to merge 1 commit into
mandykoh:mainfrom
ChrisJr404:bound-webp-iccp-alloc
Open

ChrisJr404 wants to merge 1 commit into
mandykoh:mainfrom
ChrisJr404:bound-webp-iccp-alloc

Conversation

@ChrisJr404

Copy link
Copy Markdown

When extracting metadata from a WebP file, readICCP allocated a buffer sized directly from the ICCP chunk header's 32-bit length field before reading anything, so a truncated file that simply declares a large length forces a correspondingly large allocation.

Failure mode: a ~40-byte WebP whose ICCP chunk claims a length of 0xFFFFFFFF makes readICCP do make([]byte, ch.Length) (~4GB) up front. The recover() in extractMetadata only converts panics into errors; it does not cover the resulting out-of-memory. autometa.Load reaches this path for any RIFF/WEBP input, so a single small crafted file can exhaust memory.

Repro (declaring 512 MiB, provided no data) shows ~536 MB allocated for a ~40-byte input before the fix.

Fix: read the profile incrementally with io.CopyN into a bytes.Buffer so memory tracks the bytes actually present in the stream. Valid files are unaffected (the declared length matches the data that follows); a truncated stream now fails without a large allocation.

Test notes: added TestExtractMetadataICCPAllocation, which declares a 512 MiB ICCP length with no following data and asserts the allocation stays bounded. It fails on the previous code (~536 MB allocated) and passes with this change. go test ./... and go vet ./... are clean.

readICCP pre-allocated a buffer sized directly from the ICCP chunk's
untrusted 32-bit length field before reading any data, so a tiny WebP
declaring a huge ICCP length forced an allocation of up to ~4GB (the
surrounding recover only turns panics into errors and does not cover
out-of-memory). Read the profile incrementally with io.CopyN into a
bytes.Buffer so memory tracks the bytes actually present and a truncated
stream fails without a large allocation.

Signed-off-by: Chris (ChrisJr404) <11917633+ChrisJr404@users.noreply.github.com>
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.

1 participant