[php-src] master: Merge branch 'PHP-8.5'
| From: | David Carlier | Date: | Fri, 18 Sep 2026 21:20:03 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.5' | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-141271@lists.php.net to get a copy of this message | ||
Author: David Carlier (devnexen)
Date: 2026-09-18T22:19:38+01:00
Commit: https://github.com/php/php-src/commit/d975bbb6c48df0f49b4fc85165beedca81b3ca42
Raw diff: https://github.com/php/php-src/commit/d975bbb6c48df0f49b4fc85165beedca81b3ca42.diff
Merge branch 'PHP-8.5'
* PHP-8.5:
ext/zip: ZipArchive::close() use-after-free from a progress or cancel callback.
# Conflicts:
# ext/zip/php_zip.c
# ext/zip/php_zip.h
Changed paths:
A ext/zip/tests/gh23747.phpt
M ext/zip/php_zip.c
M ext/zip/php_zip.h
Diff:
diff --git a/ext/zip/php_zip.c b/ext/zip/php_zip.c
index 93004531dac2..88fdcaa9b03e 100644
--- a/ext/zip/php_zip.c
+++ b/ext/zip/php_zip.c
@@ -634,6 +634,16 @@ static char * php_zipobj_get_zip_comment(ze_zip_object *obj, int *len) /* {{{
*/
}
/* }}} */
+static bool php_zipobj_closing(ze_zip_object *obj) /* {{{ */
+{
+ if (obj->archive && obj->archive->close) {
+ zend_throw_error(NULL, "Already being closed");
+ return true;
+ }
+ return false;
+}
+/* }}} */
+
/* Close and free the zip_t. If the archive was opened as a string, the
* final contents of the archive will be assigned to *out_str and that
* string will afterwards be owned by the caller.
@@ -647,7 +657,9 @@ static bool php_zipobj_close(ze_zip_object *obj, zend_string **out_str) /* {{{ *
bool success = false;
if (intern) {
+ archive->close = true;
int err = zip_close(intern);
+ archive->close = false;
if (err) {
php_error_docref(NULL, E_WARNING, "%s", zip_strerror(intern));
/* Save error for property reader */
@@ -1599,6 +1611,11 @@ PHP_METHOD(ZipArchive, open)
RETURN_FALSE;
}
+ if (php_zipobj_closing(ze_obj)) {
+ efree(resolved_path);
+ RETURN_THROWS();
+ }
+
/* If we already have an opened zip, free it */
php_zipobj_close(ze_obj, NULL);
@@ -1646,6 +1663,10 @@ PHP_METHOD(ZipArchive, openString)
ze_zip_object *ze_obj = Z_ZIP_P(self);
php_zip_archive *archive;
+ if (php_zipobj_closing(ze_obj)) {
+ RETURN_THROWS();
+ }
+
php_zipobj_close(ze_obj, NULL);
zip_error_t err;
@@ -1712,6 +1733,10 @@ PHP_METHOD(ZipArchive, close)
ZIP_FROM_OBJECT(intern, self);
+ if (php_zipobj_closing(Z_ZIP_P(self))) {
+ RETURN_THROWS();
+ }
+
RETURN_BOOL(php_zipobj_close(Z_ZIP_P(self), NULL));
}
/* }}} */
@@ -1732,6 +1757,10 @@ PHP_METHOD(ZipArchive, closeString)
RETURN_THROWS();
}
+ if (php_zipobj_closing(Z_ZIP_P(self))) {
+ RETURN_THROWS();
+ }
+
zend_string *ret = NULL;
bool success = php_zipobj_close(Z_ZIP_P(self), &ret);
ZEND_ASSERT(ret);
diff --git a/ext/zip/php_zip.h b/ext/zip/php_zip.h
index a67d2042d7ca..18f09b20850a 100644
--- a/ext/zip/php_zip.h
+++ b/ext/zip/php_zip.h
@@ -72,6 +72,7 @@ typedef struct _php_zip_archive {
zend_string *out_str;
bool from_string;
bool bailout_callback;
+ bool close;
#ifdef HAVE_PROGRESS_CALLBACK
zend_fcall_info_cache progress_callback;
#endif
diff --git a/ext/zip/tests/gh23747.phpt b/ext/zip/tests/gh23747.phpt
new file mode 100644
index 000000000000..24e977410e17
--- /dev/null
+++ b/ext/zip/tests/gh23747.phpt
@@ -0,0 +1,102 @@
+--TEST--
+GH-23747 (ZipArchive::close() from inside a progress or cancel callback causes segv)
+--CREDITS--
+djarfluka
+--EXTENSIONS--
+zip
+--SKIPIF--
+<?php
+if (!method_exists(ZipArchive::class, 'registerProgressCallback')) {
+ die('skip progress callbacks are not supported');
+}
+if (!method_exists(ZipArchive::class, 'registerCancelCallback')) {
+ die('skip cancel callbacks are not supported');
+}
+?>
+--FILE--
+<?php
+function populate(ZipArchive $zip, string $filename): void {
+ $zip->open($filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
+ for ($i = 0; $i < 64; $i++) {
+ $zip->addFromString("f$i.txt", str_repeat('x', 2000));
+ }
+}
+
+$filename = __DIR__ . '/gh23747.zip';
+
+$zip = new ZipArchive();
+populate($zip, $filename);
+$zip->registerProgressCallback(0.0, function ($rate) use ($zip) {
+ static $done = false;
+ if (!$done) {
+ $done = true;
+ try {
+ $zip->close();
+ } catch (Error $e) {
+ echo $e::class, ': ', $e->getMessage(), PHP_EOL;
+ }
+ }
+});
+var_dump($zip->close());
+
+$zip = new ZipArchive();
+populate($zip, $filename);
+$zip->registerCancelCallback(function () use ($zip) {
+ static $done = false;
+ if (!$done) {
+ $done = true;
+ try {
+ $zip->close();
+ } catch (Error $e) {
+ echo $e::class, ': ', $e->getMessage(), PHP_EOL;
+ }
+ }
+ return 0;
+});
+var_dump($zip->close());
+
+$zip = new ZipArchive();
+populate($zip, $filename);
+$zip->registerProgressCallback(0.0, function ($rate) use ($zip) {
+ static $done = false;
+ if (!$done) {
+ $done = true;
+ try {
+ $zip->close();
+ } catch (Error $e) {
+ echo $e::class, ': ', $e->getMessage(), PHP_EOL;
+ }
+ }
+});
+var_dump($zip->open($filename));
+var_dump($zip->count());
+
+$zip = new ZipArchive();
+populate($zip, $filename);
+$zip->registerProgressCallback(0.0, function ($rate) use ($zip, $filename) {
+ static $done = false;
+ if (!$done) {
+ $done = true;
+ try {
+ $zip->open($filename);
+ } catch (Error $e) {
+ echo $e::class, ': ', $e->getMessage(), PHP_EOL;
+ }
+ }
+});
+var_dump($zip->close());
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/gh23747.zip');
+?>
+--EXPECT--
+Error: Already being closed
+bool(true)
+Error: Already being closed
+bool(true)
+Error: Already being closed
+bool(true)
+int(64)
+Error: Already being closed
+bool(true)