Skip to content

Commit eab209d

Browse files
committed
Fix GH-17518: offset overflow phar extractTo()
`search` can be the empty string, so we need to check the length before checking the last char. Closes GH-17519.
1 parent a1d1269 commit eab209d

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ PHP NEWS
3939
- Opcache:
4040
. Fixed bug GH-17307 (Internal closure causes JIT failure). (nielsdos)
4141

42+
- Phar:
43+
. Fixed bug GH-17518 (offset overflow phar extractTo()). (nielsdos)
44+
4245
- PHPDBG:
4346
. Fix crashes in function registration + test. (nielsdos, Girgias)
4447

ext/phar/phar_object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4306,7 +4306,7 @@ static int extract_helper(phar_archive_data *archive, zend_string *search, char
43064306
if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, error)) return -1;
43074307
extracted++;
43084308
} ZEND_HASH_FOREACH_END();
4309-
} else if ('/' == ZSTR_VAL(search)[ZSTR_LEN(search) - 1]) {
4309+
} else if (ZSTR_LEN(search) > 0 && '/' == ZSTR_VAL(search)[ZSTR_LEN(search) - 1]) {
43104310
/* ends in "/" -- extract all entries having that prefix */
43114311
ZEND_HASH_MAP_FOREACH_PTR(&archive->manifest, entry) {
43124312
if (0 != strncmp(ZSTR_VAL(search), entry->filename, ZSTR_LEN(search))) continue;

ext/phar/tests/gh17518.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
GH-17518 (offset overflow phar extractTo())
3+
--EXTENSIONS--
4+
phar
5+
--INI--
6+
phar.readonly=0
7+
--FILE--
8+
<?php
9+
$fname = __DIR__.'/gh17518.phar.php';
10+
$phar = new Phar($fname);
11+
$phar['a'] = 'b';
12+
try {
13+
$phar->extractTo(__DIR__ . '/gh17518', '');
14+
} catch (Throwable $e) {
15+
echo $e::class, ": ", $e->getMessage(), "\n";
16+
}
17+
?>
18+
--CLEAN--
19+
<?php
20+
@unlink(__DIR__.'/gh17518.phar.php');
21+
?>
22+
--EXPECTF--
23+
PharException: phar error: attempted to extract non-existent file or directory "" from phar "%sgh17518.phar.php"

0 commit comments

Comments
 (0)