Conversation
1) ras_dec.c:352 - the bit accumulator z is a signed int; for depth >= 24 the (z << 8) shift overflows into the sign bit (UB). z is now uint_fast32_t. 2) ras_dec.c:206 - hdr.length = RAS_ROWSIZE(&hdr) * hdr.height overflows 32-bit int_fast32_t for large width*depth*height. RAS_ROWSIZE now computes in int_fast64_t, as does the pad computation which multiplies width * depth. 3) ras_dec.c:412 - numcolors = 1 << hdr->depth invokes UB for depth = 32, and the bound check ran after the UB. Compare the bound in uint_fast64_t before materializing numcolors. Fixes jasper-software#413.
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 #413 — three UBSan sites reachable from
jas_image_decodeon crafted RAS files:ras_dec.c:352— the bit accumulatorzisint;(z << 8)overflows into the sign bit fordepth >= 24.zis nowuint_fast32_t(consumers mask withRAS_ONESanyway).ras_dec.c:206—hdr.length = RAS_ROWSIZE(&hdr) * hdr.heightoverflowsint_fast32_t(32-bit on i386/Darwin) for largewidth * depth * height, e.g.width=8192, depth=32, height=65536→32768 * 65536= 2^31.RAS_ROWSIZEnow computes inint_fast64_t, as does thepadcomputation which also multiplieswidth * depth.ras_dec.c:412—numcolors = 1 << hdr->depthis UB fordepth = 32(checked only after the UB). The bound check now runs onJAS_POW2_X(uint_fast64_t, hdr->depth)beforenumcolorsis materialized;depth <= 8is required anyway sinceRAS_CMAP_MAXSIZis 256.Testing
No PoC was attached to the issue, so I generated minimal reproducers (32-byte RAS headers + data bytes):
depth=32, maptype=EQUALRGB→ras_dec.c:412:17: runtime error: shift exponent 32 is too largetype=OLD, 8192x65536x32→ras_dec.c:206:34: runtime error: signed integer overflow: 32768 * 65536depth=32, data byte 0x80→ras_dec.c:352:12: runtime error: left shift of 8388608 by 8All three report cleanly after the patch; malformed files are rejected gracefully.