Skip to content

ext/gd: imagecrop rect argument overflow fixes. #18006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3560,6 +3560,26 @@ PHP_FUNCTION(imagecrop)
RETURN_THROWS();
}

if ((rect.width > 0 && rect.x > INT_MAX - rect.width)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to check already when assigning to the members of gdRect (which are ints) above.

And I'm not sure whether allowing negative values makes any sense; from a quick glance, that would be treated like zero. Maybe still support that for 8.3+, but additionally throw for master.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some work on master prior, counting on this one before going further.

zend_argument_value_error(2, "overflow with \"x\" and \"width\" keys");
RETURN_THROWS();
}

if ((rect.width < 0 && rect.x < INT_MIN - rect.width)) {
zend_argument_value_error(2, "underflow with \"x\" and \"width\" keys");
RETURN_THROWS();
}

if ((rect.height > 0 && rect.y > INT_MAX - rect.height)) {
zend_argument_value_error(2, "overflow with \"y\" and \"height\" keys");
RETURN_THROWS();
}

if ((rect.height < 0 && rect.y < INT_MIN - rect.height)) {
zend_argument_value_error(2, "underflow with \"y\" and \"height\" keys");
RETURN_THROWS();
}

im_crop = gdImageCrop(im, &rect);

if (im_crop == NULL) {
Expand Down
45 changes: 45 additions & 0 deletions ext/gd/tests/imagecrop_overflow.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
imagecrop() overflows when the combo x/width or y/height is over INT_MAX or under INT_MIN.
--EXTENSIONS--
gd
--FILE--
<?php
$img = imagecreatetruecolor(10, 10);

$arr = ["x" => 2147483647, "y" => 2147483647, "width" => 10, "height" => 10];

try {
imagecrop($img, $arr);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

$arr = ["x" => -2147483648, "y" => 0, "width" => -10, "height" => 10];

try {
imagecrop($img, $arr);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

$arr = ["x" => 1, "y" => 2147483647, "width" => 10, "height" => 10];

try {
imagecrop($img, $arr);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

$arr = ["x" => 1, "y" => -2147483648, "width" => 10, "height" => -10];

try {
imagecrop($img, $arr);
} catch (\ValueError $e) {
echo $e->getMessage();
}
?>
--EXPECT--
imagecrop(): Argument #2 ($rectangle) overflow with "x" and "width" keys
imagecrop(): Argument #2 ($rectangle) underflow with "x" and "width" keys
imagecrop(): Argument #2 ($rectangle) overflow with "y" and "height" keys
imagecrop(): Argument #2 ($rectangle) underflow with "y" and "height" keys
Loading