Skip to content

corec: fix out-of-bounds write in memheap-backed arrays (fixes #211)#219

Open
shadypayload wants to merge 1 commit into
Matroska-Org:masterfrom
shadypayload:fix/corec-memheap-array-header-offset
Open

corec: fix out-of-bounds write in memheap-backed arrays (fixes #211)#219
shadypayload wants to merge 1 commit into
Matroska-Org:masterfrom
shadypayload:fix/corec-memheap-array-header-offset

Conversation

@shadypayload
Copy link
Copy Markdown

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 the datahead layout for every array. But memheap-backed arrays use dataheaphead, and since alignas(max_align_t) was added to data[] (b60fdf3) and the two headers were unified (da1e44b), their layouts diverged:

datahead    : [ Size(8) ][ pad(8) ][ data@16 ]   Size at data-16
dataheaphead: [ Heap(8) ][ Size(8)][ data@16 ]   Size at data-8

For a memheap-backed array, Data_Head() reads 8 bytes too early and treats the Heap self-pointer as the size. Masked of its flag bits it's a huge value, so ArrayResize() skips allocation and the write lands in the MemHeap_Default global. AddressSanitizer on the #211 repro:

ERROR: AddressSanitizer: global-buffer-overflow WRITE of size 4
  in ReadBlockData libmatroska2/matroskamain.c:1513
... located 0 bytes after global variable 'MemHeap_Default'

(#211's backtrace shows the same bug at a different site — a binary element read via MemRead/ebmlbinary.c.)

Fix

Give datahead an unused leading pointer so its {Size, data} tail lands at the same offsets as dataheaphead. ABI-neutral: header stays 16 bytes, data stays max-aligned.

Testing

Diagnosis was assisted by AI tooling (ASan + gdb); I reviewed and verified the change and take responsibility for it.

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>
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