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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/gpac/gpac/issues/1329
## Description:
Issue: gpac/gpac#1329
Title: AddressSanitizer: heap-use-after-free in trak_Read isomedia/box_code_base.c:7153
State: closed
Created by: Clingto
Created at: 2019-11-09 11:15:44+00:00

Issue Body:
System info:  
Ubuntu 16.04.6 LTS, X64, gcc 5.4.0, gpac (latest master 00dfc93)  
Compile Command:
```
$ CC="gcc -fsanitize=address -g" CXX="g++ -fsanitize=address -g" ./configure --static-mp4box
$ make
```
Run Command:
```
$ MP4Box -diso -out /dev/null $POC-new-trak_Read
```
POC file:  
https://github.com/Clingto/POC/blob/master/gpac-MP4Box/gpac-00dfc93-crashes/POC-new-trak_Read

gdb info:
```C
Program received signal SIGSEGV, Segmentation fault.
0x0000000000505aee in stco_dump ()
(gdb) bt
#0  0x0000000000505aee in stco_dump ()
#1  0x0000000000514918 in gf_isom_box_dump_ex ()
#2  0x0000000000502e15 in gf_isom_box_array_dump ()
#3  0x00000000005149dc in gf_isom_box_dump_done ()
#4  0x0000000000503a1b in stbl_dump ()
#5  0x0000000000514918 in gf_isom_box_dump_ex ()
#6  0x0000000000502e15 in gf_isom_box_array_dump ()
#7  0x00000000005149dc in gf_isom_box_dump_done ()
#8  0x000000000050615d in minf_dump ()
#9  0x0000000000514918 in gf_isom_box_dump_ex ()
#10 0x0000000000502e15 in gf_isom_box_array_dump ()
#11 0x00000000005149dc in gf_isom_box_dump_done ()
#12 0x000000000050644d in mdia_dump ()
#13 0x0000000000514918 in gf_isom_box_dump_ex ()
#14 0x0000000000502e15 in gf_isom_box_array_dump ()
#15 0x00000000005149dc in gf_isom_box_dump_done ()
#16 0x000000000050435f in trak_dump ()
#17 0x0000000000514918 in gf_isom_box_dump_ex ()
#18 0x0000000000502e15 in gf_isom_box_array_dump ()
#19 0x00000000005149dc in gf_isom_box_dump_done ()
#20 0x000000000050337a in moov_dump ()
#21 0x0000000000514918 in gf_isom_box_dump_ex ()
#22 0x0000000000502f10 in gf_isom_dump ()
#23 0x0000000000425faa in dump_isom_xml ()
#24 0x000000000041c69a in mp4boxMain ()
#25 0x00007ffff72ed830 in __libc_start_main (main=0x40eb70 <main>, argc=5, argv=0x7fffffffe328, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe318) at ../csu/libc-start.c:291
#26 0x000000000040eba9 in _start ()

```
ASAN info:
```C
==27939==ERROR: AddressSanitizer: heap-use-after-free on address 0x60600000eea0 at pc 0x000000aea883 bp 0x7fffffff7f90 sp 0x7fffffff7f80
READ of size 4 at 0x60600000eea0 thread T0
    #0 0xaea882 in trak_Read isomedia/box_code_base.c:7148
    #1 0x6c3d6e in gf_isom_box_read isomedia/box_funcs.c:1528
    #2 0x6c3d6e in gf_isom_box_parse_ex isomedia/box_funcs.c:208
    #3 0x6c47bc in gf_isom_box_array_read_ex isomedia/box_funcs.c:1419
    #4 0xadc064 in moov_Read isomedia/box_code_base.c:3745
    #5 0x6c5114 in gf_isom_box_read isomedia/box_funcs.c:1528
    #6 0x6c5114 in gf_isom_box_parse_ex isomedia/box_funcs.c:208
    #7 0x6c5974 in gf_isom_parse_root_box isomedia/box_funcs.c:42
    #8 0x6da6a0 in gf_isom_parse_movie_boxes isomedia/isom_intern.c:206
    #9 0x6dd2f3 in gf_isom_parse_movie_boxes isomedia/isom_intern.c:194
    #10 0x6dd2f3 in gf_isom_open_file isomedia/isom_intern.c:615
    #11 0x42f88a in mp4boxMain /home/aota09/yyp/fuzzcompare/test/gpac/test-crash/build_asan_00dfc93/applications/mp4box/main.c:4767
    #12 0x7ffff638082f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #13 0x41e228 in _start (/home/aota09/yyp/fuzzcompare/test/gpac/test-crash/bin_asan/bin/MP4Box+0x41e228)

0x60600000eea0 is located 0 bytes inside of 56-byte region [0x60600000eea0,0x60600000eed8)
freed by thread T0 here:
    #0 0x7ffff6f022ca in __interceptor_free (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x982ca)
    #1 0x6c393f in gf_isom_box_del isomedia/box_funcs.c:1508

previously allocated by thread T0 here:
    #0 0x7ffff6f02602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0xae2b8d in stco_New isomedia/box_code_base.c:5616

SUMMARY: AddressSanitizer: heap-use-after-free isomedia/box_code_base.c:7148 trak_Read
Shadow bytes around the buggy address:
  0x0c0c7fff9d80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0c7fff9d90: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0c7fff9da0: fa fa fa fa 00 00 00 00 00 00 00 00 fa fa fa fa
  0x0c0c7fff9db0: 00 00 00 00 00 00 00 fa fa fa fa fa 00 00 00 00
  0x0c0c7fff9dc0: 00 00 00 00 fa fa fa fa 00 00 00 00 00 00 00 04
=>0x0c0c7fff9dd0: fa fa fa fa[fd]fd fd fd fd fd fd fa fa fa fa fa
  0x0c0c7fff9de0: 00 00 00 00 00 00 00 fa fa fa fa fa 00 00 00 00
  0x0c0c7fff9df0: 00 00 00 fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c0c7fff9e00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0c7fff9e10: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0c7fff9e20: 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
  Heap right redzone:      fb
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack partial redzone:   f4
  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
==27939==ABORTING
```
---
Edit

