Skip to content
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
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ PHP NEWS
. Fixed session upload progress to respect session.use_only_cookies.
(jorgsowa)

- SPL:
. Fixed use-after-free when an error handler replaces the backing store of
an ArrayObject during an offset access or ArrayIterator::seek().
(Nicolas Grekas)

- Standard:
. Fixed dns_get_record() returning partially built records for malformed
DNS responses. (Marti2203)
Expand Down
27 changes: 22 additions & 5 deletions ext/spl/spl_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object
{
zval *retval;
spl_hash_key key;
HashTable *ht = spl_array_get_hash_table(intern);
HashTable *ht;

if (!offset || Z_ISUNDEF_P(offset) || !ht) {
if (!offset || Z_ISUNDEF_P(offset)) {
return &EG(uninitialized_zval);
}

Expand All @@ -342,7 +342,11 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object
&EG(error_zval) : &EG(uninitialized_zval);
}

/* Fetched after get_hash_key(), whose errors may call a handler that replaces the storage */
ht = spl_array_get_hash_table(intern);

if (key.key) {
find_key:
retval = zend_hash_find(ht, key.key);
if (retval) {
if (Z_TYPE_P(retval) == IS_INDIRECT) {
Expand All @@ -358,7 +362,11 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object
break;
case BP_VAR_RW:
zend_error(E_WARNING,"Undefined array key \"%s\"", ZSTR_VAL(key.key));
ZEND_FALLTHROUGH;
/* The error handler may have replaced the storage,
* which can free the table that retval points to */
ht = spl_array_get_hash_table(intern);
type = BP_VAR_W;
goto find_key;
case BP_VAR_W: {
ZVAL_NULL(retval);
}
Expand All @@ -376,6 +384,8 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object
break;
case BP_VAR_RW:
zend_error(E_WARNING,"Undefined array key \"%s\"", ZSTR_VAL(key.key));
/* The error handler may have replaced the storage */
ht = spl_array_get_hash_table(intern);
ZEND_FALLTHROUGH;
case BP_VAR_W: {
zval value;
Expand All @@ -397,6 +407,8 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object
break;
case BP_VAR_RW:
zend_error(E_WARNING, "Undefined array key " ZEND_LONG_FMT, key.h);
/* The error handler may have replaced the storage */
ht = spl_array_get_hash_table(intern);
ZEND_FALLTHROUGH;
case BP_VAR_W: {
zval value;
Expand Down Expand Up @@ -596,14 +608,17 @@ static bool spl_array_has_dimension_ex(bool check_inherited, zend_object *object
}

if (!value) {
HashTable *ht = spl_array_get_hash_table(intern);
HashTable *ht;
spl_hash_key key;

if (get_hash_key(&key, intern, offset) == FAILURE) {
zend_illegal_container_offset(object->ce->name, offset, BP_VAR_IS);
return 0;
}

/* Fetched after get_hash_key(), whose errors may call a handler that replaces the storage */
ht = spl_array_get_hash_table(intern);

if (key.key) {
tmp = zend_hash_find(ht, key.key);
spl_hash_key_release(&key);
Expand Down Expand Up @@ -1687,7 +1702,7 @@ PHP_METHOD(ArrayIterator, seek)
zend_long opos, position;
zval *object = ZEND_THIS;
spl_array_object *intern = Z_SPLARRAY_P(object);
HashTable *aht = spl_array_get_hash_table(intern);
HashTable *aht;
int result;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) {
Expand All @@ -1702,6 +1717,8 @@ PHP_METHOD(ArrayIterator, seek)

while (position-- > 0 && (result = spl_array_next(intern)) == SUCCESS);

/* Fetched after parsing the argument, as its errors may call a handler that replaces the storage */
aht = spl_array_get_hash_table(intern);
if (result == SUCCESS && zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS) {
return; /* ok */
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
--TEST--
ArrayObject: an error handler can replace the backing store during an offset access
--FILE--
<?php
function replace_in_error_handler(ArrayObject $ao): void
{
set_error_handler(function (int $errno, string $errstr) use ($ao) {
echo $errstr, "\n";
$ao->exchangeArray(['replaced' => str_repeat('x', 3)]);
});
}

echo "-- read\n";
$ao = new ArrayObject(['a' => 1, 'b' => 2]);
replace_in_error_handler($ao);
var_dump($ao[1.5]);
restore_error_handler();

echo "-- isset\n";
$ao = new ArrayObject(['a' => 1, 'b' => 2]);
replace_in_error_handler($ao);
var_dump(isset($ao[1.5]));
restore_error_handler();

echo "-- read-write of a missing key\n";
$ao = new ArrayObject(['a' => 1], ArrayObject::ARRAY_AS_PROPS);
replace_in_error_handler($ao);
$ao->b .= 'x';
restore_error_handler();
var_dump($ao->getArrayCopy());

echo "-- read-write of an unset property of a backing object\n";
class C {
public $p;
}
$o = new C();
unset($o->p);
$ao = @new ArrayObject($o, ArrayObject::ARRAY_AS_PROPS);
unset($o);
replace_in_error_handler($ao);
$ao->p .= 'x';
restore_error_handler();
var_dump($ao->getArrayCopy());

echo "-- ArrayIterator::seek()\n";
$it = new ArrayIterator(['a', 'b']);
set_error_handler(function (int $errno, string $errstr) use ($it) {
echo $errstr, "\n";
$it->__construct(['x', 'y', 'z']);
});
$it->seek(1.5);
restore_error_handler();
var_dump($it->current());
?>
--EXPECT--
-- read
Implicit conversion from float 1.5 to int loses precision
Undefined array key 1
NULL
-- isset
Implicit conversion from float 1.5 to int loses precision
bool(false)
-- read-write of a missing key
Undefined array key "b"
array(2) {
["replaced"]=>
string(3) "xxx"
["b"]=>
string(1) "x"
}
-- read-write of an unset property of a backing object
Undefined array key "p"
array(2) {
["replaced"]=>
string(3) "xxx"
["p"]=>
string(1) "x"
}
-- ArrayIterator::seek()
Implicit conversion from float 1.5 to int loses precision
string(1) "y"
Loading