Fix GH-17162: zend_array_try_init() with dtor can cause engine UAF

Closes GH-17167.
This commit is contained in:
Niels Dossche 2024-12-15 16:18:43 +01:00
parent 0a3442fbe6
commit ee0daa59db
No known key found for this signature in database
GPG key ID: B8A8AD166DF0E2E5
3 changed files with 27 additions and 1 deletions

2
NEWS
View file

@ -4,6 +4,8 @@ PHP NEWS
- Core: - Core:
. Fixed bug GH-17106 (ZEND_MATCH_ERROR misoptimization). (ilutov) . Fixed bug GH-17106 (ZEND_MATCH_ERROR misoptimization). (ilutov)
. Fixed bug GH-17162 (zend_array_try_init() with dtor can cause engine UAF).
(nielsdos)
- DBA: - DBA:
. Skip test if inifile is disabled. (orlitzky) . Skip test if inifile is disabled. (orlitzky)

21
Zend/tests/gh17162.phpt Normal file
View file

@ -0,0 +1,21 @@
--TEST--
GH-17162 (zend_array_try_init() with dtor can cause engine UAF)
--FILE--
<?php
class Test {
function __destruct() {
global $box;
$box->value = null;
}
}
$box = [new Test];
// Using getimagesize() for the test because it's always available,
// but any function that uses zend_try_array_init() would work.
try {
getimagesize("dummy", $box);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
Attempt to assign property "value" on null

View file

@ -1478,7 +1478,10 @@ static zend_always_inline zval *zend_try_array_init_size(zval *zv, uint32_t size
} }
zv = &ref->val; zv = &ref->val;
} }
zval_ptr_dtor(zv); zval garbage;
ZVAL_COPY_VALUE(&garbage, zv);
ZVAL_NULL(zv);
zval_ptr_dtor(&garbage);
ZVAL_ARR(zv, arr); ZVAL_ARR(zv, arr);
return zv; return zv;
} }