Skip to content

Commit 4ef300f

Browse files
cmb69patrickallaert
authored andcommitted
Fix #81726: phar wrapper: DOS when using quine gzip file
The phar wrapper needs to uncompress the file; the uncompressed file might be compressed, so the wrapper implementation loops. This raises potential DOS issues regarding too deep or even infinite recursion (the latter are called compressed file quines[1]). We avoid that by introducing a recursion limit; we choose the somewhat arbitrary limit `3`. This issue has been reported by real_as3617 and gPayl0ad. [1] <https://honno.dev/gzip-quine/>
1 parent 7fd14eb commit 4ef300f

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ PHP NEWS
33
?? ??? ????, PHP 8.1.11
44

55
- Core:
6+
. Fixed bug #81726: phar wrapper: DOS when using quine gzip file.
7+
(CVE-2022-31628). (cmb)
68
. Fixed bug GH-9323 (Crash in ZEND_RETURN/GC/zend_call_function)
79
(Tim Starling)
810
. Fixed bug GH-9361 (Segmentation fault on script exit #9379). (cmb,

ext/phar/phar.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,8 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
16261626
const char zip_magic[] = "PK\x03\x04";
16271627
const char gz_magic[] = "\x1f\x8b\x08";
16281628
const char bz_magic[] = "BZh";
1629-
char *pos, test = '\0';
1629+
char *pos;
1630+
int recursion_count = 3; // arbitrary limit to avoid too deep or even infinite recursion
16301631
const int window_size = 1024;
16311632
char buffer[1024 + sizeof(token)]; /* a 1024 byte window + the size of the halt_compiler token (moving window) */
16321633
const zend_long readsize = sizeof(buffer) - sizeof(token);
@@ -1654,8 +1655,7 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
16541655
MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated entry)")
16551656
}
16561657

1657-
if (!test) {
1658-
test = '\1';
1658+
if (recursion_count) {
16591659
pos = buffer+tokenlen;
16601660
if (!memcmp(pos, gz_magic, 3)) {
16611661
char err = 0;
@@ -1715,7 +1715,10 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
17151715
compression = PHAR_FILE_COMPRESSED_GZ;
17161716

17171717
/* now, start over */
1718-
test = '\0';
1718+
if (!--recursion_count) {
1719+
MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\"");
1720+
break;
1721+
}
17191722
continue;
17201723
} else if (!memcmp(pos, bz_magic, 3)) {
17211724
php_stream_filter *filter;
@@ -1753,7 +1756,10 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
17531756
compression = PHAR_FILE_COMPRESSED_BZ2;
17541757

17551758
/* now, start over */
1756-
test = '\0';
1759+
if (!--recursion_count) {
1760+
MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\"");
1761+
break;
1762+
}
17571763
continue;
17581764
}
17591765

ext/phar/tests/bug81726.gz

204 Bytes
Binary file not shown.

ext/phar/tests/bug81726.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Bug #81726 (phar wrapper: DOS when using quine gzip file)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("phar")) die("skip phar extension not available");
6+
if (!extension_loaded("zlib")) die("skip zlib extension not available");
7+
?>
8+
--FILE--
9+
<?php
10+
var_dump(fopen("phar://" . __DIR__ . "/bug81726.gz", "r"));
11+
?>
12+
--EXPECTF--
13+
Warning: fopen(phar://%s): failed to open stream: unable to decompress gzipped phar archive "%s" in %s on line %d
14+
bool(false)

0 commit comments

Comments
 (0)