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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
216 changes: 213 additions & 3 deletions Zend/zend_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_UCONTEXT_H
#include <sys/ucontext.h>
#endif
#if defined(ZTS) && !defined(ZEND_WIN32)
#include <pthread.h>
#endif

#ifdef ZEND_SIGNALS

Expand Down Expand Up @@ -77,6 +83,32 @@ 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
/* 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;

/* {{{ zend_signal_handler_defer
Expand All @@ -88,9 +120,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_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)) {
return;
}
/* Forward to default handler handler */
zend_signal_handler(signo, siginfo, context);
return;
Expand Down Expand Up @@ -215,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 {
Expand All @@ -238,13 +280,21 @@ 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;
} else {
SIGG(handlers)[signo-1].handler = (void *) act->sa_handler;
}

#ifdef ZTS
zend_signal_count_handler(signo, was_handler, zend_signal_entry_is_handler(&SIGG(handlers)[signo-1]));
#endif

memset(&sa, 0, sizeof(sa));
if (SIGG(handlers)[signo-1].handler == (void *) SIG_IGN) {
sa.sa_sigaction = (void *) SIG_IGN;
Expand Down Expand Up @@ -294,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;
Expand All @@ -320,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)) {
Expand Down Expand Up @@ -387,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;
Expand All @@ -413,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
Expand Down Expand Up @@ -446,3 +521,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
8 changes: 8 additions & 0 deletions Zend/zend_signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <signal.h>

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 */
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion ext/pcntl/pcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Loading