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

- SPL:
. Fixed use-after-free in ArrayObject when the destructor of a replaced
value changes the backing store. (Nicolas Grekas)

- Standard:
. Fixed dns_get_record() returning partially built records for malformed
DNS responses. (Marti2203)
Expand Down
28 changes: 26 additions & 2 deletions ext/spl/spl_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,17 @@ static zval *spl_array_read_dimension(zend_object *object, zval *offset, int typ
return spl_array_read_dimension_ex(1, object, offset, type, rv);
} /* }}} */

/* Stores the value, then destroys the previous one: its destructor may
* change the storage, which can free the table that data points to. */
static void spl_array_replace_value(zval *data, zval *value)
{
zval garbage;

ZVAL_COPY_VALUE(&garbage, data);
ZVAL_COPY_VALUE(data, value);
zval_ptr_dtor(&garbage);
}

static void spl_array_write_dimension_ex(int check_inherited, zend_object *object, zval *offset, zval *value) /* {{{ */
{
spl_array_object *intern = spl_array_from_obj(object);
Expand Down Expand Up @@ -505,10 +516,23 @@ static void spl_array_write_dimension_ex(int check_inherited, zend_object *objec
}

if (key.key) {
zend_hash_update_ind(ht, key.key, value);
zval *data = zend_hash_find(ht, key.key);
if (data) {
if (Z_TYPE_P(data) == IS_INDIRECT) {
data = Z_INDIRECT_P(data);
}
spl_array_replace_value(data, value);
} else {
zend_hash_add_new(ht, key.key, value);
}
spl_hash_key_release(&key);
} else {
zend_hash_index_update(ht, key.h, value);
zval *data = zend_hash_index_find(ht, key.h);
if (data) {
spl_array_replace_value(data, value);
} else {
zend_hash_index_add_new(ht, key.h, value);
}
}
} /* }}} */

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
ArrayObject: the destructor of a replaced value can change the backing store
--FILE--
<?php
class D {
public function __construct(private Closure $onDestruct) {}

public function __destruct()
{
($this->onDestruct)();
}
}

function grow(ArrayObject $ao): D
{
return new D(function () use ($ao) {
// Makes the storage grow, which reallocates it
for ($i = 0; $i < 100; $i++) {
$ao["k$i"] = $i;
}
});
}

echo "-- the storage grows, string key\n";
$ao = new ArrayObject();
$ao['a'] = grow($ao);
$ao['a'] = 'new';
var_dump(count($ao), $ao['a']);

echo "-- the storage grows, integer key\n";
$ao = new ArrayObject();
$ao[0] = grow($ao);
$ao[0] = 'new';
var_dump(count($ao), $ao[0]);

echo "-- the storage is replaced\n";
$ao = new ArrayObject();
$ao['a'] = new D(function () use ($ao) {
$ao->exchangeArray(['replaced' => 1]);
});
$ao['a'] = 'new';
var_dump($ao->getArrayCopy());
?>
--EXPECT--
-- the storage grows, string key
int(101)
string(3) "new"
-- the storage grows, integer key
int(101)
string(3) "new"
-- the storage is replaced
array(1) {
["replaced"]=>
int(1)
}
Loading