Skip to content

Commit 71eff10

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: Fix GH-21986: PharData::getContent() crash on infinite recursion with symlinks.
2 parents 9d72072 + b2de3cf commit 71eff10

2 files changed

Lines changed: 63 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: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,34 @@ phar_entry_info *phar_get_link_source(phar_entry_info *entry) /* {{{ */
6666
{
6767
phar_entry_info *link_entry;
6868
char *link;
69+
uint32_t depth = 0, max_depth;
6970

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

74-
link = phar_get_link_location(entry);
75-
if (NULL != (link_entry = zend_hash_str_find_ptr(&(entry->phar->manifest), entry->link, strlen(entry->link))) ||
76-
NULL != (link_entry = zend_hash_str_find_ptr(&(entry->phar->manifest), link, strlen(link)))) {
77-
if (link != entry->link) {
78-
efree(link);
75+
max_depth = zend_hash_num_elements(&(entry->phar->manifest));
76+
77+
while (entry->link) {
78+
if (UNEXPECTED(++depth > max_depth)) {
79+
return NULL;
7980
}
80-
return phar_get_link_source(link_entry);
81-
} else {
82-
if (link != entry->link) {
83-
efree(link);
81+
link = phar_get_link_location(entry);
82+
83+
if (NULL != (link_entry = zend_hash_str_find_ptr(&(entry->phar->manifest), entry->link, strlen(entry->link))) ||
84+
NULL != (link_entry = zend_hash_str_find_ptr(&(entry->phar->manifest), link, strlen(link)))) {
85+
if (link != entry->link) {
86+
efree(link);
87+
}
88+
entry = link_entry;
89+
} else {
90+
if (link != entry->link) {
91+
efree(link);
92+
}
93+
return NULL;
8494
}
85-
return NULL;
8695
}
96+
return entry;
8797
}
8898
/* }}} */
8999

0 commit comments

Comments
 (0)