Skip to content

Zend: Pin closure in zend_call_function() before deprecation check - #25941

Open
hohphu8 wants to merge 1 commit into
php:masterfrom
hohphu8:fix-pcntl-signal-dispatch-closure-uaf
Open

hohphu8 wants to merge 1 commit into
php:masterfrom
hohphu8:fix-pcntl-signal-dispatch-closure-uaf

Conversation

@hohphu8

@hohphu8 hohphu8 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

In zend_call_function() (Zend/zend_execute_API.c), zend_deprecated_function() and
zend_param_must_be_ref() can invoke a user error handler before the call frame takes a reference to a ZEND_ACC_CLOSURE (ZEND_CLOSURE_OBJECT(func)). If the error handler releases the last external reference to the closure (for example via spl_autoload_unregister() or pcntl_signal()), the closure object and its zend_function are freed while zend_call_function() is still setting up the call, causing a use-after-free (zend_mm_heap corrupted).

Following GH-22151 (pinned_this), this hoists the ZEND_CLOSURE_OBJECT(func) refcount (pinned_closure) before the deprecation check, releases it on all early-return error paths, and transfers that reference directly to the call frame once ZEND_CALL_CLOSURE is set — adding no extra refcount operations on the normal call path.

Adds Zend/tests/gh25941.phpt (covering spl_autoload_unregister() with normal continuation and exception unwind) and ext/pcntl/tests/pcntl_signal_dispatch_closure_uaf.phpt. Verified crash -> PASS on master (Debug + ZTS), with Zend/tests/closures/ and ext/pcntl/tests/*signal*.phpt passing 155/155.

Note: AI-assisted code audit; manually verified and reproduced locally.

@iliaal

iliaal commented Nov 30, 2026

Copy link
Copy Markdown
Member

This isn't pcntl-specific. zend_call_function() emits the #[Deprecated] notice before the call frame holds a reference to the closure, so the same crash happens on master through autoloading (an error handler that spl_autoload_unregister()s a deprecated closure). GH-22151 already pins $this there; pinning the closure next to it until the frame takes its own reference fixes both. Since it needs an error handler that replaces the callback, this should target master rather than 8.4.

@hohphu8

hohphu8 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

This isn't pcntl-specific. zend_call_function() emits the #[Deprecated] notice before the call frame holds a reference to the closure, so the same crash happens on master through autoloading (an error handler that spl_autoload_unregister()s a deprecated closure). GH-22151 already pins $this there; pinning the closure next to it until the frame takes its own reference fixes both. Since it needs an error handler that replaces the callback, this should target master rather than 8.4.

Ah, that makes sense! I'll look into it, thanks!

@hohphu8
hohphu8 force-pushed the fix-pcntl-signal-dispatch-closure-uaf branch from d5b81a7 to b67866a Compare November 30, 2026 14:02
@hohphu8
hohphu8 changed the base branch from PHP-8.4 to master November 30, 2026 14:04
@hohphu8 hohphu8 changed the title ext/pcntl: Fix UAF in pcntl_signal_dispatch() when handler is replaced Zend: Pin closure in zend_call_function() before deprecation check Nov 30, 2026
@hohphu8

hohphu8 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

Updated the PR to target master, with the engine-level fix and regression tests for both cases. Thanks again for the guidance!

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.

2 participants