[php-src] Issue #25172: ext/openssl: TLS session reuse shares request-lifetime callback structures with persistent streams
| From: | cxxz16 | Date: | Tue, 03 Nov 2026 13:33:24 +0000 |
| Subject: | [php-src] Issue #25172: ext/openssl: TLS session reuse shares request-lifetime callback structures with persistent streams | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-254001@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/25172
Author: cxxz16
### Description
category: use-after-free + invalid free (allocator-domain mismatch);
CWE-416 (with CWE-415/CWE-763 characteristics on the release path).
Context: a PHP script enables crypto on a persistent (pfsockopen) stream
with session reuse from a non-persistent parent TLS stream that carries
PSK/session/early-data callbacks
(stream_socket_enable_crypto($child, true, $method, $parent)).
Observed: ASan reports "attempting free on address which was not
malloc()-ed" in php_openssl_sockop_close (ext/openssl/xp_ssl.c:3443),
reached from php_stream_free ← stream_resource_persistent_dtor ←
zend_destroy_rsrc_list at shutdown: the persistent stream's destructor
executes pefree(ptr, persistent=1) on a pointer that was allocated on the
non-persistent (ZendMM request) heap. Before the free, the same path runs
zend_fcc_dtor() (Zend/zend_API.h:782) on fcall_info_cache entries inside
the dangling structure, i.e. OBJ_RELEASE() on pointer fields read from
released request memory.
Root cause, three facts that combine:
1. The callback structures are allocated with the allocating stream's
persistence: pecalloc(1, sizeof(...), is_persistent)
(ext/openssl/xp_ssl.c:2021-2023 for psk_callbacks; :2324-2327 for
session_callbacks; :2139 for early_data_callbacks). For a
non-persistent parent they live on the ZendMM request heap and contain
zend_fcall_info_cache entries referencing request-lifetime PHP closures.
2. Setting these callbacks directly on a persistent stream is explicitly
rejected ("not supported for persistent streams", xp_ssl.c:2005-2010,
:2309-2313, :2126-2130) — the intended invariant is that these
structures never outlive a request.
3. The session-reuse path breaks that invariant: xp_ssl.c:2772-2784 shares
the parent's session_callbacks / psk_callbacks / early_data_callbacks
into the child stream, bumping only a manual refcount, with no check on
the child's persistence. A pfsockopen() child enters the persistent
stream pool and survives request shutdown; the structure it points to
does not (ZendMM request heap teardown releases it wholesale, ignoring
the manual refcount).
When the persistent child is later destroyed (worker shutdown under FPM, or
in-request via the liveness-check pclose path in main/streams/transports.c),
the destructor at xp_ssl.c:3435-3443 runs zend_fcc_dtor() and
pefree(..., persistent=1) on the dangling structure.
All three callback structures share the same sharing and destruction code,
so session_callbacks (3 fcall_info_cache members) and early_data_callbacks
are affected by the same defect through the same path.
Expected: on session reuse onto a persistent stream, either refuse the
reuse for streams carrying these callbacks, or deep-copy the callback
structures (and their referenced closures) into persistent storage.
Evidence: asan/TLS-01-session-reuse-uaf.asan.txt.
PoC: poc/ (poc.php is five effective lines; run.sh provides the plain TCP
listener and sets USE_ZEND_ALLOC=1).
Note on reachability: the trigger is a pure-PHP sequence of documented
stream calls; under PHP-FPM the persistent stream pool is per-worker, so
the dangling state survives across requests of the same worker and the
destructor fires on worker recycle or on an in-request reconnect.
```
<?php
$ctx = stream_context_create(['ssl' => ['psk_client_cb' =>
fn() => new Openssl\Psk(str_repeat('k', 32), 'id')]]);
$p = stream_socket_client('tcp://127.0.0.1:4433', $e, $s, 5, STREAM_CLIENT_CONNECT, $ctx);
if (!$p) { echo "no listener?\n"; exit(1); }
@stream_socket_enable_crypto($p, true, STREAM_CRYPTO_METHOD_TLS_CLIENT); // setup ok, handshake may
fail
$s = pfsockopen('127.0.0.1', 4433); // persistent stream
var_dump((bool)$s);
stream_socket_enable_crypto($s, true, STREAM_CRYPTO_METHOD_TLS_CLIENT, $p); // shares psk_callbacks
echo "setup done\n";
// process shutdown: persistent stream close -> --refcount -> 0 -> fcc dtor on dangling
closure
```
[run.sh](https://github.com/user-attachments/files/34321347/run.sh)
[TLS-01-session-reuse-uaf.asan.txt](https://github.com/user-attachments/files/34321291/TLS-01-session-reuse-uaf.asan.txt)
### PHP Version
```plain
Component: ext/openssl TLS session-reuse path in php-src master, commit
b9ea38ef639f5b9bbb67d27834be9c52b922520b (cloned 2026-10-21).
PHP: 8.7.0-dev (cli), NTS. The session-reuse feature
(stream_socket_enable_crypto 4th parameter) exists since PHP 8.6.
Platform: Linux x86-64 (Ubuntu 22.04 container, Docker).
Sanitizer: AddressSanitizer (-fsanitize=address -g -O1).
Configure: --disable-all --enable-cli --with-openssl
Recorded runtime environment:
USE_ZEND_ALLOC=1 (ZendMM ON is required to observe the defect: with
the allocator off, pecalloc/pefree fall through to
libc and the invalid free is masked)
ZEND_DONT_UNLOAD_MODULES=1
ASAN_OPTIONS=abort_on_error=0:detect_leaks=0
The PoC needs only a plain TCP listener on 127.0.0.1:4433; the TLS handshake
is allowed to fail (the defect is established during setup, before any
handshake result matters).
```
### Operating System
_No response_