2be09a
commit bfe68fe3c475fe34bed4e017d6e63196c305c934
2be09a
Author: Florian Weimer <fweimer@redhat.com>
2be09a
Date:   Wed Nov 24 08:59:54 2021 +0100
2be09a
2be09a
    nptl: Do not set signal mask on second setjmp return [BZ #28607]
2be09a
    
2be09a
    __libc_signal_restore_set was in the wrong place: It also ran
2be09a
    when setjmp returned the second time (after pthread_exit or
2be09a
    pthread_cancel).  This is observable with blocked pending
2be09a
    signals during thread exit.
2be09a
    
2be09a
    Fixes commit b3cae39dcbfa2432b3f3aa28854d8ac57f0de1b8
2be09a
    ("nptl: Start new threads with all signals blocked [BZ #25098]").
2be09a
    
2be09a
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2be09a
    (cherry picked from commit e186fc5a31e46f2cbf5ea1a75223b4412907f3d8)
2be09a
2be09a
diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c
2be09a
index bc213f0bc4e948bd..3db0c9fdf40ae2bf 100644
2be09a
--- a/nptl/pthread_create.c
2be09a
+++ b/nptl/pthread_create.c
2be09a
@@ -407,8 +407,6 @@ start_thread (void *arg)
2be09a
   unwind_buf.priv.data.prev = NULL;
2be09a
   unwind_buf.priv.data.cleanup = NULL;
2be09a
 
2be09a
-  __libc_signal_restore_set (&pd->sigmask);
2be09a
-
2be09a
   /* Allow setxid from now onwards.  */
2be09a
   if (__glibc_unlikely (atomic_exchange_acq (&pd->setxid_futex, 0) == -2))
2be09a
     futex_wake (&pd->setxid_futex, 1, FUTEX_PRIVATE);
2be09a
@@ -418,6 +416,8 @@ start_thread (void *arg)
2be09a
       /* Store the new cleanup handler info.  */
2be09a
       THREAD_SETMEM (pd, cleanup_jmp_buf, &unwind_buf);
2be09a
 
2be09a
+      __libc_signal_restore_set (&pd->sigmask);
2be09a
+
2be09a
       LIBC_PROBE (pthread_start, 3, (pthread_t) pd, pd->start_routine, pd->arg);
2be09a
 
2be09a
       /* Run the code the user provided.  */
2be09a
diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile
2be09a
index df8943f4860a39d8..c65710169697ad95 100644
2be09a
--- a/sysdeps/pthread/Makefile
2be09a
+++ b/sysdeps/pthread/Makefile
2be09a
@@ -118,6 +118,7 @@ tests += tst-cnd-basic tst-mtx-trylock tst-cnd-broadcast \
2be09a
 	 tst-unload \
2be09a
 	 tst-unwind-thread \
2be09a
 	 tst-pt-vfork1 tst-pt-vfork2 tst-vfork1x tst-vfork2x \
2be09a
+	 tst-pthread-exit-signal \
2be09a
 	 tst-pthread-setuid-loop \
2be09a
 	 tst-pthread_cancel-exited \
2be09a
 	 tst-pthread_cancel-select-loop \
2be09a
diff --git a/sysdeps/pthread/tst-pthread-exit-signal.c b/sysdeps/pthread/tst-pthread-exit-signal.c
2be09a
new file mode 100644
2be09a
index 0000000000000000..b4526fe663671068
2be09a
--- /dev/null
2be09a
+++ b/sysdeps/pthread/tst-pthread-exit-signal.c
2be09a
@@ -0,0 +1,45 @@
2be09a
+/* Test that pending signals are not delivered on thread exit (bug 28607).
2be09a
+   Copyright (C) 2021 Free Software Foundation, Inc.
2be09a
+   This file is part of the GNU C Library.
2be09a
+
2be09a
+   The GNU C Library is free software; you can redistribute it and/or
2be09a
+   modify it under the terms of the GNU Lesser General Public
2be09a
+   License as published by the Free Software Foundation; either
2be09a
+   version 2.1 of the License, or (at your option) any later version.
2be09a
+
2be09a
+   The GNU C Library is distributed in the hope that it will be useful,
2be09a
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
2be09a
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2be09a
+   Lesser General Public License for more details.
2be09a
+
2be09a
+   You should have received a copy of the GNU Lesser General Public
2be09a
+   License along with the GNU C Library; if not, see
2be09a
+   <https://www.gnu.org/licenses/>.  */
2be09a
+
2be09a
+/* Due to bug 28607, pthread_kill (or pthread_cancel) restored the
2be09a
+   signal mask during during thread exit, triggering the delivery of a
2be09a
+   blocked pending signal (SIGUSR1 in this test).  */
2be09a
+
2be09a
+#include <support/xthread.h>
2be09a
+#include <support/xsignal.h>
2be09a
+
2be09a
+static void *
2be09a
+threadfunc (void *closure)
2be09a
+{
2be09a
+  sigset_t sigmask;
2be09a
+  sigfillset (&sigmask);
2be09a
+  xpthread_sigmask (SIG_SETMASK, &sigmask, NULL);
2be09a
+  xpthread_kill (pthread_self (), SIGUSR1);
2be09a
+  pthread_exit (NULL);
2be09a
+  return NULL;
2be09a
+}
2be09a
+
2be09a
+static int
2be09a
+do_test (void)
2be09a
+{
2be09a
+  pthread_t thr = xpthread_create (NULL, threadfunc, NULL);
2be09a
+  xpthread_join (thr);
2be09a
+  return 0;
2be09a
+}
2be09a
+
2be09a
+#include <support/test-driver.c>