This bug issue still exists in latest version 0.8.0: 4c19ae5 and 0.9.0: 1de1f8d

asan
```
[33m[iso file] Box "stco" (start 817) has 142 extra bytes
[0m[33m[iso file] Track with no sample description box !
[0m=================================================================
==11412==ERROR: AddressSanitizer: heap-use-after-free on address 0x60600000eea0 at pc 0x000000aee083 bp 0x7fffffff7f70 sp 0x7fffffff7f60
READ of size 4 at 0x60600000eea0 thread T0
    #0 0xaee082 in trak_Read isomedia/box_code_base.c:7153
    #1 0x6c4ade in gf_isom_box_read isomedia/box_funcs.c:1529
    #2 0x6c4ade in gf_isom_box_parse_ex isomedia/box_funcs.c:208
    #3 0x6c552c in gf_isom_box_array_read_ex isomedia/box_funcs.c:1420
    #4 0xadf844 in moov_Read isomedia/box_code_base.c:3745
    #5 0x6c5e84 in gf_isom_box_read isomedia/box_funcs.c:1529
    #6 0x6c5e84 in gf_isom_box_parse_ex isomedia/box_funcs.c:208
    #7 0x6c66e4 in gf_isom_parse_root_box isomedia/box_funcs.c:42
    #8 0x6dc060 in gf_isom_parse_movie_boxes isomedia/isom_intern.c:206
    #9 0x6decb3 in gf_isom_parse_movie_boxes isomedia/isom_intern.c:194
    #10 0x6decb3 in gf_isom_open_file isomedia/isom_intern.c:615
    #11 0x42f93d in mp4boxMain /home/aota09/yyp/fuzzcompare/test/gpac/test-crash/build_asan_1de1f8d-0.9/applications/mp4box/main.c:4789
    #12 0x7ffff638082f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #13 0x41e278 in _start (/home/aota09/yyp/fuzzcompare/test/gpac/test-crash/bin_asan_1de1f8d-0.9/bin/MP4Box+0x41e278)

0x60600000eea0 is located 0 bytes inside of 56-byte region [0x60600000eea0,0x60600000eed8)
freed by thread T0 here:
    #0 0x7ffff6f022ca in __interceptor_free (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x982ca)
    #1 0x6c46af in gf_isom_box_del isomedia/box_funcs.c:1509

previously allocated by thread T0 here:
    #0 0x7ffff6f02602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0xae636d in stco_New isomedia/box_code_base.c:5616

SUMMARY: AddressSanitizer: heap-use-after-free isomedia/box_code_base.c:7153 trak_Read
Shadow bytes around the buggy address:
  0x0c0c7fff9d80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0c7fff9d90: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0c7fff9da0: fa fa fa fa 00 00 00 00 00 00 00 00 fa fa fa fa
  0x0c0c7fff9db0: 00 00 00 00 00 00 00 fa fa fa fa fa 00 00 00 00
  0x0c0c7fff9dc0: 00 00 00 00 fa fa fa fa 00 00 00 00 00 00 00 04
=>0x0c0c7fff9dd0: fa fa fa fa[fd]fd fd fd fd fd fd fa fa fa fa fa
  0x0c0c7fff9de0: 00 00 00 00 00 00 00 fa fa fa fa fa 00 00 00 00
  0x0c0c7fff9df0: 00 00 00 fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c0c7fff9e00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0c7fff9e10: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0c7fff9e20: 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
  Heap right redzone:      fb
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack partial redzone:   f4
  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
==11412==ABORTING
```
---

Addition: This bug was found with our fuzzer, which is based on AFL. Our fuzzer is developed by Yuanpingyu(cfenicey@gmail.com) 、Yanhao and Marsman1996(lqliuyuwei@outlook.com)


Comments:
Comment by carnil on 2019-12-31 10:00:49+00:00:
CVE-2019-20169 was assigned for this issue.

---

Comment by aureliendavid on 2020-01-08 12:44:04+00:00:
Thanks for the report. 

This should be fixed in master / 0.8.0 as of the above commit. 

It will be included in filters / 0.9.0 in the next merge. 

Feel free to reopen the issue if necessary.

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 ed4ec31ea84a1a94abb17669db33f5c4e7a679cf
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;
}