guard jpeg gray fast path against subsampled first component#672
Open
metsw24-max wants to merge 1 commit into
Open
guard jpeg gray fast path against subsampled first component#672metsw24-max wants to merge 1 commit into
metsw24-max wants to merge 1 commit into
Conversation
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.
Grayscale fast path over-reads a subsampled first component.
ImageJpegLoader::FromStreamhas a fast path, gated byCanCopyGray, that returns a grayscale image by copying the first component plane straight into the output. It copiesimg_xbyimg_ybytes out ofimg_comp[0].data, whichJpegProcessFrameHeadersizes only to that component's ownw2byh2(img_mcu_x * h * 8byimg_mcu_y * v * 8).The frame header never requires the first component to be full resolution.
It checks only that each sampling factor divides the maximum, so a JPEG whose first component is subsampled (for example a JFIF three-component image with component 0 at 1x1 and component 1 at 2x2, or any four-component image) passes validation while leaving
w2byh2smaller than the image. Decoding it asSimdPixelFormatGray8then reads past the end of the component buffer and copies the out-of-bounds heap bytes into the returned image.Fix: gate the fast path on the first component covering the image.
CanCopyGraynow also requiresimg_comp[0].w2 >= img_xandimg_comp[0].h2 >= img_y, so a subsampled first component falls through to the generalJpegToRgbapath that resamples every plane to full resolution before converting. This matches the neighbouringIsYuv420andIsYuv444fast paths, which already gate on plane size, and it leaves single-component and full-resolution images on the fast path so decode performance is unchanged.