Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions Zend/tests/gh21776.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
GH-21776 (Heap use-after-free in zend_object_is_lazy via magic __isset)
--FILE--
<?php
class C {
function __isset($x) {
$GLOBALS['o'] = 0;
return true;
}
}
$o = new C;
$o->a ?? 0;
echo "OK\n";
?>
--EXPECT--
OK
14 changes: 10 additions & 4 deletions Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ ZEND_API zval *zend_std_read_property(zend_object *zobj, zend_string *name, int
uintptr_t property_offset;
const zend_property_info *prop_info = NULL;
uint32_t *guard = NULL;
bool release_zobj = false;

#if DEBUG_OBJECT_HANDLERS
fprintf(stderr, "Read object #%d property: %s\n", zobj->handle, ZSTR_VAL(name));
Expand Down Expand Up @@ -937,7 +938,7 @@ ZEND_API zval *zend_std_read_property(zend_object *zobj, zend_string *name, int
if (zobj->ce->__get && !((*guard) & IN_GET)) {
goto call_getter;
}
OBJ_RELEASE(zobj);
release_zobj = true;
} else if (zobj->ce->__get && !((*guard) & IN_GET)) {
goto call_getter_addref;
}
Expand Down Expand Up @@ -986,7 +987,7 @@ ZEND_API zval *zend_std_read_property(zend_object *zobj, zend_string *name, int
zend_object *instance = zend_lazy_object_init(zobj);
if (!instance) {
retval = &EG(uninitialized_zval);
goto exit;
goto release_zobj_exit;
}

if (UNEXPECTED(guard && (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS))) {
Expand All @@ -999,11 +1000,12 @@ ZEND_API zval *zend_std_read_property(zend_object *zobj, zend_string *name, int
(*guard) |= guard_type;
retval = zend_std_read_property(instance, name, type, cache_slot, rv);
(*guard) &= ~guard_type;
return retval;
goto release_zobj_exit;
}
}

return zend_std_read_property(instance, name, type, cache_slot, rv);
retval = zend_std_read_property(instance, name, type, cache_slot, rv);
goto release_zobj_exit;
}
}
if (type != BP_VAR_IS) {
Expand All @@ -1015,6 +1017,10 @@ ZEND_API zval *zend_std_read_property(zend_object *zobj, zend_string *name, int
}
retval = &EG(uninitialized_zval);

release_zobj_exit:
if (release_zobj) {
OBJ_RELEASE(zobj);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You could move this above exit with a new label and adjust the gotos in if (UNEXPECTED(zend_lazy_object_must_init(zobj))) { to go there instead. This check is not necessary the majority of the time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fair enough, let me adjust, I just looked at bug report and it mention 8.5 so started from there

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved the release into a release_zobj_exit: label above exit: and routed the three lazy-init-block jumps there. The isset-false, getter, and wrong-offset paths still goto exit and skip the flag check. Retargeted to master.

exit:
return retval;
}
Expand Down
Loading