Skip to content

Fix GH-15210: phpdbg_print_changed_zvals working on a real copy instead. #15229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions sapi/phpdbg/phpdbg_watch.c
Original file line number Diff line number Diff line change
Expand Up @@ -1168,8 +1168,9 @@ int phpdbg_print_changed_zvals(void) {

if (zend_hash_num_elements(PHPDBG_G(watchlist_mem)) > 0) {
/* we must not add elements to the hashtable while iterating over it (resize => read into freed memory) */
mem_list = PHPDBG_G(watchlist_mem);
PHPDBG_G(watchlist_mem) = PHPDBG_G(watchlist_mem_backup);
mem_list = malloc(phpdbg_pagesize > sizeof(HashTable) ? phpdbg_pagesize : sizeof(HashTable));
zend_hash_init(mem_list, zend_hash_num_elements(PHPDBG_G(watchlist_mem)), NULL, NULL, false);
zend_hash_copy(mem_list, PHPDBG_G(watchlist_mem), (copy_ctor_func_t) zval_add_ref);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't totally understand why we need to duplicate the memory? Is it because the HashTable might be allocated with ZMM?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in fact is to avoid the hashtable corruption of the original watchpoint list, working on a copy then copy back once all is done. it fixed the issue for me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would increasing the refcounter on the HashTable also fix it or not?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to work if I allow cow violation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be wrong, we need a temporary hash table so that resizes don't cause problems. Increasing the refcount won't protect you against that and only papers over the real issue.


ZEND_HASH_MAP_FOREACH_NUM_KEY(mem_list, page) {
phpdbg_btree_position pos = phpdbg_btree_find_between(&PHPDBG_G(watchpoint_tree), page, page + phpdbg_pagesize);
Expand All @@ -1192,7 +1193,13 @@ int phpdbg_print_changed_zvals(void) {
phpdbg_reenable_memory_watches();

if (mem_list) {
PHPDBG_G(watchlist_mem) = mem_list;
zend_hash_destroy(PHPDBG_G(watchlist_mem));
free(PHPDBG_G(watchlist_mem));
PHPDBG_G(watchlist_mem) = malloc(phpdbg_pagesize > sizeof(HashTable) ? phpdbg_pagesize : sizeof(HashTable));
zend_hash_init(PHPDBG_G(watchlist_mem), phpdbg_pagesize / (sizeof(Bucket) + sizeof(uint32_t)), NULL, NULL, true);
zend_hash_copy(PHPDBG_G(watchlist_mem), mem_list, (copy_ctor_func_t) zval_add_ref);
zend_hash_destroy(mem_list);
free(mem_list);
phpdbg_reenable_memory_watches();
}

Expand Down
40 changes: 40 additions & 0 deletions sapi/phpdbg/tests/gh15210.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
GH-15210 use after free after continue
--CREDITS--
YuanchengJiang
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't really use credit sections any longer, as those were more to go around the limitations of SVN

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okidoki

--PHPDBG--
b 4
r
w $a[0]
w r $b
c
q
--FILE--
<?php
header_register_callback(function() { echo "sent";});
$a = [0];
$a[0] = 1;
$b = &$a;
$a[0] = 2;
$a[1] = 3;
$c = [1];
$b = &$c;
?>
--EXPECTF--
[Successful compilation of %s]
prompt> [Breakpoint #0 added at %s:%d]
prompt> [Breakpoint #0 at %s:%d, hits: 1]
>00004: $a[0] = 1;
00005: $b = &$a;
00006: $a[0] = 2;
prompt> [Added watchpoint #0 for $a[0]]
prompt> [Added recursive watchpoint #1 for $b]
prompt> [Breaking on watchpoint $a[0]]
Old value: [Breaking on watchpoint $a[0]]
Old value: 0
New value: 1
>00002: header_register_callback(function() { echo "sent";});
00003: $a = [0];
00004: $a[0] = 1;
prompt> [$a[0] has been removed, removing watchpoint]
[$b has been removed, removing watchpoint recursively]
Loading