Skip to content

Commit d50532b

Browse files
cmb69smalyshev
authored andcommittedOct 24, 2022
Fix #81739: OOB read due to insufficient validation in imageloadfont()
If we swap the byte order of the relevant header bytes, we need to make sure again that the following multiplication does not overflow.
1 parent 8b919c3 commit d50532b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
 

‎ext/gd/gd.c

+7
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,12 @@ PHP_FUNCTION(imageloadfont)
14851485
font->w = FLIPWORD(font->w);
14861486
font->h = FLIPWORD(font->h);
14871487
font->nchars = FLIPWORD(font->nchars);
1488+
if (overflow2(font->nchars, font->h) || overflow2(font->nchars * font->h, font->w )) {
1489+
php_error_docref(NULL, E_WARNING, "Error reading font, invalid font header");
1490+
efree(font);
1491+
php_stream_close(stream);
1492+
RETURN_FALSE;
1493+
}
14881494
body_size = font->w * font->h * font->nchars;
14891495
}
14901496

@@ -1495,6 +1501,7 @@ PHP_FUNCTION(imageloadfont)
14951501
RETURN_FALSE;
14961502
}
14971503

1504+
ZEND_ASSERT(body_size > 0);
14981505
font->data = emalloc(body_size);
14991506
b = 0;
15001507
while (b < body_size && (n = php_stream_read(stream, &font->data[b], body_size - b)) > 0) {

‎ext/gd/tests/bug81739.phpt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Bug #81739 (OOB read due to insufficient validation in imageloadfont())
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("gd")) die("skip gd extension not available");
6+
?>
7+
--FILE--
8+
<?php
9+
$s = fopen(__DIR__ . "/font.font", "w");
10+
// header without character data
11+
fwrite($s, "\x01\x00\x00\x00\x20\x00\x00\x00\x08\x00\x00\x00\x08\x00\x00\x00");
12+
fclose($s);
13+
var_dump(imageloadfont(__DIR__ . "/font.font"));
14+
?>
15+
--CLEAN--
16+
<?php
17+
@unlink(__DIR__ . "/font.font");
18+
?>
19+
--EXPECTF--
20+
Warning: imageloadfont(): %croduct of memory allocation multiplication would exceed INT_MAX, failing operation gracefully
21+
in %s on line %d
22+
23+
Warning: imageloadfont(): Error reading font, invalid font header in %s on line %d
24+
bool(false)

0 commit comments

Comments
 (0)