Skip to content

Commit 6bbb567

Browse files
committed
ext/gd: imagecrop rect argument overflow fixes.
``` ext/gd/libgd/gd.c:2275:14: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int' #0 0x5d6a2103e1db in php_gd_gdImageCopy /home/dcarlier/Contribs/php-src/ext/gd/libgd/gd.c:2275 #1 0x5d6a210a2b63 in gdImageCrop /home/dcarlier/Contribs/php-src/ext/gd/libgd/gd_crop.c:57 #2 0x5d6a21018ca4 in zif_imagecrop /home/dcarlier/Contribs/php-src/ext/gd/gd.c:3575 #3 0x5d6a21e46e7a in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER /home/dcarlier/Contribs/php-src/Zend/zend_vm_execute.h:1337 #4 0x5d6a221188da in execute_ex /home/dcarlier/Contribs/php-src/Zend/zend_vm_execute.h:57246 #5 0x5d6a221366bd in zend_execute /home/dcarlier/Contribs/php-src/Zend/zend_vm_execute.h:61634 #6 0x5d6a21d107a6 in zend_execute_scripts /home/dcarlier/Contribs/php-src/Zend/zend.c:1895 #7 0x5d6a21a63409 in php_execute_script /home/dcarlier/Contribs/php-src/main/main.c:2529 #8 0x5d6a22516d5e in do_cli /home/dcarlier/Contribs/php-src/sapi/cli/php_cli.c:966 #9 0x5d6a2251981d in main /home/dcarlier/Contribs/php-src/sapi/cli/php_cli.c:1341 #10 0x7f10d002a3b7 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58 #11 0x7f10d002a47a in __libc_start_main_impl ../csu/libc-start.c:360 #12 0x5d6a20a06da4 in _start (/home/dcarlier/Contribs/php-src/sapi/cli/php+0x2806da4) (BuildId: d9a79c7e0e4872311439d7313cb3a81fe04190a2) ```
1 parent 9be9f70 commit 6bbb567

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

ext/gd/gd.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3560,6 +3560,26 @@ PHP_FUNCTION(imagecrop)
35603560
RETURN_THROWS();
35613561
}
35623562

3563+
if ((rect.width > 0 && rect.x > INT_MAX - rect.width)) {
3564+
zend_argument_value_error(2, "overflow with \"x\" and \"width\" keys");
3565+
RETURN_THROWS();
3566+
}
3567+
3568+
if ((rect.width < 0 && rect.x < INT_MIN - rect.width)) {
3569+
zend_argument_value_error(2, "underflow with \"x\" and \"width\" keys");
3570+
RETURN_THROWS();
3571+
}
3572+
3573+
if ((rect.height > 0 && rect.y > INT_MAX - rect.height)) {
3574+
zend_argument_value_error(2, "overflow with \"y\" and \"height\" keys");
3575+
RETURN_THROWS();
3576+
}
3577+
3578+
if ((rect.height < 0 && rect.y < INT_MIN - rect.height)) {
3579+
zend_argument_value_error(2, "underflow with \"y\" and \"height\" keys");
3580+
RETURN_THROWS();
3581+
}
3582+
35633583
im_crop = gdImageCrop(im, &rect);
35643584

35653585
if (im_crop == NULL) {

ext/gd/tests/imagecrop_overflow.phpt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
imagecrop() overflows when the combo x/width or y/height is over INT_MAX or
3+
under INT_MIN.
4+
--EXTENSIONS--
5+
gd
6+
--FILE--
7+
<?php
8+
$img = imagecreatetruecolor(10, 10);
9+
10+
$arr = array("x" => 2147483647, "y" => 2147483647, "width" => 10, "height" => 10);
11+
12+
try {
13+
imagecrop($img, $arr);
14+
} catch (\ValueError $e) {
15+
echo $e->getMessage() . PHP_EOL;
16+
}
17+
18+
$arr = array("x" => -2147483648, "y" => 0, "width" => -10, "height" => 10);
19+
20+
try {
21+
imagecrop($img, $arr);
22+
} catch (\ValueError $e) {
23+
echo $e->getMessage() . PHP_EOL;
24+
}
25+
26+
$arr = array("x" => 1, "y" => 2147483647, "width" => 10, "height" => 10);
27+
28+
try {
29+
imagecrop($img, $arr);
30+
} catch (\ValueError $e) {
31+
echo $e->getMessage() . PHP_EOL;
32+
}
33+
34+
$arr = array("x" => 1, "y" => -2147483648, "width" => 10, "height" => -10);
35+
36+
try {
37+
imagecrop($img, $arr);
38+
} catch (\ValueError $e) {
39+
echo $e->getMessage();
40+
}
41+
?>
42+
--EXPECT--
43+
imagecrop(): Argument #2 ($rectangle) overflow with "x" and "width" keys
44+
imagecrop(): Argument #2 ($rectangle) underflow with "x" and "width" keys
45+
imagecrop(): Argument #2 ($rectangle) overflow with "y" and "height" keys
46+
imagecrop(): Argument #2 ($rectangle) underflow with "y" and "height" keys

0 commit comments

Comments
 (0)