Skip to content

Commit a6fdc5b

Browse files
committed
Fix bug #71498: Out-of-Bound Read in phar_parse_zipfile()
1 parent b1bd411 commit a6fdc5b

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

ext/phar/tests/bug71488.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Phar: bug #71488: Stack overflow when decompressing tar archives
77
$p = new PharData(__DIR__."/bug71488.tar");
88
$newp = $p->decompress("test");
99
?>
10+
1011
DONE
1112
--CLEAN--
1213
<?php

ext/phar/tests/bug71498.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Phar: bug #71498: Out-of-Bound Read in phar_parse_zipfile()
3+
--SKIPIF--
4+
<?php if (!extension_loaded("phar")) die("skip"); ?>
5+
--FILE--
6+
<?php
7+
try {
8+
$p = new PharData(__DIR__."/bug71498.zip");
9+
} catch(UnexpectedValueException $e) {
10+
echo $e->getMessage();
11+
}
12+
?>
13+
14+
DONE
15+
--EXPECTF--
16+
phar error: end of central directory not found in zip-based phar "%s/bug71498.zip"
17+
DONE

ext/phar/tests/bug71498.zip

64.1 KB
Binary file not shown.

ext/phar/zip.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ static void phar_zip_u2d_time(time_t time, char *dtime, char *ddate) /* {{{ */
159159
*
160160
* Parse a new one and add it to the cache, returning either SUCCESS or
161161
* FAILURE, and setting pphar to the pointer to the manifest entry
162-
*
162+
*
163163
* This is used by phar_open_from_fp to process a zip-based phar, but can be called
164164
* directly.
165165
*/
@@ -199,7 +199,7 @@ int phar_parse_zipfile(php_stream *fp, char *fname, int fname_len, char *alias,
199199
}
200200

201201
while ((p=(char *) memchr(p + 1, 'P', (size_t) (size - (p + 1 - buf)))) != NULL) {
202-
if (!memcmp(p + 1, "K\5\6", 3)) {
202+
if ((p - buf) + sizeof(locator) <= size && !memcmp(p + 1, "K\5\6", 3)) {
203203
memcpy((void *)&locator, (void *) p, sizeof(locator));
204204
if (PHAR_GET_16(locator.centraldisk) != 0 || PHAR_GET_16(locator.disknumber) != 0) {
205205
/* split archives not handled */
@@ -1161,7 +1161,7 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, long len, int defau
11611161
static const char newstub[] = "<?php // zip-based phar archive stub file\n__HALT_COMPILER();";
11621162
char halt_stub[] = "__HALT_COMPILER();";
11631163
char *tmp;
1164-
1164+
11651165
php_stream *stubfile, *oldfile;
11661166
php_serialize_data_t metadata_hash;
11671167
int free_user_stub, closeoldfile = 0;

0 commit comments

Comments
 (0)