Skip to content

Commit e0f5d62

Browse files
committed
Fix bug #77586 - phar_tar_writeheaders_int() buffer overflow
1 parent 759e841 commit e0f5d62

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

NEWS

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ PHP NEWS
33
?? ??? 2019, PHP 7.1.27
44

55
- Core:
6-
. Fixed bug #77630 (rename() across the device may allow unwanted access during
6+
. Fixed bug #77630 (rename() across the device may allow unwanted access during
77
processing). (Stas)
8-
8+
99
- EXIF:
1010
. Fixed bug #77509 (Uninitialized read in exif_process_IFD_in_TIFF). (Stas)
11-
. Fixed bug #77540 (Invalid Read on exif_process_SOFn). (Stas)
12-
. Fixed bug #77563 (Uninitialized read in exif_process_IFD_in_MAKERNOTE). (Stas)
11+
. Fixed bug #77540 (Invalid Read on exif_process_SOFn). (Stas)
12+
. Fixed bug #77563 (Uninitialized read in exif_process_IFD_in_MAKERNOTE). (Stas)
1313
. Fixed bug #77659 (Uninitialized read in exif_process_IFD_in_MAKERNOTE). (Stas)
1414

1515
- PHAR:
1616
. Fixed bug #77396 (Null Pointer Dereference in phar_create_or_parse_filename).
17-
(bishop)
17+
(bishop)
18+
. Fixed bug #77586 (phar_tar_writeheaders_int() buffer overflow). (bishop)
1819

1920
- SPL:
2021
. Fixed bug #77431 (openFile() silently truncates after a null byte). (cmb)

ext/phar/tar.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,12 @@ static int phar_tar_writeheaders_int(phar_entry_info *entry, void *argument) /*
762762
header.typeflag = entry->tar_type;
763763

764764
if (entry->link) {
765-
strncpy(header.linkname, entry->link, strlen(entry->link));
765+
if (strlcpy(header.linkname, entry->link, sizeof(header.linkname)) >= sizeof(header.linkname)) {
766+
if (fp->error) {
767+
spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, link \"%s\" is too long for format", entry->phar->fname, entry->link);
768+
}
769+
return ZEND_HASH_APPLY_STOP;
770+
}
766771
}
767772

768773
strncpy(header.magic, "ustar", sizeof("ustar")-1);

ext/phar/tests/bug71488.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ DONE
1313
<?php
1414
@unlink(__DIR__."/bug71488.test");
1515
?>
16-
--EXPECT--
17-
DONE
16+
--EXPECTF--
17+
Fatal error: Uncaught BadMethodCallException: tar-based phar "%s/bug71488.test" cannot be created, link "%s" is too long for format in %sbug71488.php:%d
18+
Stack trace:%A

ext/phar/tests/bug77586.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Bug #77586 Symbolic link names in tar-formatted phar must be less than 100 bytes.
3+
--SKIPIF--
4+
<?php if (!extension_loaded("phar") || true /* blocked by bug 65332 */) die("skip"); ?>
5+
--FILE--
6+
<?php
7+
$dir = __DIR__."/bug77586";
8+
$phar = new PharData($dir . "/bug77586.tar");
9+
$phar->buildFromDirectory($dir . "/files");
10+
?>
11+
--CLEAN--
12+
<?php
13+
$dir = __DIR__."/bug77586";
14+
unlink($dir . "/bug77586.tar");
15+
?>
16+
--EXPECTF--
17+
Fatal error: Uncaught PharException: tar-based phar "%s/bug77586.tar" cannot be created, link "%s" is too long for format %s
18+
Stack trace:
19+
#0 %s/bug77586.php(%d): PharData->buildFromDirectory('%s')
20+
#1 {main}
21+
thrown in %s/bug77586.php %s on line %d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

0 commit comments

Comments
 (0)