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
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ PHP NEWS
(kocsismate)

- Zip:
. Fixed bug GH-21682 (ZipArchive instances should not be serializable).
serialize()/unserialize() now throw unless a subclass overrides
__serialize()/__unserialize(). (iliaal)
. Fixed ZipArchive callback being called after executor has shut down.
(ilutov)
. Support minimum version for libzip dependency updated to 1.0.0.
Expand Down
29 changes: 0 additions & 29 deletions ext/standard/tests/strings/bug72434.phpt

This file was deleted.

25 changes: 25 additions & 0 deletions ext/zip/php_zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "ext/standard/php_filestat.h"
#include "zend_attributes.h"
#include "zend_interfaces.h"
#include "zend_exceptions.h"
#include "php_zip.h"
#include "php_zip_arginfo.h"

Expand Down Expand Up @@ -1645,6 +1646,30 @@ PHP_METHOD(ZipArchive, count)
}
/* }}} */

PHP_METHOD(ZipArchive, __serialize)
{
ZEND_PARSE_PARAMETERS_NONE();

zend_throw_exception_ex(NULL, 0,
"Serialization of '%s' is not allowed, override __serialize() and __unserialize() to implement it",
ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name));
}

PHP_METHOD(ZipArchive, __unserialize)
{
zval *data;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY(data)
ZEND_PARSE_PARAMETERS_END();

(void) data;

zend_throw_exception_ex(NULL, 0,
"Unserialization of '%s' is not allowed, override __serialize() and __unserialize() to implement it",
ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name));
}

/* {{{ clear the internal status */
PHP_METHOD(ZipArchive, clearError)
{
Expand Down
4 changes: 4 additions & 0 deletions ext/zip/php_zip.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,10 @@ public function closeString(): string|false {}
/** @tentative-return-type */
public function count(): int {}

public function __serialize(): array {}

public function __unserialize(array $data): void {}

/** @tentative-return-type */
public function getStatusString(): string {}

Expand Down
13 changes: 12 additions & 1 deletion ext/zip/php_zip_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions ext/zip/tests/bug72434.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Bug #72434: ZipArchive class Use After Free Vulnerability in PHP's GC algorithm and unserialize
--EXTENSIONS--
zip
--FILE--
<?php
$free_me = array(new StdClass());
$serialized_payload = 'a:3:{i:1;N;i:2;O:10:"ZipArchive":1:{s:8:"filename";'.serialize($free_me).'}i:1;R:4;}';
try {
$unserialized_payload = unserialize($serialized_payload);
var_dump($unserialized_payload);
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}
?>
--EXPECT--
Unserialization of 'ZipArchive' is not allowed, override __serialize() and __unserialize() to implement it
16 changes: 16 additions & 0 deletions ext/zip/tests/gh21682.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
GH-21682 (ZipArchive serialization is rejected)
--EXTENSIONS--
zip
--FILE--
<?php
$a = new ZipArchive();
try {
serialize($a);
echo "ERROR: should have thrown\n";
} catch (\Exception $e) {
echo $e->getMessage() . "\n";
}
?>
--EXPECT--
Serialization of 'ZipArchive' is not allowed, override __serialize() and __unserialize() to implement it
32 changes: 32 additions & 0 deletions ext/zip/tests/gh21682_subclass.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
GH-21682 (ZipArchive subclass implements serialization via __serialize()/__unserialize())
--EXTENSIONS--
zip
--FILE--
<?php
class MyArchive extends ZipArchive
{
public function __serialize(): array
{
return ['data' => $this->closeString()];
}

public function __unserialize(array $data): void
{
$this->openString($data['data']);
}
}

$zip = new MyArchive();
$zip->openString();
$zip->addFromString('test1', 'abc123');

$roundtrip = unserialize(serialize($zip));
var_dump($roundtrip instanceof MyArchive);
var_dump($roundtrip->numFiles);
var_dump($roundtrip->getFromName('test1'));
?>
--EXPECT--
bool(true)
int(1)
string(6) "abc123"
26 changes: 26 additions & 0 deletions ext/zip/tests/gh21682_subclass_no_overrides.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
GH-21682 (ZipArchive subclass without overrides inherits the serialization throw)
--EXTENSIONS--
zip
--FILE--
<?php
class MyZip extends ZipArchive {}

$zip = new MyZip();
try {
serialize($zip);
echo "ERROR: should have thrown\n";
} catch (\Exception $e) {
echo $e->getMessage() . "\n";
}

try {
unserialize('O:5:"MyZip":0:{}');
echo "ERROR: should have thrown\n";
} catch (\Exception $e) {
echo $e->getMessage() . "\n";
}
?>
--EXPECT--
Serialization of 'MyZip' is not allowed, override __serialize() and __unserialize() to implement it
Unserialization of 'MyZip' is not allowed, override __serialize() and __unserialize() to implement it
Loading