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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/gpac/gpac/issues/1587
## Description:
Issue: gpac/gpac#1587
Title: A heap-buffer-overflow in box_dump.c:350
State: closed
Created by: seviezhou
Created at: 2020-09-04 15:05:21+00:00

Issue Body:
## System info

Ubuntu x86_64, gcc (Ubuntu 5.5.0-12ubuntu1), MP4Box (latest master [5a884e](https://github.com/gpac/gpac/commit/5a884e376e1f67b3d40ec91c0b70ab49219fe3cd))

## Configure

CFLAGS="-g -fsanitize=address" LDFLAGS="-fsanitize=address" ./configure --static-mp4box

## Command line

./bin/gcc/MP4Box -disox -ttxt -2 -dump-chap-ogg -dump-cover -drtp -bt -out /dev/null @@

## AddressSanitizer output

```
=================================================================
==66502==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000e054 at pc 0x7f91d0a841d9 bp 0x7ffcd7145d60 sp 0x7ffcd71454d8
READ of size 5 at 0x60200000e054 thread T0
    #0 0x7f91d0a841d8  (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x601d8)
    #1 0x7f91d0a84bbc in __interceptor_vfprintf (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x60bbc)
    #2 0x55ad61358dd0 in gf_fprintf utils/os_file.c:1512
    #3 0x55ad619271c4 in url_box_dump isomedia/box_dump.c:350
    #4 0x55ad61979ed0 in gf_isom_box_dump isomedia/box_funcs.c:1926
    #5 0x55ad6192490a in gf_isom_box_array_dump isomedia/box_dump.c:101
    #6 0x55ad6197a057 in gf_isom_box_dump_done isomedia/box_funcs.c:1933
    #7 0x55ad6192d385 in dref_box_dump isomedia/box_dump.c:863
    #8 0x55ad61979ed0 in gf_isom_box_dump isomedia/box_funcs.c:1926
    #9 0x55ad6192490a in gf_isom_box_array_dump isomedia/box_dump.c:101
    #10 0x55ad6197a057 in gf_isom_box_dump_done isomedia/box_funcs.c:1933
    #11 0x55ad61927135 in dinf_box_dump isomedia/box_dump.c:339
    #12 0x55ad61979ed0 in gf_isom_box_dump isomedia/box_funcs.c:1926
    #13 0x55ad6192490a in gf_isom_box_array_dump isomedia/box_dump.c:101
    #14 0x55ad6197a057 in gf_isom_box_dump_done isomedia/box_funcs.c:1933
    #15 0x55ad61931825 in minf_box_dump isomedia/box_dump.c:1253
    #16 0x55ad61979ed0 in gf_isom_box_dump isomedia/box_funcs.c:1926
    #17 0x55ad6192490a in gf_isom_box_array_dump isomedia/box_dump.c:101
    #18 0x55ad6197a057 in gf_isom_box_dump_done isomedia/box_funcs.c:1933
    #19 0x55ad619323a5 in mdia_box_dump isomedia/box_dump.c:1296
    #20 0x55ad61979ed0 in gf_isom_box_dump isomedia/box_funcs.c:1926
    #21 0x55ad6192490a in gf_isom_box_array_dump isomedia/box_dump.c:101
    #22 0x55ad6197a057 in gf_isom_box_dump_done isomedia/box_funcs.c:1933
    #23 0x55ad61928bb8 in trak_box_dump isomedia/box_dump.c:550
    #24 0x55ad61979ed0 in gf_isom_box_dump isomedia/box_funcs.c:1926
    #25 0x55ad6192490a in gf_isom_box_array_dump isomedia/box_dump.c:101
    #26 0x55ad6197a057 in gf_isom_box_dump_done isomedia/box_funcs.c:1933
    #27 0x55ad61925df0 in moov_box_dump isomedia/box_dump.c:217
    #28 0x55ad61979ed0 in gf_isom_box_dump isomedia/box_funcs.c:1926
    #29 0x55ad61924c92 in gf_isom_dump isomedia/box_dump.c:135
    #30 0x55ad612fef09 in dump_isom_xml /home/seviezhou/gpac/applications/mp4box/filedump.c:1671
    #31 0x55ad612d0754 in mp4boxMain /home/seviezhou/gpac/applications/mp4box/main.c:5550
    #32 0x7f91cfa50b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #33 0x55ad612afbe9 in _start (/home/seviezhou/gpac/bin/gcc/MP4Box+0x280be9)

0x60200000e054 is located 0 bytes to the right of 4-byte region [0x60200000e050,0x60200000e054)
allocated by thread T0 here:
    #0 0x7f91d0abc612 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98612)
    #1 0x55ad62bea6a9 in url_box_read isomedia/box_code_base.c:580

SUMMARY: AddressSanitizer: heap-buffer-overflow ??:0 ??
Shadow bytes around the buggy address:
  0x0c047fff9bb0: fa fa 00 fa fa fa 00 00 fa fa 00 00 fa fa 02 fa
  0x0c047fff9bc0: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 00
  0x0c047fff9bd0: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 00
  0x0c047fff9be0: fa fa 00 00 fa fa 00 05 fa fa 00 00 fa fa 00 00
  0x0c047fff9bf0: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 00
=>0x0c047fff9c00: fa fa 00 fa fa fa 00 00 fa fa[04]fa fa fa 00 00
  0x0c047fff9c10: fa fa 00 00 fa fa 00 05 fa fa 00 00 fa fa 00 00
  0x0c047fff9c20: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 00
  0x0c047fff9c30: fa fa 00 00 fa fa 00 00 fa fa fd fa fa fa 00 fa
  0x0c047fff9c40: fa fa 00 00 fa fa 00 00 fa fa 00 07 fa fa fd fa
  0x0c047fff9c50: fa fa 00 02 fa fa 04 fa fa fa fd fa fa fa 07 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
==66502==ABORTING
```

## POC

[heap-overflow-url_box_dump-box_dump-350.zip](https://github.com/gpac/gpac/files/5175604/heap-overflow-url_box_dump-box_dump-350.zip)



Attachments:
https://github.com/gpac/gpac/files/5175604/heap-overflow-url_box_dump-box_dump-350.zip

Commit References:
5a884e376e1f67b3d40ec91c0b70ab49219fe3cd

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 362fc486b5c0eea04f26793d5623f6a9272bd85a
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;
}