Skip to content

Commit

Permalink
Fix #75102: PharData says invalid checksum for valid tar
Browse files Browse the repository at this point in the history
Apparently, there are broken tarballs out there which are actually in
ustar format, but did not write the `ustar` marker.  Since popular tar
tools like GNU tar and 7zip have no issues dealing with such tarballs,
Phar should also be more resilient.

Thus, when the first checksum check of a tarball in (presumed) in old-
style format fails, we check whether the checksum would be suitable for
ustar format; if so, we treat the tarball as being in ustar format.

Closes GH-6479.
  • Loading branch information
cmb69 committed Dec 4, 2020
1 parent 8f8e6f9 commit 8588ae7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ PHP NEWS

- Phar:
. Fixed bug #73809 (Phar Zip parse crash - mmap fail). (cmb)
. Fixed #75102 (`PharData` says invalid checksum for valid tar). (cmb)

- Phpdbg:
. Fixed bug #76813 (Access violation near NULL on source operand). (cmb)
Expand Down
9 changes: 9 additions & 0 deletions ext/phar/tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ int phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, char *alia
memset(hdr->checksum, ' ', sizeof(hdr->checksum));
sum2 = phar_tar_checksum(buf, old?sizeof(old_tar_header):sizeof(tar_header));

if (old && sum2 != sum1) {
uint32_t sum3 = phar_tar_checksum(buf, sizeof(tar_header));
if (sum3 == sum1) {
/* apparently a broken tar which is in ustar format w/o setting the ustar marker */
sum2 = sum3;
old = 0;
}
}

size = entry.uncompressed_filesize = entry.compressed_filesize =
phar_tar_number(hdr->size, sizeof(hdr->size));

Expand Down
13 changes: 13 additions & 0 deletions ext/phar/tests/bug75102.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
Bug #75102 (`PharData` says invalid checksum for valid tar)
--SKIPIF--
<?php
if (!extension_loaded('phar')) die('skip phar extension not available');
?>
--FILE--
<?php
$phar = new PharData(__DIR__ . '/bug75102.tar');
var_dump(file_get_contents($phar['test.txt']->getPathName()));
?>
--EXPECT--
string(9) "yada yada"
Binary file added ext/phar/tests/bug75102.tar
Binary file not shown.

0 comments on commit 8588ae7

Please sign in to comment.