Skip to content

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 into
OpenPrinting:masterfrom
alepacheco:raster-geometry-checks
Nov 26, 2026
Merged

tillkamppeter merged 4 commits into
OpenPrinting:masterfrom
alepacheco:raster-geometry-checks

Conversation

@alepacheco

Copy link
Copy Markdown
Contributor

Four fixes for raster geometry validation, write_flate() line buffers, PostScript stack growth, and preset resolution calculations, rebased onto current master (2c5d305c):

  1. Range-check the page geometry in ppdRasterInterpretPPD() (ppd/raster-interpret.c, ppd/raster-private.h, new test_ppd_raster): guard float-to-unsigned conversions with ppd_unsigned(), reject out-of-range bitmap dimensions, compute cupsBytesPerLine in 64 bits and cap it at PPD_MAX_BYTES_PER_LINE (16 MiB, matching libcups), and assign cupsWidth, cupsHeight, and cupsBytesPerLine together once all checks pass.
  2. Allocate write_flate()'s line buffers on the heap (ppd/rastertops.c, new test_ppd_rastertops): building on 70236f83 (which capped cupsBytesPerLine at 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 two 6 * line_bytes VLAs still overflow an 8 MiB stack) work safely without per-line malloc/free, and reject invalid cupsBytesPerLine before emitting PostScript output while keeping the guard in write_flate().
  3. Copy the pushed object before growing the PostScript stack (ppd/raster-interpret.c, new case in testppd): 9cf7a3e6 made ppd_copy_stack() and ppd_index_stack() snapshot before calling ppd_push_stack(); this makes ppd_push_stack() itself safe for any caller passing a pointer into st->objs and raises alloc_objs only after realloc() succeeds.
  4. Compute ppdCacheAssignPresets()'s pixel counts in 64 bits (ppd/ppd-cache.c, new Group 12 in test_ppd_cache): avoid 32-bit wrap to zero (division by zero) and misranked quality presets for resolutions such as 65536x65536.

All new tests (test_ppd_raster, test_ppd_rastertops, test_ppd_cache Group 12, and testppd's "copy across stack growth" case) pass under both plain and ASan+UBSan make check, and the rest of the test suite matches master (test_ppd_raster, test_ppd_rastertops, and test_ppd_cache Group 12 fail or crash on unmodified master).

Please preserve commit authorship when merging (e.g. rebase and merge) so the four commits and their tests stay intact.

Comment thread ppd/raster-interpret.c Fixed
Comment thread ppd/raster-interpret.c Fixed
Comment thread ppd/raster-interpret.c Fixed
Comment thread ppd/raster-interpret.c Fixed
Comment thread ppd/raster-interpret.c Fixed
Comment thread ppd/raster-interpret.c Fixed
Comment thread ppd/raster-interpret.c Fixed
Comment thread ppd/raster-interpret.c Fixed
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
alepacheco force-pushed the raster-geometry-checks branch from 859fd75 to 07d9075 Compare November 26, 2026 07:02
@tillkamppeter
tillkamppeter merged commit 3c2138d into OpenPrinting:master Nov 26, 2026
15 checks passed
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.

3 participants