Skip to content

Commit dc4bfef

Browse files
hohphu8LamentXU123
authored andcommitted
Fix phar_set_inode() using the zend_string header instead of the filename (#25768)
Copy the filename bytes when computing the inode for a Phar entry. Copying from the zend_string header can give entries with different, same-length filenames the same inode number. Add a regression test that checks the inodes after reopening the archive in a separate process. Closes #25768
1 parent 351c6a9 commit dc4bfef

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ PHP NEWS
166166
for an empty set is modified). (Tomasz Kowalczyk)
167167

168168
- Phar:
169+
. Fixed Phar file stats using the string header instead of the entry
170+
filename to compute the inode number. (Hohphu)
169171
. Fixed bug GH-25528 (Phar::setSignatureAlgorithm(): OpenSSL signing reads a
170172
freed private key). (Artem Ukrainskiy)
171173

‎ext/phar/phar_internal.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ static inline void phar_set_inode(phar_entry_info *entry) /* {{{ */
425425
}
426426

427427
len2 = MIN(tmp_len - len1, ZSTR_LEN(entry->filename));
428-
memcpy(tmp + len1, entry->filename, len2);
428+
memcpy(tmp + len1, ZSTR_VAL(entry->filename), len2);
429429

430430
entry->inode = (unsigned short) zend_hash_func(tmp, tmp_len);
431431
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
Phar: distinct same-length filenames in a phar archive should not collide on st_ino (phar_set_inode)
3+
--EXTENSIONS--
4+
phar
5+
--INI--
6+
phar.readonly=0
7+
--FILE--
8+
<?php
9+
$fname = __DIR__ . '/h13_inode_test.phar';
10+
@unlink($fname);
11+
12+
$p = new Phar($fname);
13+
$p['aaaaa.txt'] = '1';
14+
$p['bbbbb.txt'] = '2';
15+
$p['cccccc.txt'] = '3';
16+
unset($p);
17+
18+
// Verify in a clean subprocess (-n) so PHAR_G(phar_fname_map) cache is not reused
19+
$code = <<<'PHP'
20+
$f = $argv[1];
21+
$a = stat("phar://$f/aaaaa.txt")["ino"];
22+
$b = stat("phar://$f/bbbbb.txt")["ino"];
23+
$c = stat("phar://$f/cccccc.txt")["ino"];
24+
var_dump($a > 0, $a !== $b, $a !== $c, $b !== $c);
25+
PHP;
26+
27+
$spec = [
28+
0 => ['pipe', 'r'],
29+
1 => ['pipe', 'w'],
30+
2 => ['pipe', 'w'],
31+
];
32+
$proc = proc_open([PHP_BINARY, '-n', '-r', $code, '--', $fname], $spec, $pipes);
33+
fclose($pipes[0]);
34+
$stdout = stream_get_contents($pipes[1]);
35+
$stderr = stream_get_contents($pipes[2]);
36+
fclose($pipes[1]);
37+
fclose($pipes[2]);
38+
$rc = proc_close($proc);
39+
40+
var_dump($rc === 0, $stderr === '');
41+
echo $stdout;
42+
?>
43+
--CLEAN--
44+
<?php
45+
@unlink(__DIR__ . '/h13_inode_test.phar');
46+
?>
47+
--EXPECT--
48+
bool(true)
49+
bool(true)
50+
bool(true)
51+
bool(true)
52+
bool(true)
53+
bool(true)

0 commit comments

Comments
 (0)