From c0e6d5680f19753b69a641c085d39b8ba746a435 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Thu, 24 Sep 2026 08:58:14 -0400 Subject: [PATCH] ext/standard: Reject out-of-range XBM dimensions XBM width and height were read with sscanf("%d"), whose behaviour on overflow is undefined; glibc wraps 4294967298 to 2, so getimagesize() reported a width of 2 and the bundled GD decoded a 2-pixel image, while negative values came back as huge unsigned sizes. Parse the value with strtol() and accept only 1 to INT_MAX in both readers, and bound the bundled GD row byte count times height by INT_MAX. --- NEWS | 8 ++++ ext/gd/libgd/gd_xbm.c | 41 +++++++++++------ ext/gd/tests/xbm_out_of_range.phpt | 44 ++++++++++++++++++ ext/standard/image.c | 45 +++++++++++-------- .../tests/image/xbm_out_of_range.phpt | 21 +++++++++ 5 files changed, 127 insertions(+), 32 deletions(-) create mode 100644 ext/gd/tests/xbm_out_of_range.phpt create mode 100644 ext/standard/tests/image/xbm_out_of_range.phpt diff --git a/NEWS b/NEWS index 26abaf2f03ab..13d5c4aa8a97 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,10 @@ PHP NEWS - Curl: . Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire) +- GD: + . Fixed imagecreatefromxbm() accepting XBM files with out-of-range + dimensions. (Ilia Alshanetsky) + - MBString: . Fixed stack overflow in mb_check_encoding() and mb_convert_encoding() with deeply nested arrays. (Eyüp Can Akman) @@ -23,6 +27,10 @@ PHP NEWS or never terminates). (Ilia Alshanetsky) . Fixed memory leak in JIT ASSIGN_DIM when the assignment fails. (Jorg Sowa) +- Standard: + . Fixed getimagesize() reporting wrapped dimensions for XBM images with + out-of-range width or height. (Ilia Alshanetsky) + - Streams: . Fixed bug GH-22063 (Stream filter chain UAF on self-removal during callback). (Ilia Alshanetsky) diff --git a/ext/gd/libgd/gd_xbm.c b/ext/gd/libgd/gd_xbm.c index 8efb34fbb0d4..c7ec182b990d 100644 --- a/ext/gd/libgd/gd_xbm.c +++ b/ext/gd/libgd/gd_xbm.c @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include "gd.h" #include "gdhelpers.h" #include "gd_errors.h" @@ -34,7 +36,9 @@ gdImagePtr gdImageCreateFromXbm(FILE * fd) char fline[MAX_XBM_LINE_SIZE]; char iname[MAX_XBM_LINE_SIZE]; char *type; - int value; + char *end; + int offset; + long value; unsigned int width = 0, height = 0; int fail = 0; int max_bit = 0; @@ -52,18 +56,22 @@ gdImagePtr gdImageCreateFromXbm(FILE * fd) if (strlen(fline) == MAX_XBM_LINE_SIZE-1) { return 0; } - if (sscanf(fline, "#define %s %d", iname, &value) == 2) { - if (!(type = strrchr(iname, '_'))) { - type = iname; - } else { - type++; - } + if (sscanf(fline, "#define %s %n", iname, &offset) == 1) { + errno = 0; + value = strtol(fline + offset, &end, 10); + if (end != fline + offset && errno == 0 && value > 0 && value <= INT_MAX) { + if (!(type = strrchr(iname, '_'))) { + type = iname; + } else { + type++; + } - if (!strcmp("width", type)) { - width = (unsigned int) value; - } - if (!strcmp("height", type)) { - height = (unsigned int) value; + if (!strcmp("width", type)) { + width = (unsigned int) value; + } + if (!strcmp("height", type)) { + height = (unsigned int) value; + } } } else { if ( sscanf(fline, "static unsigned char %s = {", iname) == 1 @@ -76,10 +84,15 @@ gdImagePtr gdImageCreateFromXbm(FILE * fd) max_bit = 32768; } if (max_bit) { - bytes = (width + 7) / 8 * height; - if (!bytes) { + unsigned int row_bytes; + if (width == 0 || height == 0) { + return 0; + } + row_bytes = (width + 7) / 8; + if (height > INT_MAX / row_bytes) { return 0; } + bytes = (int) (row_bytes * height); if (!(type = strrchr(iname, '_'))) { type = iname; } else { diff --git a/ext/gd/tests/xbm_out_of_range.phpt b/ext/gd/tests/xbm_out_of_range.phpt new file mode 100644 index 000000000000..53301c1a360f --- /dev/null +++ b/ext/gd/tests/xbm_out_of_range.phpt @@ -0,0 +1,44 @@ +--TEST-- +imagecreatefromxbm() rejects out-of-range XBM dimensions +--EXTENSIONS-- +gd +--SKIPIF-- + +--FILE-- + +--CLEAN-- + +--EXPECTF-- +4294967298 x 1 + +Warning: imagecreatefromxbm(): "%s" is not a valid XBM file in %s on line %d +bool(false) +1 x 4294967298 + +Warning: imagecreatefromxbm(): "%s" is not a valid XBM file in %s on line %d +bool(false) +2147483648 x 1 + +Warning: imagecreatefromxbm(): "%s" is not a valid XBM file in %s on line %d +bool(false) +-5 x 1 + +Warning: imagecreatefromxbm(): "%s" is not a valid XBM file in %s on line %d +bool(false) +2147483647 x 9 + +Warning: imagecreatefromxbm(): "%s" is not a valid XBM file in %s on line %d +bool(false) diff --git a/ext/standard/image.c b/ext/standard/image.c index f58569495d39..f9c8df8a16c4 100644 --- a/ext/standard/image.c +++ b/ext/standard/image.c @@ -17,6 +17,9 @@ #include "php.h" #include +#include +#include +#include #ifdef HAVE_FCNTL_H #include #endif @@ -1000,7 +1003,9 @@ static int php_get_xbm(php_stream *stream, struct gfxinfo **result) char *fline; char *iname; char *type; - int value; + char *end; + int offset; + long value; unsigned int width = 0, height = 0; if (result) { @@ -1017,25 +1022,29 @@ static int php_get_xbm(php_stream *stream, struct gfxinfo **result) break; } iname = estrdup(fline); /* simple way to get necessary buffer of required size */ - if (sscanf(fline, "#define %s %d", iname, &value) == 2) { - if (!(type = strrchr(iname, '_'))) { - type = iname; - } else { - type++; - } + if (sscanf(fline, "#define %s %n", iname, &offset) == 1) { + errno = 0; + value = strtol(fline + offset, &end, 10); + if (end != fline + offset && errno == 0 && value > 0 && value <= INT_MAX) { + if (!(type = strrchr(iname, '_'))) { + type = iname; + } else { + type++; + } - if (!strcmp("width", type)) { - width = (unsigned int) value; - if (height) { - efree(iname); - break; + if (!strcmp("width", type)) { + width = (unsigned int) value; + if (height) { + efree(iname); + break; + } } - } - if (!strcmp("height", type)) { - height = (unsigned int) value; - if (width) { - efree(iname); - break; + if (!strcmp("height", type)) { + height = (unsigned int) value; + if (width) { + efree(iname); + break; + } } } } diff --git a/ext/standard/tests/image/xbm_out_of_range.phpt b/ext/standard/tests/image/xbm_out_of_range.phpt new file mode 100644 index 000000000000..1252b00c945a --- /dev/null +++ b/ext/standard/tests/image/xbm_out_of_range.phpt @@ -0,0 +1,21 @@ +--TEST-- +getimagesize() rejects out-of-range XBM dimensions +--FILE-- + +--CLEAN-- + +--EXPECT-- +4294967298 x 1: false +1 x 4294967298: false +2147483648 x 1: false +-5 x 1: false +2147483647 x 1: width="2147483647" height="1"