From b0b0253109dbb4a8e9bc9059a39a4283a9b26fc0 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Fri, 27 Nov 2026 06:51:59 -0500 Subject: [PATCH] sapi/phpdbg: Fix use-after-free releasing recursive watchpoints on arrays 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. --- NEWS | 4 ++++ sapi/phpdbg/phpdbg_watch.c | 24 ++++++++++++------------ sapi/phpdbg/tests/gh22480_2.phpt | 2 +- sapi/phpdbg/tests/watch_008.phpt | 32 ++++++++++++++++++++++++++++++++ sapi/phpdbg/tests/watch_009.phpt | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 sapi/phpdbg/tests/watch_008.phpt create mode 100644 sapi/phpdbg/tests/watch_009.phpt diff --git a/NEWS b/NEWS index 73c9b4fefad7..1427bf716cf0 100644 --- a/NEWS +++ b/NEWS @@ -210,6 +210,10 @@ PHP NEWS . Fixed bug GH-25722 (phar_archive_delref() double free on shared archive at shutdown). (David Carlier) +- PHPDBG: + . Fixed use-after-free with recursive watchpoints on arrays when the watch is + deleted or the script ends. (Ilia Alshanetsky) + - POSIX: . Fixed bug GH-25530 (posix_get_last_error() returns an error from a previous request). (Artem Ukrainskiy) diff --git a/sapi/phpdbg/phpdbg_watch.c b/sapi/phpdbg/phpdbg_watch.c index 1c63f26ea70e..2c181fe0d872 100644 --- a/sapi/phpdbg/phpdbg_watch.c +++ b/sapi/phpdbg/phpdbg_watch.c @@ -960,20 +960,20 @@ void phpdbg_remove_watch_element(phpdbg_watch_element *element) { phpdbg_clean_watch_element(cur); phpdbg_free_watch_element(cur); } - while (child) { - phpdbg_watch_element *cur = child; - child = child->child; - if (cur->flags & PHPDBG_WATCH_RECURSIVE_ROOT) { - phpdbg_remove_watch_element_recursively(cur); - child = NULL; - } else { - phpdbg_clean_watch_element(cur); - } - phpdbg_free_watch_element(cur); - } if (element->flags & PHPDBG_WATCH_RECURSIVE_ROOT) { phpdbg_remove_watch_element_recursively(element); } else { + while (child) { + phpdbg_watch_element *cur = child; + child = child->child; + if (cur->flags & PHPDBG_WATCH_RECURSIVE_ROOT) { + phpdbg_remove_watch_element_recursively(cur); + child = NULL; + } else { + phpdbg_clean_watch_element(cur); + } + phpdbg_free_watch_element(cur); + } phpdbg_clean_watch_element(element); } zend_hash_index_del(&PHPDBG_G(watch_elements), element->id); @@ -1479,7 +1479,7 @@ static int phpdbg_watchpoint_parse_wrapper(char *name, size_t namelen, char *key PHPDBG_G(watch_tmp)->child = element; } - if (element->child) { + if (element->child && !(element->flags & PHPDBG_WATCH_RECURSIVE_ROOT)) { element = element->child; } diff --git a/sapi/phpdbg/tests/gh22480_2.phpt b/sapi/phpdbg/tests/gh22480_2.phpt index aa6e13dbf8d5..09ed192ff0a8 100644 --- a/sapi/phpdbg/tests/gh22480_2.phpt +++ b/sapi/phpdbg/tests/gh22480_2.phpt @@ -15,7 +15,7 @@ prompt> [Breakpoint #0 added at %s:5] prompt> [Breakpoint #0 at %s:5, hits: 1] >00005: $a['x'] = 2; 00006: -prompt> [Added watchpoint #0 for $a[]] +prompt> [Added recursive watchpoint #0 for $a] prompt> [$a[x] is already being watched] prompt> [Breaking on watchpoint $a] Old value%s diff --git a/sapi/phpdbg/tests/watch_008.phpt b/sapi/phpdbg/tests/watch_008.phpt new file mode 100644 index 000000000000..c79ec72473a3 --- /dev/null +++ b/sapi/phpdbg/tests/watch_008.phpt @@ -0,0 +1,32 @@ +--TEST-- +Recursive watchpoint on an array is released with its children at shutdown +--PHPDBG-- +b 5 +r +w $x +w r $a +c +c +q +--EXPECTF-- +[Successful compilation of %s] +prompt> [Breakpoint #0 added at %s:5] +prompt> [Breakpoint #0 at %s:5, hits: 1] +>00005: $x = 2; + 00006: +prompt> [Added watchpoint #0 for $x] +prompt> [Added recursive watchpoint #1 for $a] +prompt> [Breaking on watchpoint $x] +Old value: 1 +New value: 2 +>00006: +prompt> [$x has been removed, removing watchpoint] +[$a has been removed, removing watchpoint recursively] +[Script ended normally] +prompt> +--FILE-- + [Breakpoint #0 added at %s:4] +prompt> [Breakpoint #0 at %s:4, hits: 1] +>00004: $b = [1, [2]]; + 00005: $c = 1; + 00006: +prompt> [Added recursive watchpoint #0 for $b] +prompt> [Breaking on watchpoint $b] +Old value: +New value: Array ([0] => 1,[1] => Array ([0] => 2)) +>00005: $c = 1; + 00006: +prompt> [Removed watchpoint 0] +prompt> [Script ended normally] +prompt> +--FILE-- +