Skip to content

opj_compress: fix stack buffer overflow in -OutFor option parsing (#1642) - #1653

Open
steps-re wants to merge 1 commit into
uclouvain:masterfrom
steps-re:fix/opj-compress-outfor-overflow
Open

opj_compress: fix stack buffer overflow in -OutFor option parsing (#1642)#1653
steps-re wants to merge 1 commit into
uclouvain:masterfrom
steps-re:fix/opj-compress-outfor-overflow

Conversation

@steps-re

@steps-re steps-re commented Jul 7, 2026

Copy link
Copy Markdown

Fixes #1642.

Bug

parse_cmdline_encoder() copies the user-controlled -OutFor argument into a fixed 50-byte stack buffer with an unbounded sprintf:

char outformat[50];
char *of = opj_optarg;
sprintf(outformat, ".%s", of);

Any -OutFor value longer than 48 chars overflows outformat, corrupting adjacent stack variables. The value comes straight from the command line, so this is a straightforward stack buffer overflow (src/bin/jp2/opj_compress.c:710-712), still present on HEAD.

Fix

Reject over-long values through the existing "Unknown output format" error path (using the file's existing opj_strnlen_s), and use a bounded snprintf as defense-in-depth. Valid values (j2k, j2c, jp2) are unaffected.

Verification

  • Built with -fsanitize=address. Before: a 200-char -OutForAddressSanitizer: stack-buffer-overflow WRITE of size 200 ... parse_cmdline_encoder opj_compress.c:712. After: same input → clean Unknown output format message, exit 1, ASan clean; clean at the 48-char boundary too.
  • Valid -OutFor jp2 / -OutFor j2k still produce correct output files (verified JP2 signature box / j2k output), ASan-clean.

…louvain#1642)

parse_cmdline_encoder() copied the user-controlled -OutFor argument into a
fixed 50-byte stack buffer with an unbounded sprintf(outformat, ".%s", of),
overflowing the buffer for any value longer than 48 characters.

Guard the length with opj_strnlen_s() and reject over-long values with the
existing 'Unknown output format' error, and use a bounded snprintf() as
defense-in-depth. Valid values (j2k, j2c, jp2) are unaffected.

Signed-off-by: Mike German <mike@stepsventures.com>
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.

Stack buffer overflow in opj_compress -OutFor option parsing

1 participant