Skip to content
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

ext/gd: calls with array types check strengthening. #18005

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
ext/gd: calls with array types check strengthening.
devnexen committed Mar 8, 2025

Verified

This commit was signed with the committer’s verified signature.
devnexen David CARLIER
commit 3233235a8a50b671410f15a19f168f7d1513734f
62 changes: 56 additions & 6 deletions ext/gd/gd.c
Original file line number Diff line number Diff line change
@@ -652,7 +652,20 @@ PHP_FUNCTION(imagesetstyle)
stylearr = safe_emalloc(sizeof(int), num_styles, 0);

ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(styles), item) {
stylearr[index++] = zval_get_long(item);
bool failed = false;
ZVAL_DEREF(item);
zend_long tmp = zval_try_get_long(item, &failed);
if (failed) {
efree(stylearr);
zend_argument_value_error(2, "value must be of type int, %s given", zend_zval_type_name(item));
RETURN_THROWS();
}
if (ZEND_LONG_EXCEEDS_INT(tmp)) {
efree(stylearr);
zend_argument_value_error(2, "value must be between %d and %d", INT_MIN, INT_MAX);
RETURN_THROWS();
}
stylearr[index++] = (int) tmp;
} ZEND_HASH_FOREACH_END();

gdImageSetStyle(im, stylearr, index);
@@ -3648,7 +3661,20 @@ static void php_image_filter_scatter(INTERNAL_FUNCTION_PARAMETERS)
colors = emalloc(num_colors * sizeof(int));

ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hash_colors), color) {
*(colors + i++) = (int) zval_get_long(color);
bool failed = false;
ZVAL_DEREF(color);
zend_long tmp = zval_try_get_long(color, &failed);
if (failed) {
efree(colors);
zend_argument_value_error(5, "value must be of type int, %s given", zend_zval_type_name(color));
RETURN_THROWS();
}
if (tmp < 0 || ZEND_LONG_INT_OVFL(tmp)) {
efree(colors);
zend_argument_value_error(5, "value must be between 0 and %d", INT_MAX);
RETURN_THROWS();
}
colors[i++] = (int) tmp;
} ZEND_HASH_FOREACH_END();

RETVAL_BOOL(gdImageScatterColor(im, (int)scatter_sub, (int)scatter_plus, colors, num_colors));
@@ -3822,6 +3848,22 @@ PHP_FUNCTION(imageantialias)
}
/* }}} */

static bool _php_gd_zval_try_get_c_int(zval *tmp, const char *field, int *res) {
Copy link
Member

Choose a reason for hiding this comment

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

Symbols starting with _ are reserved in C, so please avoid this.

Suggested change
static bool _php_gd_zval_try_get_c_int(zval *tmp, const char *field, int *res) {
static bool php_gd_zval_try_get_c_int(zval *tmp, const char *field, int *res) {

zend_long r;
bool failed = false;
r = zval_try_get_long(tmp, &failed);
if (failed) {
zend_argument_value_error(2, "\"%s\" key must be of type int, %s given", field, zend_zval_type_name(tmp));
return false;
}
if (UNEXPECTED(ZEND_LONG_EXCEEDS_INT(r))) {
zend_argument_value_error(2, "\"%s\" key must be between %d and %d", field, INT_MIN, INT_MAX);
return false;
}
*res = (int)r;
return true;
}

/* {{{ Crop an image using the given coordinates and size, x, y, width and height. */
PHP_FUNCTION(imagecrop)
{
@@ -3840,28 +3882,36 @@ PHP_FUNCTION(imagecrop)
im = php_gd_libgdimageptr_from_zval_p(IM);

if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "x", sizeof("x") -1)) != NULL) {
rect.x = zval_get_long(tmp);
if (!_php_gd_zval_try_get_c_int(tmp, "x", &rect.x)) {
RETURN_THROWS();
}
} else {
zend_argument_value_error(2, "must have an \"x\" key");
RETURN_THROWS();
}

if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "y", sizeof("y") - 1)) != NULL) {
rect.y = zval_get_long(tmp);
if (!_php_gd_zval_try_get_c_int(tmp, "y", &rect.y)) {
RETURN_THROWS();
}
} else {
zend_argument_value_error(2, "must have a \"y\" key");
RETURN_THROWS();
}

