justdan96 / tsMuxer

tsMuxer is a transport stream muxer for remuxing/muxing elementary streams, EVO/VOB/MPG, MKV/MKA, MP4/MOV, TS, M2TS to TS to M2TS. Supported video codecs H.264/AVC, H.265/HEVC, VC-1, MPEG2. Supported audio codecs AAC, AC3 / E-AC3(DD+), DTS/ DTS-HD.
Apache License 2.0
829 stars 140 forks source link

heap buffer over-read is found in movDemuxer::simpleDemuxBlock #839

Closed iwashiira closed 3 months ago

iwashiira commented 3 months ago

Our fuzzer found heap buffer over-read in movDemuxer. in the current master(75c9cb3). PoC is here.

#include "bufferedReaderManager.h"
#include "vod_common.h"
#include "abstractDemuxer.h"
#include "movDemuxer.h"
#include <cstdint>
#include <fs/systemlog.h>

using namespace std;

BufferedReaderManager readManager(2, DEFAULT_FILE_BLOCK_SIZE, DEFAULT_FILE_BLOCK_SIZE + MAX_AV_PACKET_SIZE,
                                  DEFAULT_FILE_BLOCK_SIZE / 2);

int main(int argc, char* argv[]) {

    string fileName = argv[1];
    AbstractDemuxer* demuxer = new MovDemuxer(readManager);

    uint32_t fileBlockSize = demuxer->getFileBlockSize();
    demuxer->openFile(fileName);
    int64_t discardedSize = 0;
    DemuxedData demuxedData;

    PIDSet acceptedPidSet;

    demuxer->simpleDemuxBlock(demuxedData, acceptedPidSet, discardedSize);

    return 0;
}

Following is an output of ASAN. vuln13.mov is in poc13.zip

$ tsmuxer ../crash/vuln13.mov
=================================================================
==29210==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000000e8 at pc 0x555555968631 bp 0x7fffffffdc90 sp 0x7fffffffdc80
READ of size 8 at 0x6020000000e8 thread T0
    #0 0x555555968630 in MovDemuxer::simpleDemuxBlock(std::map<int, MemoryBlock, std::less<int>, std::allocator<std::pair<int const, MemoryBlock> > >&, std::set<int, std::less<int>, std::allocator<int> > const&, long&) (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x414630)
    #1 0x5555558b6f91 in main (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x362f91)
    #2 0x7ffff6f70d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #3 0x7ffff6f70e3f in __libc_start_main_impl ../csu/libc-start.c:392
    #4 0x5555557cc0d4 in _start (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x2780d4)

0x6020000000e8 is located 8 bytes to the right of 16-byte region [0x6020000000d0,0x6020000000e0)
allocated by thread T0 here:
    #0 0x7ffff767a1e7 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:99
    #1 0x555555991401 in __gnu_cxx::new_allocator<std::pair<long, long> >::allocate(unsigned long, void const*) (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x43d401)
    #2 0x55555598dd21 in std::allocator_traits<std::allocator<std::pair<long, long> > >::allocate(std::allocator<std::pair<long, long> >&, unsigned long) (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x439d21)
    #3 0x55555598950f in std::_Vector_base<std::pair<long, long>, std::allocator<std::pair<long, long> > >::_M_allocate(unsigned long) (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x43550f)
    #4 0x5555559842b1 in void std::vector<std::pair<long, long>, std::allocator<std::pair<long, long> > >::_M_realloc_insert<long&, long&>(__gnu_cxx::__normal_iterator<std::pair<long, long>*, std::vector<std::pair<long, long>, std::allocator<std::pair<long, long> > > >, long&, long&) (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x4302b1)
    #5 0x55555597f4b6 in std::pair<long, long>& std::vector<std::pair<long, long>, std::allocator<std::pair<long, long> > >::emplace_back<long&, long&>(long&, long&) (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x42b4b6)
    #6 0x55555596c28c in MovDemuxer::mov_read_mdat(MovDemuxer::MOVAtom) (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x41828c)
    #7 0x55555596a75d in MovDemuxer::ParseTableEntry(MovDemuxer::MOVAtom) (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x41675d)
    #8 0x55555596b08d in MovDemuxer::mov_read_default(MovDemuxer::MOVAtom) (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x41708d)
    #9 0x55555596781d in MovDemuxer::readHeaders() (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x41381d)
    #10 0x55555596801f in MovDemuxer::simpleDemuxBlock(std::map<int, MemoryBlock, std::less<int>, std::allocator<std::pair<int const, MemoryBlock> > >&, std::set<int, std::less<int>, std::allocator<int> > const&, long&) (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x41401f)
    #11 0x5555558b6f91 in main (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x362f91)
    #12 0x7ffff6f70d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

SUMMARY: AddressSanitizer: heap-buffer-overflow (/home/vagrant/tsmuxer/for-build/build/tsMuxer/tsmuxer+0x414630) in MovDemuxer::simpleDemuxBlock(std::map<int, MemoryBlock, std::less<int>, std::allocator<std::pair<int const, MemoryBlock> > >&, std::set<int, std::less<int>, std::allocator<int> > const&, long&)
Shadow bytes around the buggy address:
  0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff8000: fa fa fd fa fa fa 00 00 fa fa 00 00 fa fa 00 fa
=>0x0c047fff8010: fa fa fd fd fa fa 00 fa fa fa 00 00 fa[fa]fd fd
  0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==29210==ABORTING

It is caused by this line. https://github.com/justdan96/tsMuxer/blob/75c9cb3514815d07378007d36cc90c3f209e7b36/tsMuxer/movDemuxer.cpp#L727

Since there is no comparison between the size of m_mdat_dataand the index called m_curChunk, heap buffer over-read occurs when m_curChunk ≥ m_mdat_data.size() . https://github.com/justdan96/tsMuxer/blob/75c9cb3514815d07378007d36cc90c3f209e7b36/tsMuxer/movDemuxer.cpp#L739

Ricerca Security, Inc.

iwashiira commented 3 months ago

After the modification, this input also ignites other vulnerabilities, which will be reported in a separate issue later.