Skip to content
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
14 changes: 14 additions & 0 deletions ext/phar/phar_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -3560,9 +3560,23 @@ PHP_METHOD(Phar, copy)
newentry.filename_len = tmp_len;
newentry.fp_refcount = 0;

if (newentry.link) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Won't this code cause a leak if phar_copy_entry_fp fails down below?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, it would. I now free both in the error path in 1e44fc0.

newentry.link = estrdup(newentry.link);
}

if (newentry.tmp) {
newentry.tmp = estrdup(newentry.tmp);
}

if (oldentry->fp_type != PHAR_FP) {
if (FAILURE == phar_copy_entry_fp(oldentry, &newentry, &error)) {
efree(newentry.filename);
if (newentry.link) {
efree(newentry.link);
}
if (newentry.tmp) {
efree(newentry.tmp);
}
php_stream_close(newentry.fp);
zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
efree(error);
Expand Down
62 changes: 62 additions & 0 deletions ext/phar/tests/gh25092.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--TEST--
GH-25092 (Double free in Phar::copy() on link and mounted entries)
--EXTENSIONS--
phar
--INI--
phar.readonly=0
--FILE--
<?php
function tar_symlink($name, $target) {
$h = str_pad($name, 100, "\0");
$h .= "0000777\0";
$h .= "0000000\0";
$h .= "0000000\0";
$h .= "00000000000\0";
$h .= "00000000000\0";
$h .= str_repeat(' ', 8);
$h .= '2';
$h .= str_pad($target, 100, "\0");
$h .= "ustar\0" . "00";
$h = str_pad($h, 512, "\0");

$sum = 0;
for ($i = 0; $i < 512; $i++) {
$sum += ord($h[$i]);
}
return substr_replace($h, sprintf("%06o\0 ", $sum), 148, 8);
}

$tar = __DIR__ . '/gh25092.tar';
file_put_contents($tar, tar_symlink('l', 'a') . str_repeat("\0", 1024));

$t = new PharData($tar);
$t->copy('l', 'l2');
unset($t);

$t = new PharData($tar);
var_dump(isset($t['l']), isset($t['l2']));
unset($t);

$phar = __DIR__ . '/gh25092.phar';
file_put_contents(__DIR__ . '/gh25092.txt', 'hello');

$p = new Phar($phar);
$p->addFromString('x.txt', 'x');
$p->setStub('<?php __HALT_COMPILER(); ?>');
unset($p);

$p = new Phar($phar);
Phar::mount('phar://' . $phar . '/m.txt', __DIR__ . '/gh25092.txt');
$p->copy('m.txt', 'c.txt');
var_dump(isset($p['c.txt']));
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/gh25092.tar');
@unlink(__DIR__ . '/gh25092.phar');
@unlink(__DIR__ . '/gh25092.txt');
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
Loading