Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ PHP NEWS
. Fixed bug GH-25268 (NULL php_libxml_ref_obj dereference in
dom_objects_store_clone_obj when cloning Dom\Notation). (David Carlier)

- 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)
Expand Down
32 changes: 20 additions & 12 deletions ext/gd/libgd/gd_xbm.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include "gd.h"
#include "gdhelpers.h"
#include "gd_errors.h"
Expand All @@ -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;
Expand All @@ -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
Expand Down
32 changes: 32 additions & 0 deletions ext/gd/tests/xbm_out_of_range.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
imagecreatefromxbm() rejects out-of-range XBM dimensions
--EXTENSIONS--
gd
--SKIPIF--
<?php
if (!GD_BUNDLED) {
die("skip dimension range checks require bundled GD");
}
?>
--FILE--
<?php
$path = __DIR__ . '/xbm_out_of_range.tmp.xbm';
foreach ([['4294967298', '1'], ['1', '4294967298']] as [$w, $h]) {
file_put_contents($path, "#define x_width $w\n#define x_height $h\nstatic char x_bits[] = {\n0x00, 0x00 };\n");
echo "$w x $h\n";
var_dump(imagecreatefromxbm($path));
}
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/xbm_out_of_range.tmp.xbm');
?>
--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)
Loading