0b26f7
commit 40bade26d5bcbda3d21fb598c5063d9df62de966
0b26f7
Author: Florian Weimer <fweimer@redhat.com>
0b26f7
Date:   Fri Oct 1 18:16:41 2021 +0200
0b26f7
0b26f7
    nptl: pthread_kill must send signals to a specific thread [BZ #28407]
0b26f7
    
0b26f7
    The choice between the kill vs tgkill system calls is not just about
0b26f7
    the TID reuse race, but also about whether the signal is sent to the
0b26f7
    whole process (and any thread in it) or to a specific thread.
0b26f7
    
0b26f7
    This was caught by the openposix test suite:
0b26f7
    
0b26f7
      LTP: openposix test suite - FAIL: SIGUSR1 is member of new thread pendingset.
0b26f7
      <https://gitlab.com/cki-project/kernel-tests/-/issues/764>
0b26f7
    
0b26f7
    Fixes commit 526c3cf11ee9367344b6b15d669e4c3cb461a2be ("nptl: Fix race
0b26f7
    between pthread_kill and thread exit (bug 12889)").
0b26f7
    
0b26f7
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
0b26f7
    Tested-by: Carlos O'Donell <carlos@redhat.com>
0b26f7
    (cherry picked from commit eae81d70574e923ce3c59078b8df857ae192efa6)
0b26f7
0b26f7
diff --git a/nptl/pthread_kill.c b/nptl/pthread_kill.c
0b26f7
index a44dc8f2d9baa925..35bf1f973eaeda90 100644
0b26f7
--- a/nptl/pthread_kill.c
0b26f7
+++ b/nptl/pthread_kill.c
0b26f7
@@ -40,7 +40,7 @@ __pthread_kill_implementation (pthread_t threadid, int signo, int no_tid)
0b26f7
          below.  POSIX only guarantees delivery of a single signal,
0b26f7
          which may not be the right one.)  */
0b26f7
       pid_t tid = INTERNAL_SYSCALL_CALL (gettid);
0b26f7
-      int ret = INTERNAL_SYSCALL_CALL (kill, tid, signo);
0b26f7
+      int ret = INTERNAL_SYSCALL_CALL (tgkill, __getpid (), tid, signo);
0b26f7
       return INTERNAL_SYSCALL_ERROR_P (ret) ? INTERNAL_SYSCALL_ERRNO (ret) : 0;
0b26f7
     }
0b26f7
 
0b26f7
@@ -59,8 +59,6 @@ __pthread_kill_implementation (pthread_t threadid, int signo, int no_tid)
0b26f7
     ret = no_tid;
0b26f7
   else
0b26f7
     {
0b26f7
-      /* Using tgkill is a safety measure.  pd->exit_lock ensures that
0b26f7
-	 the target thread cannot exit.  */
0b26f7
       ret = INTERNAL_SYSCALL_CALL (tgkill, __getpid (), pd->tid, signo);
0b26f7
       ret = INTERNAL_SYSCALL_ERROR_P (ret) ? INTERNAL_SYSCALL_ERRNO (ret) : 0;
0b26f7
     }
0b26f7
diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile
0b26f7
index d4bd2d4e3ee6a496..0af9c59b425aefb1 100644
0b26f7
--- a/sysdeps/pthread/Makefile
0b26f7
+++ b/sysdeps/pthread/Makefile
0b26f7
@@ -121,6 +121,7 @@ tests += tst-cnd-basic tst-mtx-trylock tst-cnd-broadcast \
0b26f7
 	 tst-pthread-setuid-loop \
0b26f7
 	 tst-pthread_cancel-exited \
0b26f7
 	 tst-pthread_cancel-select-loop \
0b26f7
+	 tst-pthread-raise-blocked-self \
0b26f7
 	 tst-pthread_kill-exited \
0b26f7
 	 tst-pthread_kill-exiting \
0b26f7
 	 # tests
