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