Skip to content

Commit 73dcbcd

Browse files
committed
Improve page header validation in cupsRasterReadHeader.
1 parent 8818d5b commit 73dcbcd

3 files changed

Lines changed: 27 additions & 17 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ v3.0.1 - YYYY-MM-DD
99
- Updated `httpAddrLookup` to return a numeric address when the resolver
1010
returns "localhost" for a non-loopback address.
1111
- Updated `cupsFileGetConf` and `cupsFilePutConf` to escape more characters.
12+
- Updated `cupsRasterReadHeader` to validate more of the page header values.
1213
- Fixed a bug when the `ippFindXxx` and `ippSetXxx` functions were mixed
1314
(Issue #138)
1415

cups/raster-error.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Raster error handling for CUPS.
33
//
4-
// Copyright © 2022 by OpenPrinting.
4+
// Copyright © 2022-2026 by OpenPrinting.
55
// Copyright © 2007-2018 by Apple Inc.
66
// Copyright © 2007 by Easy Software Products.
77
//
@@ -55,7 +55,6 @@ _cupsRasterAddError(const char *f, // I - Printf-style error message
5555
char *temp; // New buffer
5656
size_t size; // Size of buffer
5757

58-
5958
size = (size_t)(buf->end - buf->start + 2 * bytes + 1024);
6059

6160
if (buf->start)
@@ -79,6 +78,9 @@ _cupsRasterAddError(const char *f, // I - Printf-style error message
7978
* Append the message to the end of the current string...
8079
*/
8180

81+
if (buf->current > buf->start)
82+
*(buf->current ++) = ' ';
83+
8284
memcpy(buf->current, s, (size_t)bytes);
8385
buf->current += bytes - 1;
8486
}

cups/raster-stream.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Raster file routines for CUPS.
33
//
4-
// Copyright © 2022-2025 by OpenPrinting.
4+
// Copyright © 2022-2026 by OpenPrinting.
55
// Copyright © 2007-2019 by Apple Inc.
66
// Copyright © 1997-2006 by Easy Software Products.
77
//
@@ -20,6 +20,8 @@
2020
#define _CUPS_MAX_BYTES_PER_LINE (16 * 1024 * 1024)
2121
#define _CUPS_MAX_BITS_PER_COLOR 16
2222
#define _CUPS_MAX_BITS_PER_PIXEL 240
23+
#define _CUPS_MAX_HEIGHT 0x00ffffff
24+
#define _CUPS_MAX_WIDTH 0x00ffffff
2325

2426

2527
//
@@ -353,7 +355,7 @@ cupsRasterInitHeader(
353355
h->cupsWidth = (unsigned)(media->width * xdpi / 2540);
354356
h->cupsHeight = (unsigned)(media->length * ydpi / 2540);
355357

356-
if (h->cupsWidth > 0x00ffffff || h->cupsHeight > 0x00ffffff)
358+
if (h->cupsWidth > _CUPS_MAX_WIDTH || h->cupsHeight > _CUPS_MAX_HEIGHT)
357359
{
358360
_cupsRasterAddError("Raster dimensions too large.");
359361
DEBUG_puts("1cupsRasterInitHeader: Returning false.");
@@ -1669,6 +1671,18 @@ cups_raster_update(cups_raster_t *r) // I - Raster stream
16691671
DEBUG_printf("6cups_raster_update: remaining=%u", r->remaining);
16701672

16711673
// Validate the page header...
1674+
if (r->header.cupsBitsPerColor != 1 && r->header.cupsBitsPerColor != 2 && r->header.cupsBitsPerColor != 4 && r->header.cupsBitsPerColor != 8 && r->header.cupsBitsPerColor != 16)
1675+
{
1676+
_cupsRasterAddError("Invalid bits per color %u.", r->header.cupsBitsPerColor);
1677+
ret = false;
1678+
}
1679+
1680+
if ((r->header.cupsColorOrder != CUPS_ORDER_CHUNKED && r->header.cupsBitsPerPixel != r->header.cupsBitsPerColor) || (r->header.cupsColorOrder == CUPS_ORDER_CHUNKED && r->header.cupsBitsPerPixel != (r->header.cupsBitsPerColor * r->header.cupsNumColors)))
1681+
{
1682+
_cupsRasterAddError("Invalid bits per pixel %u.", r->header.cupsBitsPerPixel);
1683+
ret = false;
1684+
}
1685+
16721686
if (r->header.cupsBytesPerLine == 0)
16731687
{
16741688
_cupsRasterAddError("Invalid raster line length 0.");
@@ -1684,28 +1698,21 @@ cups_raster_update(cups_raster_t *r) // I - Raster stream
16841698
_cupsRasterAddError("Raster line length %u is not a multiple of the pixel size (%d).", r->header.cupsBytesPerLine, r->bpp);
16851699
ret = false;
16861700
}
1687-
1688-
if (r->header.cupsBitsPerColor == 0 || r->header.cupsBitsPerColor > _CUPS_MAX_BITS_PER_COLOR)
1689-
{
1690-
_cupsRasterAddError("Invalid bits per color %u.", r->header.cupsBitsPerColor);
1691-
ret = false;
1692-
}
1693-
1694-
if (r->header.cupsBitsPerPixel == 0 || r->header.cupsBitsPerPixel > _CUPS_MAX_BITS_PER_PIXEL)
1701+
else if (r->header.cupsBytesPerLine != ((r->header.cupsWidth * r->header.cupsBitsPerPixel + 7) / 8))
16951702
{
1696-
_cupsRasterAddError("Invalid bits per pixel %u.", r->header.cupsBitsPerPixel);
1703+
_cupsRasterAddError("Raster line length %u does not match width (%u) and bits per pixel (%u).", r->header.cupsBytesPerLine, r->header.cupsWidth, r->header.cupsBitsPerPixel);
16971704
ret = false;
16981705
}
16991706

1700-
if (r->header.cupsWidth == 0)
1707+
if (r->header.cupsWidth == 0 || r->header.cupsWidth > _CUPS_MAX_WIDTH)
17011708
{
1702-
_cupsRasterAddError("Invalid raster width 0.");
1709+
_cupsRasterAddError("Invalid raster width %u.", r->header.cupsWidth);
17031710
ret = false;
17041711
}
17051712

1706-
if (r->header.cupsHeight == 0)
1713+
if (r->header.cupsHeight == 0 || r->header.cupsHeight > _CUPS_MAX_HEIGHT)
17071714
{
1708-
_cupsRasterAddError("Invalid raster height 0.");
1715+
_cupsRasterAddError("Invalid raster height %u.", r->header.cupsHeight);
17091716
ret = false;
17101717
}
17111718

0 commit comments

Comments
 (0)