94084c
commit e3e589829d16af9f7e73c7b70f74f3c5d5003e45
94084c
Author: Florian Weimer <fweimer@redhat.com>
94084c
Date:   Thu Dec 9 09:49:32 2021 +0100
94084c
94084c
    nptl: Add glibc.pthread.rseq tunable to control rseq registration
94084c
    
94084c
    This tunable allows applications to register the rseq area instead
94084c
    of glibc.
94084c
    
94084c
    Reviewed-by: Szabolcs Nagy <szabolcs.nagy@arm.com>
94084c
    Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
94084c
94084c
diff --git a/manual/tunables.texi b/manual/tunables.texi
94084c
index 658547c6137bf177..1f5c410288eeecec 100644
94084c
--- a/manual/tunables.texi
94084c
+++ b/manual/tunables.texi
94084c
@@ -413,6 +413,16 @@ The value is measured in bytes.  The default is @samp{41943040}
94084c
 (fourty mibibytes).
94084c
 @end deftp
94084c
 
94084c
+@deftp Tunable glibc.pthread.rseq
94084c
+The @code{glibc.pthread.rseq} tunable can be set to @samp{0}, to disable
94084c
+restartable sequences support in @theglibc{}.  This enables applications
94084c
+to perform direct restartable sequence registration with the kernel.
94084c
+The default is @samp{1}, which means that @theglibc{} performs
94084c
+registration on behalf of the application.
94084c
+
94084c
+Restartable sequences are a Linux-specific extension.
94084c
+@end deftp
94084c
+
94084c
 @node Hardware Capability Tunables
94084c
 @section Hardware Capability Tunables
94084c
 @cindex hardware capability tunables
94084c
diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c
94084c
index d2b40924dafad316..f405fa356c2955ce 100644
94084c
--- a/nptl/pthread_create.c
94084c
+++ b/nptl/pthread_create.c
94084c
@@ -369,7 +369,10 @@ start_thread (void *arg)
94084c
   __ctype_init ();
94084c
 
94084c
   /* Register rseq TLS to the kernel.  */
94084c
-  rseq_register_current_thread (pd);
94084c
+  {
94084c
+    bool do_rseq = THREAD_GETMEM (pd, flags) & ATTR_FLAG_DO_RSEQ;
94084c
+    rseq_register_current_thread (pd, do_rseq);
94084c
+  }
94084c
 
94084c
 #ifndef __ASSUME_SET_ROBUST_LIST
94084c
   if (__nptl_set_robust_list_avail)
94084c
@@ -678,6 +681,11 @@ __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr,
94084c
   pd->flags = ((iattr->flags & ~(ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
94084c
 	       | (self->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)));
94084c
 
94084c
+  /* Inherit rseq registration state.  Without seccomp filters, rseq
94084c
+     registration will either always fail or always succeed.  */
94084c
+  if ((int) THREAD_GETMEM_VOLATILE (self, rseq_area.cpu_id) >= 0)
94084c
+    pd->flags |= ATTR_FLAG_DO_RSEQ;
94084c
+
94084c
   /* Initialize the field for the ID of the thread which is waiting
94084c
      for us.  This is a self-reference in case the thread is created
94084c
      detached.  */
94084c
diff --git a/sysdeps/nptl/dl-tls_init_tp.c b/sysdeps/nptl/dl-tls_init_tp.c
94084c
index fedb876fdb2642d2..b39dfbff2c6678d5 100644
94084c
--- a/sysdeps/nptl/dl-tls_init_tp.c
94084c
+++ b/sysdeps/nptl/dl-tls_init_tp.c
94084c
@@ -23,6 +23,9 @@
94084c
 #include <tls.h>
94084c
 #include <rseq-internal.h>
94084c
 
94084c
+#define TUNABLE_NAMESPACE pthread
94084c
+#include <dl-tunables.h>
94084c
+
94084c
 #ifndef __ASSUME_SET_ROBUST_LIST
94084c
 bool __nptl_set_robust_list_avail;
94084c
 rtld_hidden_data_def (__nptl_set_robust_list_avail)
94084c
@@ -92,7 +95,13 @@ __tls_init_tp (void)
94084c
       }
94084c
   }
94084c
 
94084c
-  rseq_register_current_thread (pd);
94084c
+  {
94084c
+    bool do_rseq = true;
94084c
+#if HAVE_TUNABLES
94084c
+    do_rseq = TUNABLE_GET (rseq, int, NULL);
94084c
+#endif
94084c
+    rseq_register_current_thread (pd, do_rseq);
94084c
+  }
94084c
 
