Skip to content

sapi/phpdbg: Fix use-after-free releasing recursive watchpoints on arrays - #479

Closed
iliaal wants to merge 7 commits into
PHP-8.4from
fix/phpdbg-watch-release-uaf
Closed

iliaal wants to merge 7 commits into
PHP-8.4from
fix/phpdbg-watch-release-uaf

Conversation

@iliaal

@iliaal iliaal commented Nov 27, 2026

Copy link
Copy Markdown
Owner

With w r $a on an array, phpdbg registered the watchpoint id on the internal $a[] child instead of the watch the user created. That child is freed whenever the watch's subtree is torn down, so phpdbg_release_watch_elements() read freed memory at script end, and w d deleted the wrong watch.

Registering the real watch exposed a second use-after-free in phpdbg_remove_watch_element(), which freed a recursive root's child before recursing into it. That already crashed when deleting a recursive watch whose variable had become an array.

The notice for a recursive watch on an array now reads [Added recursive watchpoint #N for $a], matching what phpdbg prints for w r on a non-array; gh22480_2.phpt is updated for it.

Found while validating staging #366. watch_008 and watch_009 fail before and pass after under ASAN on PHP-8.4; sapi/phpdbg/tests pass on 8.4, 8.6 and master.

iliaal and others added 7 commits November 27, 2026 12:59
DCE rewrites a dead instruction whose operand still needs freeing into a
FREE, but kept the instruction's extended_value. After (float) and (array)
casts and empty() it has bit 0 set, which ZEND_HANDLE_EXCEPTION reads as
ZEND_FREE_ON_RETURN: when a destructor run by that FREE throws, the throw
point moves by the FREE's op2, which is (uint32_t)-1, and
cleanup_live_vars() frees the operand a second time.

Closes phpGH-25836
The DB-Lib error and message handlers estrdup() server and library text
into the connection's pdo_dblib_err. For a persistent connection, errors
raised through PDO::exec() and the transaction and lastInsertId() calls
stay there until the next statement execute, so a request that ends after
such an error leaves pointers into the reset request heap. The next
request efree()s them from the message or error handler or the cursor
closer, and module shutdown does from the handle closer. Free them from
persistent_shutdown once the last PDO object using the connection is
released.

Closes phpGH-25159
…#25719)

Dereference the options element of the context parameters before checking
its type, so stream_context_create() and stream_context_set_params()
accept references to valid options arrays. References to other types
still raise a TypeError.

Co-authored-by: Gina Peter Banyard <girgias@php.net>

Closes php#25719
…down.

Objects outliving RSHUTDOWN dropped an empty archive at refcount 0
through the already destroyed fname map, while another object still
held it. The archive is now left to its last holder.

Fix phpGH-25722
Close phpGH-25728
socket_addrinfo_lookup(), socket_bind(), socket_connect(), socket_sendto()
and socket_sendmsg() would otherwise resolve only the part before the NUL
byte.

Closes phpGH-25774
When zend_string_safe_falloc() fails under memory_limit,
php_str_to_str_i_ex() returned NULL without releasing the lowercased needle.

Closes phpGH-25848
…rays

A recursive watch on an array registered its id on the child element
phpdbg creates for the array rather than on the watch root. The root owns
that child and frees it whenever its subtree is torn down, leaving a
dangling pointer in watch_elements that phpdbg_release_watch_elements()
read at shutdown. Register the root instead, and let
phpdbg_remove_watch_element() leave a root's subtree to
phpdbg_remove_watch_element_recursively(): it freed the root's child before
recursing into it, which already crashed on deleting a recursive watch
whose variable had become an array.
@iliaal
iliaal force-pushed the fix/phpdbg-watch-release-uaf branch from 15771f3 to b0b0253 Compare November 28, 2026 15:31
@iliaal

iliaal commented Nov 28, 2026

Copy link
Copy Markdown
Owner Author

Promoted upstream as php/php-src phpGH-25876.

@iliaal iliaal closed this Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants