Skip to content

Commit ef6fe39

Browse files
cmb69sgolemon
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 afef2f0 commit ef6fe39

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

ext/phar/phar.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,8 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
16351635
const char zip_magic[] = "PK\x03\x04";
16361636
const char gz_magic[] = "\x1f\x8b\x08";
16371637
const char bz_magic[] = "BZh";
1638-
char *pos, test = '\0';
1638+
char *pos;
1639+
int recursion_count = 3; // arbitrary limit to avoid too deep or even infinite recursion
16391640
const int window_size = 1024;
16401641
char buffer[1024 + sizeof(token)]; /* a 1024 byte window + the size of the halt_compiler token (moving window) */
16411642
const zend_long readsize = sizeof(buffer) - sizeof(token);
@@ -1663,8 +1664,7 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
16631664
MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated entry)")
16641665
}
16651666

1666-
if (!test) {
1667-
test = '\1';
1667+
if (recursion_count) {
16681668
pos = buffer+tokenlen;
16691669
if (!memcmp(pos, gz_magic, 3)) {
16701670
char err = 0;
@@ -1724,7 +1724,10 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
17241724
compression = PHAR_FILE_COMPRESSED_GZ;
17251725

17261726
/* now, start over */
1727-
test = '\0';
1727+
if (!--recursion_count) {
1728+
MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\"");
1729+
break;
1730+
}
17281731
continue;
17291732
} else if (!memcmp(pos, bz_magic, 3)) {
17301733
php_stream_filter *filter;
@@ -1762,7 +1765,10 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
17621765
compression = PHAR_FILE_COMPRESSED_BZ2;
17631766

17641767
/* now, start over */
1765-
test = '\0';
1768+
if (!--recursion_count) {
1769+
MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\"");
1770+
break;
1771+
}
17661772
continue;
17671773
}
17681774

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)