Repository navigation
Range-check raster page geometry; heap line buffers in write_flate(); safe PostScript stack growth; 64-bit preset pixel counts - #125
Merged
tillkamppeter merged 4 commits intoNov 26, 2026
Conversation
ppdRasterInterpretPPD() converted the page size, margins, imaging box and bitmap size from float to unsigned without checking that the values fit, and computed cupsBytesPerLine in 32 bits. A large custom page size (or a large PageSize/HWResolution from setpagedevice code) therefore gave undefined conversions and a cupsBytesPerLine that wrapped around to a value smaller than the line, so filters allocating line buffers from it wrote past their end. - Convert PageSize, Margins, ImagingBoundingBox, and the PageSize and HWResolution values set by setpagedevice with a new ppd_unsigned() helper that gives 0 for out-of-range values: a page size or resolution of 0 is refused by the existing check, and an out-of-range margin or imaging box value is 0 instead of undefined. - Refuse a bitmap width or height that does not fit in 32 bits. - Compute cupsBytesPerLine in 64 bits and refuse lines over 16 MiB (PPD_MAX_BYTES_PER_LINE, in raster-private.h), the same limit libcups applies to raster streams. - Set cupsWidth, cupsHeight and cupsBytesPerLine together once all the checks pass, so a refused header carries no partial bitmap size. Android's copy of libppd has a narrower fix of its own for this and for write_flate() (AOSP commit 6ce8492f, CVE-2026-49927): it refuses a cupsBytesPerLine over 32 bits but leaves the float conversions and allows lines that libcups itself would refuse. Add test_ppd_raster, which checks these headers are refused and that an ordinary page's cupsBytesPerLine holds a whole line.
70236f8 capped header.cupsBytesPerLine at 16 MiB before declaring the two 6 * cupsBytesPerLine stack arrays in write_flate(), so lines above about 700 KB still overflow a default 8 MiB stack (up to 192 MiB at the 16 MiB limit). write_flate() also allocated and freed two new line buffers for every line. - Reject a page whose cupsBytesPerLine is 0 or over PPD_MAX_BYTES_PER_LINE (16 MiB) in ppdFilterRasterToPS() before writing anything for it, and keep the same guard in write_flate(). - Allocate the raster line, the converted line and the output buffer once per page on the heap, checked, and free them on every return, the short read return included. - Deflate the line where it is instead of copying it to a third buffer. Add test_ppd_rastertops, which runs ppdFilterRasterToPS() on 1-bit RGB, 8-bit grayscale and 2000000-pixel-wide 8-bit grayscale pages and checks that the image data inflates to the expected bytes.
ppd_push_stack() reallocated the stack before copying the object it was given, so a caller that passed a pointer into the stack itself copied from freed memory once the push grew the stack. 9cf7a3e makes the two callers that did, ppd_copy_stack() and ppd_index_stack(), copy the object first; this makes ppd_push_stack() itself safe for any caller. Copy the object to a local before the realloc. Also raise alloc_objs only once the realloc has succeeded, so that a failed push leaves the stack as it was. Add a "copy across stack growth" case to testppd's ppdRasterExecPS() tests: "20 copy" on a stack of 21 objects, then index reads across the grown stack.
ppdCacheAssignPresets() ranks each choice that sets a resolution by the ratio of its pixel count (res_x * res_y) to the default resolution's. Both products were unsigned int, so they wrap from 65536x65536 up, to 0 at exactly 2^32 pixels: a PPD with such a default or choice made ppdCacheCreateWithPPD() divide by zero, and other large resolutions were ranked as draft quality. Compute both products and the ratio in unsigned long long. Each factor is already nonzero there, so neither divisor can be 0. test_ppd_cache: new group 12 builds the cache for a 65536dpi Resolution choice, once as a choice and once as the default, and checks which choices the Mono/Draft and Mono/High presets select.
alepacheco
force-pushed
the
raster-geometry-checks
branch
from
November 26, 2026 07:02
859fd75 to
07d9075
Compare
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.
Four fixes for raster geometry validation,
write_flate()line buffers, PostScript stack growth, and preset resolution calculations, rebased onto currentmaster(2c5d305c):ppdRasterInterpretPPD()(ppd/raster-interpret.c,ppd/raster-private.h, newtest_ppd_raster): guard float-to-unsigned conversions withppd_unsigned(), reject out-of-range bitmap dimensions, computecupsBytesPerLinein 64 bits and cap it atPPD_MAX_BYTES_PER_LINE(16 MiB, matching libcups), and assigncupsWidth,cupsHeight, andcupsBytesPerLinetogether once all checks pass.write_flate()'s line buffers on the heap (ppd/rastertops.c, newtest_ppd_rastertops): building on70236f83(which cappedcupsBytesPerLineat 16 MiB before the stack VLAs), move the line buffers off the stack and allocate them once per page on the heap so valid wide lines (e.g. 2,000,000 bytes, where two6 * line_bytesVLAs still overflow an 8 MiB stack) work safely without per-linemalloc/free, and reject invalidcupsBytesPerLinebefore emitting PostScript output while keeping the guard inwrite_flate().ppd/raster-interpret.c, new case intestppd):9cf7a3e6madeppd_copy_stack()andppd_index_stack()snapshot before callingppd_push_stack(); this makesppd_push_stack()itself safe for any caller passing a pointer intost->objsand raisesalloc_objsonly afterrealloc()succeeds.ppdCacheAssignPresets()'s pixel counts in 64 bits (ppd/ppd-cache.c, new Group 12 intest_ppd_cache): avoid 32-bit wrap to zero (division by zero) and misranked quality presets for resolutions such as65536x65536.All new tests (
test_ppd_raster,test_ppd_rastertops,test_ppd_cacheGroup 12, andtestppd's"copy across stack growth"case) pass under both plain and ASan+UBSanmake check, and the rest of the test suite matchesmaster(test_ppd_raster,test_ppd_rastertops, andtest_ppd_cacheGroup 12 fail or crash on unmodifiedmaster).Please preserve commit authorship when merging (e.g. rebase and merge) so the four commits and their tests stay intact.