Skip to content

decoder: keep NAL units in place when the NAL unit list grows - #4058

Open
anabobillo3 wants to merge 1 commit into
cisco:masterfrom
anabobillo3:decoder-nal-list-stable
Open

anabobillo3 wants to merge 1 commit into
cisco:masterfrom
anabobillo3:decoder-nal-list-stable

Conversation

@anabobillo3

Copy link
Copy Markdown

Fixes #3684.

When an access unit has more NAL units than the decoder's NAL unit list holds, MemGetNextNal() grows the list through ExpandNalUnitList(). That function allocated a whole new list, copied every SNalUnit into it and freed the old one, so each NAL unit moved to a new address. The decoder keeps plain pointers into those units while it decodes:

  • pCtx->pSliceHeader (set in WelsDqLayerDecodeStart()): the picture reordering in welsDecoderExt.cpp reads it (BufferingReadyPicture(), ReleaseBufferedReadyPictureReorder()), which is the read reported in Heap use after free on ISVCDecoder::DecodeFrameNoDelay #3684, and ParseDecRefPicMarking() writes pCtx->pSliceHeader->iPicOrderCntLsb through it on MMCO 5;
  • pCtx->pNalCur, and the current dq layer's pRefPicMarking, pRefPicListReordering, pPredWeightTable and pRefPicBaseMarking.

After a list growth all of them pointed into freed memory.

Change

NAL units no longer move once allocated. ExpandNalUnitList() allocates a block that holds only the new units and the bigger array of NAL unit pointers. It copies the existing pointers into that array unchanged, keeping their order, and links the block from the access unit (SAccessUnit::pExpandedBlock). MemFreeNalList() frees every linked block along with the list. The SAccessUnit itself doesn't move either. This covers every pointer into a NAL unit, current and future, so nothing needs retargeting after a growth.

Test

DecoderNalUnitListTest.ExpandNalUnitListKeepsNalUnitsInPlace (in test/decoder/DecUT_ParseSyntax.cpp) takes every NAL unit of a new list, then grows the list twice through MemGetNextNal(). It checks that the units handed out before are still the same list entries, with their contents intact, and that a write through a slice header pointer taken before the growth lands in the list.

Results (ASan build, USE_ASM=No, BUILDTYPE=Debug):

  • Before this change, the new test fails: the units handed out before the growth are no longer the list's entries. After it, the test passes, and passes 5 times in a row with leak detection on. The Debug build's memory monitor also balances, so the extra blocks are freed.
  • The rest of codec_unittest, run test by test before and after the change, gives the same results (523 tests, all passing after).
  • A stream that grows the list within one access unit before a slice with MMCO 5 makes ASan report a heap-use-after-free write in ParseDecRefPicMarking() through pCtx->pSliceHeader, with DecodeFrame2() and error concealment on, and also in parse-only mode. With this change it decodes cleanly in both modes. The input attached to Heap use after free on ISVCDecoder::DecodeFrameNoDelay #3684 no longer triggers the error on current master with the driver from the issue (the stream now takes another path), but the list growth and the dangling pCtx->pSliceHeader it reported are the same.
  • The decoder fuzzer and a parse-only fuzzer ran for 15 minutes each on the fixed tree (ASan + UBSan) with no crashes.

When an access unit has more NAL units than the list holds,
ExpandNalUnitList() allocated a new list, copied every SNalUnit into it
and freed the old one. The decoder keeps pointers into those units
(pCtx->pSliceHeader, pCtx->pNalCur, the current dq layer's
pRefPicMarking, pRefPicListReordering, pPredWeightTable and
pRefPicBaseMarking), and they were left pointing into the freed list:
the picture reordering reads pCtx->pSliceHeader (cisco#3684), and
ParseDecRefPicMarking() writes through it on MMCO 5.

Grow only the array of NAL unit pointers instead. The new block holds
the bigger array and the new units; existing units keep their address
and their order, and each block is linked from the access unit and
freed in MemFreeNalList().

Fixes cisco#3684
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.

Heap use after free on ISVCDecoder::DecodeFrameNoDelay

1 participant