94084c
   /* Set initial thread's stack block from 0 up to __libc_stack_end.
94084c
      It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
94084c
diff --git a/sysdeps/nptl/dl-tunables.list b/sysdeps/nptl/dl-tunables.list
94084c
index ac5d053298725468..d24f4be0d08ba407 100644
94084c
--- a/sysdeps/nptl/dl-tunables.list
94084c
+++ b/sysdeps/nptl/dl-tunables.list
94084c
@@ -27,5 +27,11 @@ glibc {
94084c
       type: SIZE_T
94084c
       default: 41943040
94084c
     }
94084c
+    rseq {
94084c
+      type: INT_32
94084c
+      minval: 0
94084c
+      maxval: 1
94084c
+      default: 1
94084c
+    }
94084c
   }
94084c
 }
94084c
diff --git a/sysdeps/nptl/internaltypes.h b/sysdeps/nptl/internaltypes.h
94084c
index 50a2ad19ae7210ae..8205c6d15a918952 100644
94084c
--- a/sysdeps/nptl/internaltypes.h
94084c
+++ b/sysdeps/nptl/internaltypes.h
94084c
@@ -49,6 +49,7 @@ struct pthread_attr
94084c
 #define ATTR_FLAG_OLDATTR		0x0010
94084c
 #define ATTR_FLAG_SCHED_SET		0x0020
94084c
 #define ATTR_FLAG_POLICY_SET		0x0040
94084c
+#define ATTR_FLAG_DO_RSEQ		0x0080
94084c
 
94084c
 /* Used to allocate a pthread_attr_t object which is also accessed
94084c
    internally.  */
94084c
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
94084c
index f84ccd6bbb3b16ad..d30d21898b402d1e 100644
94084c
--- a/sysdeps/unix/sysv/linux/Makefile
94084c
+++ b/sysdeps/unix/sysv/linux/Makefile
94084c
@@ -135,6 +135,12 @@ tests-internal += \
94084c
   tst-sigcontext-get_pc \
94084c
   # tests-internal
94084c
 
94084c
+ifneq (no,$(have-tunables))
94084c
+tests-internal += \
94084c
+  tst-rseq-disable \
94084c
+  # tests-internal $(have-tunables)
94084c
+endif
94084c
+
94084c
 tests-time64 += \
94084c
   tst-adjtimex-time64 \
94084c
   tst-clock_adjtime-time64 \
94084c
@@ -226,6 +232,8 @@ $(objpfx)tst-mman-consts.out: ../sysdeps/unix/sysv/linux/tst-mman-consts.py
94084c
 	  < /dev/null > $@ 2>&1; $(evaluate-test)
94084c
 $(objpfx)tst-mman-consts.out: $(sysdeps-linux-python-deps)
94084c
 
94084c
+tst-rseq-disable-ENV = GLIBC_TUNABLES=glibc.pthread.rseq=0
94084c
+
94084c
 endif # $(subdir) == misc
94084c
 
94084c
 ifeq ($(subdir),time)
94084c
diff --git a/sysdeps/unix/sysv/linux/rseq-internal.h b/sysdeps/unix/sysv/linux/rseq-internal.h
94084c
index 909f5478251d3d13..15bc7ffd6eda632d 100644
94084c
--- a/sysdeps/unix/sysv/linux/rseq-internal.h
94084c
+++ b/sysdeps/unix/sysv/linux/rseq-internal.h
94084c
@@ -21,22 +21,27 @@
94084c
 #include <sysdep.h>
94084c
 #include <errno.h>
94084c
 #include <kernel-features.h>
94084c
+#include <stdbool.h>
94084c
 #include <stdio.h>
94084c
 #include <sys/rseq.h>
94084c
 
94084c
 #ifdef RSEQ_SIG
94084c
 static inline void
94084c
-rseq_register_current_thread (struct pthread *self)
94084c
+rseq_register_current_thread (struct pthread *self, bool do_rseq)
94084c
 {
94084c
-  int ret = INTERNAL_SYSCALL_CALL (rseq,
94084c
-                                   &self->rseq_area, sizeof (self->rseq_area),
94084c
-                                   0, RSEQ_SIG);
94084c
-  if (INTERNAL_SYSCALL_ERROR_P (ret))
94084c
-    THREAD_SETMEM (self, rseq_area.cpu_id, RSEQ_CPU_ID_REGISTRATION_FAILED);
94084c
+  if (do_rseq)
94084c
+    {
94084c
+      int ret = INTERNAL_SYSCALL_CALL (rseq, &self->rseq_area,
94084c
+                                       sizeof (self->rseq_area),
94084c
+                                       0, RSEQ_SIG);
94084c
+      if (!INTERNAL_SYSCALL_ERROR_P (ret))
94084c
+        return;
94084c
+    }
94084c
+  THREAD_SETMEM (self, rseq_area.cpu_id, RSEQ_CPU_ID_REGISTRATION_FAILED);
94084c
 }
