Skip to content

Fix GH-25541: Signal on a thread that never ran PHP crashes or kills the process - #25580

Open
phcorp wants to merge 5 commits into
php:PHP-8.4from
phcorp:fix-gh25541-minimal
Open

phcorp wants to merge 5 commits into
php:PHP-8.4from
phcorp:fix-gh25541-minimal

Conversation

@phcorp

@phcorp phcorp commented Nov 19, 2026 •

Copy link
Copy Markdown

Under ZTS, the kernel may deliver a process signal to any thread that does not block it, including threads that never ran PHP and have no TSRM storage. Examples are the Go runtime threads in FrankenPHP, or any thread an embedding application creates. When that happens:

  • with --disable-zend-signals, pcntl_signal_handler() reads PCNTL_G() through a NULL TSRM pointer and crashes;
  • with zend signals (the default), zend_signal_handler_defer() falls back to the original disposition, so SIG_DFL kills the process even though the script registered a pcntl handler.

Reproducer (ZTS, with the new zend_test hook): pcntl_signal(SIGUSR1, fn() => print "ok\n"); zend_test_raise_on_foreign_thread(SIGUSR1); pcntl_signal_dispatch();. On a stock 8.4 build, the process dies from SIGUSR1, or crashes with --disable-zend-signals.

Fix: on a thread without TSRM, for a signal that at least one PHP thread still handles (neither SIG_DFL nor SIG_IGN) and the embedder does not, the handler adds the signal to the mask saved in its ucontext (so the thread blocks it after sigreturn) and sends it to the process again with kill(getpid(), signo). The kernel then delivers it to a thread that does not block it, and foreign threads stop taking it after the first time, so it ends up on a PHP thread. A thread unblocks re-routed signals when it starts a request, which covers threads created by a masked thread. Faults and abort() keep their default action on the thread they hit; SIGABRT counts as abort() when it was sent to that thread (SI_TKILL on Linux, SI_LWP on FreeBSD), so a SIGABRT sent with kill() is re-routed. Signals with no PHP handler, or handled by the embedder, keep the original disposition, as before. A per-signal count of PHP threads with a handler follows zend_sigaction(), request start and thread exit, so a handler set back to SIG_DFL stops the re-routing. There's no new thread and no lock. Re-routing is enabled on Linux, macOS and FreeBSD, where I measured it (glibc and musl, arm64 and amd64; macOS arm64; FreeBSD 15.1 arm64).

