Skip to content

Fix use-after-free serializing an array grown by an element's hook - #22714

Merged
iliaal merged 1 commit into
php:masterfrom
iliaal:fix-serialize-array-ref-uaf
Jul 15, 2026
Merged

iliaal merged 1 commit into
php:masterfrom
iliaal:fix-serialize-array-ref-uaf

Conversation

@iliaal

@iliaal iliaal commented Jul 13, 2026 •

Copy link
Copy Markdown
Member

The IS_ARRAY case of php_var_serialize_intern() walks the array's HashTable without holding a reference while php_var_serialize_nested_data() recurses into user hooks, so a __serialize() that grows the same array through a by-reference alias reallocs the backing store mid-walk and the iterator reads freed memory. The object case, var_dump, and var_export already hold a ref across the walk; this adds the same GC_ADDREF/GC_DTOR_NO_REF for arrays, so the append separates a copy instead of reallocating in place. __sleep() and Serializable::serialize() reach the same walk, so one test covers all three.

Reproducer (invalid read in php_var_serialize_nested_data under ASan/valgrind; a normal build usually reads the freed memory and passes, so the test is an ASan canary):

<?php
class G {
    public $ref;
    public function __serialize(): array {
        for ($i = 0; $i < 128; $i++) $this->ref[] = 'x' . $i;
        return ['d' => 1];
    }
}
$g = new G();
$inner = [$g, 'tail'];
$g->ref = &$inner;
$top = [&$inner];
serialize($top);

Comment thread ext/standard/var.c Resolved
Comment thread ext/standard/var.c Outdated
smart_str_appendl(buf, "a:", 2);
myht = Z_ARRVAL_P(struc);
bool rcn = !is_root && (in_rcn_array || GC_REFCOUNT(myht) > 1);
if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {

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.

GC_TRY_ADDREF

Comment thread ext/standard/var.c Outdated
buf, struc, myht, zend_array_count(myht), /* incomplete_class */ 0, var_hash,
!is_root && (in_rcn_array || GC_REFCOUNT(myht) > 1));
rcn);
if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {

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.

GC_TRY_DTOR_NO_REF

@iliaal
iliaal force-pushed the fix-serialize-array-ref-uaf branch from f2cd4b0 to 8ae20f7 Compare July 15, 2026 10:58
@iliaal
iliaal requested review from LamentXU123 and ndossche July 15, 2026 11:00
@ndossche

Copy link
Copy Markdown
Member

I would only merge this in master though

@iliaal

iliaal commented Jul 15, 2026

Copy link
Copy Markdown
Member Author

I would only merge this in master though

Any reason for master only? Change seem fairly safe to me and does address UAF.

@ndossche

Copy link
Copy Markdown
Member

I would only merge this in master though

Any reason for master only? Change seem fairly safe to me and does address UAF.

Generally, for maliciously-crafted code we only do these changes on master to avoid breakage.
For me personally, I'm worried about any refcount interactions in the serialization code. Since the serializer relies on refcounts in some places, we had some breakage of such code in the past.
If the code sits in master for a while and does not break anything, we can consider backporting it.

@iliaal

iliaal commented Jul 15, 2026

Copy link
Copy Markdown
Member Author

I would only merge this in master though

Any reason for master only? Change seem fairly safe to me and does address UAF.

Generally, for maliciously-crafted code we only do these changes on master to avoid breakage. For me personally, I'm worried about any refcount interactions in the serialization code. Since the serializer relies on refcounts in some places, we had some breakage of such code in the past. If the code sits in master for a while and does not break anything, we can consider backporting it.

Fair enough I'll update the PR to master

The IS_ARRAY case of php_var_serialize_intern() walked the array's
HashTable without holding a reference across
php_var_serialize_nested_data(), which recurses into user hooks
(__serialize, __sleep, Serializable::serialize). A hook that grows the
same array through a by-reference alias reallocs the backing store
mid-walk, so the iterator reads freed memory. Hold a ref across the walk,
as the object path and var_dump/var_export already do, so the append
separates a copy instead of reallocating in place.
@iliaal
iliaal force-pushed the fix-serialize-array-ref-uaf branch from 8ae20f7 to 01107d8 Compare July 15, 2026 18:48
@iliaal
iliaal changed the base branch from PHP-8.4 to master July 15, 2026 18:48
@iliaal
iliaal merged commit cc8abaf into php:master Jul 15, 2026
23 of 24 checks passed
@windshock

windshock commented Nov 4, 2026 •

Copy link
Copy Markdown

Status check on 2026-11-05: the IS_ARRAY hunk is still absent from all four release branches (GitHub compare cc8abaf9f...PHP-8.2/8.3/8.4/8.5 → diverged), so the data below stands as of today.

Following up on this PR with ASAN data for a potential backport decision (no action requested — just measurements).

We ran the reproducer from the PR description (the __serialize() that grows the array 128× through a by-reference alias) against the current release lines under ASan (USE_ZEND_ALLOC=0, 3 runs each):

build result
8.3.36 heap-use-after-free 3/3 (read in php_var_serialize_nested_data, var.c:1062)
8.4.27 heap-use-after-free 3/3 (same site)
8.5.12 heap-use-after-free 3/3 (same site)
master clean 3/3 (GC_TRY_ADDREF/GC_TRY_DTOR_NO_REF guard present)

So all three release lines currently shipped (including the 8.3.36/8.4.27/8.5.12 releases from October 21) still reproduce the invalid read that cc8abaf fixed on master, consistent with the reviewer's "merge in master first, consider backport later if stable" decision at merge time.

2026-11-01 update: second independent trigger confirmed — a plain array walked as an object property (nested walk holds no refcount on the HT), with a __sleep() hook growing it through a reference alias ($r = &$h->arr; $r[] = 1): heap-use-after-free 3/3 on 8.4.27 (USE_ZEND_ALLOC=0; read in php_var_serialize_nested_data var.c:1062, freed by zend_hash_packed_fgrow), master clean 3/3 via the var.c IS_ARRAY GC_TRY_ADDREF hunk of cc8abaf. GitHub API check: the hunk is absent from the tips of PHP-8.4, PHP-8.3 and PHP-8.5 alike — the gap spans every current release branch, not just the shipped patches. Backport experiment: we rebuilt 8.4.27 with only the 2-line IS_ARRAY hunk applied — the reference-alias trigger is suppressed 3/3 (ASan clean) with no roundtrip regressions in our sanity set, while the separate SPL storage route (ArrayObject in-place growth, see below) remains unpatched by this hunk as expected — confirming the hunk is a safe, narrow candidate for the plain-array class. (The same hunk does NOT cover the SPL storage route — spl_array_write bypasses separation — which remains open on master per our separate measurement.)

Context for weighing a backport: the trigger requires user code running inside a serialization hook (__serialize() mutating the container through an alias), so this is the "maliciously-crafted-code" class discussed at merge time rather than something remotely triggerable. On the stability side of the original concern: we did not observe any refcount/interaction regressions from the guard in our builds (master with the guard runs clean across our repro suite).

If a backport is ever considered, we can re-run the reproducer against the backport branches and report results. (We also verified the same guard pattern is what master relies on — no other compensation exists in the release-line serializer walks.)

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.

4 participants