Skip to content

Commit 360ccf5

Browse files
committed
Improve page header validation in cupsRasterReadHeader (Issue #1501)
1 parent 265468f commit 360ccf5

2 files changed

Lines changed: 26 additions & 21 deletions

File tree

cups/raster-error.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
/*
22
* Raster error handling for CUPS.
33
*
4-
* Copyright © 2020-2024 by OpenPrinting.
4+
* Copyright © 2020-2026 by OpenPrinting.
55
* Copyright © 2007-2018 by Apple Inc.
66
* Copyright © 2007 by Easy Software Products.
77
*
88
* Licensed under Apache License v2.0. See the file "LICENSE" for more
99
* information.
1010
*/
1111

12-
/*
13-
* Include necessary headers...
14-
*/
15-
1612
#include "cups-private.h"
1713
#include "raster-private.h"
1814
#include "debug-internal.h"
@@ -60,7 +56,6 @@ _cupsRasterAddError(const char *f, /* I - Printf-style error message */
6056
char *temp; /* New buffer */
6157
size_t size; /* Size of buffer */
6258

63-
6459
size = (size_t)(buf->end - buf->start + 2 * bytes + 1024);
6560

6661
if (buf->start)
@@ -84,6 +79,9 @@ _cupsRasterAddError(const char *f, /* I - Printf-style error message */
8479
* Append the message to the end of the current string...
8580
*/
8681

82+
if (buf->current > buf->start)
83+
*(buf->current ++) = ' ';
84+
8785
memcpy(buf->current, s, (size_t)bytes);
8886
buf->current += bytes - 1;
8987
}

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 © 2020-2025 by OpenPrinting.
4+
// Copyright © 2020-2026 by OpenPrinting.
55
// Copyright © 2007-2019 by Apple Inc.
66
// Copyright © 1997-2006 by Easy Software Products.
77
//
@@ -23,6 +23,8 @@
2323
#define _CUPS_MAX_BYTES_PER_LINE (16 * 1024 * 1024)
2424
#define _CUPS_MAX_BITS_PER_COLOR 16
2525
#define _CUPS_MAX_BITS_PER_PIXEL 240
26+
#define _CUPS_MAX_HEIGHT 0x00ffffff
27+
#define _CUPS_MAX_WIDTH 0x00ffffff
2628

2729

2830
//
@@ -357,7 +359,7 @@ cupsRasterInitHeader(
357359
h->cupsWidth = (unsigned)(media->width * xdpi / 2540);
358360
h->cupsHeight = (unsigned)(media->length * ydpi / 2540);
359361

360-
if (h->cupsWidth > 0x00ffffff || h->cupsHeight > 0x00ffffff)
362+
if (h->cupsWidth > _CUPS_MAX_WIDTH || h->cupsHeight > _CUPS_MAX_HEIGHT)
361363
{
362364
_cupsRasterAddError("Raster dimensions too large.");
363365
return (false);
@@ -1825,6 +1827,18 @@ cups_raster_update(cups_raster_t *r) // I - Raster stream
18251827
r->remaining = r->header.cupsHeight;
18261828

18271829
// Validate the page header...
1830+
if (r->header.cupsBitsPerColor != 1 && r->header.cupsBitsPerColor != 2 && r->header.cupsBitsPerColor != 4 && r->header.cupsBitsPerColor != 8 && r->header.cupsBitsPerColor != 16)
1831+
{
1832+
_cupsRasterAddError("Invalid bits per color %u.", r->header.cupsBitsPerColor);
1833+
ret = 0;
1834+
}
1835+
1836+
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)))
1837+
{
1838+
_cupsRasterAddError("Invalid bits per pixel %u.", r->header.cupsBitsPerPixel);
1839+
ret = 0;
1840+
}
1841+
18281842
if (r->header.cupsBytesPerLine == 0)
18291843
{
18301844
_cupsRasterAddError("Invalid raster line length 0.");
@@ -1840,28 +1854,21 @@ cups_raster_update(cups_raster_t *r) // I - Raster stream
18401854
_cupsRasterAddError("Raster line length %u is not a multiple of the pixel size (%d).", r->header.cupsBytesPerLine, r->bpp);
18411855
ret = 0;
18421856
}
1843-
1844-
if (r->header.cupsBitsPerColor == 0 || r->header.cupsBitsPerColor > _CUPS_MAX_BITS_PER_COLOR)
1857+
else if (r->header.cupsBytesPerLine != ((r->header.cupsWidth * r->header.cupsBitsPerPixel + 7) / 8))
18451858
{
1846-
_cupsRasterAddError("Invalid bits per color %u.", r->header.cupsBitsPerColor);
1847-
ret = 0;
1848-
}
1849-
1850-
if (r->header.cupsBitsPerPixel == 0 || r->header.cupsBitsPerPixel > _CUPS_MAX_BITS_PER_PIXEL)
1851-
{
1852-
_cupsRasterAddError("Invalid bits per pixel %u.", r->header.cupsBitsPerPixel);
1859+
_cupsRasterAddError("Raster line length %u does not match width (%u) and bits per pixel (%u).", r->header.cupsBytesPerLine, r->header.cupsWidth, r->header.cupsBitsPerPixel);
18531860
ret = 0;
18541861
}
18551862

1856-
if (r->header.cupsWidth == 0)
1863+
if (r->header.cupsWidth == 0 || r->header.cupsWidth > _CUPS_MAX_WIDTH)
18571864
{
1858-
_cupsRasterAddError("Invalid raster width 0.");
1865+
_cupsRasterAddError("Invalid raster width %u.", r->header.cupsWidth);
18591866
ret = 0;
18601867
}
18611868

1862-
if (r->header.cupsHeight == 0)
1869+
if (r->header.cupsHeight == 0 || r->header.cupsHeight > _CUPS_MAX_HEIGHT)
18631870
{
1864-
_cupsRasterAddError("Invalid raster height 0.");
1871+
_cupsRasterAddError("Invalid raster height %u.", r->header.cupsHeight);
18651872
ret = 0;
18661873
}
18671874

0 commit comments

Comments
 (0)