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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/gpac/gpac/issues/1337
## Description:
Issue: gpac/gpac#1337
Title: AddressSanitizer: 2 memory leaks of metx_New(), abst_Read()
State: closed
Created by: Clingto
Created at: 2019-11-09 12:00:28+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-memory-leak
```
POC file:  
https://github.com/Clingto/POC/blob/master/gpac-MP4Box/gpac-00dfc93-crashes/POC-new-memory-leak

gdb info:
```C
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[iso file] Box "metx" size 15 (start 89) invalid (read 25)
[iso file] Box "abst" size 24 (start 0) invalid (read 104)
[iso file] Incomplete box abst - start 0 size 24
[iso file] Incomplete file while reading for dump - aborting parsing
[iso file] Box "metx" size 15 (start 89) invalid (read 25)
[iso file] Box "abst" size 24 (start 0) invalid (read 104)
Truncated file - missing 24 bytes
[Inferior 1 (process 6276) exited with code 01]
```
ASAN info:
```C
==26041==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 224 byte(s) in 2 object(s) allocated from:
    #0 0x7ffff6f02602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0xaefada in metx_New isomedia/box_code_base.c:8367

Direct leak of 8 byte(s) in 2 object(s) allocated from:
    #0 0x7ffff6f02602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x792106 in abst_Read isomedia/box_code_adobe.c:95
    #2 0xb62c63  (/bin/MP4Box+0xb62c63)

Indirect leak of 32 byte(s) in 2 object(s) allocated from:
    #0 0x7ffff6f02602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x46c27d in gf_list_new utils/list.c:602

Indirect leak of 16 byte(s) in 4 object(s) allocated from:
    #0 0x7ffff6f02602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x7ffff63eb489 in __strdup (/lib/x86_64-linux-gnu/libc.so.6+0x8b489)
```


SUMMARY: AddressSanitizer: 280 byte(s) leaked in 10 allocation(s).

---

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 09:59:49+00:00:
CVE-2019-20171 was assigned for this issue.

---

Comment by aureliendavid on 2020-01-08 12:45:30+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.

---

Comment by aureliendavid on 2020-01-08 13:26:12+00:00:
I had forgotten something about this one. Should be ok now.

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