Reject mismatched frame size in libjpeg encoder - #249
Draft
rootkiller6788 wants to merge 1 commit into
Draft
Conversation
The extras JPEG encoder reads ppf.info.ysize rows and ppf.info.xsize columns from the frame's pixel buffer, but the shared VerifyImageSize check for frame size matching the image size is commented out. A PackedPixelFile whose info dimensions are larger than the frame buffer causes an out-of-bounds read in the encoder copy loops. Add an explicit frame size check in JPEGEncoder::Encode and a test that mirrors the existing jpegli encoder mismatch test.
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.
What
The extras JPEG encoder (
lib/extras/enc/jpg.cc) encodes frames with the loopfor (size_t y = 0; y < info.ysize; ++y)readingpixels + y * image.stride, and per-pixelfor (size_t x = 0; x < info.xsize; ++x)reads. The only gate before this isEncoder::VerifyPackedImage→Encoder::VerifyImageSize, whoseimage.xsize != info.xsize || image.ysize != info.ysizecheck is commented out (see the TODO atlib/extras/enc/encode.cc).When a
PackedPixelFilecarriesinfo.xsize/info.ysizelarger than the frame'scolor.xsize/color.ysize, the encoder reads past the pixel buffer — the same out-of-bounds read that PR #213 fixed for the jpegli encoder (lib/extras/enc/jpegli.cc), which this encoder path was missing.Why it matters
Encoder::FromExtension(".jpg")is the public route used bydjpegli(-o out.jpg) and by the extras encoder framework. An inconsistentPackedPixelFilecurrently leads to a heap-buffer-overflow read (crash under ASan/MSan) instead of a clean error.Fix
Add an explicit frame-size check in
JPEGEncoder::Encode, returningJPEGLI_FAILURE("Frame size does not match image size.")on mismatch, plus a regression testJpegliLibjpegEncodeRejectsMismatchedFrameSizethat mirrors the existingJpegliEncodeRejectsMismatchedFrameSizetest for the jpegli encoder.Verified against real code:
EncodeWithLibJpeg/EncodeWithSJpegiterateinfo.ysize/info.xsizeover the frame buffer, and no other guard rejects the mismatch.