Skip to content

Fix GH-13230: phpdbg use-after-free at shutdown - #22111

Closed
iliaal wants to merge 1 commit into
php:masterfrom
iliaal:fix/gh-13230-phpdbg-watch-uaf
Closed

iliaal wants to merge 1 commit into
php:masterfrom
iliaal:fix/gh-13230-phpdbg-watch-uaf

Conversation

@iliaal

@iliaal iliaal commented May 21, 2026 •

Copy link
Copy Markdown
Member

When a watched element is removed and its watchpoint freed, element->watch kept pointing at the freed watchpoint, and a later backup on the same watch chain copied from it. Clear the pointer and skip the backup for a detached element. bug73927.phpt reproduces the read under ASAN once its SKIP_ASAN guard is dropped.

Fixes #13230

@kamil-tekiela

Copy link
Copy Markdown
Member

Thanks for working on this. I tested this PR and current master under ASAN (USE_ZEND_ALLOC=0, and run-tests --asan). After recent master changes, only part of this PR is still needed.

Still needed

  • element->watch = NULL in phpdbg_clean_watch_element() and the NULL check in phpdbg_backup_watch_element(). Without them, the bug73927.phpt steps still give a heap-use-after-free on master. With them, the error is gone.
  • Removing the SKIPIF from bug73927.phpt. With --asan, the test fails on master and passes with the fix, so it works as the regression test.

Can be dropped

  • The RSHUTDOWN teardown (phpdbg_destroy_watch_request_state()). 207dd1d ([phpdbg] Multiple Use-After-Free bugs in watchpoint management due to ignored return values #22480) added phpdbg_release_watch_elements() as a post-deactivate handler, which does the same work. This is also the cause of the conflict. On the PR's own base, the RSHUTDOWN version leaks: phpdbg_btree_init() drops the btree branches, and the live elements and watchpoints are not freed. watch_001–004 and gh15210_002 leak with USE_ZEND_ALLOC=0. CI does not see this, because --asan sets USE_TRACKED_ALLOC=1.
  • The expected-output changes in watch_.phpt and gh15210_.phpt. On master, the "has been removed" notices print from live memory, because the teardown now runs after the executor frees the variables. With the two lines above, master passes with the current expected output.
  • zend_hash_del(&PHPDBG_G(watch_recreation), element->str) in phpdbg_free_watch_element(). Master with only the two lines above gives the same test results. 9d6fd4f already removes queued entries, with an identity check. Implicit elements of one chain share the same str, so a delete by name alone could remove another element's entry.

So on master, the fix reduces to this, plus the bug73927.phpt SKIPIF removal and a NEWS entry:

 void phpdbg_backup_watch_element(phpdbg_watch_element *element) {
+	if (!element->watch) {
+		return;
+	}
 	memcpy(&element->backup, &element->watch->backup, /* element->watch->size */ sizeof(element->backup));
 }
@@ void phpdbg_clean_watch_element(phpdbg_watch_element *element) {
 		if (zend_hash_num_elements(elements) == 0) {
 			phpdbg_remove_watchpoint(element->watch);
 		}
+		element->watch = NULL;
 	}
 }

phpdbg_clean_watch_element() left element->watch pointing at the
watchpoint that phpdbg_remove_watchpoint() had just freed, so a later
phpdbg_backup_watch_element() on the same chain copied from freed
memory. Clear the pointer and skip the backup for a detached element.
Drop the SKIP_ASAN guard from bug73927.phpt, which reproduces the read.

Fixes phpGH-13230
@iliaal
iliaal force-pushed the fix/gh-13230-phpdbg-watch-uaf branch from fd289bc to 88c00cb Compare November 9, 2026 13:34
@iliaal

iliaal commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

Confirmed on master: bug73927.phpt still hits the UAF in phpdbg_backup_watch_element() without those two lines, and with them the phpdbg tests pass under --asan with the existing expected output. Reduced the PR to that, thanks.

@iliaal
iliaal requested a review from kamil-tekiela November 9, 2026 13:34
@kamil-tekiela

Copy link
Copy Markdown
Member

@ndossche If you have a chance, could you please take a look at it to see if it makes sense?

@iliaal iliaal closed this in 8eb02db Nov 11, 2026
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.

use-after-free at phpdbg shutdown

3 participants