From 85615b37b7c67ae73e353f715ecd6220ae7d055c Mon Sep 17 00:00:00 2001 From: phcorp <725023+phcorp@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:44:15 +0100 Subject: [PATCH 1/5] Fix GH-25541: Signal on a thread that never ran PHP crashes or kills the process Under ZTS, a process signal can be delivered to a thread that has no TSRM storage, e.g. a Go runtime thread in FrankenPHP or any thread an embedder creates. pcntl_signal_handler() then read PCNTL_G() through a NULL pointer (--disable-zend-signals), and zend_signal_handler_defer() fell back to the original disposition, so SIG_DFL killed the process although the script registered a handler. On such a thread, for a signal PHP handles, add the signal to the mask saved in the interrupted context and send it to the process again. The kernel delivers it to a thread that does not block it, which converges on a PHP thread. Threads unblock re-routed signals when they start a request. Faults and abort() keep their default action on the thread they hit. Re-routing is enabled on Linux, macOS and FreeBSD, where it was verified. --- NEWS | 2 + Zend/zend.c | 3 + Zend/zend_signal.c | 167 +++++++++++++++++++++++- Zend/zend_signal.h | 8 ++ configure.ac | 1 + ext/pcntl/pcntl.c | 33 ++++- ext/pcntl/tests/gh25541.phpt | 28 ++++ ext/pcntl/tests/gh25541_default.phpt | 28 ++++ ext/pcntl/tests/gh25541_sent_fault.phpt | 26 ++++ ext/zend_test/test.c | 28 ++++ ext/zend_test/test.stub.php | 4 + ext/zend_test/test_arginfo.h | 14 +- 12 files changed, 338 insertions(+), 4 deletions(-) create mode 100644 ext/pcntl/tests/gh25541.phpt create mode 100644 ext/pcntl/tests/gh25541_default.phpt create mode 100644 ext/pcntl/tests/gh25541_sent_fault.phpt diff --git a/NEWS b/NEWS index 01788956353b..2dc173208e7d 100644 --- a/NEWS +++ b/NEWS @@ -103,6 +103,8 @@ PHP NEWS - PCNTL: . Fixed pcntl_signal() raising a fatal error for SIGKILL and SIGSTOP. (lacatoire) + . Fixed bug GH-25541 (Signal delivered to a thread that never ran PHP + crashes or kills the process under ZTS). (phcorp) - PCRE: . Fixed \C being allowed in patterns enabling UTF mode with (*UTF) instead diff --git a/Zend/zend.c b/Zend/zend.c index 1b1dd9e12e05..30129e265525 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -1317,6 +1317,9 @@ ZEND_API void zend_activate(void) /* {{{ */ { #ifdef ZTS virtual_cwd_activate(); +# ifndef ZEND_WIN32 + zend_signal_unblock_rerouted(); +# endif #endif gc_reset(); init_compiler(); diff --git a/Zend/zend_signal.c b/Zend/zend_signal.c index 83f8aa0852db..1b94e1593544 100644 --- a/Zend/zend_signal.c +++ b/Zend/zend_signal.c @@ -37,6 +37,12 @@ #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_SYS_UCONTEXT_H +#include +#endif +#if defined(ZTS) && !defined(ZEND_WIN32) +#include +#endif #ifdef ZEND_SIGNALS @@ -77,6 +83,14 @@ static const int zend_sigs[] = { TIMEOUT_SIG, SIGHUP, SIGINT, SIGQUIT, SIGTERM, /* True globals, written only at process startup */ static zend_signal_entry_t global_orig_handlers[NSIG]; + +#ifdef ZTS +/* Signals for which a PHP thread registered a handler: only these are sent on + * from threads that never ran PHP, the others keep the original disposition. + * Never cleared: a PHP thread that no longer handles the signal applies the + * original disposition, as the thread that received it would have. */ +static zend_atomic_bool global_php_handled[NSIG]; +#endif static sigset_t global_sigmask; /* {{{ zend_signal_handler_defer @@ -88,9 +102,16 @@ static void zend_signal_handler_defer(int signo, siginfo_t *siginfo, void *conte #ifdef ZTS /* A signal could hit after TSRM shutdown, in this case globals are already freed. - * Or it could be delivered to a thread that didn't execute PHP yet. - * In the latter case we act as if SIGG(active) is false. */ + * Or it could be delivered to a thread that didn't execute PHP yet: if a PHP + * thread handles this signal and the embedder does not, send it on to the + * PHP threads. Otherwise act as if SIGG(active) is false. */ if (tsrm_is_shutdown() || !tsrm_is_managed_thread()) { + if (zend_atomic_bool_load_ex(&global_php_handled[signo-1]) + && (global_orig_handlers[signo-1].handler == (void *) SIG_DFL + || global_orig_handlers[signo-1].handler == (void *) SIG_IGN) + && zend_signal_reroute_from_foreign_thread(signo, siginfo, context)) { + return; + } /* Forward to default handler handler */ zend_signal_handler(signo, siginfo, context); return; @@ -245,6 +266,13 @@ ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct siga SIGG(handlers)[signo-1].handler = (void *) act->sa_handler; } +#ifdef ZTS + if (SIGG(handlers)[signo-1].handler != (void *) SIG_IGN + && SIGG(handlers)[signo-1].handler != (void *) SIG_DFL) { + zend_atomic_bool_store_ex(&global_php_handled[signo-1], true); + } +#endif + memset(&sa, 0, sizeof(sa)); if (SIGG(handlers)[signo-1].handler == (void *) SIG_IGN) { sa.sa_sigaction = (void *) SIG_IGN; @@ -446,3 +474,138 @@ ZEND_API void zend_signal_startup(void) #endif /* ZEND_SIGNALS */ + +#if defined(ZTS) && !defined(ZEND_WIN32) +/* Platforms whose kernel restores the thread's mask from uc_sigmask on return + * from a handler, and keeps a process signal that every thread blocks pending + * (see zend_signal_reroute_from_foreign_thread()). */ +# if defined(HAVE_SYS_UCONTEXT_H) && (defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)) +# define ZEND_SIGNAL_CAN_REROUTE 1 +# endif + +# ifdef NSIG +# define ZEND_SIGNAL_TRACKED NSIG +# else +# define ZEND_SIGNAL_TRACKED 65 +# endif + +/* Signals re-routed at least once, so some threads may block them. */ +static zend_atomic_bool zend_signal_rerouted[ZEND_SIGNAL_TRACKED]; + +/* {{{ zend_signal_is_synchronous + * Faults, and abort() on this thread, must take their default action on the + * thread they hit. The same signals sent with kill() are ordinary signals. */ +ZEND_API bool zend_signal_is_synchronous(int signo, const siginfo_t *siginfo) +{ + switch (signo) { + case SIGABRT: + return !siginfo || siginfo->si_pid == getpid(); + case SIGSEGV: + case SIGFPE: + case SIGILL: +# ifdef SIGBUS + case SIGBUS: +# endif +# ifdef SIGTRAP + case SIGTRAP: +# endif +# ifdef SIGSYS + case SIGSYS: +# endif + if (!siginfo) { + return true; + } + /* Raised by the kernel. SI_USER is 0 on Linux, where the codes of + * kill(), sigqueue() and tgkill() are not positive; it is 0x10001 on + * macOS and FreeBSD, above the codes of faults. macOS reports + * SIGSEGV and SIGBUS sent with kill() with a fault code, so those + * are treated as faults there. Elsewhere, assume a fault. */ +# if defined(__linux__) + return siginfo->si_code > 0; +# elif defined(__APPLE__) || defined(__FreeBSD__) + return siginfo->si_code > 0 && siginfo->si_code < SI_USER; +# else + return true; +# endif + } + return false; +} +/* }}} */ + +/* {{{ zend_signal_reroute_from_foreign_thread + * Called by a signal handler running on a thread that never ran PHP and has no + * TSRM storage, such as a thread of the Go runtime in FrankenPHP or any thread + * created by an embedding application. That thread can neither run the handler + * a script registered nor use the original disposition: SIG_DFL would kill the + * process although the script handles the signal. + * + * Instead, block the signal on this thread once the handler returns, by adding + * it to the mask saved in the interrupted context (Linux, macOS and FreeBSD + * restore the thread's mask from uc_sigmask on sigreturn), and send it to the + * process again. The kernel delivers it to a thread that does not block it. + * Threads without PHP converge to blocking it, so it ends up on a PHP thread. + * If every thread blocks it, it stays pending. There is no loop: the handler + * runs with the signal blocked, and this thread keeps it blocked afterwards. + * A thread that starts a request unblocks it again, see + * zend_signal_unblock_rerouted(); this covers threads created by a thread + * that blocked it. Like zend_sigaction(), this also unblocks it where the + * SAPI blocked it on purpose. + * + * Limits: the signal mask of a thread the embedder owns changes; the siginfo of + * the re-sent signal names this process as the sender; the PHP thread that + * receives it is not necessarily the one that registered the handler; macOS + * attaches a process signal that every thread blocks to the main thread, so if + * the main thread does not run PHP, a signal re-sent while all PHP threads + * block it (e.g. during pcntl_signal_dispatch()) is not delivered. + * + * Returns false when the caller must handle the signal itself: synchronous + * signals, after TSRM shutdown, and on platforms not listed above. */ +ZEND_API bool zend_signal_reroute_from_foreign_thread(int signo, const siginfo_t *siginfo, void *context) +{ +# ifdef ZEND_SIGNAL_CAN_REROUTE + int errno_save; + + if (signo <= 0 || signo >= ZEND_SIGNAL_TRACKED || !context || tsrm_is_shutdown() + || zend_signal_is_synchronous(signo, siginfo)) { + return false; + } + + errno_save = errno; + zend_atomic_bool_store_ex(&zend_signal_rerouted[signo], true); + sigaddset(&((ucontext_t *) context)->uc_sigmask, signo); + kill(getpid(), signo); + errno = errno_save; + return true; +# else + (void) signo; + (void) siginfo; + (void) context; + return false; +# endif +} +/* }}} */ + +/* {{{ zend_signal_unblock_rerouted + * Called when a thread starts a request: the thread, or the one that created + * it, may have blocked a signal while it did not run PHP. */ +ZEND_API void zend_signal_unblock_rerouted(void) +{ +# ifdef ZEND_SIGNAL_CAN_REROUTE + sigset_t set; + bool any = false; + int signo; + + sigemptyset(&set); + for (signo = 1; signo < ZEND_SIGNAL_TRACKED; signo++) { + if (zend_atomic_bool_load_ex(&zend_signal_rerouted[signo])) { + sigaddset(&set, signo); + any = true; + } + } + if (any) { + pthread_sigmask(SIG_UNBLOCK, &set, NULL); + } +# endif +} +/* }}} */ +#endif diff --git a/Zend/zend_signal.h b/Zend/zend_signal.h index e88892da35aa..233c991b8d1a 100644 --- a/Zend/zend_signal.h +++ b/Zend/zend_signal.h @@ -111,4 +111,12 @@ ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct siga #endif /* ZEND_SIGNALS */ +#if defined(ZTS) && !defined(ZEND_WIN32) +# include + +ZEND_API bool zend_signal_is_synchronous(int signo, const siginfo_t *siginfo); +ZEND_API bool zend_signal_reroute_from_foreign_thread(int signo, const siginfo_t *siginfo, void *context); +ZEND_API void zend_signal_unblock_rerouted(void); +#endif + #endif /* ZEND_SIGNAL_H */ diff --git a/configure.ac b/configure.ac index 5612a0696a1f..fac178816e1a 100644 --- a/configure.ac +++ b/configure.ac @@ -395,6 +395,7 @@ AC_CHECK_HEADERS(m4_normalize([ sys/vfs.h sys/sysexits.h sys/uio.h + sys/ucontext.h sys/wait.h sys/loadavg.h unistd.h diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 67b129a15096..777a6e925102 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -1297,7 +1297,38 @@ static void pcntl_signal_handler(int signo, siginfo_t *siginfo, void *context) static void pcntl_signal_handler(int signo) #endif { - struct php_pcntl_pending_signal *psig = PCNTL_G(spares); + struct php_pcntl_pending_signal *psig; + +#ifdef ZTS + /* Not a PHP thread: there is no PCNTL_G() here. */ + if (UNEXPECTED(tsrm_is_shutdown() || !tsrm_is_managed_thread())) { + const siginfo_t *info = NULL; + +# ifdef HAVE_STRUCT_SIGINFO_T + if (zend_signal_reroute_from_foreign_thread(signo, siginfo, context)) { + return; + } + info = siginfo; +# endif + /* A fault happens again on return, now with the default action. + * abort() raises SIGABRT again by itself, and the syscall that + * raised SIGSYS just fails. */ + if (zend_signal_is_synchronous(signo, info) && signo != SIGABRT +# ifdef SIGSYS + && signo != SIGSYS +# endif + ) { + struct sigaction sa; + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = SIG_DFL; + sigemptyset(&sa.sa_mask); + sigaction(signo, &sa, NULL); + } + return; + } +#endif + + psig = PCNTL_G(spares); if (!psig) { /* oops, too many signals for us to track, so we'll forget about this one */ return; diff --git a/ext/pcntl/tests/gh25541.phpt b/ext/pcntl/tests/gh25541.phpt new file mode 100644 index 000000000000..ab391618ca26 --- /dev/null +++ b/ext/pcntl/tests/gh25541.phpt @@ -0,0 +1,28 @@ +--TEST-- +GH-25541 (Signal handler not run for a signal delivered to a thread that never ran PHP) +--EXTENSIONS-- +pcntl +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +int(3) diff --git a/ext/pcntl/tests/gh25541_default.phpt b/ext/pcntl/tests/gh25541_default.phpt new file mode 100644 index 000000000000..e3e266b15465 --- /dev/null +++ b/ext/pcntl/tests/gh25541_default.phpt @@ -0,0 +1,28 @@ +--TEST-- +GH-25541 (Signal without a handler, delivered to a thread that never ran PHP, keeps its default action) +--EXTENSIONS-- +pcntl +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) +bool(true) diff --git a/ext/pcntl/tests/gh25541_sent_fault.phpt b/ext/pcntl/tests/gh25541_sent_fault.phpt new file mode 100644 index 000000000000..799acee9990b --- /dev/null +++ b/ext/pcntl/tests/gh25541_sent_fault.phpt @@ -0,0 +1,26 @@ +--TEST-- +GH-25541 (SIGTRAP sent with kill() to a thread that never ran PHP is not a fault) +--EXTENSIONS-- +pcntl +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 4bd96976a965..611184052a69 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -41,6 +41,9 @@ #include "zend_system_id.h" #include "zend_mm_custom_handlers.h" #include +#if defined(ZTS) && !defined(PHP_WIN32) +# include +#endif // `php.h` sets `NDEBUG` when not `PHP_DEBUG` which will make `assert()` from // assert.h a no-op. In order to have `assert()` working on NDEBUG builds, we @@ -783,6 +786,31 @@ static ZEND_FUNCTION(zend_test_raise_and_throw) zend_throw_exception(NULL, "Exception after raise()", 0); } +#if defined(ZTS) && !defined(PHP_WIN32) +static void *zend_test_raise_thread(void *arg) +{ + raise((int) (intptr_t) arg); + return NULL; +} + +/* Raises a signal on a thread that never ran PHP and has no TSRM storage. */ +static ZEND_FUNCTION(zend_test_raise_on_foreign_thread) +{ + zend_long signo; + pthread_t thread; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_LONG(signo) + ZEND_PARSE_PARAMETERS_END(); + + if (pthread_create(&thread, NULL, zend_test_raise_thread, (void *) (intptr_t) signo) != 0) { + zend_throw_error(NULL, "pthread_create() failed"); + RETURN_THROWS(); + } + pthread_join(thread, NULL); +} +#endif + static bool has_opline(zend_execute_data *execute_data) { return execute_data diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index 785390dd70cc..828a61883534 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -307,6 +307,10 @@ function zend_test_crash(?string $message = null): void {} function zend_test_raise_and_throw(int $signal): void {} +#if defined(ZTS) && !defined(PHP_WIN32) + function zend_test_raise_on_foreign_thread(int $signal): void {} +#endif + function zend_test_fill_packed_array(array &$array): void {} /** @return resource */ diff --git a/ext/zend_test/test_arginfo.h b/ext/zend_test/test_arginfo.h index 45545edf5db8..e75e73671b47 100644 --- a/ext/zend_test/test_arginfo.h +++ b/ext/zend_test/test_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 20856900222b20ff71d57bf575ad7034ffc34bf4 */ + * Stub hash: 84b5600c974d901618c660cedb2a0df2f854e618 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -155,6 +155,12 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_raise_and_throw, 0, 1, ZEND_ARG_TYPE_INFO(0, signal, IS_LONG, 0) ZEND_END_ARG_INFO() +#if defined(ZTS) && !defined(PHP_WIN32) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_raise_on_foreign_thread, 0, 1, IS_VOID, 0) + ZEND_ARG_TYPE_INFO(0, signal, IS_LONG, 0) +ZEND_END_ARG_INFO() +#endif + ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_fill_packed_array, 0, 1, IS_VOID, 0) ZEND_ARG_TYPE_INFO(1, array, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -336,6 +342,9 @@ static ZEND_FUNCTION(zend_test_is_string_marked_as_valid_utf8); static ZEND_FUNCTION(zend_get_map_ptr_last); static ZEND_FUNCTION(zend_test_crash); static ZEND_FUNCTION(zend_test_raise_and_throw); +#if defined(ZTS) && !defined(PHP_WIN32) +static ZEND_FUNCTION(zend_test_raise_on_foreign_thread); +#endif static ZEND_FUNCTION(zend_test_fill_packed_array); static ZEND_FUNCTION(zend_test_create_throwing_resource); static ZEND_FUNCTION(get_open_basedir); @@ -452,6 +461,9 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(zend_get_map_ptr_last, arginfo_zend_get_map_ptr_last) ZEND_FE(zend_test_crash, arginfo_zend_test_crash) ZEND_FE(zend_test_raise_and_throw, arginfo_zend_test_raise_and_throw) +#if defined(ZTS) && !defined(PHP_WIN32) + ZEND_FE(zend_test_raise_on_foreign_thread, arginfo_zend_test_raise_on_foreign_thread) +#endif ZEND_FE(zend_test_fill_packed_array, arginfo_zend_test_fill_packed_array) ZEND_FE(zend_test_create_throwing_resource, arginfo_zend_test_create_throwing_resource) ZEND_FE(get_open_basedir, arginfo_get_open_basedir) From 9e158b5ad4c99e6536cb42bf33ad6b8adcd46022 Mon Sep 17 00:00:00 2001 From: phcorp <725023+phcorp@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:38:26 +0100 Subject: [PATCH 2/5] Only re-route a signal while a PHP thread still handles it A signal whose handler was set back to SIG_DFL kept being sent on from a thread that never ran PHP. If every PHP thread blocked it, the signal stayed pending instead of taking its default action. Count the PHP threads whose handler is neither SIG_DFL nor SIG_IGN, and only re-route while that count is positive. The count follows every write to a thread's handlers and drops when a thread's storage is freed. zend_test_start_foreign_thread() starts a thread that never runs PHP and stays alive, so a test can block the signal on the PHP thread only. --- Zend/zend_signal.c | 69 ++++++++++++++++---- ext/pcntl/tests/gh25541_blocked.phpt | 29 ++++++++ ext/pcntl/tests/gh25541_handler_removed.phpt | 34 ++++++++++ ext/zend_test/test.c | 24 +++++++ ext/zend_test/test.stub.php | 2 + ext/zend_test/test_arginfo.h | 7 +- 6 files changed, 153 insertions(+), 12 deletions(-) create mode 100644 ext/pcntl/tests/gh25541_blocked.phpt create mode 100644 ext/pcntl/tests/gh25541_handler_removed.phpt diff --git a/Zend/zend_signal.c b/Zend/zend_signal.c index 1b94e1593544..37c1079726aa 100644 --- a/Zend/zend_signal.c +++ b/Zend/zend_signal.c @@ -85,11 +85,29 @@ static const int zend_sigs[] = { TIMEOUT_SIG, SIGHUP, SIGINT, SIGQUIT, SIGTERM, static zend_signal_entry_t global_orig_handlers[NSIG]; #ifdef ZTS -/* Signals for which a PHP thread registered a handler: only these are sent on - * from threads that never ran PHP, the others keep the original disposition. - * Never cleared: a PHP thread that no longer handles the signal applies the - * original disposition, as the thread that received it would have. */ -static zend_atomic_bool global_php_handled[NSIG]; +/* Number of PHP threads whose handler for the signal is neither SIG_DFL nor + * SIG_IGN. Only signals with a handler are sent on from threads that never ran + * PHP; the others keep the original disposition on the thread they hit. */ +static zend_atomic_int global_php_handlers[NSIG]; + +static bool zend_signal_entry_is_handler(const zend_signal_entry_t *entry) +{ + return entry->handler != (void *) SIG_DFL && entry->handler != (void *) SIG_IGN; +} + +/* Called by a PHP thread when its handler for signo changes. */ +static void zend_signal_count_handler(int signo, bool before, bool after) +{ + zend_atomic_int *count = &global_php_handlers[signo-1]; + int expected; + + if (before == after) { + return; + } + expected = zend_atomic_int_load_ex(count); + while (!zend_atomic_int_compare_exchange_ex(count, &expected, expected + (after ? 1 : -1))) { + } +} #endif static sigset_t global_sigmask; @@ -106,7 +124,7 @@ static void zend_signal_handler_defer(int signo, siginfo_t *siginfo, void *conte * thread handles this signal and the embedder does not, send it on to the * PHP threads. Otherwise act as if SIGG(active) is false. */ if (tsrm_is_shutdown() || !tsrm_is_managed_thread()) { - if (zend_atomic_bool_load_ex(&global_php_handled[signo-1]) + if (zend_atomic_int_load_ex(&global_php_handlers[signo-1]) > 0 && (global_orig_handlers[signo-1].handler == (void *) SIG_DFL || global_orig_handlers[signo-1].handler == (void *) SIG_IGN) && zend_signal_reroute_from_foreign_thread(signo, siginfo, context)) { @@ -236,6 +254,9 @@ static void zend_signal_handler(int signo, siginfo_t *siginfo, void *context) if (p_sig.flags & SA_RESETHAND) { SIGG(handlers)[signo-1].flags = 0; SIGG(handlers)[signo-1].handler = SIG_DFL; +#ifdef ZTS + zend_signal_count_handler(signo, true, false); +#endif } (*(void (*)(int, siginfo_t*, void*))p_sig.handler)(signo, siginfo, context); } else { @@ -259,6 +280,10 @@ ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct siga oldact->sa_mask = global_sigmask; } if (act != NULL) { +#ifdef ZTS + bool was_handler = zend_signal_entry_is_handler(&SIGG(handlers)[signo-1]); +#endif + SIGG(handlers)[signo-1].flags = act->sa_flags; if (act->sa_flags & SA_SIGINFO) { SIGG(handlers)[signo-1].handler = (void *) act->sa_sigaction; @@ -267,10 +292,7 @@ ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct siga } #ifdef ZTS - if (SIGG(handlers)[signo-1].handler != (void *) SIG_IGN - && SIGG(handlers)[signo-1].handler != (void *) SIG_DFL) { - zend_atomic_bool_store_ex(&global_php_handled[signo-1], true); - } + zend_signal_count_handler(signo, was_handler, zend_signal_entry_is_handler(&SIGG(handlers)[signo-1])); #endif memset(&sa, 0, sizeof(sa)); @@ -322,12 +344,19 @@ static zend_result zend_signal_register(int signo, void (*handler)(int, siginfo_ return FAILURE; } +#ifdef ZTS + bool was_handler = zend_signal_entry_is_handler(&SIGG(handlers)[signo-1]); +#endif + SIGG(handlers)[signo-1].flags = sa.sa_flags; if (sa.sa_flags & SA_SIGINFO) { SIGG(handlers)[signo-1].handler = (void *)sa.sa_sigaction; } else { SIGG(handlers)[signo-1].handler = (void *)sa.sa_handler; } +#ifdef ZTS + zend_signal_count_handler(signo, was_handler, zend_signal_entry_is_handler(&SIGG(handlers)[signo-1])); +#endif sa.sa_flags = SA_SIGINFO; /* we'll use a siginfo handler */ sa.sa_sigaction = handler; @@ -348,6 +377,12 @@ void zend_signal_activate(void) { size_t x; +#ifdef ZTS + for (x = 1; x < NSIG; x++) { + zend_signal_count_handler((int) x, zend_signal_entry_is_handler(&SIGG(handlers)[x-1]), + zend_signal_entry_is_handler(&global_orig_handlers[x-1])); + } +#endif memcpy(&SIGG(handlers), &global_orig_handlers, sizeof(global_orig_handlers)); if (SIGG(reset)) { @@ -415,6 +450,18 @@ static void zend_signal_globals_ctor(zend_signal_globals_t *zend_signal_globals) } /* }}} */ +#ifdef ZTS +static void zend_signal_globals_dtor(zend_signal_globals_t *zend_signal_globals) /* {{{ */ +{ + int signo; + + for (signo = 1; signo < NSIG; signo++) { + zend_signal_count_handler(signo, zend_signal_entry_is_handler(&zend_signal_globals->handlers[signo-1]), false); + } +} +/* }}} */ +#endif + void zend_signal_init(void) /* {{{ */ { int signo; @@ -441,7 +488,7 @@ ZEND_API void zend_signal_startup(void) { #ifdef ZTS - ts_allocate_fast_id(&zend_signal_globals_id, &zend_signal_globals_offset, sizeof(zend_signal_globals_t), (ts_allocate_ctor) zend_signal_globals_ctor, NULL); + ts_allocate_fast_id(&zend_signal_globals_id, &zend_signal_globals_offset, sizeof(zend_signal_globals_t), (ts_allocate_ctor) zend_signal_globals_ctor, (ts_allocate_dtor) zend_signal_globals_dtor); #else zend_signal_globals_ctor(&zend_signal_globals); #endif diff --git a/ext/pcntl/tests/gh25541_blocked.phpt b/ext/pcntl/tests/gh25541_blocked.phpt new file mode 100644 index 000000000000..798e63b5f873 --- /dev/null +++ b/ext/pcntl/tests/gh25541_blocked.phpt @@ -0,0 +1,29 @@ +--TEST-- +GH-25541 (Signal re-routed while the PHP thread blocks it is handled once unblocked) +--EXTENSIONS-- +pcntl +posix +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) diff --git a/ext/pcntl/tests/gh25541_handler_removed.phpt b/ext/pcntl/tests/gh25541_handler_removed.phpt new file mode 100644 index 000000000000..ca1852becdc1 --- /dev/null +++ b/ext/pcntl/tests/gh25541_handler_removed.phpt @@ -0,0 +1,34 @@ +--TEST-- +GH-25541 (Signal whose handler was removed keeps its default action on a thread that never ran PHP) +--EXTENSIONS-- +pcntl +posix +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) +bool(true) diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 611184052a69..9323d03a11f4 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -809,6 +809,30 @@ static ZEND_FUNCTION(zend_test_raise_on_foreign_thread) } pthread_join(thread, NULL); } + +static void *zend_test_idle_thread(void *arg) +{ + (void) arg; + for (;;) { + pause(); + } + return NULL; +} + +/* Starts a thread that never runs PHP and stays alive until the process exits. + * It inherits the caller's signal mask. */ +static ZEND_FUNCTION(zend_test_start_foreign_thread) +{ + pthread_t thread; + + ZEND_PARSE_PARAMETERS_NONE(); + + if (pthread_create(&thread, NULL, zend_test_idle_thread, NULL) != 0) { + zend_throw_error(NULL, "pthread_create() failed"); + RETURN_THROWS(); + } + pthread_detach(thread); +} #endif static bool has_opline(zend_execute_data *execute_data) diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index 828a61883534..914b36e17c83 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -309,6 +309,8 @@ function zend_test_raise_and_throw(int $signal): void {} #if defined(ZTS) && !defined(PHP_WIN32) function zend_test_raise_on_foreign_thread(int $signal): void {} + + function zend_test_start_foreign_thread(): void {} #endif function zend_test_fill_packed_array(array &$array): void {} diff --git a/ext/zend_test/test_arginfo.h b/ext/zend_test/test_arginfo.h index e75e73671b47..28720e99c01a 100644 --- a/ext/zend_test/test_arginfo.h +++ b/ext/zend_test/test_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 84b5600c974d901618c660cedb2a0df2f854e618 */ + * Stub hash: 48ccd956ff33cb3abb8ed0a88329d6eff2a2ed5b */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -159,6 +159,9 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_raise_on_foreign_thread, 0, 1, IS_VOID, 0) ZEND_ARG_TYPE_INFO(0, signal, IS_LONG, 0) ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_start_foreign_thread, 0, 0, IS_VOID, 0) +ZEND_END_ARG_INFO() #endif ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_fill_packed_array, 0, 1, IS_VOID, 0) @@ -344,6 +347,7 @@ static ZEND_FUNCTION(zend_test_crash); static ZEND_FUNCTION(zend_test_raise_and_throw); #if defined(ZTS) && !defined(PHP_WIN32) static ZEND_FUNCTION(zend_test_raise_on_foreign_thread); +static ZEND_FUNCTION(zend_test_start_foreign_thread); #endif static ZEND_FUNCTION(zend_test_fill_packed_array); static ZEND_FUNCTION(zend_test_create_throwing_resource); @@ -463,6 +467,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(zend_test_raise_and_throw, arginfo_zend_test_raise_and_throw) #if defined(ZTS) && !defined(PHP_WIN32) ZEND_FE(zend_test_raise_on_foreign_thread, arginfo_zend_test_raise_on_foreign_thread) + ZEND_FE(zend_test_start_foreign_thread, arginfo_zend_test_start_foreign_thread) #endif ZEND_FE(zend_test_fill_packed_array, arginfo_zend_test_fill_packed_array) ZEND_FE(zend_test_create_throwing_resource, arginfo_zend_test_create_throwing_resource) From 351719a6a97381cce28426940d22bf418d214fa6 Mon Sep 17 00:00:00 2001 From: phcorp <725023+phcorp@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:08:05 +0100 Subject: [PATCH 3/5] Don't reset SIGG(handlers) on a thread without TSRM On a thread without TSRM, or after TSRM shutdown, zend_signal_handler() calls the handler that was installed before PHP started. When that handler has SA_RESETHAND, it then wrote to SIGG(handlers), which such a thread has no storage for, and crashed. It also decremented the count of PHP threads handling the signal, although that thread was never counted. Only reset the entry when it comes from this thread's SIGG(handlers). macOS does not report SA_RESETHAND back from sigaction(), so it was not affected. --- Zend/zend_signal.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Zend/zend_signal.c b/Zend/zend_signal.c index 37c1079726aa..ebfc43c6393e 100644 --- a/Zend/zend_signal.c +++ b/Zend/zend_signal.c @@ -220,9 +220,13 @@ static void zend_signal_handler(int signo, siginfo_t *siginfo, void *context) struct sigaction sa; sigset_t sigset; zend_signal_entry_t p_sig; + /* Whether p_sig is this thread's entry in SIGG(handlers), which only then + * may be reset for SA_RESETHAND. */ + bool own_handler = true; #ifdef ZTS if (tsrm_is_shutdown() || !tsrm_is_managed_thread()) { p_sig = global_orig_handlers[signo-1]; + own_handler = false; } else #endif p_sig = SIGG(handlers)[signo-1]; @@ -251,7 +255,7 @@ static void zend_signal_handler(int signo, siginfo_t *siginfo, void *context) } } else if (p_sig.handler != SIG_IGN) { if (p_sig.flags & SA_SIGINFO) { - if (p_sig.flags & SA_RESETHAND) { + if ((p_sig.flags & SA_RESETHAND) && own_handler) { SIGG(handlers)[signo-1].flags = 0; SIGG(handlers)[signo-1].handler = SIG_DFL; #ifdef ZTS From cd1b99c92f386fa6856784127824b35428f9dc3d Mon Sep 17 00:00:00 2001 From: phcorp <725023+phcorp@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:57:02 +0100 Subject: [PATCH 4/5] Fix GH-25548: Execution timeout on a thread that never ran PHP is lost Under ZTS without zend signals, the process-wide execution timer can fire on a thread that has no TSRM storage. zend_timeout_handler() printed a message and returned, and as it was installed with SA_RESETHAND, the next signal killed the process. Install the handler with SA_SIGINFO and without SA_RESETHAND under ZTS, and re-route the signal from such a thread to the PHP threads, as GH-25541 does for pcntl, while the timer is armed; otherwise ignore it there, so that the thread does not block the signal for nothing. With zend signals, the timeout handler is already re-routed. A PHP thread that has no timeout armed ignores the signal: it is stale (the timer was disarmed) or belongs to another thread's timer. --- NEWS | 2 ++ Zend/tests/gh25548.phpt | 20 +++++++++++++ Zend/tests/gh25548_stale_timer.phpt | 19 +++++++++++++ Zend/zend_execute_API.c | 44 +++++++++++++++++++++++++++++ ext/zend_test/test.c | 14 +++++++++ ext/zend_test/test.stub.php | 2 ++ ext/zend_test/test_arginfo.h | 7 ++++- 7 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 Zend/tests/gh25548.phpt create mode 100644 Zend/tests/gh25548_stale_timer.phpt diff --git a/NEWS b/NEWS index 2dc173208e7d..e032b3544920 100644 --- a/NEWS +++ b/NEWS @@ -29,6 +29,8 @@ PHP NEWS . Fixed object_properties_load() adding integer keys to property tables, which made the properties unreadable and clone hit an assertion. (Nicolas Grekas) + . Fixed bug GH-25548 (Execution timeout delivered to a thread that never ran + PHP is lost and kills the process under ZTS). (phcorp) - Curl: . Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire) diff --git a/Zend/tests/gh25548.phpt b/Zend/tests/gh25548.phpt new file mode 100644 index 000000000000..3a67075a54e1 --- /dev/null +++ b/Zend/tests/gh25548.phpt @@ -0,0 +1,20 @@ +--TEST-- +GH-25548 (Execution timeout delivered to a thread that never ran PHP) +--EXTENSIONS-- +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +Fatal error: Maximum execution time of 30 seconds exceeded in %s on line %d diff --git a/Zend/tests/gh25548_stale_timer.phpt b/Zend/tests/gh25548_stale_timer.phpt new file mode 100644 index 000000000000..16e2aff677c7 --- /dev/null +++ b/Zend/tests/gh25548_stale_timer.phpt @@ -0,0 +1,19 @@ +--TEST-- +GH-25548 (Signal of a disarmed execution timer delivered to a thread that never ran PHP) +--EXTENSIONS-- +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +survived diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c index aeb68bdd6be5..21d3e96a1611 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -1505,6 +1505,13 @@ static void zend_timeout_handler(int dummy) /* {{{ */ return; } +# ifndef ZEND_MAX_EXECUTION_TIMERS + /* The timer is process-wide. If this thread has no timeout armed, the signal + * is stale (the timer was disarmed) or was meant for another thread. */ + if (!EG(timeout_seconds)) { + return; + } +# endif #else if (zend_atomic_bool_load_ex(&EG(timed_out))) { /* Die on hard timeout */ @@ -1554,6 +1561,27 @@ static void zend_timeout_handler(int dummy) /* {{{ */ /* }}} */ #endif +#if defined(ZTS) && !defined(ZEND_WIN32) && !defined(ZEND_MAX_EXECUTION_TIMERS) \ + && defined(HAVE_SETITIMER) && !defined(ZEND_SIGNALS) && defined(HAVE_SIGACTION) +# define ZEND_TIMEOUT_SIGNAL_REROUTE 1 +/* Whether the process-wide timer is armed. */ +static zend_atomic_bool zend_timer_armed; + +/* The timer signal may reach a thread that never ran PHP: send it on to a PHP + * thread while the timer is armed, and ignore it otherwise. */ +static void zend_timeout_signal_handler(int signo, siginfo_t *si, void *uc) /* {{{ */ +{ + if (!tsrm_is_managed_thread()) { + if (!zend_atomic_bool_load_ex(&zend_timer_armed) + || zend_signal_reroute_from_foreign_thread(signo, si, uc)) { + return; + } + } + zend_timeout_handler(signo); +} +/* }}} */ +#endif + #ifdef ZEND_WIN32 VOID CALLBACK tq_timer_cb(PVOID arg, BOOLEAN timed_out) { @@ -1646,6 +1674,11 @@ static void zend_set_timeout_ex(zend_long seconds, bool reset_signals) /* {{{ */ } signo = SIGPROF; # endif +# ifdef ZEND_TIMEOUT_SIGNAL_REROUTE + if (seconds) { + zend_atomic_bool_store_ex(&zend_timer_armed, true); + } +# endif if (reset_signals) { # ifdef ZEND_SIGNALS @@ -1655,9 +1688,17 @@ static void zend_set_timeout_ex(zend_long seconds, bool reset_signals) /* {{{ */ # ifdef HAVE_SIGACTION struct sigaction act; +# ifdef ZTS + /* No SA_RESETHAND: the disposition is process-wide, and a signal + * re-routed from another thread must still find the handler. */ + act.sa_sigaction = zend_timeout_signal_handler; + sigemptyset(&act.sa_mask); + act.sa_flags = SA_ONSTACK | SA_SIGINFO; +# else act.sa_handler = zend_timeout_handler; sigemptyset(&act.sa_mask); act.sa_flags = SA_ONSTACK | SA_RESETHAND | SA_NODEFER; +# endif sigaction(signo, &act, NULL); # else signal(signo, zend_timeout_handler); @@ -1707,6 +1748,9 @@ void zend_unset_timeout(void) /* {{{ */ setitimer(ITIMER_REAL, &no_timeout, NULL); # else setitimer(ITIMER_PROF, &no_timeout, NULL); +# endif +# ifdef ZEND_TIMEOUT_SIGNAL_REROUTE + zend_atomic_bool_store_ex(&zend_timer_armed, false); # endif } #endif diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 9323d03a11f4..e7e1365a3229 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -833,6 +833,20 @@ static ZEND_FUNCTION(zend_test_start_foreign_thread) } pthread_detach(thread); } + +/* The signal of the execution timer if it is process-wide, null otherwise. */ +static ZEND_FUNCTION(zend_test_timeout_signal) +{ + ZEND_PARSE_PARAMETERS_NONE(); + +#if defined(ZEND_MAX_EXECUTION_TIMERS) || !defined(HAVE_SETITIMER) + RETURN_NULL(); +#elif defined(__CYGWIN__) || defined(__PASE__) || (defined(__aarch64__) && defined(__APPLE__)) + RETURN_LONG(SIGALRM); +#else + RETURN_LONG(SIGPROF); +#endif +} #endif static bool has_opline(zend_execute_data *execute_data) diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index 914b36e17c83..ac498325c6af 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -311,6 +311,8 @@ function zend_test_raise_and_throw(int $signal): void {} function zend_test_raise_on_foreign_thread(int $signal): void {} function zend_test_start_foreign_thread(): void {} + + function zend_test_timeout_signal(): ?int {} #endif function zend_test_fill_packed_array(array &$array): void {} diff --git a/ext/zend_test/test_arginfo.h b/ext/zend_test/test_arginfo.h index 28720e99c01a..f41319e6388d 100644 --- a/ext/zend_test/test_arginfo.h +++ b/ext/zend_test/test_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 48ccd956ff33cb3abb8ed0a88329d6eff2a2ed5b */ + * Stub hash: 8d9bda09350e35211527847e5bc41c9b9f9ddbce */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -162,6 +162,9 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_start_foreign_thread, 0, 0, IS_VOID, 0) ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_timeout_signal, 0, 0, IS_LONG, 1) +ZEND_END_ARG_INFO() #endif ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_fill_packed_array, 0, 1, IS_VOID, 0) @@ -348,6 +351,7 @@ static ZEND_FUNCTION(zend_test_raise_and_throw); #if defined(ZTS) && !defined(PHP_WIN32) static ZEND_FUNCTION(zend_test_raise_on_foreign_thread); static ZEND_FUNCTION(zend_test_start_foreign_thread); +static ZEND_FUNCTION(zend_test_timeout_signal); #endif static ZEND_FUNCTION(zend_test_fill_packed_array); static ZEND_FUNCTION(zend_test_create_throwing_resource); @@ -468,6 +472,7 @@ static const zend_function_entry ext_functions[] = { #if defined(ZTS) && !defined(PHP_WIN32) ZEND_FE(zend_test_raise_on_foreign_thread, arginfo_zend_test_raise_on_foreign_thread) ZEND_FE(zend_test_start_foreign_thread, arginfo_zend_test_start_foreign_thread) + ZEND_FE(zend_test_timeout_signal, arginfo_zend_test_timeout_signal) #endif ZEND_FE(zend_test_fill_packed_array, arginfo_zend_test_fill_packed_array) ZEND_FE(zend_test_create_throwing_resource, arginfo_zend_test_create_throwing_resource) From 68b50418a7d9f38e048ca9e62b57ae15f12bf917 Mon Sep 17 00:00:00 2001 From: phcorp <725023+phcorp@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:08:10 +0100 Subject: [PATCH 5/5] Skip the GH-25548 tests where signals are not re-routed Re-routing is only enabled on Linux, macOS and FreeBSD, as in the GH-25541 tests. --- Zend/tests/gh25548.phpt | 1 + Zend/tests/gh25548_stale_timer.phpt | 1 + 2 files changed, 2 insertions(+) diff --git a/Zend/tests/gh25548.phpt b/Zend/tests/gh25548.phpt index 3a67075a54e1..eb0ffd1dd7b2 100644 --- a/Zend/tests/gh25548.phpt +++ b/Zend/tests/gh25548.phpt @@ -6,6 +6,7 @@ zend_test --FILE-- --FILE--