0b26f7
diff --git a/sysdeps/pthread/tst-pthread-raise-blocked-self.c b/sysdeps/pthread/tst-pthread-raise-blocked-self.c
0b26f7
new file mode 100644
0b26f7
index 0000000000000000..128e1a6071c0b15f
0b26f7
--- /dev/null
0b26f7
+++ b/sysdeps/pthread/tst-pthread-raise-blocked-self.c
0b26f7
@@ -0,0 +1,92 @@
0b26f7
+/* Test that raise sends signal to current thread even if blocked.
0b26f7
+   Copyright (C) 2021 Free Software Foundation, Inc.
0b26f7
+   This file is part of the GNU C Library.
0b26f7
+
0b26f7
+   The GNU C Library is free software; you can redistribute it and/or
0b26f7
+   modify it under the terms of the GNU Lesser General Public
0b26f7
+   License as published by the Free Software Foundation; either
0b26f7
+   version 2.1 of the License, or (at your option) any later version.
0b26f7
+
0b26f7
+   The GNU C Library is distributed in the hope that it will be useful,
0b26f7
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
0b26f7
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0b26f7
+   Lesser General Public License for more details.
0b26f7
+
0b26f7
+   You should have received a copy of the GNU Lesser General Public
0b26f7
+   License along with the GNU C Library; if not, see
0b26f7
+   <https://www.gnu.org/licenses/>.  */
0b26f7
+
0b26f7
+#include <signal.h>
0b26f7
+#include <support/check.h>
0b26f7
+#include <support/xsignal.h>
0b26f7
+#include <support/xthread.h>
0b26f7
+#include <pthread.h>
0b26f7
+#include <unistd.h>
0b26f7
+
0b26f7
+/* Used to create a dummy thread ID distinct from all other thread
0b26f7
+   IDs.  */
0b26f7
+static void *
0b26f7
+noop (void *ignored)
0b26f7
+{
0b26f7
+  return NULL;
0b26f7
+}
0b26f7
+
0b26f7
+static volatile pthread_t signal_thread;
0b26f7
+
0b26f7
+static void
0b26f7
+signal_handler (int signo)
0b26f7
+{
0b26f7
+  signal_thread = pthread_self ();
0b26f7
+}
0b26f7
+
0b26f7
+/* Used to ensure that waiting_thread has launched and can accept
0b26f7
+   signals.  */
0b26f7
+static pthread_barrier_t barrier;
0b26f7
+
0b26f7
+static void *
0b26f7
+waiting_thread (void *ignored)
0b26f7
+{
0b26f7
+  xpthread_barrier_wait (&barrier);
0b26f7
+  pause ();
0b26f7
+  return NULL;
0b26f7
+}
0b26f7
+
0b26f7
+static int
0b26f7
+do_test (void)
0b26f7
+{
0b26f7
+  xsignal (SIGUSR1, signal_handler);
0b26f7
+  xpthread_barrier_init (&barrier, NULL, 2);
0b26f7
+
0b26f7
+  /* Distinct thread ID value to */
0b26f7
+  pthread_t dummy = xpthread_create (NULL, noop, NULL);
0b26f7
+  signal_thread = dummy;
0b26f7
+
0b26f7
+  pthread_t helper = xpthread_create (NULL, waiting_thread, NULL);
0b26f7
+
0b26f7
+  /* Make sure that the thread is running.  */
0b26f7
+  xpthread_barrier_wait (&barrier);
0b26f7
+
0b26f7
+  /* Block signals on this thread.  */
0b26f7
+  sigset_t set;
0b26f7
+  sigfillset (&set);
0b26f7
+  xpthread_sigmask (SIG_BLOCK, &set, NULL);
0b26f7
+
0b26f7
+  /* Send the signal to this thread.  It must not be delivered.  */
0b26f7
+  raise (SIGUSR1);
0b26f7
+  TEST_VERIFY (signal_thread == dummy);
0b26f7
+
0b26f7
+  /* Wait a bit to give a chance for signal delivery (increases
0b26f7
+     chances of failure with bug 28407).  */
0b26f7
+  usleep (50 * 1000);
0b26f7
+
0b26f7
+  /* Unblocking should cause synchronous delivery of the signal.  */
0b26f7
+  xpthread_sigmask (SIG_UNBLOCK, &set, NULL);
0b26f7
+  TEST_VERIFY (signal_thread == pthread_self ());
0b26f7
+
0b26f7
+  xpthread_cancel (helper);
0b26f7
+  xpthread_join (helper);
0b26f7
+  xpthread_join (dummy);
0b26f7
+  return 0;
0b26f7
+}
0b26f7
+
0b26f7
+#include <support/test-driver.c>