Skip to content

guard jpeg gray fast path against subsampled first component#672

Open
metsw24-max wants to merge 1 commit into
ermig1979:masterfrom
metsw24-max:jpeg-gray-subsampled-first-component
Open

guard jpeg gray fast path against subsampled first component#672
metsw24-max wants to merge 1 commit into
ermig1979:masterfrom
metsw24-max:jpeg-gray-subsampled-first-component

Conversation

@metsw24-max

Copy link
Copy Markdown
Contributor

Grayscale fast path over-reads a subsampled first component.
ImageJpegLoader::FromStream has a fast path, gated by CanCopyGray, that returns a grayscale image by copying the first component plane straight into the output. It copies img_x by img_y bytes out of img_comp[0].data, which JpegProcessFrameHeader sizes only to that component's own w2 by h2 (img_mcu_x * h * 8 by img_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 w2 by h2 smaller than the image. Decoding it as SimdPixelFormatGray8 then 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.
CanCopyGray now also requires img_comp[0].w2 >= img_x and img_comp[0].h2 >= img_y, so a subsampled first component falls through to the general JpegToRgba path that resamples every plane to full resolution before converting. This matches the neighbouring IsYuv420 and IsYuv444 fast 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.

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.

1 participant