if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "width", sizeof("width") - 1)) != NULL) {
rect.width = zval_get_long(tmp);
if (!_php_gd_zval_try_get_c_int(tmp, "width", &rect.width)) {
RETURN_THROWS();
}
} else {
zend_argument_value_error(2, "must have a \"width\" key");
RETURN_THROWS();
}

if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "height", sizeof("height") - 1)) != NULL) {
rect.height = zval_get_long(tmp);
if (!_php_gd_zval_try_get_c_int(tmp, "height", &rect.height)) {
RETURN_THROWS();
}
} else {
zend_argument_value_error(2, "must have a \"height\" key");
RETURN_THROWS();
8 changes: 4 additions & 4 deletions ext/gd/tests/bug66356.phpt
Original file line number Diff line number Diff line change
@@ -7,9 +7,9 @@ gd
$img = imagecreatetruecolor(10, 10);

// POC #1
var_dump(imagecrop($img, array("x" => "a", "y" => 0, "width" => 10, "height" => 10)));
var_dump(imagecrop($img, array("x" => 0, "y" => 0, "width" => 10, "height" => 10)));

$arr = array("x" => "a", "y" => "12b", "width" => 10, "height" => 10);
$arr = array("x" => 2147483647, "y" => 2147483647, "width" => 10, "height" => 10);
var_dump(imagecrop($img, $arr));
print_r($arr);

@@ -32,8 +32,8 @@ object(GdImage)#2 (0) {
}
Array
(
[x] => a
[y] => 12b
[x] => 2147483647
[y] => 2147483647
[width] => 10
[height] => 10
)
87 changes: 87 additions & 0 deletions ext/gd/tests/gh18005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
--TEST--
GH-18005: imagesetstyle, imagefilter, imagecrop array values type checks
--EXTENSIONS--
gd
--SKIPIF--
<?php
if (PHP_INT_SIZE != 8) die('skip this test is for 64bit platforms only');
?>
--FILE--
<?php
class A {}
$img = imagecreatetruecolor(1, 1);
try {
imagesetstyle($img, [0, new A()]);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
imagesetstyle($img, [0, PHP_INT_MIN]);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
imagefilter($img, IMG_FILTER_SCATTER, 0, 0, array(new A()));
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
imagefilter($img, IMG_FILTER_SCATTER, 0, 0, array(-1));
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
imagecrop($img, array("x" => PHP_INT_MIN, "y" => 0, "width" => 0, "height" => 0));
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
imagecrop($img, array("x" => 0, "y" => PHP_INT_MIN, "width" => 0, "height" => 0));
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
imagecrop($img, array("x" => 0, "y" => 0, "width" => PHP_INT_MAX, "height" => 0));
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
imagecrop($img, array("x" => 0, "y" => 0, "width" => 0, "height" => PHP_INT_MAX));
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
imagecrop($img, array("x" => new A(), "y" => 0, "width" => 0, "height" => 0));
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
imagecrop($img, array("x" => 0, "y" => new A(), "width" => 0, "height" => 0));
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
imagecrop($img, array("x" => 0, "y" => 0, "width" => new A(), "height" => 0));
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
imagecrop($img, array("x" => 0, "y" => 0, "width" => 0, "height" => new A()));
} catch (\ValueError $e) {
echo $e->getMessage();
}
?>
--EXPECTF--
imagesetstyle(): Argument #2 ($style) value must be of type int, A given
imagesetstyle(): Argument #2 ($style) value must be between %i and %d
imagefilter(): Argument #5 value must be of type int, A given
imagefilter(): Argument #5 value must be between 0 and 2147483647
imagecrop(): Argument #2 ($rectangle) "x" key must be between %i and %d
imagecrop(): Argument #2 ($rectangle) "y" key must be between %i and %d
imagecrop(): Argument #2 ($rectangle) "width" key must be between %i and %d
imagecrop(): Argument #2 ($rectangle) "height" key must be between %i and %d
imagecrop(): Argument #2 ($rectangle) "x" key must be of type int, A given
imagecrop(): Argument #2 ($rectangle) "y" key must be of type int, A given
imagecrop(): Argument #2 ($rectangle) "width" key must be of type int, A given
imagecrop(): Argument #2 ($rectangle) "height" key must be of type int, A given