Skip to content

Commit f59b67a

Browse files
committed
Fix bug #69441 (Buffer Overflow when parsing tar/zip/phar in phar_set_inode)
1 parent 45facd1 commit f59b67a

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

ext/phar/phar_internal.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,10 +618,13 @@ static inline void phar_set_inode(phar_entry_info *entry TSRMLS_DC) /* {{{ */
618618
{
619619
char tmp[MAXPATHLEN];
620620
int tmp_len;
621+
size_t len;
621622

622-
tmp_len = entry->filename_len + entry->phar->fname_len;
623-
memcpy(tmp, entry->phar->fname, entry->phar->fname_len);
624-
memcpy(tmp + entry->phar->fname_len, entry->filename, entry->filename_len);
623+
tmp_len = MIN(MAXPATHLEN, entry->filename_len + entry->phar->fname_len);
624+
len = MIN(entry->phar->fname_len, tmp_len);
625+
memcpy(tmp, entry->phar->fname, len);
626+
len = MIN(tmp_len - len, entry->filename_len);
627+
memcpy(tmp + entry->phar->fname_len, entry->filename, len);
625628
entry->inode = (unsigned short)zend_get_hash_value(tmp, tmp_len);
626629
}
627630
/* }}} */

ext/phar/tests/bug69441.phar

5.64 KB
Binary file not shown.

ext/phar/tests/bug69441.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Phar: bug #69441: Buffer Overflow when parsing tar/zip/phar in phar_set_inode
3+
--SKIPIF--
4+
<?php if (!extension_loaded("phar")) die("skip"); ?>
5+
--FILE--
6+
<?php
7+
$fname = dirname(__FILE__) . '/bug69441.phar';
8+
try {
9+
$r = new Phar($fname, 0);
10+
} catch(UnexpectedValueException $e) {
11+
echo $e;
12+
}
13+
?>
14+
15+
==DONE==
16+
--EXPECTF--
17+
exception 'UnexpectedValueException' with message 'phar error: corrupted central directory entry, no magic signature in zip-based phar "%s/bug69441.phar"' in %s/bug69441.php:%d
18+
Stack trace:
19+
#0 %s/bug69441.php(%d): Phar->__construct('%s', 0)
20+
#1 {main}
21+
==DONE==

0 commit comments

Comments
 (0)