From 9d9a3fb8c9105caf15463be252b87f1d9adb319c Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Wed, 15 Mar 2023 22:28:08 +0100 Subject: [PATCH] Fix ZTS zend signal crashes due to NULL globals Fixes GH-8789. Fixes GH-10015. This is one small part of the underlying bug for GH-10737, as in my attempts to reproduce the issue I constantly hit this crash easily. (The fix for the other underlying issue for that bug will follow soon.) It's possible that a signal arrives at a thread that never handled a PHP request before. This causes the signal globals to dereference a NULL pointer because the TSRM pointers for the thread aren't set up to point to the thread resources yet. PR GH-9766 previously fixed this for master by ignoring the signal if the thread didn't handle a PHP request yet. While this fixes the crash bug, I think the solution is suboptimal for 3 reasons: 1) The signal is ignored and a message is printed saying there is a bug. However, this is not a bug at all. For example in Apache, the signal set up happens on child process creation, and the thread resource creation happens lazily when the first request is handled by the thread. Hence, the fact that the thread resources aren't set up yet is not actually buggy behaviour. 2) I believe since it was believed to be buggy behaviour, that fix was only applied to master, so 8.1 & 8.2 keep on crashing. 3) We can do better than ignoring the signal. By just acting in the same way as if the signals aren't active. This means we need to take the same path as if the TSRM had already shut down. --- Zend/zend_signal.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Zend/zend_signal.c b/Zend/zend_signal.c index 3c090ccb8c52..3b9dd514bcc4 100644 --- a/Zend/zend_signal.c +++ b/Zend/zend_signal.c @@ -85,8 +85,10 @@ void zend_signal_handler_defer(int signo, siginfo_t *siginfo, void *context) zend_signal_queue_t *queue, *qtmp; #ifdef ZTS - /* A signal could hit after TSRM shutdown, in this case globals are already freed. */ - if (tsrm_is_shutdown()) { + /* 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. */ + if (tsrm_is_shutdown() || !tsrm_get_ls_cache()) { /* Forward to default handler handler */ zend_signal_handler(signo, siginfo, context); return; @@ -178,7 +180,7 @@ static void zend_signal_handler(int signo, siginfo_t *siginfo, void *context) sigset_t sigset; zend_signal_entry_t p_sig; #ifdef ZTS - if (tsrm_is_shutdown()) { + if (tsrm_is_shutdown() || !tsrm_get_ls_cache()) { p_sig.flags = 0; p_sig.handler = SIG_DFL; } else