It also fixes a crash that already exists on 8.4: if the embedder installed an SA_SIGINFO | SA_RESETHAND handler before PHP started, a signal on a thread without TSRM ran that handler and then wrote to SIGG(handlers), which that thread has no storage for. With a pcntl handler for the same signal, this segfaults on stock 8.4 (measured on glibc, and with this PR's earlier commits on musl and FreeBSD). macOS doesn't report SA_RESETHAND back from sigaction(), so it isn't affected.

Not covered:

  • It changes the signal mask of threads the embedder owns, for signals PHP handles. At request start, PHP threads unblock re-routed signals, as zend_sigaction() already does for the signals it registers.
  • siginfo of the delivered signal names this process as the sender.
  • The PHP thread that receives the signal is not necessarily the one that registered the handler (ZTS: pcntl signal handled by one PHP thread ends the process or is lost when delivered elsewhere #25549).
  • On macOS, a signal re-sent while every thread blocks it is attached to the main thread, so if the main thread doesn't run PHP it is lost. Linux and FreeBSD keep it pending process-wide. Also on macOS, SIGSEGV and SIGBUS sent with kill() carry a fault code, so they are treated as faults. SIGABRT from abort() can't be told apart from one sent with kill() there, so it is re-routed; abort() still ends the process with SIGABRT.

GH-25548 (the execution timer) is a separate PR on top of this one.

Questions for reviewers:

  1. Is changing a foreign thread's mask acceptable for embedders, or should it be opt-in?
  2. This adds three ZEND_API functions on 8.4 so pcntl can use them without zend signals. Is that OK on a stable branch, or should pcntl keep its own copy?

Merge-up notes (this PR and #25581 merged up locally through 8.5, 8.6 and master, built with and without zend signals on macOS arm64 and Linux glibc; the GH-25541/GH-25548 tests, ext/pcntl, ext/posix, ext/zend_test and Zend/tests pass on each; the SIGABRT commit was only checked for conflicts there, and adds none):

  • PHP-8.6 and master: drop the # ifdef HAVE_STRUCT_SIGINFO_T / # endif around the re-route in pcntl_signal_handler(), since ext/pcntl simplification. #13975 removed that macro. Otherwise it still compiles, but signals are dropped with --disable-zend-signals, which no CI job builds.
  • Each branch: ext/zend_test/test.c conflicts on the includes (keep both sides; <signal.h> is already included there), and the zend_test arginfo headers need build/gen_stub.php.
  • master: in Zend/zend_signal.c, zend_atomic_bool/zend_atomic_int become atomic_bool/atomic_int, zend_atomic_*_load_ex()/_store_ex() become atomic_load()/atomic_store(), and zend_atomic_int_compare_exchange_ex() becomes atomic_compare_exchange_strong(); zend_signal_startup() passes zend_signal_globals_dtor to ts_allocate_tls_id().
  • master, if Remove Zend Signals #25627 lands first: Zend/zend_signal.c is gone, but pcntl and zend_activate() still need zend_signal_is_synchronous(), zend_signal_reroute_from_foreign_thread() and zend_signal_unblock_rerouted(), which don't depend on zend signals. They move to a file that stays.

@devnexen

Copy link
Copy Markdown
Member

I do not feel particularly confident overall, can this test be tried ?

--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--
<?php
if (!function_exists('zend_test_start_foreign_thread')) die('skip ZTS only');
?>
--FILE--
<?php
$pid = pcntl_fork();
if ($pid === 0) {
    pcntl_signal(SIGUSR1, function () {});
    pcntl_signal(SIGUSR1, SIG_DFL);
    zend_test_start_foreign_thread();

    /* The PHP thread blocks the signal, so the kernel picks the other thread. */
    pcntl_sigprocmask(SIG_BLOCK, [SIGUSR1]);
    posix_kill(posix_getpid(), SIGUSR1);
    usleep(500000);
    echo "child survived", PHP_EOL;
    exit(0);
}

pcntl_waitpid($pid, $status);
var_dump(pcntl_wifsignaled($status));
var_dump(pcntl_wifsignaled($status) && pcntl_wtermsig($status) === SIGUSR1);
?>
--EXPECT--
bool(true)
bool(true)

@phcorp

phcorp commented Nov 19, 2026

Copy link
Copy Markdown
Author

Thanks, your doubt was justified: the test found a real regression. I ran it three times per build, on stock 8.4 and on this branch, with and without zend signals:

  • Linux (glibc and musl) and FreeBSD: stock 8.4 passes. This branch failed with zend signals ("child survived"). The flag that enables re-routing was set when a handler was registered and never cleared, so the other thread kept sending SIGUSR1 on, and with the PHP thread blocking it, nothing took it.
  • macOS: it fails on stock 8.4 too, because sigprocmask() there blocks the signal in every thread, the foreign one included. So it skips on macOS.

Fixed in 9e158b5. The flag is now a count of PHP threads whose handler is neither SIG_DFL nor SIG_IGN. zend_sigaction() updates it, as do request start and the freeing of a thread's storage. Re-routing only happens while the count is positive.

I added your test unchanged except for the macOS skip, with zend_test_start_foreign_thread() in zend_test. I also added gh25541_blocked.phpt for the opposite case: the handler is still set and the PHP thread blocks the signal, and the handler must run once it unblocks it. On stock 8.4 that one kills the process (SIGUSR1), or crashes with --disable-zend-signals.

With the fix, the five gh25541 tests pass 5/5 on glibc, musl and FreeBSD in both modes, and ext/pcntl, ext/posix, ext/zend_test and Zend/tests show no new failures.

@devnexen

Copy link
Copy Markdown
Member

It seems better, however it probably needs noticeably more work. I ll get back to in the following days.

@phcorp

phcorp commented Nov 19, 2026

Copy link
Copy Markdown
Author

Thanks, take your time. In the meantime I've added one commit here, 351719a, after a review of the range. It fixes a crash that already exists on 8.4. Say the embedder installed an SA_SIGINFO | SA_RESETHAND handler before PHP started. A signal on a thread without TSRM then ran that handler and wrote to SIGG(handlers), which that thread has no storage for. With a pcntl handler for the same signal, stock 8.4 segfaults; I measured it on glibc, and on musl and FreeBSD with this PR's earlier commits. It doesn't happen on macOS, because sigaction() there doesn't report SA_RESETHAND back. The other changes went to #25581. If you can say which part you think needs the most work, I'll start there.

@devnexen

Copy link
Copy Markdown
Member

If you can say which part you think needs the most work, I'll start there.

The re-routing, e.g. a handled SIGABRT sent with kill() is taken for abort(). zend_signal_is_synchronous() compares si_pid with getpid(), which is also true for the signal the re-route sends. To keep an eye on for master however.

…ls 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.
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.
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_signal_is_synchronous() took a SIGABRT whose sender is this process
for abort(). That is also true of kill(getpid(), SIGABRT) from a script,
and of the signal the re-route itself sends, so with a pcntl handler set:
- a SIGABRT the process sends itself, received by a thread without
  TSRM, killed the process (zend signals) or was dropped (without);
- a SIGABRT from another process, re-sent by one such thread and received
  by a second one, did the same.

abort() needs no exception: when a handler returns, it restores the
default action and raises SIGABRT again, also when the handler left it
blocked in uc_sigmask (measured on glibc, musl and macOS). macOS can't
tell kill() from raise() within a process anyway.
@phcorp
phcorp force-pushed the fix-gh25541-minimal branch from 351719a to c2b07bd Compare November 21, 2026 09:01
@phcorp

phcorp commented Nov 21, 2026

Copy link
Copy Markdown
Author

You're right, thanks. I measured it before changing anything.

What siginfo tells apart (C probe without PHP, SIGABRT handler installed):

source glibc 2.41 / musl FreeBSD 15.1 macOS
raise(), pthread_kill(), abort() SI_TKILL, own pid SI_LWP, own pid si_code 0, own pid
kill(getpid()) SI_USER, own pid SI_USER, own pid si_code 0, own pid
kill() from another process SI_USER, other pid SI_USER, other pid other pid

So on macOS, abort() and kill(getpid(), SIGABRT) look the same, and the si_pid test can't work there. On Linux and FreeBSD it can only work by checking SI_TKILL or SI_LWP.

Is the exception needed? No. When a handler returns, abort() restores the default action and raises SIGABRT again. The process still dies from SIGABRT when the handler left it blocked in uc_sigmask, as the re-route does (glibc, musl, FreeBSD, macOS).

Reproducers (3 runs each, ZTS debug; stock measured on glibc and musl, the rest on glibc, musl and FreeBSD, all the same):

test stock 8.4, zend signals stock 8.4, without PR before PR now
gh25541_abort_kill: script sends itself SIGABRT, the PHP thread blocks it, 1 foreign thread killed (SIGABRT) segfault killed (SIGABRT) / dropped handled
gh25541_abort_external: SIGABRT from another process, 2 foreign threads killed (SIGABRT) segfault killed (SIGABRT) / dropped handled
gh25541_abort: abort() on a foreign thread, SIGABRT handled killed (SIGABRT) segfault (no helper) killed (SIGABRT)

With a single foreign thread, the second case passes on the PR before the fix. With two, the second one receives the signal the first one re-sent, so the re-route itself caused that failure, as you said.

Fixed in c2b07bd: zend_signal_is_synchronous() no longer lists SIGABRT, and pcntl drops its signo != SIGABRT exception. zend_test_abort_on_foreign_thread() is added for the third test, and is skipped in arginfo_zpp_mismatch.inc like zend_test_crash. The branch is rebased on PHP-8.4.

The 8 gh25541 tests pass 3/3 on glibc, musl and FreeBSD, with and without zend signals. The 4 that run on macOS pass there in both modes; the others need per-thread sigprocmask() and skip there. ext/pcntl, ext/posix, ext/zend_test and Zend/tests fail the same tests with and without the fix: on Linux, the ones stock 8.4 fails there too (pcntl_setns and posix_get_last_error, from the container); on FreeBSD, stack_limit_001, stack_limit_006 and posix_get_last_error. All of this was measured on arm64 only this time.

About #25627 for master: if it lands, zend_signal.c goes away, but pcntl still needs zend_signal_is_synchronous(), zend_signal_reroute_from_foreign_thread() and zend_signal_unblock_rerouted() (the last one from zend_activate()). They don't depend on zend signals, so on master they would move to a file that stays, or into pcntl. I'll do that in the merge-up of whichever lands second.

The previous commit re-routed every SIGABRT. When the handler leaves
SIGABRT blocked on the thread that called abort(), glibc 2.39 and older
can't raise it again and the process ends with another signal (SIGTRAP
on aarch64, measured on glibc 2.35, 2.36 and 2.39).

Count SIGABRT as abort() again when it was sent to this thread: SI_TKILL
on Linux, SI_LWP on FreeBSD. kill(), including the re-route's own,
reports SI_USER, so it is still re-routed. macOS reports both alike, and
its abort() still ends with SIGABRT, so SIGABRT is re-routed there.

Also poll pcntl_signal_dispatch() in the tests that block the signal, and
skip the other foreign-thread helpers in arginfo_zpp_mismatch.inc.
@phcorp

phcorp commented Nov 21, 2026

Copy link
Copy Markdown
Author

Correction to my last comment: abort() does need the exception. I had only measured glibc 2.41. If the handler leaves SIGABRT blocked, as the re-route does, abort() ends with SIGTRAP instead of SIGABRT on glibc 2.35, 2.36 and 2.39 (aarch64; I haven't measured x86_64). gh25541_abort.phpt fails 3/3 on Ubuntu 24.04 with c2b07bd, in both modes.

Fixed in d0984a2: SIGABRT counts as abort() again, but only when it was sent to this thread: SI_TKILL on Linux, SI_LWP on FreeBSD. kill(), including the re-route's own, reports SI_USER there, so both of your cases stay fixed. macOS can't tell the two apart, but its abort() still ends with SIGABRT after a re-route, so SIGABRT is re-routed there. pcntl keeps its signo != SIGABRT exception for abort() again.

Also from a review of the range: the tests that block the signal now poll pcntl_signal_dispatch() instead of dispatching once after a fixed wait, and arginfo_zpp_mismatch.inc also skips the two other foreign-thread helpers.

The 8 gh25541 tests pass 3/3 with and without zend signals on glibc 2.39, glibc 2.41, musl and FreeBSD 15.1. The 4 that run on macOS pass there in both modes. ext/pcntl, ext/posix, ext/zend_test and Zend/tests fail only the tests that already fail without this PR in those environments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants