Skip to content

Commit c50b37d

Browse files
committed
Fix metadata leak when phar convert logic fails
Closes GH-19756.
1 parent 6a2adcd commit c50b37d

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ PHP NEWS
4848
- Phar:
4949
. Fixed memory leaks when verifying OpenSSL signature. (Girgias)
5050
. Fix memory leak in phar tar temporary file error handling code. (nielsdos)
51+
. Fix metadata leak when phar convert logic fails. (nielsdos)
5152

5253
- Standard:
5354
. Fixed bug GH-16649 (UAF during array_splice). (alexandre-daubois)

ext/phar/phar_object.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,6 +2281,7 @@ static zend_object *phar_convert_to_other(phar_archive_data *source, int convert
22812281
}
22822282

22832283
if (FAILURE == phar_copy_file_contents(&newentry, phar->fp)) {
2284+
phar_metadata_tracker_free(&phar->metadata_tracker, phar->is_persistent);
22842285
zend_hash_destroy(&(phar->manifest));
22852286
php_stream_close(phar->fp);
22862287
efree(phar);
@@ -2318,6 +2319,7 @@ static zend_object *phar_convert_to_other(phar_archive_data *source, int convert
23182319
return ret;
23192320
} else {
23202321
if(phar != NULL) {
2322+
phar_metadata_tracker_free(&phar->metadata_tracker, phar->is_persistent);
23212323
zend_hash_destroy(&(phar->manifest));
23222324
zend_hash_destroy(&(phar->mounted_dirs));
23232325
zend_hash_destroy(&(phar->virtual_dirs));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Phar convert logic leaks metadata
3+
--EXTENSIONS--
4+
phar
5+
--INI--
6+
phar.require_hash=0
7+
phar.readonly=0
8+
--FILE--
9+
<?php
10+
$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.zip';
11+
12+
$phar = new PharData($fname);
13+
$phar->setMetadata("foobar");
14+
$phar['x'] = 'hi';
15+
try {
16+
$phar->convertToData(Phar::ZIP, Phar::NONE, 'phar.zip');
17+
} catch (BadMethodCallException $e) {
18+
echo $e->getMessage(),"\n";
19+
}
20+
?>
21+
--CLEAN--
22+
<?php unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.zip');?>
23+
--EXPECTF--
24+
data phar "%s" has invalid extension phar.zip

0 commit comments

Comments
 (0)