From 0fec31c97e7f1766ab3f912fe34730da3dd2296b Mon Sep 17 00:00:00 2001 From: lacatoire Date: Thu, 1 Oct 2026 19:06:43 +0200 Subject: [PATCH 1/2] Fix pcntl_signal() fatal error for SIGKILL and SIGSTOP pcntl_signal() on SIGKILL or SIGSTOP aborted the script with an uncatchable E_ERROR raised by zend_sigaction(), because sigaction() always fails for these signals. The SIG_ERR handling in pcntl_signal() was never reached. Return false with the usual "Error assigning signal" warning and EINVAL as last error instead. Fixes: #2863 --- ext/pcntl/pcntl.c | 7 ++++ .../tests/pcntl_signal_sigkill_sigstop.phpt | 35 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 ext/pcntl/tests/pcntl_signal_sigkill_sigstop.phpt diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 1858ee6e4a72..a5f38a9aaa1b 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -760,6 +760,13 @@ PHP_FUNCTION(pcntl_signal) RETURN_THROWS(); } + /* sigaction() always fails for these, and zend_sigaction() turns that into a fatal error */ + if (signo == SIGKILL || signo == SIGSTOP) { + PCNTL_G(last_error) = EINVAL; + php_error_docref(NULL, E_WARNING, "Error assigning signal"); + RETURN_FALSE; + } + if (!PCNTL_G(spares)) { /* since calling malloc() from within a signal handler is not portable, * pre-allocate a few records for recording signals */ diff --git a/ext/pcntl/tests/pcntl_signal_sigkill_sigstop.phpt b/ext/pcntl/tests/pcntl_signal_sigkill_sigstop.phpt new file mode 100644 index 000000000000..0016cd065722 --- /dev/null +++ b/ext/pcntl/tests/pcntl_signal_sigkill_sigstop.phpt @@ -0,0 +1,35 @@ +--TEST-- +pcntl_signal() returns false for SIGKILL and SIGSTOP +--EXTENSIONS-- +pcntl +--FILE-- + +--EXPECTF-- +Warning: pcntl_signal(): Error assigning signal in %s on line %d +bool(false) + +Warning: pcntl_signal(): Error assigning signal in %s on line %d +bool(false) + +Warning: pcntl_signal(): Error assigning signal in %s on line %d +bool(false) +bool(true) + +Warning: pcntl_signal(): Error assigning signal in %s on line %d +bool(false) + +Warning: pcntl_signal(): Error assigning signal in %s on line %d +bool(false) + +Warning: pcntl_signal(): Error assigning signal in %s on line %d +bool(false) +bool(true) +alive From b1df3242d5dc811b6dcf74bbe99c43c3eb0cdec9 Mon Sep 17 00:00:00 2001 From: lacatoire Date: Tue, 17 Nov 2026 14:04:12 +0100 Subject: [PATCH 2/2] Validate the handler before the SIGKILL and SIGSTOP check An invalid handler for SIGKILL or SIGSTOP returned false instead of throwing the TypeError or ValueError raised for every other signal. --- ext/pcntl/pcntl.c | 23 ++++++++++--------- .../tests/pcntl_signal_sigkill_sigstop.phpt | 14 +++++++++++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index a5f38a9aaa1b..67b129a15096 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -760,6 +760,18 @@ PHP_FUNCTION(pcntl_signal) RETURN_THROWS(); } + if (Z_TYPE_P(handle) == IS_LONG) { + if (Z_LVAL_P(handle) != (zend_long) SIG_DFL && Z_LVAL_P(handle) != (zend_long) SIG_IGN) { + zend_argument_value_error(2, "must be either SIG_DFL or SIG_IGN when an integer value is given"); + RETURN_THROWS(); + } + } else if (!zend_is_callable_ex(handle, NULL, 0, NULL, NULL, NULL)) { + PCNTL_G(last_error) = EINVAL; + + zend_argument_type_error(2, "must be of type callable|int, %s given", zend_zval_value_name(handle)); + RETURN_THROWS(); + } + /* sigaction() always fails for these, and zend_sigaction() turns that into a fatal error */ if (signo == SIGKILL || signo == SIGSTOP) { PCNTL_G(last_error) = EINVAL; @@ -788,10 +800,6 @@ PHP_FUNCTION(pcntl_signal) /* Special long value case for SIG_DFL and SIG_IGN */ if (Z_TYPE_P(handle) == IS_LONG) { - if (Z_LVAL_P(handle) != (zend_long) SIG_DFL && Z_LVAL_P(handle) != (zend_long) SIG_IGN) { - zend_argument_value_error(2, "must be either SIG_DFL or SIG_IGN when an integer value is given"); - RETURN_THROWS(); - } if (php_signal(signo, (Sigfunc *) Z_LVAL_P(handle), (int) restart_syscalls) == (void *)SIG_ERR) { PCNTL_G(last_error) = errno; php_error_docref(NULL, E_WARNING, "Error assigning signal"); @@ -801,13 +809,6 @@ PHP_FUNCTION(pcntl_signal) RETURN_TRUE; } - if (!zend_is_callable_ex(handle, NULL, 0, NULL, NULL, NULL)) { - PCNTL_G(last_error) = EINVAL; - - zend_argument_type_error(2, "must be of type callable|int, %s given", zend_zval_value_name(handle)); - RETURN_THROWS(); - } - /* Register with the OS first so that on failure we don't record a handler that was never installed */ if (php_signal4(signo, pcntl_signal_handler, (int) restart_syscalls, 1) == (void *)SIG_ERR) { PCNTL_G(last_error) = errno; diff --git a/ext/pcntl/tests/pcntl_signal_sigkill_sigstop.phpt b/ext/pcntl/tests/pcntl_signal_sigkill_sigstop.phpt index 0016cd065722..ab68bf799dcb 100644 --- a/ext/pcntl/tests/pcntl_signal_sigkill_sigstop.phpt +++ b/ext/pcntl/tests/pcntl_signal_sigkill_sigstop.phpt @@ -10,6 +10,16 @@ foreach ([SIGKILL, SIGSTOP] as $signo) { var_dump(pcntl_signal($signo, function () {})); var_dump(pcntl_get_last_error() === PCNTL_EINVAL); } + +foreach ([SIGKILL, SIGSTOP] as $signo) { + foreach (['not_a_function', 42] as $handler) { + try { + var_dump(pcntl_signal($signo, $handler)); + } catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), "\n"; + } + } +} echo "alive\n"; ?> --EXPECTF-- @@ -32,4 +42,8 @@ bool(false) Warning: pcntl_signal(): Error assigning signal in %s on line %d bool(false) bool(true) +TypeError: pcntl_signal(): Argument #2 ($handler) must be of type callable|int, string given +ValueError: pcntl_signal(): Argument #2 ($handler) must be either SIG_DFL or SIG_IGN when an integer value is given +TypeError: pcntl_signal(): Argument #2 ($handler) must be of type callable|int, string given +ValueError: pcntl_signal(): Argument #2 ($handler) must be either SIG_DFL or SIG_IGN when an integer value is given alive