Skip to content

Commit 7f85ba6

Browse files
committed
Improve page header validation in cupsRasterReadHeader (Issue #1501)
1 parent c5ce534 commit 7f85ba6

3 files changed

Lines changed: 74 additions & 11 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Changes in CUPS v2.4.17 (YYYY-MM-DD)
88
- The scheduler followed symbolic links when cleaning out its temporary
99
directory (Issue #1448)
1010
- Updated `cupsFileGetConf` and `cupsFilePutConf` to escape more characters.
11+
- Updated `cupsRasterReadHeader` to validate more of the page header values
12+
(Issue #1501)
1113
- Fixed an issue with the class/printer CGI name checking.
1214
- Fixed notifier logging bug that would result in nul bytes getting into the
1315
log (Issue #1450)

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: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
* Raster file routines for CUPS.
33
*
4-
* Copyright © 2020-2024 by OpenPrinting.
5-
* Copyright 2007-2019 by Apple Inc.
6-
* Copyright 1997-2006 by Easy Software Products.
4+
* Copyright © 2020-2026 by OpenPrinting.
5+
* Copyright © 2007-2019 by Apple Inc.
6+
* Copyright © 1997-2006 by Easy Software Products.
77
*
88
* This file is part of the CUPS Imaging library.
99
*
@@ -22,6 +22,17 @@
2222
#endif /* HAVE_STDINT_H */
2323

2424

25+
/*
26+
* Limits...
27+
*/
28+
29+
#define _CUPS_MAX_BYTES_PER_LINE (16 * 1024 * 1024)
30+
#define _CUPS_MAX_BITS_PER_COLOR 16
31+
#define _CUPS_MAX_BITS_PER_PIXEL 240
32+
#define _CUPS_MAX_HEIGHT 0x00ffffff
33+
#define _CUPS_MAX_WIDTH 0x00ffffff
34+
35+
2536
/*
2637
* Private structures...
2738
*/
@@ -1558,6 +1569,9 @@ cups_raster_read(cups_raster_t *r, /* I - Raster stream */
15581569
static int /* O - 1 on success, 0 on failure */
15591570
cups_raster_update(cups_raster_t *r) /* I - Raster stream */
15601571
{
1572+
int ret = 1; /* Return value */
1573+
1574+
15611575
if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1 ||
15621576
r->header.cupsNumColors == 0)
15631577
{
@@ -1664,11 +1678,60 @@ cups_raster_update(cups_raster_t *r) /* I - Raster stream */
16641678
else
16651679
r->remaining = r->header.cupsHeight;
16661680

1681+
/*
1682+
* Validate the page header...
1683+
*/
1684+
1685+
if (r->header.cupsBitsPerColor != 1 && r->header.cupsBitsPerColor != 2 && r->header.cupsBitsPerColor != 4 && r->header.cupsBitsPerColor != 8 && r->header.cupsBitsPerColor != 16)
1686+
{
1687+
_cupsRasterAddError("Invalid bits per color %u.", r->header.cupsBitsPerColor);
1688+
ret = 0;
1689+
}
1690+
1691+
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)))
1692+
{
1693+
_cupsRasterAddError("Invalid bits per pixel %u.", r->header.cupsBitsPerPixel);
1694+
ret = 0;
1695+
}
1696+
1697+
if (r->header.cupsBytesPerLine == 0)
1698+
{
1699+
_cupsRasterAddError("Invalid raster line length 0.");
1700+
ret = 0;
1701+
}
1702+
else if (r->header.cupsBytesPerLine > _CUPS_MAX_BYTES_PER_LINE)
1703+
{
1704+
_cupsRasterAddError("Raster line length %u is greater than %d bytes.", r->header.cupsBytesPerLine, _CUPS_MAX_BYTES_PER_LINE);
1705+
ret = 0;
1706+
}
1707+
else if ((r->header.cupsBytesPerLine % r->bpp) != 0)
1708+
{
1709+
_cupsRasterAddError("Raster line length %u is not a multiple of the pixel size (%d).", r->header.cupsBytesPerLine, r->bpp);
1710+
ret = 0;
1711+
}
1712+
else if (r->header.cupsBytesPerLine != ((r->header.cupsWidth * r->header.cupsBitsPerPixel + 7) / 8))
1713+
{
1714+
_cupsRasterAddError("Raster line length %u does not match width (%u) and bits per pixel (%u).", r->header.cupsBytesPerLine, r->header.cupsWidth, r->header.cupsBitsPerPixel);
1715+
ret = 0;
1716+
}
1717+
1718+
if (r->header.cupsWidth == 0 || r->header.cupsWidth > _CUPS_MAX_WIDTH)
1719+
{
1720+
_cupsRasterAddError("Invalid raster width %u.", r->header.cupsWidth);
1721+
ret = 0;
1722+
}
1723+
1724+
if (r->header.cupsHeight == 0 || r->header.cupsHeight > _CUPS_MAX_HEIGHT)
1725+
{
1726+
_cupsRasterAddError("Invalid raster height %u.", r->header.cupsHeight);
1727+
ret = 0;
1728+
}
1729+
16671730
/*
16681731
* Allocate the compression buffer...
16691732
*/
16701733

1671-
if (r->compressed)
1734+
if (ret && r->compressed)
16721735
{
16731736
if (r->pixels != NULL)
16741737
free(r->pixels);
@@ -1687,7 +1750,7 @@ cups_raster_update(cups_raster_t *r) /* I - Raster stream */
16871750
r->count = 0;
16881751
}
16891752

1690-
return (1);
1753+
return (ret);
16911754
}
16921755

16931756

0 commit comments

Comments
 (0)