94084c
 #else /* RSEQ_SIG */
94084c
 static inline void
94084c
-rseq_register_current_thread (struct pthread *self)
94084c
+rseq_register_current_thread (struct pthread *self, bool do_rseq)
94084c
 {
94084c
   THREAD_SETMEM (self, rseq_area.cpu_id, RSEQ_CPU_ID_REGISTRATION_FAILED);
94084c
 }
94084c
diff --git a/sysdeps/unix/sysv/linux/tst-rseq-disable.c b/sysdeps/unix/sysv/linux/tst-rseq-disable.c
94084c
new file mode 100644
94084c
index 0000000000000000..000e351872fc2f76
94084c
--- /dev/null
94084c
+++ b/sysdeps/unix/sysv/linux/tst-rseq-disable.c
94084c
@@ -0,0 +1,89 @@
94084c
+/* Test disabling of rseq registration via tunable.
94084c
+   Copyright (C) 2021 Free Software Foundation, Inc.
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
+#include <errno.h>
94084c
+#include <stdio.h>
94084c
+#include <support/check.h>
94084c
+#include <support/namespace.h>
94084c
+#include <support/xthread.h>
94084c
+#include <sysdep.h>
94084c
+#include <unistd.h>
94084c
+
94084c
+#ifdef RSEQ_SIG
94084c
+
94084c
+/* Check that rseq can be registered and has not been taken by glibc.  */
94084c
+static void
94084c
+check_rseq_disabled (void)
94084c
+{
94084c
+  struct pthread *pd = THREAD_SELF;
94084c
+  TEST_COMPARE ((int) pd->rseq_area.cpu_id, RSEQ_CPU_ID_REGISTRATION_FAILED);
94084c
+
94084c
+  int ret = syscall (__NR_rseq, &pd->rseq_area, sizeof (pd->rseq_area),
94084c
+                     0, RSEQ_SIG);
94084c
+  if (ret == 0)
94084c
+    {
94084c
+      ret = syscall (__NR_rseq, &pd->rseq_area, sizeof (pd->rseq_area),
94084c
+                     RSEQ_FLAG_UNREGISTER, RSEQ_SIG);
94084c
+      TEST_COMPARE (ret, 0);
94084c
+      pd->rseq_area.cpu_id = RSEQ_CPU_ID_REGISTRATION_FAILED;
94084c
+    }
94084c
+  else
94084c
+    {
94084c
+      TEST_VERIFY (errno != -EINVAL);
94084c
+      TEST_VERIFY (errno != -EBUSY);
94084c
+    }
94084c
+}
94084c
+
94084c
+static void *
94084c
+thread_func (void *ignored)
94084c
+{
94084c
+  check_rseq_disabled ();
94084c
+  return NULL;
94084c
+}
94084c
+
94084c
+static void
94084c
+proc_func (void *ignored)
94084c
+{
94084c
+  check_rseq_disabled ();
94084c
+}
94084c
+
94084c
+static int
94084c
+do_test (void)
94084c
+{
94084c
+  puts ("info: checking main thread");
94084c
+  check_rseq_disabled ();
94084c
+
94084c
+  puts ("info: checking main thread (2)");
94084c
+  check_rseq_disabled ();
94084c
+
94084c
+  puts ("info: checking new thread");
94084c
+  xpthread_join (xpthread_create (NULL, thread_func, NULL));
94084c
+
94084c
+  puts ("info: checking subprocess");
94084c
+  support_isolate_in_subprocess (proc_func, NULL);
94084c
+
94084c
+  return 0;
94084c
+}
94084c
+#else /* !RSEQ_SIG */
94084c
+static int
94084c
+do_test (void)
94084c
+{
94084c
+  FAIL_UNSUPPORTED ("glibc does not define RSEQ_SIG, skipping test");
94084c
+}
94084c
+#endif
94084c
+
94084c
+#include <support/test-driver.c>