Skip to content

Commit 47c7da9

Browse files
Fix GH-17935: Separate ArrayObject and ArrayIterator storage on write
ArrayObject and ArrayIterator assumed they were the only owner of their storage, but __serialize(), __debugInfo(), var_export(), json_encode() and the sort methods hand it out or borrow it without copying it. Writing to the object then modified the array that was handed out, and failed the refcount assertion of zend_hash.c in debug builds. The storage is now separated before any write. When this happens while it is iterated, iterators move to the copy of their position that zend_array_dup() registers on the new table. When the storage is replaced, they keep their position and get registered on the new table. An error handler can now separate the storage by writing to the object, so the table is fetched after the code that can call one: get_hash_key() on read paths, and argument parsing in ArrayIterator::seek(). This also fixes use-after-frees when a handler calls exchangeArray() there, or while an element is being created.
1 parent 6c0388f commit 47c7da9

10 files changed

Lines changed: 605 additions & 11 deletions

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ PHP NEWS
152152
(Ilia Alshanetsky)
153153

154154
- SPL:
155+
. Fixed bug GH-17935 (Assertion failure Zend/zend_hash.c when appending to
156+
serialized ArrayObject). (Nicolas Grekas)
155157
. Fixed bug GH-23385 (SplDoublyLinkedList::serialize() use-after-free when
156158
__serialize() removes an element). (David Carlier)
157159

‎ext/spl/spl_array.c‎

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,75 @@ static inline bool spl_array_is_object(spl_array_object *intern) /* {{{ */
108108
}
109109
/* }}} */
110110

111+
/* Returns the storage, separated from its other holders so that it can be written to. */
112+
static HashTable *spl_array_get_hash_table_for_write(spl_array_object *intern)
113+
{
114+
while (intern->ar_flags & SPL_ARRAY_USE_OTHER) {
115+
intern = Z_SPLARRAY_P(&intern->array);
116+
}
117+
if (!(intern->ar_flags & SPL_ARRAY_IS_SELF) && Z_TYPE(intern->array) == IS_ARRAY) {
118+
SEPARATE_ARRAY(&intern->array);
119+
return Z_ARRVAL(intern->array);
120+
}
121+
return spl_array_get_hash_table(intern);
122+
}
123+
111124
static zend_result spl_array_skip_protected(spl_array_object *intern, HashTable *aht);
112125

113-
static zend_never_inline void spl_array_create_ht_iter(HashTable *ht, spl_array_object* intern) /* {{{ */
126+
static void spl_array_create_ht_iter(HashTable *ht, spl_array_object* intern) /* {{{ */
114127
{
115128
intern->ht_iter = zend_hash_iterator_add(ht, zend_hash_get_current_pos(ht));
116129
zend_hash_internal_pointer_reset_ex(ht, &EG(ht_iterators)[intern->ht_iter].pos);
117130
spl_array_skip_protected(intern, ht);
118131
}
119132
/* }}} */
120133

