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
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.4.24

- Core:
. Fixed bug GH-22878 (Use-after-free of callable via autoloader). (iliaal)

- Date:
. Fixed leak on double DatePeriod::__construct() call. (ilutov)

Expand Down
41 changes: 41 additions & 0 deletions Zend/tests/gh22878.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
GH-22878 (Use-after-free of callable via autoloader)
--FILE--
<?php
spl_autoload_register(function (string $class): void {
$GLOBALS['cb'] = null;
eval("class $class { public static function __callStatic(\$name, \$args) { echo strlen(\$name), \"\\n\"; } }");
});

$method = str_repeat('m', 256);
$cb = ['GH22878ArrayCuf', $method];
unset($method);
call_user_func($cb);

$method = str_repeat('m', 256);
$cb = ['GH22878ArrayCufa', $method];
unset($method);
call_user_func_array($cb, []);

$method = str_repeat('m', 256);
$cb = ['GH22878ArrayDynamic', $method];
unset($method);
$cb();

$method = str_repeat('m', 256);
$cb = 'GH22878Str::' . $method;
unset($method);
call_user_func($cb);

$cb = 'GH22878Lit::literalMethod';
call_user_func($cb);

echo "done\n";
?>
--EXPECT--
256
256
256
256
13
done
17 changes: 16 additions & 1 deletion Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -4179,6 +4179,9 @@ ZEND_API bool zend_is_callable_at_frame(
bool ret;
zend_fcall_info_cache fcc_local;
bool strict_class = 0;
zval callable_copy;

ZVAL_UNDEF(&callable_copy);

if (fcc == NULL) {
fcc = &fcc_local;
Expand Down Expand Up @@ -4206,11 +4209,17 @@ ZEND_API bool zend_is_callable_at_frame(
return 1;
}

ZVAL_COPY(&callable_copy, callable);
callable = &callable_copy;

check_func:
ret = zend_is_callable_check_func(callable, frame, fcc, strict_class, error, check_flags & IS_CALLABLE_SUPPRESS_DEPRECATIONS);
if (fcc == &fcc_local) {
zend_release_fcall_info_cache(fcc);
}
if (!Z_ISUNDEF(callable_copy)) {
zval_ptr_dtor(&callable_copy);
}
return ret;

case IS_ARRAY:
Expand Down Expand Up @@ -4244,7 +4253,11 @@ ZEND_API bool zend_is_callable_at_frame(
return 1;
}

ZVAL_COPY(&callable_copy, method);
callable = &callable_copy;

if (!zend_is_callable_check_class(Z_STR_P(obj), get_scope(frame), frame, fcc, &strict_class, error, check_flags & IS_CALLABLE_SUPPRESS_DEPRECATIONS)) {
zval_ptr_dtor(&callable_copy);
return 0;
}
} else {
Expand All @@ -4256,9 +4269,11 @@ ZEND_API bool zend_is_callable_at_frame(
fcc->called_scope = fcc->calling_scope;
return 1;
}

ZVAL_COPY(&callable_copy, method);
callable = &callable_copy;
}

callable = method;
goto check_func;
}
return 0;
Expand Down
10 changes: 7 additions & 3 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -5008,23 +5008,27 @@ static zend_never_inline zend_execute_data *zend_init_dynamic_call_array(zend_ar
}

if (Z_TYPE_P(obj) == IS_STRING) {
zend_string *method_name = zend_string_copy(Z_STR_P(method));
zend_class_entry *called_scope = zend_fetch_class_by_name(Z_STR_P(obj), NULL, ZEND_FETCH_CLASS_DEFAULT | ZEND_FETCH_CLASS_EXCEPTION);

if (UNEXPECTED(called_scope == NULL)) {
zend_string_release(method_name);
return NULL;
}

if (called_scope->get_static_method) {
fbc = called_scope->get_static_method(called_scope, Z_STR_P(method));
fbc = called_scope->get_static_method(called_scope, method_name);
} else {
fbc = zend_std_get_static_method(called_scope, Z_STR_P(method), NULL);
fbc = zend_std_get_static_method(called_scope, method_name, NULL);
}
if (UNEXPECTED(fbc == NULL)) {
if (EXPECTED(!EG(exception))) {
zend_undefined_method(called_scope, Z_STR_P(method));
zend_undefined_method(called_scope, method_name);
}
zend_string_release(method_name);
return NULL;
}
zend_string_release(method_name);
if (!(fbc->common.fn_flags & ZEND_ACC_STATIC)) {
zend_non_static_method_call(fbc);
if (fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
Expand Down
16 changes: 16 additions & 0 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -3906,9 +3906,16 @@ ZEND_VM_HANDLER(118, ZEND_INIT_USER_CALL, CONST, CONST|TMPVAR|CV, NUM)
void *object_or_called_scope;
zend_execute_data *call;
uint32_t call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC;
zval held_callable;

SAVE_OPLINE();
function_name = GET_OP2_ZVAL_PTR(BP_VAR_R);
ZVAL_UNDEF(&held_callable);
ZVAL_DEREF(function_name);
if (UNEXPECTED(Z_TYPE_P(function_name) == IS_ARRAY)) {
ZVAL_COPY(&held_callable, function_name);
function_name = &held_callable;
}
if (zend_is_callable_ex(function_name, NULL, 0, NULL, &fcc, &error)) {
ZEND_ASSERT(!error);

Expand All @@ -3917,6 +3924,9 @@ ZEND_VM_HANDLER(118, ZEND_INIT_USER_CALL, CONST, CONST|TMPVAR|CV, NUM)
* For the CONST and CV case we reuse the same exception block below
* to make sure we don't increase VM size too much. */
if (!(OP2_TYPE & (IS_TMP_VAR|IS_VAR)) && UNEXPECTED(EG(exception))) {
if (UNEXPECTED(!Z_ISUNDEF(held_callable))) {
zval_ptr_dtor(&held_callable);
}
FREE_OP2();
HANDLE_EXCEPTION();
}
Expand All @@ -3940,6 +3950,9 @@ ZEND_VM_HANDLER(118, ZEND_INIT_USER_CALL, CONST, CONST|TMPVAR|CV, NUM)
call_info |= ZEND_CALL_RELEASE_THIS | ZEND_CALL_HAS_THIS;
}

if (UNEXPECTED(!Z_ISUNDEF(held_callable))) {
zval_ptr_dtor(&held_callable);
}
FREE_OP2();
if ((OP2_TYPE & (IS_TMP_VAR|IS_VAR)) && UNEXPECTED(EG(exception))) {
if (call_info & ZEND_CALL_CLOSURE) {
Expand All @@ -3954,6 +3967,9 @@ ZEND_VM_HANDLER(118, ZEND_INIT_USER_CALL, CONST, CONST|TMPVAR|CV, NUM)
init_func_run_time_cache(&func->op_array);
}
} else {
if (UNEXPECTED(!Z_ISUNDEF(held_callable))) {
zval_ptr_dtor(&held_callable);
}
zend_type_error("%s(): Argument #1 ($callback) must be a valid callback, %s", Z_STRVAL_P(RT_CONSTANT(opline, opline->op1)), error);
efree(error);
FREE_OP2();
Expand Down
50 changes: 50 additions & 0 deletions Zend/zend_vm_execute.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading