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

================= Bug Report (1/2) ==================
## Source: Red Hat Bugzilla
## URL: https://bugzilla.redhat.com/show_bug.cgi?id=2335172
## Description:
Bugzilla ID: 2335172
Title: CVE-2024-56826 openjpeg: heap buffer overflow in bin/common/color.c

Description
OSIDB Bzimport 2025-01-01 16:44:34 UTC
This bug is triggered when we use opj_decompress with the -r option and its argument set to 2. Version v2.5.2 also has this vulnerability.

Reproducible: Always

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

References: 
https://github.com/uclouvain/openjpeg/issues/1563
https://github.com/uclouvain/openjpeg/commit/e492644fbded4c820ca55b5e50e598d346e850e8
https://bugzilla.redhat.com/show_bug.cgi?id=2333954


================= Bug Report (2/2) ==================
## Source: GitHub Issue
## URL: https://github.com/uclouvain/openjpeg/issues/1563
## Description:
Issue: uclouvain/openjpeg#1563
Title: heap-buffer-overflow at bin/common/color.c:215:42 in sycc422_to_rgb in openjpeg/opj_decompress
State: closed
Created by: Frank-Z7
Created at: 2024-11-25 17:39:31+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 -r option and its argument set to 2.

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 poc1openjpeg -o tmp.pnm -r 2
```

### ASAN Log

./bin/opj_decompress -i poc1openjpeg -o tmp.pnm -r 2

```shell
=================================================================
==949515==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6100000004f0 at pc 0x5592639a0b5b bp 0x7ffcda978c30 sp 0x7ffcda978c28
READ of size 4 at 0x6100000004f0 thread T0
    #0 0x5592639a0b5a in sycc422_to_rgb /openjpeg/src/bin/common/color.c:215:42
    #1 0x55926399dbdb in color_sycc_to_rgb /openjpeg/src/bin/common/color.c:431:9
    #2 0x559263969df8 in main /openjpeg/src/bin/jp2/opj_decompress.c:1629:13
    #3 0x7f97ebdc3d8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId: 490fef8403240c91833978d494d39e537409b92e)
    #4 0x7f97ebdc3e3f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e3f) (BuildId: 490fef8403240c91833978d494d39e537409b92e)
    #5 0x5592638a5544 in _start (/openjpeg/bin/opj_decompress+0x27544) (BuildId: e80f8792cc97998ba478d7e96d59085d10b53f68)

0x6100000004f0 is located 0 bytes to the right of 176-byte region [0x610000000440,0x6100000004f0)
allocated by thread T0 here:
    #0 0x559263928f07 in posix_memalign (/openjpeg/bin/opj_decompress+0xaaf07) (BuildId: e80f8792cc97998ba478d7e96d59085d10b53f68)
    #1 0x7f97ec2d4ab5 in opj_aligned_alloc_n /openjpeg/src/lib/openjp2/opj_malloc.c:61:9
    #2 0x7f97ec2d4939 in opj_aligned_malloc /openjpeg/src/lib/openjp2/opj_malloc.c:209:12
    #3 0x7f97ec1dfa64 in opj_image_data_alloc /openjpeg/src/lib/openjp2/openjpeg.c:1135:17
    #4 0x7f97ec2b5c5a in opj_alloc_tile_component_data /openjpeg/src/lib/openjp2/tcd.c:765:39
    #5 0x7f97ec2c0401 in opj_tcd_decode_tile /openjpeg/src/lib/openjp2/tcd.c:1629:18
    #6 0x7f97ec174011 in opj_j2k_decode_tile /openjpeg/src/lib/openjp2/j2k.c:10132:11
    #7 0x7f97ec1a8993 in opj_j2k_decode_tiles /openjpeg/src/lib/openjp2/j2k.c:12082:15
    #8 0x7f97ec16a668 in opj_j2k_exec /openjpeg/src/lib/openjp2/j2k.c:9177:33
    #9 0x7f97ec17e960 in opj_j2k_decode /openjpeg/src/lib/openjp2/j2k.c:12411:11
    #10 0x7f97ec1dd200 in opj_decode /openjpeg/src/lib/openjp2/openjpeg.c:526:16
    #11 0x5592639692b5 in main /openjpeg/src/bin/jp2/opj_decompress.c:1582:19
    #12 0x7f97ebdc3d8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId: 490fef8403240c91833978d494d39e537409b92e)

SUMMARY: AddressSanitizer: heap-buffer-overflow /openjpeg/src/bin/common/color.c:215:42 in sycc422_to_rgb
Shadow bytes around the buggy address:
  0x0c207fff8040: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c207fff8050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c207fff8060: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c207fff8070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c207fff8080: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
=>0x0c207fff8090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00[fa]fa
  0x0c207fff80a0: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c207fff80b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 fa fa
  0x0c207fff80c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c207fff80d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c207fff80e0: 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
==949515==ABORTING
```

### PoC

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

### 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: