Skip to content

Conversation

@xmoezzz
Copy link

@xmoezzz xmoezzz commented Dec 3, 2025

Reproduce

  1. Build jasper with UBSan enabled, for example:

    cmake -S ./jasper -B build-ubsan -DCMAKE_BUILD_TYPE=RelWithDebInfo
    -DCMAKE_C_COMPILER=clang
    -DCMAKE_C_FLAGS="-fsanitize=undefined -fno-omit-frame-pointer"
    -DCMAKE_CXX_FLAGS="-fsanitize=undefined -fno-omit-frame-pointer"
    cmake --build build-ubsan -j

  • The 'jasper' binary will be in build-ubsan/src/app/jasper
  1. Run jasper on the malformed JPEG 2000 codestream (attached as crash.bin.zip):
    crash.bin.zip

    build-ubsan/src/app/jasper
    --input crash.bin
    --output /dev/null
    --input-format jpc
    --output-format jpc

  2. UBSan reports undefined behaviour in jpc_undo_roi():

/data/jasper/src/libjasper/jpc/jpc_dec.c:2088:13: runtime error: shift exponent 32 is too large for 32-bit type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /data/jasper/src/libjasper/jpc/jpc_dec.c:2088:13
/data/jasper/src/libjasper/jpc/jpc_dec.c:2106:9: runtime error: shift exponent -32 is negative
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /data/jasper/src/libjasper/jpc/jpc_dec.c:2106:9
/data/jasper/src/libjasper/jpc/jpc_dec.c:2107:40: runtime error: shift exponent 4294967295 is too large for 64-bit type 'uint_fast32_t' (aka 'unsigned long')
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /data/jasper/src/libjasper/jpc/jpc_dec.c:2107:40

Fix

  1. After applying the patch, we can run the test again:

warning: ignoring unknown marker segment (0xff50)
warning: limiting ROI shift from 32 to 0 (bitstream is probably corrupt)
warning: forcing negative background shift to zero (bitstream is probably corrupt)
warning: limiting ROI shift from 32 to 0 (bitstream is probably corrupt)
warning: forcing negative background shift to zero (bitstream is probably corrupt)
warning: limiting ROI shift from 32 to 0 (bitstream is probably corrupt)
warning: forcing negative background shift to zero (bitstream is probably corrupt)
warning: limiting ROI shift from 32 to 0 (bitstream is probably corrupt)
warning: forcing negative background shift to zero (bitstream is probably corrupt)
warning: limiting ROI shift from 32 to 0 (bitstream is probably corrupt)
warning: forcing negative background shift to zero (bitstream is probably corrupt)
warning: limiting ROI shift from 32 to 0 (bitstream is probably corrupt)
warning: forcing negative background shift to zero (bitstream is probably corrupt)
warning: limiting ROI shift from 32 to 0 (bitstream is probably corrupt)
warning: forcing negative background shift to zero (bitstream is probably corrupt)

  1. For a normal jpc file:
    build-ubsan/src/app/jasper
    --input ./is_codestream.jp2
    --output /dev/null
    --input-format jpc
    --output-format jp2

output:
warning: skipping unknown tag type
warning: skipping unknown tag type
warning: skipping unknown tag type

test file: https://github.com/openpreserve/jpylyzer-test-files/blob/master/files/is_codestream.jp2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant