Skip to content

Commit 0e34806

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Fix GH-21986: PharData::getContent() crash on infinite recursion with symlinks.
2 parents 4c027ea + 71eff10 commit 0e34806

2 files changed

Lines changed: 64 additions & 10 deletions

File tree

ext/phar/tests/tar/gh21986.phpt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
GH-21986 (PharData::getContent() crash on circular symlink chain in tar)
3+
--EXTENSIONS--
4+
phar
5+
--FILE--
6+
<?php
7+
function tar_symlink($name, $target) {
8+
$h = str_pad($name, 100, "\0");
9+
$h .= "0000777\0";
10+
$h .= "0000000\0";
11+
$h .= "0000000\0";
12+
$h .= "00000000000\0";
13+
$h .= "00000000000\0";
14+
$h .= str_repeat(' ', 8);
15+
$h .= '2';
16+
$h .= str_pad($target, 100, "\0");
17+
$h .= "ustar\0" . "00";
18+
$h = str_pad($h, 512, "\0");
19+
20+
$sum = 0;
21+
for ($i = 0; $i < 512; $i++) {
22+
$sum += ord($h[$i]);
23+
}
24+
return substr_replace($h, sprintf("%06o\0 ", $sum), 148, 8);
25+
}
26+
27+
$tar = __DIR__ . '/gh21986.tar';
28+
file_put_contents(
29+
$tar,
30+
tar_symlink('a.txt', 'b.txt') .
31+
tar_symlink('b.txt', 'a.txt') .
32+
str_repeat("\0", 1024)
33+
);
34+
35+
$phar = new PharData($tar);
36+
var_dump($phar['a.txt']->getContent());
37+
?>
38+
--CLEAN--
39+
<?php
40+
@unlink(__DIR__ . '/gh21986.tar');
41+
?>
42+
--EXPECT--
43+
string(0) ""

ext/phar/util.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,34 @@ static zend_string *phar_get_link_location(phar_entry_info *entry) /* {{{ */
6666
phar_entry_info *phar_get_link_source(phar_entry_info *entry) /* {{{ */
6767
{
6868
phar_entry_info *link_entry;
69+
uint32_t depth = 0, max_depth;
6970

7071
if (!entry->symlink) {
7172
return entry;
7273
}
7374

74-
link_entry = zend_hash_find_ptr(&(entry->phar->manifest), entry->symlink);
75-
if (link_entry) {
76-
return phar_get_link_source(link_entry);
77-
}
75+
max_depth = zend_hash_num_elements(&(entry->phar->manifest));
76+
77+
while (entry->symlink) {
78+
if (UNEXPECTED(++depth > max_depth)) {
79+
return NULL;
80+
}
81+
zend_string *link = phar_get_link_location(entry);
7882

79-
zend_string *link = phar_get_link_location(entry);
80-
link_entry = zend_hash_find_ptr(&(entry->phar->manifest), link);
81-
zend_string_release(link);
82-
if (link_entry) {
83-
return phar_get_link_source(link_entry);
83+
if (NULL != (link_entry = zend_hash_find_ptr(&(entry->phar->manifest), entry->symlink)) ||
84+
NULL != (link_entry = zend_hash_find_ptr(&(entry->phar->manifest), link))) {
85+
if (link != entry->symlink) {
86+
zend_string_release(link);
87+
}
88+
entry = link_entry;
89+
} else {
90+
if (link != entry->symlink) {
91+
zend_string_release(link);
92+
}
93+
return NULL;
94+
}
8495
}
85-
return NULL;
96+
return entry;
8697
}
8798
/* }}} */
8899

0 commit comments

Comments
 (0)