Skip to content

Commit 03fbd46

Browse files
committed
Merge branch 'PHP-8.6'
* PHP-8.6: Fix GH-25091: UAF in phar rename() when source and destination are equal Fix GH-25090: UAF in phar rename() of an entry with metadata
2 parents 9ff9507 + 3cfaeec commit 03fbd46

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎ext/phar/stream.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,11 @@ static int phar_wrapper_rename(php_stream_wrapper *wrapper, const char *url_from
865865
url_from, url_to);
866866
return 0;
867867
}
868+
if (zend_string_equals(resource_from->path, resource_to->path)) {
869+
php_url_free(resource_from);
870+
php_url_free(resource_to);
871+
return 1;
872+
}
868873
/* transfer all data over to the new entry */
869874
memcpy((void *) &new, (void *) entry, sizeof(phar_entry_info));
870875
/* mark the old one for deletion */
@@ -873,6 +878,7 @@ static int phar_wrapper_rename(php_stream_wrapper *wrapper, const char *url_from
873878
ZVAL_UNDEF(&entry->metadata_tracker.val);
874879
entry->symlink = NULL;
875880
entry->tmp = NULL;
881+
entry->metadata_tracker.str = NULL;
876882
source = entry;
877883

878884
/* add to the manifest, and then store the pointer to the new guy in entry

‎ext/phar/tests/gh25090.phpt‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
GH-25090 (Use-after-free in phar_wrapper_rename() with entry metadata)
3+
--EXTENSIONS--
4+
phar
5+
--INI--
6+
phar.readonly=0
7+
--FILE--
8+
<?php
9+
$fname = __DIR__ . '/gh25090.phar';
10+
$phar = new Phar($fname);
11+
$phar['a'] = 'x';
12+
$phar['a']->setMetadata(['k' => 'v']);
13+
rename('phar://' . $fname . '/a', 'phar://' . $fname . '/b');
14+
$phar['c'] = 'y';
15+
var_dump($phar['b']->getMetadata());
16+
?>
17+
--CLEAN--
18+
<?php
19+
@unlink(__DIR__ . '/gh25090.phar');
20+
?>
21+
--EXPECT--
22+
array(1) {
23+
["k"]=>
24+
string(1) "v"
25+
}

‎ext/phar/tests/gh25091.phpt‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
GH-25091: Use-after-free in rename() when source and destination are the same entry
3+
--EXTENSIONS--
4+
phar
5+
--INI--
6+
phar.readonly=0
7+
--FILE--
8+
<?php
9+
$fname = __DIR__ . '/gh25091.phar';
10+
11+
$phar = new Phar($fname);
12+
$phar['a'] = 'contents';
13+
unset($phar);
14+
15+
var_dump(rename('phar://' . $fname . '/a', 'phar://' . $fname . '/a'));
16+
var_dump(file_get_contents('phar://' . $fname . '/a'));
17+
?>
18+
--CLEAN--
19+
<?php
20+
@unlink(__DIR__ . '/gh25091.phar');
21+
?>
22+
--EXPECT--
23+
bool(true)
24+
string(8) "contents"

0 commit comments

Comments
 (0)