Skip to content

Commit d2274b0

Browse files
cmb69weltling
authored andcommitted
Fix #73869: Signed Integer Overflow gd_io.c
GD2 stores the number of horizontal and vertical chunks as words (i.e. 2 byte unsigned). These values are multiplied and assigned to an int when reading the image, what can cause integer overflows. We have to avoid that, and also make sure that either chunk count is actually greater than zero. If illegal chunk counts are detected, we bail out from reading the image. (cherry picked from commit 5b5d9db3988b829e0b121b74bb3947f01c2796a1)
1 parent f1b2afc commit d2274b0

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

ext/gd/libgd/gd_gd2.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ static int _gd2GetHeader(gdIOCtxPtr in, int *sx, int *sy, int *cs, int *vers, in
136136
GD2_DBG(php_gd_error("%d Chunks vertically", *ncy));
137137

138138
if (gd2_compressed(*fmt)) {
139+
if (*ncx <= 0 || *ncy <= 0 || *ncx > INT_MAX / *ncy) {
140+
GD2_DBG(printf ("Illegal chunk counts: %d * %d\n", *ncx, *ncy));
141+
goto fail1;
142+
}
139143
nc = (*ncx) * (*ncy);
140144
GD2_DBG(php_gd_error("Reading %d chunk index entries", nc));
141145
if (overflow2(sizeof(t_chunk_info), nc)) {

ext/gd/tests/bug73869.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Bug #73869 (Signed Integer Overflow gd_io.c)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
var_dump(imagecreatefromgd2(__DIR__ . DIRECTORY_SEPARATOR . 'bug73869a.gd2'));
10+
var_dump(imagecreatefromgd2(__DIR__ . DIRECTORY_SEPARATOR . 'bug73869b.gd2'));
11+
?>
12+
===DONE===
13+
--EXPECTF--
14+
Warning: imagecreatefromgd2(): '%s' is not a valid GD2 file in %s on line %d
15+
bool(false)
16+
17+
Warning: imagecreatefromgd2(): '%s' is not a valid GD2 file in %s on line %d
18+
bool(false)
19+
===DONE===

ext/gd/tests/bug73869a.gd2

92 Bytes
Binary file not shown.

ext/gd/tests/bug73869b.gd2

18 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)