Skip to content

ext/phar: PharData refcount underflow -> double-free (8.3.x through 8.7-dev) #25722

Done

Description

@sgInnora

Target: PHP 8.3.6 through 8.7.0-dev (ext/phar)
Weakness: CWE-415 Double Free / CWE-416 Use-After-Free (refcount underflow)
Type: Memory corruption -> DoS (shutdown-phase)
Severity: CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:N/I:N/A:L = 3.3 Low — DoS, shutdown-phase only; no C/I impact demonstrated. Note on scope: the trigger requires executing PHP code that constructs two PharData objects for the same path and keeps both alive (or an application pattern that does this from attacker-influenced input); per PHP's security policy this may be classified as a non-security bug. A:L not A:H because the double-free is silently absorbed by ZMM in default config for 8.3.x/8.4.x/8.5.x (no crash, only latent heap corruption), and under php-fpm the ZMM allocator is rebuilt per-request so free-list corruption does not persist across requests. (8.6.0 and master do abort in zend_mm_panic() by default; the crash is otherwise observable with USE_ZEND_ALLOC=0 or ASAN builds.)
Reporter: Feng Ning feng@innora.ai / Innora Security Research

Summary (BLUF)

PHP ext/phar has a reference-count underflow that leads to a double-free of the
same phar_archive_data structure at request shutdown. Creating two PharData
objects for the same non-existent path, each kept alive by a reference, causes
phar_archive_delref() to decrement the archive's refcount from 1 to 0 (first
destroy) and then 0 to -1 (second destroy of the same pointer), producing a
use-after-free read plus a second free of a garbage pointer.

Affected versions

Verified affected (deterministic, USE_ZEND_ALLOC=0 -> SIGSEGV, 3/3 each):

  • PHP 8.3.6 and 8.3.37 (latest 8.3.x, 2026-11-19 security release)
  • PHP 8.4.28 (latest 8.4.x, 2026-11-19 security release)
  • PHP 8.5.12 / 8.5.13 (latest 8.5.x, 2026-11-19 security release)
  • PHP 8.6.0 (released 2026-11-19)
  • PHP master 8.7.0-dev (commit 11c9b60,
    2026-11-22)
  • Ubuntu 24.04 LTS php8.3 backport 8.3.6-0ubuntu0.24.04.14 (built 2026-11-18)

Default Zend MM behavior: 8.3.x/8.4.x/8.5.x do not crash (rc=0, silent
heap corruption); 8.6.0 and master abort in zend_mm_panic() (double-free
detected, zend_alloc.c:405). USE_ZEND_ALLOC=0 makes every branch SIGSEGV.

Net: every maintained branch from 8.3.x through 8.7.0-dev/master, plus the
Ubuntu 24.04 LTS backport, is affected, including the 2026-11-19 security
releases. PHP 8.2 was not tested (it is still in security support until
2026-12-31 but is outside our test environment).

Reproduction

<?php
try{$p=new PharData("/tmp/aaa.tar");}catch(Throwable $e){}
$_=&$p;
try{$q=new PharData("/tmp/aaa.tar");}catch(Throwable $e){}
$r=&$q;

Deterministic (5/5 for the 8.3.x build; 3/3 per branch in the version sweep).
Default phar.readonly=1 still triggers. The path must not exist; the
.tar/.zip (and other non-.phar) suffixes trigger the PharData (non-phar)
handler.

Technical detail

Root cause: two PharData objects end up sharing the same phar_archive_data*
but the archive's refcount reaches only 1 (should be 2). At request shutdown,
zend_hash_graceful_reverse_destroy() tears down the symbol table in reverse
order; each object's phar_spl_foreign_dtor calls phar_archive_delref().

gdb refcount trace (decisive):

delref: phar=0x55555702d320 refcount=1     // 1 -> 0, first destroy
DESTROY: phar=0x55555702d320
delref: phar=0x55555702d320 refcount=-1    // 0 -> -1 (underflow), second destroy
DESTROY: phar=0x55555702d320               // double-free of same pointer
SIGSEGV

Crash stack (8.3.x, default Zend MM build):
#0 tracked_free(ptr=0x55500257a45d) at Zend/zend_alloc.c:2860
#1 phar_destroy_phar_data at ext/phar/phar.c:198
#2 phar_archive_delref at ext/phar/phar.c:273
#3 phar_spl_foreign_dtor at ext/phar/phar_object.c:1074
...

Defective code (8.3.x):

  • ext/phar/phar.c:241-280 phar_archive_delref: the if (--phar->refcount < 0)
    branch can still invoke phar_destroy_phar_data() after underflow.
  • ext/phar/phar_object.c:1107 PHP_METHOD(Phar, __construct) (shared with
    PharData): the archive refcount is not correctly incremented for the second
    same-path creation.

The double-free reads already-freed fields (alias/fname/signature) and calls
pefree() on the garbage value, crashing in tracked_free. Under the default
Zend Memory Manager the double-free is silently absorbed (heap corruption), but
with USE_ZEND_ALLOC=0 (system malloc) or ASAN it deterministically aborts.

Suggested fix

Increment the archive refcount for every object that shares it (or make the
second same-path new PharData fail instead of aliasing the same archive), so
that the refcount cannot underflow. Alternatively, guard phar_archive_delref
against re-destroying an already-freed archive.

Note

This was first sent privately to security@php.net on 2026-11-22. Per PHP's
security classification policy it falls outside security scope (it needs
specific code to trigger, and the deterministic crash shown above uses
USE_ZEND_ALLOC=0), so I am filing it here as a regular bug.

Activity

  1. self-assigned this
    on Nov 25, 2026
  2. added a commit that references this issue on Nov 28, 2026
    972d667
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions