Project: gpac (gpac/gpac), language: c++
Vulnerability instance: gpac.cve-2020-22678
Sanitizer: address

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/gpac/gpac/issues/1339
## Description:
Issue: gpac/gpac#1339
Title: There is a heap-buffer-overflow in the gf_media_nalu_remove_emulation_bytes function of av_parsers.c:4722
State: closed
Created by: gutiniao
Created at: 2019-11-13 03:14:38+00:00

Issue Body:
Thanks for reporting your issue. Please make sure these boxes are checked before submitting your issue - thank you!

- [ √] I looked for a similar issue and couldn't find any.
- [ √] I tried with the latest version of GPAC. Installers available at http://gpac.io/downloads/gpac-nightly-builds/
- [ √] I give enough information for contributors to reproduce my issue (meaningful title, github labels, platform and compiler, command-line ...). I can share files anonymously with this dropbox: https://www.mediafire.com/filedrop/filedrop_hosted.php?drop=eec9e058a9486fe4e99c33021481d9e1826ca9dbc242a6cfaab0fe95da5e5d95

Detailed guidelines: http://gpac.io/2013/07/16/how-to-file-a-bug-properly/

A crafted input will lead to crash in av_parsers.c at gpac 0.8.0.
Triggered by
./MP4Box -diso POC -out /dev/null

Poc
[001gf_media_nalu_remove_emulation_bytes](https://github.com/gutiniao/afltest/blob/master/001gf_media_nalu_remove_emulation_bytes)

The ASAN information is as follows:

```
./MP4Box -diso 001gf_media_nalu_remove_emulation_bytes -out /dev/null 
[iso file] Media header timescale is 0 - defaulting to 90000
=================================================================
==23148==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000002d1 at pc 0x5632845c98b0 bp 0x7ffdce21c4e0 sp 0x7ffdce21c4d0
READ of size 1 at 0x6020000002d1 thread T0
    #0 0x5632845c98af in gf_media_nalu_remove_emulation_bytes media_tools/av_parsers.c:4722
    #1 0x5632845c991b in gf_media_avc_read_sps media_tools/av_parsers.c:4737
    #2 0x5632843ea9a9 in avcc_Read isomedia/avc_ext.c:2371
    #3 0x5632844183d4 in gf_isom_box_read isomedia/box_funcs.c:1528
    #4 0x5632844183d4 in gf_isom_box_parse_ex isomedia/box_funcs.c:208
    #5 0x563284418e10 in gf_isom_box_array_read_ex isomedia/box_funcs.c:1419
    #6 0x5632848afbf1 in video_sample_entry_Read isomedia/box_code_base.c:4405
    #7 0x5632844183d4 in gf_isom_box_read isomedia/box_funcs.c:1528
    #8 0x5632844183d4 in gf_isom_box_parse_ex isomedia/box_funcs.c:208
    #9 0x563284418e10 in gf_isom_box_array_read_ex isomedia/box_funcs.c:1419
    #10 0x5632844183d4 in gf_isom_box_read isomedia/box_funcs.c:1528
    #11 0x5632844183d4 in gf_isom_box_parse_ex isomedia/box_funcs.c:208
    #12 0x563284418e10 in gf_isom_box_array_read_ex isomedia/box_funcs.c:1419
    #13 0x5632848b38a4 in stbl_Read isomedia/box_code_base.c:5381
    #14 0x5632844183d4 in gf_isom_box_read isomedia/box_funcs.c:1528
    #15 0x5632844183d4 in gf_isom_box_parse_ex isomedia/box_funcs.c:208
    #16 0x563284418e10 in gf_isom_box_array_read_ex isomedia/box_funcs.c:1419
    #17 0x5632848ad40b in minf_Read isomedia/box_code_base.c:3500
    #18 0x5632844183d4 in gf_isom_box_read isomedia/box_funcs.c:1528
    #19 0x5632844183d4 in gf_isom_box_parse_ex isomedia/box_funcs.c:208
    #20 0x563284418e10 in gf_isom_box_array_read_ex isomedia/box_funcs.c:1419
    #21 0x5632848ab73f in mdia_Read isomedia/box_code_base.c:3021
    #22 0x5632844183d4 in gf_isom_box_read isomedia/box_funcs.c:1528
    #23 0x5632844183d4 in gf_isom_box_parse_ex isomedia/box_funcs.c:208
    #24 0x563284418e10 in gf_isom_box_array_read_ex isomedia/box_funcs.c:1419
    #25 0x5632848ba906 in trak_Read isomedia/box_code_base.c:7129
    #26 0x5632844183d4 in gf_isom_box_read isomedia/box_funcs.c:1528
    #27 0x5632844183d4 in gf_isom_box_parse_ex isomedia/box_funcs.c:208
    #28 0x563284418e10 in gf_isom_box_array_read_ex isomedia/box_funcs.c:1419
    #29 0x5632848adf64 in moov_Read isomedia/box_code_base.c:3745
    #30 0x563284419b35 in gf_isom_box_read isomedia/box_funcs.c:1528
    #31 0x563284419b35 in gf_isom_box_parse_ex isomedia/box_funcs.c:208
    #32 0x56328441a1e4 in gf_isom_parse_root_box isomedia/box_funcs.c:42
    #33 0x563284430f44 in gf_isom_parse_movie_boxes isomedia/isom_intern.c:206
    #34 0x563284433bca in gf_isom_open_file isomedia/isom_intern.c:615
    #35 0x56328417c852 in mp4boxMain /home/liuz/gpac-master/applications/mp4box/main.c:4767
    #36 0x7f0252bccb96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #37 0x56328416db19 in _start (/usr/local/gpac-asan3/bin/MP4Box+0x163b19)

0x6020000002d1 is located 0 bytes to the right of 1-byte region [0x6020000002d0,0x6020000002d1)
allocated by thread T0 here:
    #0 0x7f0253855b50 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb50)
    #1 0x5632843ea263 in avcc_Read isomedia/avc_ext.c:2343

SUMMARY: AddressSanitizer: heap-buffer-overflow media_tools/av_parsers.c:4722 in gf_media_nalu_remove_emulation_bytes
Shadow bytes around the buggy address:
  0x0c047fff8000: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 00
  0x0c047fff8010: fa fa fd fd fa fa 00 00 fa fa 00 00 fa fa 00 00
  0x0c047fff8020: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 00
  0x0c047fff8030: fa fa 00 00 fa fa 00 00 fa fa 00 05 fa fa 00 00
  0x0c047fff8040: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 00
=>0x0c047fff8050: fa fa 00 00 fa fa 00 00 fa fa[01]fa fa fa 01 fa
  0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8090: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff80a0: 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
==23148==ABORTING
```



Comments:
Comment by aureliendavid on 2020-01-09 17:59:39+00:00:
thanks for the report

this should be fixed by the commit above 

reopen if needed

Commit References:
eec9e058a9486fe4e99c33021481d9e1826ca9db

Build script:
#!/bin/bash -eu
# Minimized build script with only core build commands
set -eu
./configure --static-build --extra-cflags="${CFLAGS}" --extra-ldflags="${CFLAGS}"
make -j$(nproc)

Dockerfile:
FROM hwiwonlee/secb.base:latest
RUN apt-get update && apt-get install -y build-essential pkg-config libz-dev
RUN git clone https://github.com/gpac/gpac gpac
RUN git -C gpac checkout 6b4ab401297be43b57f9eddd675971a8a5feab44
WORKDIR $SRC/gpac
COPY build.sh $SRC/

Additional files:
filename: fuzz_parse.c
content: /* Copyright 2021 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdio.h>
#include <unistd.h>

#include <gpac/internal/isomedia_dev.h>
#include <gpac/constants.h>

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
    char filename[256];
    sprintf(filename, "/tmp/libfuzzer.%d", getpid());

    FILE *fp = fopen(filename, "wb");
    if (!fp) {
        return 0;
    }
    fwrite(data, size, 1, fp);
    fclose(fp);

    GF_ISOFile *movie = NULL;
    movie = gf_isom_open_file(filename, GF_ISOM_OPEN_READ_DUMP, NULL);
    if (movie != NULL) {
        gf_isom_close(movie);
    }
    unlink(filename);
    return 0;
}