Conversation
The precinct-size products hsamp << rpx, vsamp << rpy, and hsamp << r in jpc_pi_nextrpcl, jpc_pi_nextpcrl, and jpc_pi_nextcprl can overflow uint_fast32_t to zero because the component sampling factors (up to 255) contribute up to 8 additional bits that the existing exponent check does not account for. A zero divisor then causes a SIGFPE crash when decoding a crafted codestream. Compute these products with uint_fast64_t instead, and compute the xstep/ystep products in 64 bits with saturation for the same reason. Fixes jasper-software#420.
This branch has not been deployed
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.
Summary
Fixes #420 — a crafted JPC codestream causes a division by zero (SIGFPE) in the packet iterators.
Root cause
In
jpc_pi_nextrpcl,jpc_pi_nextpcrl, andjpc_pi_nextcprl, the precinct-size productspi->picomp->hsamp << rpx/pi->picomp->vsamp << rpy(divisors in the modulo tests)pi->picomp->hsamp << r/pi->picomp->vsamp << r(divisors inJPC_CEILDIV)trx0 << r/try0 << rand1 << rpx/1 << rpy(modulo operands)are all computed in
uint_fast32_t. The existing guard only boundsprecinct exponent + numrlvls; it does not account for the up to 8 extra bits contributed by the component sampling factorshsamp/vsamp(range 1..255 from the SIZ marker). With e.g.hsamp = 96andrpx = 27,96 << 27wraps to 0 modulo 2^32, and the subsequent% (hsamp << rpx)divides by zero — SIGFPE on i386 (and a UBSan report elsewhere).The same overflow affects the
xstep/ystepproductshsamp * (1 << k)in all three functions: the wrapped value can be zero (rejected by the existing check) or silently wrong.Fix
uint_fast64_t, preserving the mathematical semantics for every input — no valid codestreams are rejected.xstep/ystepinuint_fast64_tand saturate toUINT_FAST32_MAX; a step that exceeds the representable range behaves as before (a single iteration over the tile).Testing
hsamp=96,rpx=27→hsamp << rpx == 0→runtime error: division by zerounder UBSan).data/test(40 files): no new sanitizer reports; the 4 remaining hits are pre-existing issues injpc_dec.c/jas_image.con files from thebad/corpus, unrelated to this change.