Skip to content

Commit 3b14a5a

Browse files
cmb69smalyshev
authored andcommitted
Fix #77973: Uninitialized read in gdImageCreateFromXbm
We have to ensure that `sscanf()` does indeed read a hex value here, and bail out otherwise.
1 parent 699554e commit 3b14a5a

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

ext/gd/libgd/xbm.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ gdImagePtr gdImageCreateFromXbm(FILE * fd)
135135
}
136136
h[3] = ch;
137137
}
138-
sscanf(h, "%x", &b);
138+
if (sscanf(h, "%x", &b) != 1) {
139+
php_gd_error("invalid XBM");
140+
gdImageDestroy(im);
141+
return 0;
142+
}
139143
for (bit = 1; bit <= max_bit; bit = bit << 1) {
140144
gdImageSetPixel(im, x++, y, (b & bit) ? 1 : 0);
141145
if (x == im->sx) {

ext/gd/tests/bug77973.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Bug #77973 (Uninitialized read in gdImageCreateFromXbm)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die("skip gd extension not available");
6+
if (!function_exists('imagecreatefromxbm')) die("skip imagecreatefromxbm not available");
7+
?>
8+
--FILE--
9+
<?php
10+
$contents = hex2bin("23646566696e6520776964746820320a23646566696e652068656967687420320a737461746963206368617220626974735b5d203d7b0a7a7a787a7a");
11+
$filepath = __DIR__ . '/bug77973.xbm';
12+
file_put_contents($filepath, $contents);
13+
$im = imagecreatefromxbm($filepath);
14+
var_dump($im);
15+
?>
16+
===DONE===
17+
--EXPECTF--
18+
Warning: imagecreatefromxbm(): invalid XBM in %s on line %d
19+
20+
Warning: imagecreatefromxbm(): '%s' is not a valid XBM file in %s on line %d
21+
bool(false)
22+
===DONE===
23+
--CLEAN--
24+
<?php
25+
unlink(__DIR__ . '/bug77973.xbm');
26+
?>

0 commit comments

Comments
 (0)