Skip to content

Prevent double-free of Phar ZIP stream #6578

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 2 commits 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/tests/zip/require_hash.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ try {
@unlink(__DIR__ . '/require_hash.zip');
?>
--EXPECTF--
zip-based phar "%srequire_hash.phar.zip" does not have a signature
phar error: signature is missing in zip-based phar "%srequire_hash.phar.zip"
bool(false)
array(2) {
["hash"]=>
Expand Down
11 changes: 3 additions & 8 deletions ext/phar/zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,6 @@ int phar_parse_zipfile(php_stream *fp, char *fname, size_t fname_len, char *alia
zend_hash_str_add_mem(&mydata->manifest, entry.filename, entry.filename_len, (void *)&entry, sizeof(phar_entry_info));
}

mydata->fp = fp;

if (zend_hash_str_exists(&(mydata->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
mydata->is_data = 0;
} else {
Expand All @@ -675,14 +673,11 @@ int phar_parse_zipfile(php_stream *fp, char *fname, size_t fname_len, char *alia

/* ensure signature set */
if (!mydata->is_data && PHAR_G(require_hash) && !mydata->signature) {
php_stream_close(fp);
phar_destroy_phar_data(mydata);
if (error) {
spprintf(error, 0, "zip-based phar \"%s\" does not have a signature", fname);
}
return FAILURE;
PHAR_ZIP_FAIL("signature is missing");
}

mydata->fp = fp;
Copy link
Member

Choose a reason for hiding this comment

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

Looks like there's a bunch of usages above that use PHAR_ZIP_FAIL rather than phar_destroy_phar_data -- just want to double check whether using phar_destroy_phar_data is really okay here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, right! At least for consistency, we should use the PHAR_ZIP_FAIL() macro. However, not calling phar_destroy_phar_data() keeps some memory allocated until request shutdown; that is handled differently in ext/phar/tar.c. Might be worthwhile to give this a closer review.


zend_hash_str_add_ptr(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len, mydata);

if (actual_alias) {
Expand Down