]> granicus.if.org Git - php/commitdiff
- MFB: fix crash when some crafted font are given
authorPierre Joye <pajoye@php.net>
Thu, 17 Jul 2008 23:28:11 +0000 (23:28 +0000)
committerPierre Joye <pajoye@php.net>
Thu, 17 Jul 2008 23:28:11 +0000 (23:28 +0000)
ext/gd/gd.c
ext/gd/tests/imageloadfont_invalid.phpt [new file with mode: 0644]

index f07279ddc0f3a89e4e42107e6e983888200d33db..67b760f5997f1d2c3f6421fcaa867fb85cdc22b2 100644 (file)
@@ -1483,6 +1483,19 @@ PHP_FUNCTION(imageloadfont)
                body_size = font->w * font->h * font->nchars;
        }
 
+       if (overflow2(font->nchars, font->h)) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading font, invalid font header");
+               efree(font);
+               php_stream_close(stream);
+               RETURN_FALSE;
+       }
+       if (overflow2(font->nchars * font->h, font->w )) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading font, invalid font header");
+               efree(font);
+               php_stream_close(stream);
+               RETURN_FALSE;
+       }
+
        if (body_size != body_size_check) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading font");
                efree(font);
diff --git a/ext/gd/tests/imageloadfont_invalid.phpt b/ext/gd/tests/imageloadfont_invalid.phpt
new file mode 100644 (file)
index 0000000..b297ac7
--- /dev/null
@@ -0,0 +1,25 @@
+--TEST--
+imageloadfont() function crashes
+--SKIPIF--
+<?php 
+       if (!extension_loaded('gd')) die("skip gd extension not available\n"); 
+       if (!GD_BUNDLED) die('skip external GD libraries always fail');
+?>
+--FILE--
+<?php
+$filename = dirname(__FILE__) .  '/font.gdf';
+$bin = "\x41\x41\x41\x41\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00";
+$fp = fopen($filename, 'wb');
+fwrite($fp, $bin);
+fclose($fp);
+
+$image = imagecreatetruecolor(50, 20);
+$font = imageloadfont($filename);
+$black = imagecolorallocate($image, 0, 0, 0);
+imagestring($image, $font, 0, 0, "Hello", $black);
+?>
+--EXPECTF--
+Warning: imageloadfont(): gd warning: product of memory allocation multiplication would exceed INT_MAX, failing operation gracefully
+ in %simageloadfont_invalid.php on line %d
+
+Warning: imageloadfont(): Error reading font, invalid font header in %simageloadfont_invalid.php on line %d