Skip to content

Fix use after free during shutdown destruction #18834

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 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ PHP NEWS
?? ??? ????, PHP 8.3.23

- Core:
. Fixed GH-18833 (use after free during destruction).
Copy link
Member

Choose a reason for hiding this comment

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

Note for self: move this entry

(Daniil Gentili)
. Fixed GH-18695 (zend_ast_export() - float number is not preserved).
(Oleg Efimov)

Expand Down
24 changes: 24 additions & 0 deletions Zend/tests/gh18833.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Bug #18833 (Use after free with weakmaps dependent on destruction order)
Copy link
Member

Choose a reason for hiding this comment

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

Nit: For bugs on github, it's supposed to be GH-18833 instead of a pound sign. I'll fix this in the merge

--FILE--
<?php

class a {
public static WeakMap $map;
public static Generator $storage;
}

a::$map = new WeakMap;

$closure = function () {
$obj = new a;
a::$map[$obj] = true;
yield $obj;
};
a::$storage = $closure();
a::$storage->current();

echo "ok\n";
?>
--EXPECT--
ok
36 changes: 10 additions & 26 deletions Zend/zend_objects_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,33 +97,17 @@ ZEND_API void ZEND_FASTCALL zend_objects_store_free_object_storage(zend_objects_
end = objects->object_buckets + 1;
obj_ptr = objects->object_buckets + objects->top;

if (fast_shutdown) {
do {
obj_ptr--;
obj = *obj_ptr;
if (IS_OBJ_VALID(obj)) {
if (!(OBJ_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
GC_ADD_FLAGS(obj, IS_OBJ_FREE_CALLED);
if (obj->handlers->free_obj != zend_object_std_dtor) {
GC_ADDREF(obj);
obj->handlers->free_obj(obj);
}
Copy link
Member

Choose a reason for hiding this comment

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

We skipped zend_object_std_dtor() calls because we didn't need to deallocate the memory for each property separately. We relayed on Zend MM to deallocate all the request memory at once.

The problem was introduced by weak references that added notification in free_obj calback.
I'm not sure if this was a right/best place.
I think, a better quick fix would be checking IS_OBJ_WEAKLY_REFERENCED flag.

}
}
} while (obj_ptr != end);
} else {
do {
obj_ptr--;
obj = *obj_ptr;
if (IS_OBJ_VALID(obj)) {
if (!(OBJ_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
GC_ADD_FLAGS(obj, IS_OBJ_FREE_CALLED);
GC_ADDREF(obj);
obj->handlers->free_obj(obj);
}
do {
obj_ptr--;
obj = *obj_ptr;
if (IS_OBJ_VALID(obj)) {
if (!(OBJ_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
GC_ADD_FLAGS(obj, IS_OBJ_FREE_CALLED);
GC_ADDREF(obj);
obj->handlers->free_obj(obj);
}
} while (obj_ptr != end);
}
}
} while (obj_ptr != end);
}


Expand Down
Loading