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
45 changes: 45 additions & 0 deletions Zend/tests/gh25941.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
GH-25941: Pin closure in zend_call_function() before deprecation check
--FILE--
<?php
echo "--- autoload unregister ---\n";
$x = str_repeat('A', 64);
$fn = #[Deprecated] function (string $class) use ($x) {
echo "autoload:$class:" . strlen($x) . "\n";
};
unset($x);
spl_autoload_register($fn);
set_error_handler(function () use (&$fn) {
spl_autoload_unregister($fn);
$fn = null;
return true;
});
class_exists('GH25941_NonExistent_1');
restore_error_handler();

echo "--- autoload unregister with exception ---\n";
$y = str_repeat('B', 64);
$fn2 = #[Deprecated] function (string $class) use ($y) {
echo "unreachable\n";
};
unset($y);
spl_autoload_register($fn2);
set_error_handler(function () use (&$fn2) {
spl_autoload_unregister($fn2);
$fn2 = null;
throw new RuntimeException('abort');
});
try {
class_exists('GH25941_NonExistent_2');
} catch (RuntimeException $e) {
echo "caught:" . $e->getMessage() . "\n";
}
restore_error_handler();
echo "done\n";
?>
--EXPECT--
--- autoload unregister ---
autoload:GH25941_NonExistent_1:64
--- autoload unregister with exception ---
caught:abort
done
23 changes: 19 additions & 4 deletions Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ zend_result zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_
uint32_t call_info;
void *object_or_called_scope;
zend_object *pinned_this = NULL;
zend_object *pinned_closure = NULL;

ZVAL_UNDEF(fci->retval);

Expand Down Expand Up @@ -893,6 +894,10 @@ zend_result zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_
pinned_this = zend_object_copy(fci_cache->object);
}

if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CLOSURE)) {
pinned_closure = zend_object_copy(ZEND_CLOSURE_OBJECT(func));
}

if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_DEPRECATED)) {
zend_deprecated_function(func);

Expand All @@ -901,6 +906,9 @@ zend_result zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_
if (pinned_this) {
OBJ_RELEASE(pinned_this);
}
if (pinned_closure) {
OBJ_RELEASE(pinned_closure);
}
return SUCCESS;
}
}
Expand All @@ -912,6 +920,9 @@ zend_result zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_
if (pinned_this) {
OBJ_RELEASE(pinned_this);
}
if (pinned_closure) {
OBJ_RELEASE(pinned_closure);
}
return SUCCESS;
}
#endif
Expand Down Expand Up @@ -952,6 +963,9 @@ zend_result zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_
if (pinned_this) {
OBJ_RELEASE(pinned_this);
}
if (pinned_closure) {
OBJ_RELEASE(pinned_closure);
}
return SUCCESS;
}
}
Expand Down Expand Up @@ -1049,15 +1063,16 @@ zend_result zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_
if (pinned_this) {
OBJ_RELEASE(pinned_this);
}
if (pinned_closure) {
OBJ_RELEASE(pinned_closure);
}
return SUCCESS;
}
}

if (UNEXPECTED(func->op_array.fn_flags & ZEND_ACC_CLOSURE)) {
uint32_t call_info;
if (UNEXPECTED(pinned_closure)) {
uint32_t call_info = ZEND_CALL_CLOSURE;

GC_ADDREF(ZEND_CLOSURE_OBJECT(func));
call_info = ZEND_CALL_CLOSURE;
if (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) {
call_info |= ZEND_CALL_FAKE_CLOSURE;
}
Expand Down
25 changes: 25 additions & 0 deletions ext/pcntl/tests/pcntl_signal_dispatch_closure_uaf.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
pcntl_signal_dispatch() keeps deprecated closure handler alive when replaced in error handler
--EXTENSIONS--
pcntl
posix
--FILE--
<?php
set_error_handler(function ($errno, $errstr) {
pcntl_signal(SIGUSR1, SIG_DFL);
return true;
});

$x = str_repeat('A', 64);
pcntl_signal(SIGUSR1, #[Deprecated] function ($signo) use ($x) {
echo "called:$signo:" . strlen($x) . "\n";
});
unset($x);

posix_kill(posix_getpid(), SIGUSR1);
pcntl_signal_dispatch();
echo "done\n";
?>
--EXPECTF--
called:%d:64
done
Loading