121-
static zend_always_inline uint32_t *spl_array_get_pos_ptr(HashTable *ht, spl_array_object* intern) /* {{{ */
134+
static void spl_array_update_ht_iter(HashTable *ht, spl_array_object* intern)
122135
{
123-
if (UNEXPECTED(intern->ht_iter == (uint32_t)-1)) {
136+
uint32_t idx = intern->ht_iter;
137+
HashPosition pos = EG(ht_iterators)[idx].pos;
138+
bool has_copy = false;
139+
140+
/* When the storage is separated, zend_array_dup() registers a copy of the
141+
* iterator on the new table, with its position adjusted to it. */
142+
for (uint32_t i = EG(ht_iterators)[idx].next_copy; i != idx; i = EG(ht_iterators)[i].next_copy) {
143+
if (EG(ht_iterators)[i].ht == ht) {
144+
has_copy = true;
145+
break;
146+
}
147+
}
148+
149+
/* Moves the registration to the new table, so that the previous one stops
150+
* tracking this iterator, and takes the position of the copy if there is one. */
151+
zend_hash_iterator_pos(idx, ht);
152+
if (!has_copy) {
153+
/* The storage was replaced: the position is kept as is */
154+
EG(ht_iterators)[idx].pos = pos;
155+
}
156+
}
157+
158+
static zend_always_inline bool spl_array_ht_iter_is_on(const spl_array_object *intern, const HashTable *ht)
159+
{
160+
/* The registered table is compared rather than a cached pointer: it is
161+
* poisoned when the table is destroyed, so a new table allocated at the
162+
* same address cannot be mistaken for it. */
163+
return intern->ht_iter != (uint32_t)-1 && EG(ht_iterators)[intern->ht_iter].ht == ht;
164+
}
165+
166+
static zend_never_inline uint32_t *spl_array_get_pos_ptr_slow(HashTable *ht, spl_array_object* intern)
167+
{
168+
if (intern->ht_iter == (uint32_t)-1) {
124169
spl_array_create_ht_iter(ht, intern);
170+
} else {
171+
spl_array_update_ht_iter(ht, intern);
172+
}
173+
return &EG(ht_iterators)[intern->ht_iter].pos;
174+
}
175+
176+
static zend_always_inline uint32_t *spl_array_get_pos_ptr(HashTable *ht, spl_array_object* intern) /* {{{ */
177+
{
178+
if (UNEXPECTED(!spl_array_ht_iter_is_on(intern, ht))) {
179+
return spl_array_get_pos_ptr_slow(ht, intern);
125180
}
126181
return &EG(ht_iterators)[intern->ht_iter].pos;
127182
}
@@ -321,9 +376,9 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object
321376
{
322377
zval *retval;
323378
spl_hash_key key;
324-
HashTable *ht = spl_array_get_hash_table(intern);
379+
HashTable *ht;
325380

326-
if (!offset || Z_ISUNDEF_P(offset) || !ht) {
381+
if (!offset || Z_ISUNDEF_P(offset)) {
327382
return &EG(uninitialized_zval);
328383
}
329384

@@ -338,6 +393,13 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object
338393
&EG(error_zval) : &EG(uninitialized_zval);
339394
}
340395

396+
/* The table is fetched after get_hash_key(), whose errors may call a handler
397+
* that shares or replaces the storage. In write context, the returned zval
398+
* may be turned into a reference or written to. */
399+
ht = (type != BP_VAR_R && type != BP_VAR_IS)
400+
? spl_array_get_hash_table_for_write(intern)
401+
: spl_array_get_hash_table(intern);
402+
341403
if (key.key) {
342404
retval = zend_hash_find(ht, key.key);
343405
if (retval) {
@@ -372,6 +434,8 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object
372434
break;
373435
case BP_VAR_RW:
374436
zend_error(E_WARNING,"Undefined array key \"%pS\"", key.key);
437+
/* The error handler may have shared or replaced the storage */
438+
ht = spl_array_get_hash_table_for_write(intern);
375439
ZEND_FALLTHROUGH;
376440
case BP_VAR_W: {
377441
zval value;
@@ -393,6 +457,8 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object
393457
break;
394458
case BP_VAR_RW:
395459
zend_error(E_WARNING, "Undefined array key " ZEND_LONG_FMT, key.h);
460+
/* The error handler may have shared or replaced the storage */
461+
ht = spl_array_get_hash_table_for_write(intern);
396462
ZEND_FALLTHROUGH;
397463
case BP_VAR_W: {
398464
zval value;
@@ -480,7 +546,7 @@ static void spl_array_write_dimension_ex(int check_inherited, zend_object *objec
480546

481547
Z_TRY_ADDREF_P(value);
482548
if (!offset || Z_TYPE_P(offset) == IS_NULL) {
483-
ht = spl_array_get_hash_table(intern);
549+
ht = spl_array_get_hash_table_for_write(intern);
484550
if (UNEXPECTED(ht == intern->sentinel_array)) {
485551
return;
486552
}
@@ -494,7 +560,7 @@ static void spl_array_write_dimension_ex(int check_inherited, zend_object *objec
494560
return;
495561
}
496562

497-
ht = spl_array_get_hash_table(intern);
563+
ht = spl_array_get_hash_table_for_write(intern);
498564
if (UNEXPECTED(ht == intern->sentinel_array)) {
499565
spl_hash_key_release(&key);
500566
return;
@@ -534,7 +600,7 @@ static void spl_array_unset_dimension_ex(int check_inherited, zend_object *objec
534600
return;
535601
}
536602

537-
ht = spl_array_get_hash_table(intern);
603+
ht = spl_array_get_hash_table_for_write(intern);
538604
if (key.key) {
539605
zval *data = zend_hash_find(ht, key.key);
540606
if (data) {
@@ -592,14 +658,17 @@ static bool spl_array_has_dimension_ex(bool check_inherited, zend_object *object
592658
}
593659

594660
if (!value) {
595-
HashTable *ht = spl_array_get_hash_table(intern);
661+
HashTable *ht;
596662
spl_hash_key key;
597663

598664
if (get_hash_key(&key, intern, offset) == FAILURE) {
599665
zend_illegal_container_offset(object->ce->name, offset, BP_VAR_IS);
600666
return false;
601667
}
602668

669+
/* Fetched after get_hash_key(), whose errors may call a handler that replaces the storage */
670+
ht = spl_array_get_hash_table(intern);
671+
603672
if (key.key) {
604673
tmp = zend_hash_find(ht, key.key);
605674
spl_hash_key_release(&key);
@@ -1562,7 +1631,9 @@ static zval *spl_array_it_get_current_data(zend_object_iterator *iter) /* {{{ */
15621631
{
15631632
spl_array_iterator *array_iter = (spl_array_iterator*)iter;
15641633
spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1565-
HashTable *aht = spl_array_get_hash_table(object);
1634+
HashTable *aht = array_iter->by_ref
1635+
? spl_array_get_hash_table_for_write(object)
1636+
: spl_array_get_hash_table(object);
15661637
zval *data = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, object));
15671638
if (data && Z_TYPE_P(data) == IS_INDIRECT) {
15681639
data = Z_INDIRECT_P(data);
@@ -1705,7 +1776,7 @@ PHP_METHOD(ArrayIterator, seek)
17051776
zend_long opos, position;
17061777
zval *object = ZEND_THIS;
17071778
spl_array_object *intern = Z_SPLARRAY_P(object);
1708-
HashTable *aht = spl_array_get_hash_table(intern);
1779+
HashTable *aht;
17091780
int result;
17101781

17111782
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) {
@@ -1720,6 +1791,7 @@ PHP_METHOD(ArrayIterator, seek)
17201791

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

1794+
aht = spl_array_get_hash_table(intern);
17231795
if (result == SUCCESS && zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS) {
17241796
return; /* ok */
17251797
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
ArrayObject storage exchanged by an error handler while creating an element
3+
--FILE--
4+
<?php
5+
$ao = new ArrayObject(['a' => 1], ArrayObject::ARRAY_AS_PROPS);
6+
set_error_handler(function ($errno, $errstr) use ($ao) {
7+
echo $errstr, "\n";
8+
$ao->exchangeArray(['z' => str_repeat('z', 3)]);
9+
});
10+
$ao->b .= 'x';
11+
restore_error_handler();
12+
var_dump($ao->getArrayCopy());
13+
?>
14+
--EXPECT--
15+
Undefined array key "b"
16+
array(2) {
17+
["z"]=>
18+
string(3) "zzz"
19+
["b"]=>
20+
string(1) "x"
21+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
--TEST--
2+
ArrayObject and ArrayIterator separate their storage from the arrays they hand out before writing to it
3+
--FILE--
4+
<?php
5+
// __serialize() and __debugInfo() hand out the storage without copying it:
6+
// every write path separates the storage first
7+
8+
function check(string $label, callable $write, array $classes = ['ArrayIterator', 'ArrayObject']) {
9+
foreach ($classes as $class) {
10+
$expected = ['a' => 1, 'b' => ['x' => 1], 'c' => 3];
11+
$object = new $class($expected, ArrayObject::ARRAY_AS_PROPS);
12+
$serialized = $object->__serialize()[1];
13+
$debug = array_values($object->__debugInfo())[0];
14+
$write($object);
15+
if ($serialized !== $expected || $debug !== $expected) {
16+
echo "$class $label: FAIL\n";
17+
var_dump($serialized, $debug);
18+
}
19+
echo "$class $label: ", json_encode($object->getArrayCopy()), "\n";
20+
}
21+
}
22+
23+
check('offsetSet', function ($o) { $o['a'] = 2; });
24+
check('offsetSet()', function ($o) { $o->offsetSet('a', 2); });
25+
check('append', function ($o) { $o[] = 4; });
26+
check('append()', function ($o) { $o->append(4); });
27+
check('offsetUnset', function ($o) { unset($o['a']); });
28+
check('offsetUnset() missing', function ($o) { $o->offsetUnset('z'); });
29+
check('compound assign', function ($o) { $o['a'] += 10; });
30+
check('increment', function ($o) { $o['a']++; });
31+
check('nested write', function ($o) { $o['b']['y'] = 2; });
32+
check('nested append', function ($o) { $o['b'][] = 2; });
33+
check('nested unset', function ($o) { unset($o['b']['x']); });
34+
check('reference', function ($o) { $r = &$o['a']; $r = 5; });
35+
check('property', function ($o) { $o->a = 6; });
36+
check('property compound assign', function ($o) { $o->a .= 'x'; });
37+
check('property nested write', function ($o) { $o->b['z'] = 3; });
38+
check('property reference', function ($o) { $r = &$o->c; $r = 7; });
39+
check('property unset', function ($o) { unset($o->c); });
40+
check('foreach by reference', function ($o) { foreach ($o as &$v) { $v = 8; } });
41+
check('uasort', function ($o) { $o->uasort(fn ($a, $b) => is_array($a) <=> is_array($b)); }, ['ArrayObject']);
42+
check('ksort', function ($o) { $o->ksort(); $o['d'] = 0; }, ['ArrayObject']);
43+
?>
44+
--EXPECT--
45+
ArrayIterator offsetSet: {"a":2,"b":{"x":1},"c":3}
46+
ArrayObject offsetSet: {"a":2,"b":{"x":1},"c":3}
47+
ArrayIterator offsetSet(): {"a":2,"b":{"x":1},"c":3}
48+
ArrayObject offsetSet(): {"a":2,"b":{"x":1},"c":3}
49+
ArrayIterator append: {"a":1,"b":{"x":1},"c":3,"0":4}
50+
ArrayObject append: {"a":1,"b":{"x":1},"c":3,"0":4}
51+
ArrayIterator append(): {"a":1,"b":{"x":1},"c":3,"0":4}
52+
ArrayObject append(): {"a":1,"b":{"x":1},"c":3,"0":4}
53+
ArrayIterator offsetUnset: {"b":{"x":1},"c":3}
54+
ArrayObject offsetUnset: {"b":{"x":1},"c":3}
55+
ArrayIterator offsetUnset() missing: {"a":1,"b":{"x":1},"c":3}
56+
ArrayObject offsetUnset() missing: {"a":1,"b":{"x":1},"c":3}
57+
ArrayIterator compound assign: {"a":11,"b":{"x":1},"c":3}
58+
ArrayObject compound assign: {"a":11,"b":{"x":1},"c":3}
59+
ArrayIterator increment: {"a":2,"b":{"x":1},"c":3}
60+
ArrayObject increment: {"a":2,"b":{"x":1},"c":3}
61+
ArrayIterator nested write: {"a":1,"b":{"x":1,"y":2},"c":3}
62+
ArrayObject nested write: {"a":1,"b":{"x":1,"y":2},"c":3}
63+
ArrayIterator nested append: {"a":1,"b":{"x":1,"0":2},"c":3}
64+
ArrayObject nested append: {"a":1,"b":{"x":1,"0":2},"c":3}
65+
ArrayIterator nested unset: {"a":1,"b":[],"c":3}
66+
ArrayObject nested unset: {"a":1,"b":[],"c":3}
67+
ArrayIterator reference: {"a":5,"b":{"x":1},"c":3}
68+
ArrayObject reference: {"a":5,"b":{"x":1},"c":3}
69+
ArrayIterator property: {"a":6,"b":{"x":1},"c":3}
70+
ArrayObject property: {"a":6,"b":{"x":1},"c":3}
71+
ArrayIterator property compound assign: {"a":"1x","b":{"x":1},"c":3}
72+
ArrayObject property compound assign: {"a":"1x","b":{"x":1},"c":3}
73+
ArrayIterator property nested write: {"a":1,"b":{"x":1,"z":3},"c":3}
74+
ArrayObject property nested write: {"a":1,"b":{"x":1,"z":3},"c":3}
75+
ArrayIterator property reference: {"a":1,"b":{"x":1},"c":7}
76+
ArrayObject property reference: {"a":1,"b":{"x":1},"c":7}
77+
ArrayIterator property unset: {"a":1,"b":{"x":1}}
78+
ArrayObject property unset: {"a":1,"b":{"x":1}}
79+
ArrayIterator foreach by reference: {"a":8,"b":8,"c":8}
80+
ArrayObject foreach by reference: {"a":8,"b":8,"c":8}
81+
ArrayObject uasort: {"a":1,"c":3,"b":{"x":1}}
82+
ArrayObject ksort: {"a":1,"b":{"x":1},"c":3,"d":0}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--TEST--
2+
ArrayObject storage shared by an error handler while creating an element
3+
--FILE--
4+
<?php
5+
$ao = new ArrayObject(['a' => 1], ArrayObject::ARRAY_AS_PROPS);
6+
set_error_handler(function ($errno, $errstr) use ($ao) {
7+
echo $errstr, "\n";
8+
$GLOBALS['copies'][] = $ao->__serialize()[1];
9+
});
10+
$ao->b .= 'x';
11+
$ao->c++;
12+
$ao['d'] .= 'y';
13+
restore_error_handler();
14+
var_dump($ao->getArrayCopy(), $copies);
15+
?>
16+
--EXPECT--
17+
Undefined array key "b"
18+
Undefined array key "c"
19+
Undefined array key "d"
20+
array(4) {
21+
["a"]=>
22+
int(1)
23+
["b"]=>
24+
string(1) "x"
25+
["c"]=>
26+
int(1)
27+
["d"]=>
28+
string(1) "y"
29+
}
30+
array(3) {
31+
[0]=>
32+
array(1) {
33+
["a"]=>
34+
int(1)
35+
}
36+
[1]=>
37+
array(2) {
38+
["a"]=>
39+
int(1)
40+
["b"]=>
41+
string(1) "x"
42+
}
43+
[2]=>
44+
array(3) {
45+
["a"]=>
46+
int(1)
47+
["b"]=>
48+
string(1) "x"
49+
["c"]=>
50+
int(1)
51+
}
52+
}

0 commit comments

Comments
 (0)