Project: openjpeg (uclouvain/openjpeg), language: c++
Vulnerability instance: openjpeg.cve-2024-56827
Sanitizer: address

================= Bug Report (1/2) ==================
## Source: Red Hat Bugzilla
## URL: https://bugzilla.redhat.com/show_bug.cgi?id=2335174
## Description:
Bugzilla ID: 2335174
Title: CVE-2024-56827 openjpeg: heap buffer overflow in lib/openjp2/j2k.c

Description
OSIDB Bzimport 2025-01-01 17:04:49 UTC
This bug is triggered when we use opj_decompress with the -t option and its argument set to 1. The latest version v2.5.2 also has this vulnerability.

Reproducible: Always

Steps to Reproduce: see https://github.com/uclouvain/openjpeg/issues/1564

References:
https://github.com/uclouvain/openjpeg/issues/1564
https://github.com/uclouvain/openjpeg/commit/e492644fbded4c820ca55b5e50e598d346e850e8


================= Bug Report (2/2) ==================
## Source: GitHub Issue
## URL: https://github.com/uclouvain/openjpeg/issues/1564
## Description:
Issue: uclouvain/openjpeg#1564
Title: heap-buffer-overflow at lib/openjp2/j2k.c:8460:84 in opj_j2k_add_tlmarker in openjpeg/opj_decompress
State: closed
Created by: Frank-Z7
Created at: 2024-11-25 17:58:45+00:00

Issue Body:
### Description

Dear developers, 

We found the following heap buffer overflow bug on openjpeg, please confirm.

This bug is triggered when we use opj_decompress with the -t option and its argument set to 1.

The latest version v2.5.2 also has this vulnerability.

### Version

```shell
# ./bin/opj_decompress -h
This is the opj_decompress utility from the OpenJPEG project.
It decompresses JPEG 2000 codestreams to various image formats.
It has been compiled against openjp2 library v2.5.2.
```

### Reproduction

```shell
git clone https://github.com/uclouvain/openjpeg.git
cd openjpeg
cmake . -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_C_FLAGS="-fsanitize=address" \
-DCMAKE_CXX_FLAGS="-fsanitize=address"
make -j20

./bin/opj_decompress -i poc2openjpeg -o tmp.pnm -t 1
```

### ASAN Log

./bin/opj_decompress -i poc2openjpeg -o tmp.pnm -t 1

```shell
=================================================================
==4004218==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6030000016a8 at pc 0x7f8a2d0491f6 bp 0x7fff212eadd0 sp 0x7fff212eadc8
WRITE of size 8 at 0x6030000016a8 thread T0
    #0 0x7f8a2d0491f5 in opj_j2k_add_tlmarker /openjpeg/src/lib/openjp2/j2k.c:8460:84
    #1 0x7f8a2d045813 in opj_j2k_read_tile_header /openjpeg/src/lib/openjp2/j2k.c:9862:30
    #2 0x7f8a2d08414b in opj_j2k_decode_one_tile /openjpeg/src/lib/openjp2/j2k.c:12229:15
    #3 0x7f8a2d042668 in opj_j2k_exec /openjpeg/src/lib/openjp2/j2k.c:9177:33
    #4 0x7f8a2d059aa2 in opj_j2k_get_tile /openjpeg/src/lib/openjp2/j2k.c:12524:11
    #5 0x7f8a2d0b5777 in opj_get_decoded_tile /openjpeg/src/lib/openjp2/openjpeg.c:628:16
    #6 0x55fcd4fbe682 in main /openjpeg/src/bin/jp2/opj_decompress.c:1601:18
    #7 0x7f8a2cc9bd8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId: 490fef8403240c91833978d494d39e537409b92e)
    #8 0x7f8a2cc9be3f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e3f) (BuildId: 490fef8403240c91833978d494d39e537409b92e)
    #9 0x55fcd4efa544 in _start (/openjpeg/bin/opj_decompress+0x27544) (BuildId: e80f8792cc97998ba478d7e96d59085d10b53f68)

Address 0x6030000016a8 is a wild pointer inside of access range of size 0x000000000008.
SUMMARY: AddressSanitizer: heap-buffer-overflow /openjpeg/src/lib/openjp2/j2k.c:8460:84 in opj_j2k_add_tlmarker
Shadow bytes around the buggy address:
  0x0c067fff8280: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8290: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff82a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff82b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff82c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x0c067fff82d0: fa fa fa fa fa[fa]fa fa fa fa fa fa fa fa fa fa
  0x0c067fff82e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff82f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8300: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8310: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8320: 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
==4004218==ABORTING
```

### PoC

poc2openjpeg: https://github.com/Frank-Z7/z-vulnerabilitys/blob/main/poc2openjpeg

### Reference

https://github.com/uclouvain/openjpeg

### Environment

```
ubuntu:22.04
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
clang version 14.0.0-1ubuntu1.1
afl-fuzz++4.22a
```

*Thanks* *for* *your* *time*!


Commit References:
e80f8792cc97998ba478d7e96d59085d10b53f68
490fef8403240c91833978d494d39e537409b92e

Build script:
#!/bin/bash -eu
# Minimized build script with only core build commands
set -eu
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make clean -s -j$(nproc)
make -j$(nproc) -s

Dockerfile:
FROM hwiwonlee/secb.base:latest
RUN apt-get update && apt-get install -y make cmake g++
RUN git clone https://github.com/uclouvain/openjpeg openjpeg
RUN git -C openjpeg checkout 4ec0aca4d733a852186bbce82e58043095a372d4
WORKDIR $SRC/openjpeg
COPY build.sh $SRC/

Additional files: