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