Skip to content

Fix GH-16695: phar:// tar parser and zero-length file header blocks #16700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/phar/phar.c
Original file line number Diff line number Diff line change
Expand Up @@ -1774,7 +1774,7 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
return phar_parse_zipfile(fp, fname, fname_len, alias, alias_len, pphar, error);
}

if (got > 512) {
if (got >= 512) {
if (phar_is_tar(pos, fname)) {
php_stream_rewind(fp);
return phar_parse_tarfile(fp, fname, fname_len, alias, alias_len, pphar, is_data, compression, error);
Expand Down
10 changes: 7 additions & 3 deletions ext/phar/tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,8 @@ int phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, char *alia
entry.is_tar = 1;
entry.is_crc_checked = 1;
entry.phar = myphar;
pos += sizeof(buf);

do {
while (true) {
phar_entry_info *newentry;

pos = php_stream_tell(fp);
Expand Down Expand Up @@ -597,6 +596,11 @@ int phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, char *alia
}
}

/* Only read next header if we're not yet at the end */
if (php_stream_tell(fp) == totalsize) {
break;
}

read = php_stream_read(fp, buf, sizeof(buf));

if (read != sizeof(buf)) {
Expand All @@ -607,7 +611,7 @@ int phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, char *alia
phar_destroy_phar_data(myphar);
return FAILURE;
}
} while (!php_stream_eof(fp));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it a problem then that the eof test has been removed? Here now 261: pos = php_stream_tell(fp); pos is unchecked for error returns (are there any?; was unchecked before, too).

Probably can be moved to now 258 while (true) { as while (!php_stream_eof(fp)) { ? @nielsdos

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

php_stream_tell(fp) cannot fail.
while (!php_stream_eof(fp)) { won't work because the flag is not set in time, leading to the same issue as before.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nielsdos Thank you for the clarification.

}

if (zend_hash_str_exists(&(myphar->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
myphar->is_data = 0;
Expand Down
28 changes: 28 additions & 0 deletions ext/phar/tests/tar/gh16695_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
GH-16695 (phar:// tar parser and zero-length file header blocks)
--CREDITS--
hakre
Comment on lines +3 to +4
Copy link
Contributor

@hakre hakre Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have read this in CONTRIBUTING.md:

For newly created tests, a --CREDITS-- section should no longer be included,
as test authorship is already accurately tracked by Git. If multiple authors
should be credited, the Co-authored-by tag in the commit message may be used.

ref: 7514b5d ("Mention that zend_parse_parameters should not be tested", 2019-07-07)

at least fyi @nielsdos , if you consider this and you need a user/email for the Co-authored-by trailer: Hans Krentel (hakre) <hanskrentel@yahoo.de>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I know the CONTRIBUTING doc says that, but I find it a stupid rule because the Co-authored-by tag is not granular. (it's barely a rule imo due to the weak wording). However, a big chunk of the patch is tests of yours so I'll add a Co-authored-by tag upon merging.

--EXTENSIONS--
phar
--INI--
phar.require_hash=0
--FILE--
<?php

$reportTar = __DIR__.'/gh16695_1.tmp';

$length = file_put_contents($reportTar, base64_decode('dGxzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAwMDA3MDAAMDAwMDAwMAAwMDAwMDAwADAwMDAwMDAwMDAwADAwMDAwMDAwMDAwADAwNzcxNgAgNQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB1c3RhcgAwMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwMDAwMDAwADAwMDAwMDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA='));
var_dump($length);
$buffer = file_get_contents("phar://$reportTar/tls");
var_dump($buffer);

?>
--CLEAN--
<?php
@unlink(__DIR__.'/gh16695_1.tmp');
?>
--EXPECTF--
int(512)

Warning: file_get_contents(%stls): Failed to open stream: phar error: path "tls" is a directory in %s on line %d
bool(false)
26 changes: 26 additions & 0 deletions ext/phar/tests/tar/gh16695_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
GH-16695 (phar:// tar parser and zero-length file header blocks)
--CREDITS--
hakre
--EXTENSIONS--
phar
--INI--
phar.require_hash=0
--FILE--
<?php

$reportTar = __DIR__.'/gh16695_2.tmp';

$length = file_put_contents($reportTar, base64_decode('bWV0YS5qc29uAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=='));
var_dump($length);
$buffer = file_get_contents("phar://$reportTar/meta.json");
var_dump($buffer);

?>
--CLEAN--
<?php
@unlink(__DIR__.'/gh16695_2.tmp');
?>
--EXPECT--
int(1024)
string(122) "{"Name":"default","Metadata":{},"Endpoints":{"docker":{"Host":"unix:///run/user/1000/docker.sock","SkipTLSVerify":false}}}"
26 changes: 26 additions & 0 deletions ext/phar/tests/tar/gh16695_3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
GH-16695 (phar:// tar parser and zero-length file header blocks)
--CREDITS--
hakre
--EXTENSIONS--
phar
--INI--
phar.require_hash=0
--FILE--
<?php

$reportTar = __DIR__.'/gh16695_3.tmp';

$length = file_put_contents($reportTar, base64_decode('dGxzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAwMDA3MDAAMDAwMDAwMAAwMDAwMDAwADAwMDAwMDAwMDAwADAwMDAwMDAwMDAwADAwNzcxMQAgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB1c3RhcgAwMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwMDAwMDAwADAwMDAwMDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA='));
var_dump($length);
$buffer = file_get_contents("phar://$reportTar/tls");
var_dump($buffer);

?>
--CLEAN--
<?php
@unlink(__DIR__.'/gh16695_3.tmp');
?>
--EXPECT--
int(512)
string(0) ""
Loading