opj_compress: fix stack buffer overflow in -OutFor option parsing (#1642) - #1653
Open
steps-re wants to merge 1 commit into
Open
opj_compress: fix stack buffer overflow in -OutFor option parsing (#1642)#1653steps-re wants to merge 1 commit into
steps-re wants to merge 1 commit into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1642.
Bug
parse_cmdline_encoder()copies the user-controlled-OutForargument into a fixed 50-byte stack buffer with an unboundedsprintf:Any
-OutForvalue longer than 48 chars overflowsoutformat, 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 boundedsnprintfas defense-in-depth. Valid values (j2k,j2c,jp2) are unaffected.Verification
-fsanitize=address. Before: a 200-char-OutFor→AddressSanitizer: stack-buffer-overflow WRITE of size 200 ... parse_cmdline_encoder opj_compress.c:712. After: same input → cleanUnknown output formatmessage, exit 1, ASan clean; clean at the 48-char boundary too.-OutFor jp2/-OutFor j2kstill produce correct output files (verified JP2 signature box / j2k output), ASan-clean.