Skip to content

Fix use of uninitialized memory in pcntl SIGCHLD handling #11536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
21 changes: 9 additions & 12 deletions ext/pcntl/pcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1338,15 +1338,13 @@ 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;

psig = PCNTL_G(spares);
if (!psig) {
struct php_pcntl_pending_signal *psig_first = PCNTL_G(spares);
if (!psig_first) {
/* oops, too many signals for us to track, so we'll forget about this one */
return;
}

struct php_pcntl_pending_signal *psig_first = psig;
struct php_pcntl_pending_signal *psig = NULL;

/* Standard signals may be merged into a single one.
* POSIX specifies that SIGCHLD has the si_pid field (https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html),
Expand All @@ -1365,28 +1363,27 @@ static void pcntl_signal_handler(int signo)
pid = waitpid(WAIT_ANY, &status, WNOHANG | WUNTRACED);
} while (pid <= 0 && errno == EINTR);
if (pid <= 0) {
if (UNEXPECTED(psig == psig_first)) {
/* Don't handle multiple, revert back to the single signal handling. */
goto single_signal;
if (UNEXPECTED(!psig)) {
/* The child might've been consumed by another thread and will be handled there. */
return;
}
break;
}

psig = psig ? psig->next : psig_first;
psig->signo = signo;

#ifdef HAVE_STRUCT_SIGINFO_T
psig->siginfo = *siginfo;
psig->siginfo.si_pid = pid;
#endif

if (EXPECTED(psig->next)) {
psig = psig->next;
} else {
if (UNEXPECTED(!psig->next)) {
break;
}
}
} else {
single_signal:;
psig = psig_first;
psig->signo = signo;

#ifdef HAVE_STRUCT_SIGINFO_T
Expand Down
9 changes: 8 additions & 1 deletion ext/pcntl/tests/gh11498.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ $processes = [];

pcntl_async_signals(true);
pcntl_signal(SIGCHLD, function($sig, $info) use (&$processes) {
echo "SIGCHLD\n";
unset($processes[$info['pid']]);
}, false);

foreach (range(0, 5) as $i) {
for ($i = 0; $i <= 5; $i++) {
$process = proc_open('echo $$ > /dev/null', [], $pipes);
$pid = proc_get_status($process)['pid'];
$processes[$pid] = $process;
Expand All @@ -32,4 +33,10 @@ while (!empty($processes) && $iters > 0) {
var_dump(empty($processes));
?>
--EXPECT--
SIGCHLD
SIGCHLD
SIGCHLD
SIGCHLD
SIGCHLD
SIGCHLD
bool(true)