Skip to content

Fix GH-21682: reject ZipArchive serialization via __serialize()#21708

Merged
iliaal merged 1 commit into
php:masterfrom
iliaal:fix/gh-21682-ziparchive-not-serializable-zip
Jun 26, 2026
Merged

Fix GH-21682: reject ZipArchive serialization via __serialize()#21708
iliaal merged 1 commit into
php:masterfrom
iliaal:fix/gh-21682-ziparchive-not-serializable-zip

Conversation

@iliaal

@iliaal iliaal commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

ZipArchive wraps a libzip handle that can't survive serialization: it holds uncommitted in-memory changes and may be file-backed, so the implicit serialize()/unserialize() returned a broken object (numFiles 0) and was the GH-72434 use-after-free vector.

This adds throwing __serialize()/__unserialize() on the base, rejecting both paths while letting a subclass override them to round-trip through closeString()/openString(). The bug72434 reproducer moves to ext/zip/tests/ and now asserts the unserialize() rejection.

@DanielEScherzer DanielEScherzer 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.

Marking as "request changes" so that this doesn't merge until we can evaluate the impact of #21497

@iliaal iliaal force-pushed the fix/gh-21682-ziparchive-not-serializable-zip branch from 2d9a1c1 to 0feac9f Compare June 13, 2026 13:37
@DanielEScherzer

Copy link
Copy Markdown
Member

Per the discussion on #21694, ZipArchive::closeString() (in flight as #21497) gives subclasses a way to capture archive state, so this decision is worth revisiting once that lands.

@iliaal does this change things? Specifically, you should now be able to round-trip by serializing with closeString() and unserializing with openString()

CC @tstarling

@iliaal

iliaal commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

closeString()/openString() are an explicit, manual round-trip; they don't touch serialize(). The flag blocks the implicit serialize(), which still returns a broken object (numFiles 0) and still feeds the bug72434 UAF. That's unchanged by closeString existing.

Until ZipArchive actually implements serialization (and handles file-backed archives, which have no string export), the broken object and the UAF are real and the serialize/unserialize block is still needed. The flag and __serialize are mutually exclusive, so whoever implements that later just drops the flag. I'd add it now.

@tstarling

Copy link
Copy Markdown
Contributor

I think the change is still appropriate. ZipArchive holds uncommitted changes to the underlying file. If serialization was implemented then it would have to restore the state including uncommitted changes, as distinct from the archive file contents. For example ZipArchive::unchangeAll() makes this distinction visible to the user.

@DanielEScherzer

DanielEScherzer commented Jun 14, 2026

Copy link
Copy Markdown
Member

My concern is that ZEND_ACC_NOT_SERIALIZABLE also blocks the serialization of any subclasses, e.g. https://3v4l.org/SVqho#v8.5.7

And, now that we have openString() and closeString(), it is possible to do a userland serialization with those:

<?php

$orig;

class MyArchive extends ZipArchive {
    public function __serialize(): array {
		global $orig;
		$orig = $this->closeString();
		return [ 'data' => $orig ];
    }
    
    public function __unserialize(array $data): void {
        $this->openString($data['data']);
    }
}

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

var_dump($zip);
$serialized = serialize($zip);
$roundtrip = unserialize($serialized);

var_dump($roundtrip);

$new = $roundtrip->closeString();

var_dump($orig, $new, $orig === $new); // they are indeed the same

Maybe instead add __serialize() and __unserialize() handlers that always throw, saying they need to be overridden?

@iliaal iliaal force-pushed the fix/gh-21682-ziparchive-not-serializable-zip branch from 0feac9f to a1028ff Compare June 14, 2026 22:54
@iliaal

iliaal commented Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

Switched to throwing __serialize()/__unserialize(): the base still rejects serialize/unserialize and still closes the bug72434 UAF, since a throwing __unserialize means the payload is never injected as raw properties, while a subclass can override both to round-trip through closeString()/openString().

@iliaal iliaal requested a review from DanielEScherzer June 14, 2026 22:55

@DanielEScherzer DanielEScherzer 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.

Needs news, and PR title is no longer correct, but implementation looks good

please also add a test case for a subclass of ZipArchive that does not override the serialization functions

ZipArchive wraps a libzip handle that cannot survive serialization:
serialize() produced a string that unserialized into an empty object
with numFiles 0, and that unserialize path was the bug72434
use-after-free vector. Add __serialize() and __unserialize() that throw,
so the base class rejects (un)serialization and the UAF is closed by
construction, while a subclass can still override both to round-trip
through closeString()/openString(). Move the bug72434 test to
ext/zip/tests since it now requires the zip extension.

Fixes phpGH-21682
@iliaal iliaal force-pushed the fix/gh-21682-ziparchive-not-serializable-zip branch from a1028ff to 4e1edc7 Compare June 25, 2026 22:45
@iliaal iliaal changed the title Fix GH-21682: Add NOT_SERIALIZABLE to ZipArchive Fix GH-21682: reject ZipArchive serialization via __serialize() Jun 25, 2026
@iliaal

iliaal commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

Added the NEWS entry and a test for a non-overriding subclass (it inherits the base throw, with the subclass name in the message). Retitled and updated the description to match the __serialize()/__unserialize() approach.

@iliaal iliaal requested a review from DanielEScherzer June 26, 2026 01:16
@DanielEScherzer

Copy link
Copy Markdown
Member

Looks good, though not sure if this should be in NEWS or UPGRADING (I didn't remember that this was for master when reviewing earlier). Either way the code and tests are good

@iliaal

iliaal commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Since this is new capability I am inclined to say in belongs in NEWS as opposed to changing how something works per UPGRADING

@iliaal iliaal merged commit adc5f8d into php:master Jun 26, 2026
18 checks passed
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.

3 participants