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

================= Bug Report (1/1) ==================
## Source: Huntr
## URL: https://huntr.dev/bounties/fe778df4-3867-41d6-954b-211c81bccbbf
## Description:
Environment
Distributor ID: Debian
Description:    Debian GNU/Linux bookworm/sid
Version
I checked against the latest release as of 09/28/23 the current master branch at commit c5603fa8de0e7d4460718e28f90989ffdf925494 .
Description
This AddressSanitizer output is indicating an OOB read of invalid heap memory. This exception occurred in the function chnl_box_size at line 12758 in the file src/isomedia/box_code_base.c. This error is caused because the variable ptr->layout.channels_count can be larger than the actual number of layouts in ptr->layout.layouts[] and larger than the maximum number of layouts, which is 64. The struct that defines the maximum layouts is in the file include/gpac/isomedia.h at lines 1464-1486.
src/isomedia/box_code_base.c:lines 12756-12760
for (i=0; i<ptr->layout.channels_count; i++) {
    s->size+=1;
    if (ptr->layout.layouts[i].position==126)
        s->size+=3;
}
Specifically this is the line that causes the asan to trigger when i > 64.
src/isomedia/box_code_base.c:lines 12758
if (ptr->layout.layouts[i].position==126)
POC
./MP4Box -dash 1000 ./POC4_min
POC File
ASAN
[Dasher] No template assigned, using $File$_dash$FS$$Number$
Unknown CICP mapping for channel config 12336/0.0
[Dasher] Segment 1 is empty - pid end of stream 1
=================================================================
==4084326==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x612000001980 at pc 0x7ffff63b21d8 bp 0x7fffffff3dc0 sp 0x7fffffff3db8
READ of size 1 at 0x612000001980 thread T0
    #0 0x7ffff63b21d7 in chnl_box_size /path/gpac/src/isomedia/box_code_base.c:12758:32
    #1 0x7ffff6414b7b in gf_isom_box_size_listing /path/gpac/src/isomedia/box_funcs.c:2113:9
    #2 0x7ffff6414b7b in gf_isom_box_size /path/gpac/src/isomedia/box_funcs.c:2125:6
    #3 0x7ffff6414b7b in gf_isom_box_array_size /path/gpac/src/isomedia/box_funcs.c:540:8
    #4 0x7ffff6414bcd in gf_isom_box_size /path/gpac/src/isomedia/box_funcs.c:2131:7
    #5 0x7ffff6414bcd in gf_isom_box_array_size /path/gpac/src/isomedia/box_funcs.c:540:8
    #6 0x7ffff6414bcd in gf_isom_box_size /path/gpac/src/isomedia/box_funcs.c:2131:7
    #7 0x7ffff6414bcd in gf_isom_box_array_size /path/gpac/src/isomedia/box_funcs.c:540:8
    #8 0x7ffff6414bcd in gf_isom_box_size /path/gpac/src/isomedia/box_funcs.c:2131:7
    #9 0x7ffff6414bcd in gf_isom_box_array_size /path/gpac/src/isomedia/box_funcs.c:540:8
    #10 0x7ffff6414bcd in gf_isom_box_size /path/gpac/src/isomedia/box_funcs.c:2131:7
    #11 0x7ffff6414bcd in gf_isom_box_array_size /path/gpac/src/isomedia/box_funcs.c:540:8
    #12 0x7ffff6414bcd in gf_isom_box_size /path/gpac/src/isomedia/box_funcs.c:2131:7
    #13 0x7ffff6414bcd in gf_isom_box_array_size /path/gpac/src/isomedia/box_funcs.c:540:8
    #14 0x7ffff6414bcd in gf_isom_box_size /path/gpac/src/isomedia/box_funcs.c:2131:7
    #15 0x7ffff6414bcd in gf_isom_box_array_size /path/gpac/src/isomedia/box_funcs.c:540:8
    #16 0x7ffff6415006 in gf_isom_box_size /path/gpac/src/isomedia/box_funcs.c:2131:7
    #17 0x7ffff6491ba9 in WriteMoovAndMeta /path/gpac/src/isomedia/isom_store.c:474:7
    #18 0x7ffff648bb95 in WriteFlat /path/gpac/src/isomedia/isom_store.c:1388:7
    #19 0x7ffff6489044 in WriteToFile /path/gpac/src/isomedia/isom_store.c:2571:9
    #20 0x7ffff650cb0d in gf_isom_finalize_for_fragment /path/gpac/src/isomedia/movie_fragments.c:161:7
    #21 0x7ffff70eefc5 in mp4_mux_initialize_movie /path/gpac/src/filters/mux_isom.c:6017:6
    #22 0x7ffff70eefc5 in mp4_mux_process_fragmented /path/gpac/src/filters/mux_isom.c:6338:7
    #23 0x7ffff70e5ad3 in mp4_mux_process /path/gpac/src/filters/mux_isom.c:7273:14
    #24 0x7ffff6da12a3 in gf_filter_process_task /path/gpac/src/filter_core/filter.c:2971:7
    #25 0x7ffff6d6eceb in gf_fs_thread_proc /path/gpac/src/filter_core/filter_session.c:2076:3
    #26 0x7ffff6d6ce3f in gf_fs_run /path/gpac/src/filter_core/filter_session.c:2376:3
    #27 0x7ffff661c9da in gf_dasher_process /path/gpac/src/media_tools/dash_segmenter.c:1236:6
    #28 0x5555556c26dc in do_dash /path/gpac/applications/mp4box/mp4box.c:4831:15
    #29 0x5555556b3b6e in mp4box_main /path/gpac/applications/mp4box/mp4box.c:6245:7
    #30 0x7ffff58461c9 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #31 0x7ffff5846284 in __libc_start_main csu/../csu/libc-start.c:360:3
    #32 0x5555555dbdd0 in _start (/path/gpac/build3_asan/bin/gcc/MP4Box+0x87dd0) (BuildId: 0637ef26d76ba7d7d9f848431c3188bb4dc7e23e)

