Repository navigation
Conversation
ppm_in::open, yuv_out::configure and raw_out::configure multiplied the image width by the number of components and/or bytes per sample in 32-bit arithmetic. The width comes from the PGM/PPM header or from the SIZ marker of the codestream being decoded, so a width above 2^31 wraps the product, a few bytes are allocated, and the following fread() or sample loop writes a full line past the end of the buffer. Do the arithmetic in size_t and keep the sizes in size_t members, as pfm_in, ppm_out and tif_out already do.
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.
ASan,
ojph_compress -i wide.pgm -o out.j2c -num_decomps 0 -tile_size {1073741824,1}on a 278-byte PGM whose header isP5 2147483649 1 65535:ppm_in::opensizestemp_bufasnum_comps * width * bytes_per_sampleinui32, so 1 * 2147483649 * 2 wraps to 2, whilefreadis asked fornum_ele_per_linesamples and copies the rest of the file into the 2-byte buffer. For P6 the same wrap innum_ele_per_linealso makes the de-interleave loop read past the buffer.yuv_out::configureandraw_out::configurehave the sameui32product, withwidthtaken from the SIZ marker of the codestream being decoded. A 1x1 16-bit codestream with Xsiz patched to 0x80000001 (XTsiz 0x40000001) decoded to.rawor.yuvwithojph_expandgets a 2-byte buffer and overflows inraw_out::write(ojph_img_io.cpp:1795) /yuv_out::write(ojph_img_io.cpp:1501).The sizes are now computed in
size_tand stored insize_tmembers, matchingpfm_in,ppm_outandtif_out. With the patch, the PGM case fails with "not enough data in file", and bothojph_expandcases write the full 4294967298-byte line. Release build with-DOJPH_BUILD_TESTS=ON: ctest 138/138 passing. There is no regression test because reaching these widths needs multi-GB allocations, which is not practical in CI.