From 5a7c84f2748544946246932f59272c7a550b4aa0 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Mon, 13 Oct 2025 00:15:14 +0200 Subject: [PATCH] phar: Fix memleak+UAF when opening temp stream in buildFromDirectory() fails Obvious memleak, but can also cause a UAF depending on destruction ordering with lingering PCRE regex instances in the SPL objects. Closes GH-20157. --- NEWS | 2 ++ ext/phar/phar_object.c | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/NEWS b/NEWS index f322a7e0876..b40e46298d9 100644 --- a/NEWS +++ b/NEWS @@ -31,6 +31,8 @@ PHP NEWS . Fix a bunch of memory leaks in phar_parse_zipfile() error handling. (nielsdos) . Fix file descriptor/memory leak when opening central fp fails. (nielsdos) + . Fix memleak+UAF when opening temp stream in buildFromDirectory() fails. + (nielsdos) - Random: . Fix Randomizer::__serialize() w.r.t. INDIRECTs. (nielsdos) diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index b8577701359..c5bd8da398c 100644 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -1784,6 +1784,10 @@ PHP_METHOD(Phar, buildFromDirectory) pass.ret = return_value; pass.fp = php_stream_fopen_tmpfile(); if (pass.fp == NULL) { + zval_ptr_dtor(&iteriter); + if (apply_reg) { + zval_ptr_dtor(®exiter); + } zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" unable to create temporary file", phar_obj->archive->fname); RETURN_THROWS(); }