0x612000001980 is located 0 bytes to the right of 320-byte region [0x612000001840,0x612000001980)
allocated by thread T0 here:
    #0 0x55555565ec1e in malloc (/path/gpac/build3_asan/bin/gcc/MP4Box+0x10ac1e) (BuildId: 0637ef26d76ba7d7d9f848431c3188bb4dc7e23e)
    #1 0x7ffff63b178a in chnl_box_new /path/gpac/src/isomedia/box_code_base.c:12712:2
    #2 0x7ffff64183d5 in gf_isom_box_new /path/gpac/src/isomedia/box_funcs.c:1896:9
    #3 0x7ffff64183d5 in gf_isom_box_new_parent /path/gpac/src/isomedia/box_funcs.c:2351:14

SUMMARY: AddressSanitizer: heap-buffer-overflow /path/gpac/src/isomedia/box_code_base.c:12758:32 in chnl_box_size
Shadow bytes around the buggy address:
  0x0c247fff82e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c247fff82f0: 00 00 00 00 00 00 00 00 00 00 fa fa fa fa fa fa
  0x0c247fff8300: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c247fff8310: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c247fff8320: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c247fff8330:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c247fff8340: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c247fff8350: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c247fff8360: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c247fff8370: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c247fff8380: 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
==4084326==ABORTING
#Additional potential crash sites
I do not have a POC file to exercise these code sites, but they follow a similar logic.
https://github.com/gpac/gpac/blob/master/src/isomedia/box_dump.c#L6882-L6888
https://github.com/gpac/gpac/blob/master/src/isomedia/box_code_base.c#L12687-L12697
Impact
An OOB read on the heap can potentially cause a crash or information disclosure in some cases. Could be leveraged with other vulnerabilities for a more serious impact.

Relevant Links:
https://github.com/gpac/gpac
https://github.com/gpac/gpac/blob/master/src/isomedia/box_code_base.c#L12758
https://github.com/gpac/gpac/blob/master/include/gpac/isomedia.h#L1464-L1486
https://github.com/gpac/gpac/blob/master/src/isomedia/box_code_base.c#L12756-L12760
https://github.com/gpac/gpac/blob/master/src/isomedia/box_code_base.c#L12758
https://drive.google.com/file/d/1qOR-RLxgksH1MPdDnEDYEBUmhUE5T4eE

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 bdda789f0dcf9a1e2458a0f2b4d05638784e222b
WORKDIR $SRC/gpac
COPY build.sh $SRC/

Additional files: