Skip to content

ext/phar: unset() of an entry with an open handle use-after-free. - #25649

Closed
devnexen wants to merge 2 commits into
php:PHP-8.4from
devnexen:phar-flush-deleted-open-handle
Closed

devnexen wants to merge 2 commits into
php:PHP-8.4from
devnexen:phar-flush-deleted-open-handle

Conversation

@devnexen

Copy link
Copy Markdown
Member

Flushing closed the archive stream while a handle on a deleted entry still referenced it. The open handle check now covers deleted entries.

Flushing closed the archive stream while a handle on a deleted
entry still referenced it. The open handle check now covers
deleted entries.
@devnexen
devnexen marked this pull request as ready for review November 22, 2026 14:40

@iliaal iliaal left a comment

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.

Please address the three inline comments.

Comment thread ext/phar/phar.c
break;
}
}
if (entry->is_deleted || entry->is_mounted) {

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.

Two readers can share the old archive stream. phar_entry_delref() closes it when the first reader closes, even if the second still holds it. Can we track references to the backing stream and close it on the last release?

Comment thread ext/phar/tar.c Outdated
} else {
if (entry->fp_refcount) {
if (entry->fp_type == PHAR_FP) {
fp->free_fp = 0;

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.

On the next flush, the reader still holds the original stream, but this clears free_fp for its replacement too. Each later flush can leave another descriptor open until request shutdown. Can we retain only the stream the reader holds?

var_dump(fread($fp, 4));
var_dump(fclose($fp));
var_dump(isset($phar['b']));
var_dump($phar['a']->getContent());

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.

Can we reopen the ZIP in a fresh process here? This test passes, but PharData rejects the saved archive. The central directory declares two entries and contains only a. The writer already had this counting bug.

@iliaal iliaal left a comment

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.

Thanks, looks good now

@devnexen devnexen closed this in edfc0da Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants