Skip to content

Fix #79503: Memory leak on duplicate metadata #5431

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 3 commits into from

Conversation

cmb69
Copy link
Member

@cmb69 cmb69 commented Apr 21, 2020

This is the minimal fix for the mem leak.

@nikic wrote:

Probably we should be reporting an error instead.

It seems to me that reporing an error instead would be a BC break. Maybe better to just raise a notice/warning in addition, but keep overwriting metadata like now.

@nikic
Copy link
Member

nikic commented Apr 21, 2020

Probably we should be reporting an error instead.

It seems to me that reporing an error instead would be a BC break. Maybe better to just raise a notice/warning in addition, but keep overwriting metadata like now.

As you can only end up with duplicate metadata if you explicitly tamper with the phar (presumably), I don't think we need to be concerned about BC here. That said, it doesn't seem particularly important to me whether we make it just a warning or not.

@Girgias
Copy link
Member

Girgias commented Apr 21, 2020

How about warning in PHP 7.x, and Error in master?

@cmb69 cmb69 added the Bug label Apr 21, 2020
@nikic
Copy link
Member

nikic commented Apr 22, 2020

Looking a bit closer at the code, it seems to pretty consistently handle errors by populating error and return FAILURE, which will eventually lead to an UnexpectedValueException. It does not throw any notices or warnings. As such, I think it would be best to follow the existing error handling mechanism.

Duplicate metadata can only happen if someone tampers with the phar, so
we can and should treat that as error.
@cmb69
Copy link
Member Author

cmb69 commented Apr 22, 2020

As you can only end up with duplicate metadata if you explicitly tamper with the phar (presumably), […]

Well, then duplicate metadata would constitute a corruption, so we better bail out. I don't think it's necessary to have a special error message in this case; "tar-based phar … has invalid metadata in magic file …" seems to be good enough.

@nikic
Copy link
Member

nikic commented Apr 22, 2020

========DIFF========
001+ phar error: tar-based phar "/home/travis/build/php/php-src/ext/phar/tests/bug79503.phar" has invalid metadata in magic file ".phar/.metadata.bin"[Wed Apr 22 08:36:40 2020]  Script:  '/home/travis/build/php/php-src/ext/phar/tests/bug79503.php'
001- phar error: tar-based phar "%s%ebug79503.phar" has invalid metadata in magic file ".phar/.metadata.bin"
002+ /home/travis/build/php/php-src/ext/phar/tar.c(167) :  Freeing 0x00007f1450c04188 (21 bytes), script=/home/travis/build/php/php-src/ext/phar/tests/bug79503.php
003+ /home/travis/build/php/php-src/Zend/zend_alloc.c(2564) : Actual location (location was relayed)
004+ === Total 1 memory leaks detected ===
========DONE========
FAIL Bug #79503 (Memory leak on duplicate metadata) [ext/phar/tests/bug79503.phpt]

@@ -181,9 +181,15 @@ static int phar_tar_process_metadata(phar_entry_info *entry, php_stream *fp) /*
}

if (entry->filename_len == sizeof(".phar/.metadata.bin")-1 && !memcmp(entry->filename, ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1)) {
if (Z_TYPE(entry->phar->metadata) != IS_UNDEF) {
Copy link
Member

Choose a reason for hiding this comment

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

We should do this check earlier, before we parse, I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

Wouldn't that require to duplicate the check whether the metadata belong to an entry or the whole phar (or to remember the result of that check)? Maybe it's more straight forward to free the parsed metadata before returning?

@cmb69
Copy link
Member Author

cmb69 commented Apr 22, 2020

Thanks! Applied as ccca2c4.

@cmb69 cmb69 closed this Apr 22, 2020
@cmb69 cmb69 deleted the cmb/79503 branch April 22, 2020 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants