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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/gpac/gpac/issues/1183
## Description:
Issue: gpac/gpac#1183
Title: AddressSanitizer: memory leaks of dref_New()
State: closed
Created by: Marsman1996
Created at: 2018-12-17 15:51:57+00:00

Issue Body:
Tested in Ubuntu 18.04, 64bit, gcc 7.3.0, gpac (master d1c4bc3)

Compile cmd:
`$ ./configure --extra-cflags="-fsanitize=address,undefined -g" --extra-ldflags="-fsanitize=address,undefined -ldl -g"`
`$ make`

Triggered by 
`$ MP4Box -diso $POC`

POC file: 
https://github.com/Marsman1996/pocs/blob/master/gpac/poc13-leak

ASAN info:
```
ubuntu@ubuntu-virtual-machine:~/Desktop/crashana/gpac/gpac-d1c4bc3/build_asan$ 
./bin/gcc/MP4Box -diso ../../poc13-leak 
[iso file] Unknown box type mo2v
[iso file] Box "mvhd" is invalid in container mo2v
[iso file] Box "href" is invalid in container dinf
[iso file] Unknown box type stb.
[iso file] Box "trik" is invalid in container stb.
[iso file] Read Box type .... (0x01000000) has size 0 but is not at root/file level, skipping
[iso file] Box "stpp" size 15 invalid (read 33)
[iso file] Box "stpp" is invalid in container stb.
[iso file] Unknown box type pts.
[iso file] Box "UNKN" is larger than container box
[iso file] Missing dref box in dinf
[iso file] Box "dinf" size 44 invalid (read 494)
[iso file] Box "trik" is invalid in container minf
[iso file] Read Box type .... (0x01000000) has size 0 but is not at root/file level, skipping
[iso file] Box "stpp" size 15 invalid (read 33)
[iso file] Box "stpp" is invalid in container minf
[iso file] Unknown box type pts.
[iso file] Box "trak" size 128 invalid (read 714)
[iso file] Unknown box type 
[iso file] Incomplete box UNKN
[iso file] Incomplete file while reading for dump - aborting parsing
[iso file] Unknown box type mo2v
[iso file] Box "mvhd" is invalid in container mo2v
[iso file] Box "href" is invalid in container dinf
[iso file] Unknown box type stb.
[iso file] Box "trik" is invalid in container stb.
[iso file] Read Box type .... (0x01000000) has size 0 but is not at root/file level, skipping
[iso file] Box "stpp" size 15 invalid (read 33)
[iso file] Box "stpp" is invalid in container stb.
[iso file] Unknown box type pts.
[iso file] Box "UNKN" is larger than container box
[iso file] Missing dref box in dinf
[iso file] Box "dinf" size 44 invalid (read 494)
[iso file] Box "trik" is invalid in container minf
[iso file] Read Box type .... (0x01000000) has size 0 but is not at root/file level, skipping
[iso file] Box "stpp" size 15 invalid (read 33)
[iso file] Box "stpp" is invalid in container minf
[iso file] Unknown box type pts.
[iso file] Box "trak" size 128 invalid (read 714)
[iso file] Unknown box type 
Truncated file - missing 1936916471 bytes
Error opening file ../../poc13-leak: IsoMedia File is truncated
=================================================================
==93222==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 40 byte(s) in 1 object(s) allocated from:
    #0 0x7fed213dcb50 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb50)
    #1 0x7fed1df0b7fa in dref_New /home/ubuntu/Desktop/crashana/gpac/gpac-d1c4bc3/src/isomedia/box_code_base.c:1012

SUMMARY: AddressSanitizer: 40 byte(s) leaked in 1 allocation(s).
```





Comments:
Comment by aureliendavid on 2019-04-11 11:25:39+00:00:
with a bit of delay: this should now be fixed

---

Comment by carnil on 2019-09-20 21:34:53+00:00:
CVE-2018-21017 as assigned for this issue.

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 1d9c5ef3b86e52ba0ad54e156346ecc3a3de274f
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;
}