Skip to content

Commit 79e66f1

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: Fix use-after-free in RecursiveIteratorIterator on reentrant teardown
2 parents 860c33b + efbf473 commit 79e66f1

3 files changed

Lines changed: 105 additions & 2 deletions

File tree

‎ext/spl/spl_iterators.c‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ static void spl_recursive_it_move_forward_ex(spl_recursive_it_object *object, zv
257257
zend_class_entry *ce;
258258
zval retval, child;
259259
zend_object_iterator *sub_iter;
260+
zend_object *sub_object;
261+
uint32_t prev_level;
262+
zend_result valid_result;
263+
bool reentered;
260264

261265
SPL_FETCH_SUB_ITERATOR(iterator, object);
262266

@@ -276,7 +280,17 @@ static void spl_recursive_it_move_forward_ex(spl_recursive_it_object *object, zv
276280
iterator = object->iterators[object->level].iterator;
277281
ZEND_FALLTHROUGH;
278282
case RS_START:
279-
if (iterator->funcs->valid(iterator) == FAILURE) {
283+
sub_object = Z_OBJ(object->iterators[object->level].zobject);
284+
prev_level = object->level;
285+
GC_ADDREF(sub_object);
286+
valid_result = iterator->funcs->valid(iterator);
287+
reentered = object->level != prev_level
288+
|| object->iterators[object->level].iterator != iterator;
289+
OBJ_RELEASE(sub_object);
290+
if (reentered) {
291+
return;
292+
}
293+
if (valid_result == FAILURE) {
280294
break;
281295
}
282296
object->iterators[object->level].state = RS_TEST;
@@ -424,7 +438,7 @@ static void spl_recursive_it_move_forward_ex(spl_recursive_it_object *object, zv
424438
}
425439
}
426440
}
427-
if (object->level > 0) {
441+
if (object->level > 0 && object->iterators[object->level].iterator == iterator) {
428442
zval garbage;
429443
ZVAL_COPY_VALUE(&garbage, &object->iterators[object->level].zobject);
430444
ZVAL_UNDEF(&object->iterators[object->level].zobject);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
RecursiveIteratorIterator: next() re-entered from endChildren() must not use-after-free
3+
--FILE--
4+
<?php
5+
class RIt implements RecursiveIterator {
6+
public $data;
7+
public $pos = 0;
8+
function __construct(array $d) { $this->data = $d; }
9+
function current(): mixed { return $this->data[$this->pos] ?? null; }
10+
function key(): mixed { return $this->pos; }
11+
function next(): void { $this->pos++; }
12+
function rewind(): void { $this->pos = 0; }
13+
function valid(): bool { return $this->pos < count($this->data); }
14+
function hasChildren(): bool { return is_array($this->current()); }
15+
function getChildren(): RecursiveIterator { return new RIt($this->current()); }
16+
}
17+
18+
class ReenterRII extends RecursiveIteratorIterator {
19+
public static int $fired = 0;
20+
function endChildren(): void {
21+
if (self::$fired++ === 0) {
22+
$this->next();
23+
}
24+
}
25+
}
26+
27+
$tree = [[[1, 2], 3], 4];
28+
$it = new ReenterRII(new RIt($tree), RecursiveIteratorIterator::SELF_FIRST);
29+
$seen = [];
30+
foreach ($it as $v) {
31+
$seen[] = is_array($v) ? 'array' : $v;
32+
if (count($seen) > 20) {
33+
$seen[] = '...';
34+
break;
35+
}
36+
}
37+
echo implode(' ', $seen), "\n";
38+
echo "done\n";
39+
?>
40+
--EXPECT--
41+
array array 1 2 4
42+
done
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
RecursiveIteratorIterator: next() re-entered from a sub-iterator valid() must not use-after-free
3+
--FILE--
4+
<?php
5+
class RIt implements RecursiveIterator {
6+
public $data;
7+
public $pos = 0;
8+
public $rii;
9+
public $depth;
10+
public static bool $fired = false;
11+
function __construct(array $d, $depth = 0) { $this->data = $d; $this->depth = $depth; }
12+
function current(): mixed { return $this->data[$this->pos] ?? null; }
13+
function key(): mixed { return $this->pos; }
14+
function next(): void { $this->pos++; }
15+
function rewind(): void { $this->pos = 0; }
16+
function valid(): bool {
17+
if ($this->rii && $this->depth === 2 && $this->pos === count($this->data) && !self::$fired) {
18+
self::$fired = true;
19+
$this->rii->next();
20+
}
21+
return $this->pos < count($this->data);
22+
}
23+
function hasChildren(): bool { return is_array($this->current()); }
24+
function getChildren(): RecursiveIterator {
25+
$c = new RIt($this->current(), $this->depth + 1);
26+
$c->rii = $this->rii;
27+
return $c;
28+
}
29+
}
30+
31+
$root = new RIt([[[1, 2], 3], 4]);
32+
$rii = new RecursiveIteratorIterator($root, RecursiveIteratorIterator::SELF_FIRST);
33+
$root->rii = $rii;
34+
$seen = [];
35+
foreach ($rii as $v) {
36+
$seen[] = is_array($v) ? 'array' : $v;
37+
if (count($seen) > 20) {
38+
$seen[] = '...';
39+
break;
40+
}
41+
}
42+
echo implode(' ', $seen), "\n";
43+
echo "done\n";
44+
?>
45+
--EXPECT--
46+
array array 1 2 3 4
47+
done

0 commit comments

Comments
 (0)