Skip to content

Commit 903b182

Browse files
committed
Fix #77973: Uninitialized read in gdImageCreateFromXbm
We have to ensure that `sscanf()` does indeed read a hex value here, and bail out otherwise. (cherry picked from commit ed6dee9)
1 parent b7dc3d9 commit 903b182

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ PHP NEWS
1616

1717
- GD:
1818
. Fixed bug #77943 (imageantialias($image, false); does not work). (cmb)
19+
. Fixed bug #77973 (Uninitialized read in gdImageCreateFromXbm).
20+
(CVE-2019-11038) (cmb)
1921

2022
- JSON:
2123
. Fixed bug #77843 (Use after free with json serializer). (Nikita)

ext/gd/libgd/gd_xbm.c

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