Skip to content

Commit 300bb17

Browse files
committed
Merge branch 'PHP-8.5' into PHP-8.6
2 parents fc68de6 + 1c96238 commit 300bb17

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ PHP NEWS
5454
. Fixed bug GH-24461 (DISCARD ALL on persistent connection cleanup drops
5555
PgBouncer's prepared statements). (David Carlier, Artem Ukrainskiy)
5656

57+
- Phar:
58+
. Fixed bug GH-24740 (Oversized tar metadata exhausts the memory limit).
59+
(Lazizbek Ergashev)
60+
5761
- SAPI:
5862
. Fixed infinite loop in sapi_header_op() for headers of 4 GiB or more.
5963
(Martin Mirchev)

‎ext/phar/tar.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ zend_result phar_parse_tarfile(
602602
}
603603

604604
if (zend_string_starts_with_literal(entry.filename, ".phar/.metadata")) {
605-
if (FAILURE == phar_tar_process_metadata(newentry, fp)) {
605+
if (size > totalsize || FAILURE == phar_tar_process_metadata(newentry, fp)) {
606606
if (error) {
607607
spprintf(error, 4096, "phar error: tar-based phar \"%s\" has invalid metadata in magic file \"%s\"", fname, ZSTR_VAL(entry.filename));
608608
}

‎ext/phar/tests/tar/gh24740.phpt‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
GH-24740 (Unbounded size of .phar/.metadata* entry in TAR phar)
3+
--EXTENSIONS--
4+
phar
5+
--INI--
6+
memory_limit=16M
7+
--FILE--
8+
<?php
9+
function tar_header(string $name, int $size): string
10+
{
11+
$header = str_repeat("\0", 512);
12+
$header = substr_replace($header, $name, 0, strlen($name));
13+
$header = substr_replace($header, sprintf("%07o\0", 0644), 100, 8);
14+
$header = substr_replace($header, sprintf("%07o\0", 0), 108, 8);
15+
$header = substr_replace($header, sprintf("%07o\0", 0), 116, 8);
16+
$header = substr_replace($header, sprintf("%011o\0", $size), 124, 12);
17+
$header = substr_replace($header, sprintf("%011o\0", 1700000000), 136, 12);
18+
$header[156] = '0';
19+
$header = substr_replace($header, "ustar\0", 257, 6);
20+
$header = substr_replace($header, "00", 263, 2);
21+
22+
$checksum = 0;
23+
for ($i = 0; $i < 512; $i++) {
24+
$checksum += ($i >= 148 && $i < 156) ? 0x20 : ord($header[$i]);
25+
}
26+
27+
return substr_replace($header, sprintf("%06o\0 ", $checksum), 148, 8);
28+
}
29+
30+
$fname = __DIR__ . '/gh24740.tar';
31+
file_put_contents($fname, tar_header('.phar/.metadata.bin', 0x7FFFFE00) . str_repeat("\0", 1024));
32+
33+
try {
34+
new PharData($fname);
35+
} catch (UnexpectedValueException $e) {
36+
echo $e->getMessage(), "\n";
37+
}
38+
?>
39+
--CLEAN--
40+
<?php
41+
@unlink(__DIR__ . '/gh24740.tar');
42+
?>
43+
--EXPECTF--
44+
phar error: tar-based phar "%sgh24740.tar" has invalid metadata in magic file ".phar/.metadata.bin"

0 commit comments

Comments
 (0)