corec: fix out-of-bounds write in memheap-backed arrays (fixes #211)#219
Open
shadypayload wants to merge 1 commit into
Open
corec: fix out-of-bounds write in memheap-backed arrays (fixes #211)#219shadypayload wants to merge 1 commit into
shadypayload wants to merge 1 commit into
Conversation
Data_Head()/Data_Size() always read the Size+flags word through the
datahead layout, for both plain-heap and memheap-backed arrays, so
datahead and dataheaphead must keep their {Size, data} tail at identical
offsets.
b60fdf3 added alignas(max_align_t) to data[]; da1e44b then unified the
two headers into shared macros assuming they were compatible. They are
not:
datahead : [ Size(8) ][ pad(8) ][ data@16 ] Size at data-16
dataheaphead: [ Heap(8) ][ Size(8)][ data@16 ] Size at data-8
datahead has no leading Heap pointer, so alignas inserts 8 bytes of
padding between Size and data; dataheaphead's Heap pointer fills that
slot and keeps Size adjacent to data. Data_Head() (datahead offsets)
therefore reads a memheap-backed array's header 8 bytes early and picks
up the Heap self-pointer instead of Size. Masked of its flag bits that
is a huge value, so ArrayResize() thinks the empty array already has
enormous capacity, skips allocation, and the caller writes through
_Begin directly into the MemHeap_Default global.
Any EBML element read into a memheap-backed array triggers it (a binary
element, or a laced Block's SizeList), so it reproduces on a trivial
file. mkvalidator segfaults (or aborts in NodeContext_Done on 0.6.0);
mkclean is affected too.
Give datahead an unused leading pointer so its {Size, data} tail matches
dataheaphead, restoring the compatibility da1e44b intended. No size or
alignment change: header stays 16 bytes, data stays max-aligned.
Fixes Matroska-Org#211
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Problem
mkvalidator segfaults on a trivial ffmpeg-generated file (#211), and mkclean/mkvalidator crash on real files. Root cause is in corec, so it affects both tools.
Root cause
Data_Head()/Data_Size()read the Size+flags word via thedataheadlayout for every array. But memheap-backed arrays usedataheaphead, and sincealignas(max_align_t)was added todata[](b60fdf3) and the two headers were unified (da1e44b), their layouts diverged:For a memheap-backed array,
Data_Head()reads 8 bytes too early and treats theHeapself-pointer as the size. Masked of its flag bits it's a huge value, soArrayResize()skips allocation and the write lands in theMemHeap_Defaultglobal. AddressSanitizer on the #211 repro:(#211's backtrace shows the same bug at a different site — a binary element read via
MemRead/ebmlbinary.c.)Fix
Give
dataheadan unused leading pointer so its{Size, data}tail lands at the same offsets asdataheaphead. ABI-neutral: header stays 16 bytes, data stays max-aligned.Testing
mkvalidatorv0.6.0 crashes andmastersegfaults against sample.mkv#211 repro (dummy.mkv): segfaults before, "appears to be valid" after; mkclean produces valid output.Diagnosis was assisted by AI tooling (ASan + gdb); I reviewed and verified the change and take responsibility for it.