Skip to content
Merged
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 @@ -2708,7 +2708,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa
/* remove this from the new phar */
continue;
}
if (!entry->is_modified && entry->fp_refcount) {
if (entry->fp_refcount) {
/* open file pointers refer to this fp, do not free the stream */
switch (entry->fp_type) {
case PHAR_FP:
Expand Down
23 changes: 8 additions & 15 deletions ext/phar/phar_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,8 @@ static zend_result phar_copy_file_contents(phar_entry_info *entry, php_stream *f
{
char *error;
zend_off_t offset;
phar_entry_info *link;

ZEND_ASSERT(!entry->link);

if (FAILURE == phar_open_entry_fp(entry, &error, 1)) {
if (error) {
Expand All @@ -1949,26 +1950,14 @@ static zend_result phar_copy_file_contents(phar_entry_info *entry, php_stream *f
}

/* copy old contents in entirety */
phar_seek_efp(entry, 0, SEEK_SET, 0, 1);
phar_seek_efp(entry, 0, SEEK_SET, 0, 0);
offset = php_stream_tell(fp);
link = phar_get_link_source(entry);

if (!link) {
link = entry;
}

if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(link, 0), fp, link->uncompressed_filesize, NULL)) {
if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(entry, 0), fp, entry->uncompressed_filesize, NULL)) {
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
"Cannot convert phar archive \"%s\", unable to copy entry \"%s\" contents", entry->phar->fname, ZSTR_VAL(entry->filename));
return FAILURE;
}

if (entry->fp_type == PHAR_MOD) {
/* save for potential restore on error */
entry->cfp = entry->fp;
entry->fp = NULL;
}

/* set new location of file contents */
entry->fp_type = PHAR_FP;
entry->offset = offset;
Expand Down Expand Up @@ -2307,6 +2296,10 @@ static zend_object *phar_convert_to_other(phar_archive_data *source, int convert
return NULL;
}
no_copy:
/* Reset file pointers, they have to be reset here such that if a copy happens the original
* source fp can be accessed. */
newentry.fp = NULL;
newentry.cfp = NULL;
newentry.filename = zend_string_copy(newentry.filename);

phar_metadata_tracker_clone(&newentry.metadata_tracker);
Expand Down
4 changes: 2 additions & 2 deletions ext/phar/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,12 @@ static ssize_t phar_stream_write(php_stream *stream, const char *buf, size_t cou
{
phar_entry_data *data = (phar_entry_data *) stream->abstract;

php_stream_seek(data->fp, data->position, SEEK_SET);
php_stream_seek(data->fp, data->position + data->zero, SEEK_SET);
if (count != php_stream_write(data->fp, buf, count)) {
php_stream_wrapper_log_error(stream->wrapper, stream->flags, "phar error: Could not write %d characters to \"%s\" in phar \"%s\"", (int) count, ZSTR_VAL(data->internal_file->filename), data->phar->fname);
return -1;
}
data->position = php_stream_tell(data->fp);
data->position = php_stream_tell(data->fp) - data->zero;
if (data->position > (zend_off_t)data->internal_file->uncompressed_filesize) {
data->internal_file->uncompressed_filesize = data->position;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/phar/tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ static int phar_tar_writeheaders_int(phar_entry_info *entry, void *argument) /*
php_stream_write(fp->new, padding, ((entry->uncompressed_filesize +511)&~511) - entry->uncompressed_filesize);
}

if (!entry->is_modified && entry->fp_refcount) {
if (entry->fp_refcount) {
/* open file pointers refer to this fp, do not free the stream */
switch (entry->fp_type) {
case PHAR_FP:
Expand Down
40 changes: 40 additions & 0 deletions ext/phar/tests/gh18953.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
GH-18953 (Phar: Stream double free)
--EXTENSIONS--
phar
--INI--
phar.readonly=0
--FILE--
<?php

$phar = new Phar(__DIR__ . "/gh18953.phar");
$phar->addFromString("file", str_repeat("123", random_int(1, 1)));
$phar->addEmptyDir("dir");
$phar2 = $phar->compress(Phar::GZ);

var_dump($phar["dir"]);
var_dump($phar2["dir"]);
var_dump($phar["file"]->openFile()->fread(100));
var_dump($phar2["file"]->openFile()->fread(100));

?>
--CLEAN--
<?php
@unlink(__DIR__ . "/gh18953.phar");
@unlink(__DIR__ . "/gh18953.phar.gz");
?>
--EXPECTF--
object(PharFileInfo)#%d (2) {
["pathName":"SplFileInfo":private]=>
string(%d) "%sphar%sdir"
["fileName":"SplFileInfo":private]=>
string(3) "dir"
}
object(PharFileInfo)#%d (2) {
["pathName":"SplFileInfo":private]=>
string(%d) "%sphar.gz%sdir"
["fileName":"SplFileInfo":private]=>
string(3) "dir"
}
string(3) "123"
string(3) "123"
25 changes: 25 additions & 0 deletions ext/phar/tests/gh19038.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
GH-19038 (Phar crash and data corruption with SplFileObject)
--EXTENSIONS--
phar
--INI--
phar.readonly=0
--FILE--
<?php

$phar = new Phar(__DIR__ . "/gh19038.phar");
$phar->addFromString("file", "123");
$file = $phar["file"]->openFile();
$file->fseek(3);
var_dump($file->fwrite("456", 3));
$file->fseek(0);
echo $file->fread(100);

?>
--CLEAN--
<?php
@unlink(__DIR__ . "/gh19038.phar");
?>
--EXPECT--
int(3)
123456