[PATCH 6/6] cet: Support multi-threaded dlopen of legacy object in permissive mode

Arjun Shankar arjun@redhat.com
Mon Nov 23 12:47:00 GMT 2026


"permissive" SHSTK execution mode allows disabling SHSTK in order to
load a legacy shared library.  41560a9312ce0ec7203480eef8f865076bff9edb
correctly disabled dlopen of a legacy shared library when in permissive
SHSTK mode when running multi-threaded.  Other threads might crash when
calling functions in the legacy shared library.

This commit adds support for dlopen of legacy objects even when
multi-threaded by using a setxid broadcast to disable SHSTK on other
threads as well.

This is achieved by:

A. Making __nptl_setxid available to the loader as __rtld_setxid, then
   using it during dlopen (dl_cet_check_dlopen) to broadcast SHSTK
   disable to other threads.

B. Adding a post-processing hook in the setxid signal handler that is a
   no-op everywhere except on x86_64, where it turns off the SHSTK
   feature bit in the TCB.

A new test is added, exercising dlopen during multi-threaded execution
with concurrent dlopen and thread creation/exit while in permissive
SHSTK mode.
---
 elf/Makefile                              |   1 +
 elf/dl-setxid.c                           |  19 ++++
 elf/rtld.c                                |   1 +
 nptl/Versions                             |   1 +
 nptl/nptl_setxid.c                        |   5 +-
 sysdeps/generic/ldsodefs.h                |  13 +++
 sysdeps/generic/setxid-post.h             |  22 ++++
 sysdeps/nptl/dl-setxid.c                  |  40 ++++++++
 sysdeps/nptl/dl-setxid.h                  |  27 +++++
 sysdeps/nptl/pthreadP.h                   |   3 +-
 sysdeps/x86_64/Makefile                   |   5 +
 sysdeps/x86_64/dl-cet.c                   |  24 ++++-
 sysdeps/x86_64/nptl/setxid-post.h         |  29 ++++++
 sysdeps/x86_64/tst-cet-legacy-mt-dlopen.c | 116 ++++++++++++++++++++++
 14 files changed, 299 insertions(+), 7 deletions(-)
 create mode 100644 elf/dl-setxid.c
 create mode 100644 sysdeps/generic/setxid-post.h
 create mode 100644 sysdeps/nptl/dl-setxid.c
 create mode 100644 sysdeps/nptl/dl-setxid.h
 create mode 100644 sysdeps/x86_64/nptl/setxid-post.h
 create mode 100644 sysdeps/x86_64/tst-cet-legacy-mt-dlopen.c

diff --git a/elf/Makefile b/elf/Makefile
index d267317a81..01e58b99b3 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -137,6 +137,7 @@ rtld-routines = \
   dl-minimal \
   dl-mutex \
   dl-profile \
+  dl-setxid \
   dl-sysdep \
   dl-usage \
   rtld \
diff --git a/elf/dl-setxid.c b/elf/dl-setxid.c
new file mode 100644
index 0000000000..182d4954ac
--- /dev/null
+++ b/elf/dl-setxid.c
@@ -0,0 +1,19 @@
+/* setxid implementation for the dynamic loader.  Generic version.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* Initialization happens in __rtld_setxid_init.  */
diff --git a/elf/rtld.c b/elf/rtld.c
index 65c10edff2..f860fbfc1d 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -2348,6 +2348,7 @@ dl_main (const ElfW(Phdr) *phdr,
       rtld_timer_accum (&relocate_time, start);
 
       __rtld_mutex_init ();
+      __rtld_setxid_init ();
       __rtld_malloc_init_real (main_map);
 
       /* Update copy-relocated _r_debug if necessary.  */
diff --git a/nptl/Versions b/nptl/Versions
index b813b675b9..d5e6684b9c 100644
--- a/nptl/Versions
+++ b/nptl/Versions
@@ -392,6 +392,7 @@ libc {
     __nptl_last_event;
     __nptl_nthreads;
     __nptl_rtld_global;
+    __nptl_setxid;
     __nptl_threads_events;
     __nptl_version;
     __pthread_get_minstack;
diff --git a/nptl/nptl_setxid.c b/nptl/nptl_setxid.c
index 843eab230f..00a8cdfd3b 100644
--- a/nptl/nptl_setxid.c
+++ b/nptl/nptl_setxid.c
@@ -21,6 +21,7 @@
 #include <lowlevellock.h>
 #include <pthreadP.h>
 #include <unistd.h>
+#include <setxid-post.h>
 
 /* Check for consistency across set*id system call results.  The abort
    should not happen as long as all privileges changes happen through
@@ -73,6 +74,8 @@ __nptl_setxid_sighandler (int sig, siginfo_t *si, void *ctx)
     error = INTERNAL_SYSCALL_ERRNO (result);
   setxid_error (xidcmd, error);
 
+  setxid_sighandler_post (xidcmd, error);
+
   /* Reset the SETXID flag.  */
   struct pthread *self = THREAD_SELF;
   int flags, newval;
@@ -228,7 +231,6 @@ setxid_signal_backoff (long int *ns)
 }
 
 int
-attribute_hidden
 __nptl_setxid (struct xid_command *cmdp)
 {
   bool signalled;
@@ -354,3 +356,4 @@ __nptl_setxid (struct xid_command *cmdp)
   lll_unlock (GL (dl_stack_cache_lock), LLL_PRIVATE);
   return result;
 }
+libc_hidden_def (__nptl_setxid)
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
index 305ca6e0df..7f8cafbd38 100644
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
@@ -1454,10 +1454,17 @@ _dl_audit_objclose (struct link_map *l)
 extern int (*___rtld_mutex_lock) (pthread_mutex_t *) attribute_hidden;
 extern int (*___rtld_mutex_unlock) (pthread_mutex_t *lock) attribute_hidden;
 
+/* setxid implementation for use within the dynamic loader.
+   Use void * instead of struct xid_command * to avoid needing its
+   definition.  */
+extern int (*__rtld_setxid) (void *) attribute_hidden;
+
 /* Called after libc has been loaded, but before RELRO is activated.
    Used to initialize the function pointers to the actual
    implementations.  */
 void __rtld_mutex_init (void) attribute_hidden;
+void __rtld_setxid_init (void) attribute_hidden;
+
 #else /* !__PHREAD_NPTL */
 static inline void
 __rtld_mutex_init (void)
@@ -1465,6 +1472,12 @@ __rtld_mutex_init (void)
   /* The initialization happens later (!__PHREAD_NPTL) or is not
      needed at all (!SHARED).  */
 }
+static inline void
+__rtld_setxid_init (void)
+{
+  /* The initialization happens later (!__PHREAD_NPTL) or is not
+     needed at all (!SHARED).  */
+}
 #endif /* !__PHREAD_NPTL */
 
 /* Implementation of GL (dl_libc_freeres).  */
diff --git a/sysdeps/generic/setxid-post.h b/sysdeps/generic/setxid-post.h
new file mode 100644
index 0000000000..f49d957563
--- /dev/null
+++ b/sysdeps/generic/setxid-post.h
@@ -0,0 +1,22 @@
+/* Post-processing for setxid signal handler.  Generic version.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+static inline void __attribute__ ((always_inline))
+setxid_sighandler_post (struct xid_command *cmdp, int error)
+{
+}
diff --git a/sysdeps/nptl/dl-setxid.c b/sysdeps/nptl/dl-setxid.c
new file mode 100644
index 0000000000..fc5d00c190
--- /dev/null
+++ b/sysdeps/nptl/dl-setxid.c
@@ -0,0 +1,40 @@
+/* setxid implementation for the dynamic loader.  NPTL version.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* Use the setxid implementation in libc.  */
+
+#include <assert.h>
+#include <ldsodefs.h>
+
+/* Use void * instead of struct xid_command * to avoid needing its
+   definition.  */
+int (*__rtld_setxid) (void *) attribute_relro;
+
+void
+__rtld_setxid_init (void)
+{
+  struct link_map *libc_map = GL (dl_ns)[LM_ID_BASE].libc_map;
+
+  const ElfW(Sym) *sym
+    = _dl_lookup_direct (libc_map, "__nptl_setxid",
+                         0x36b105f1, /* dl_new_hash output.  */
+                         "GLIBC_PRIVATE",
+                         0x0963cf85); /* _dl_elf_hash output.  */
+  assert (sym != NULL);
+  __rtld_setxid = DL_SYMBOL_ADDRESS (libc_map, sym);
+}
diff --git a/sysdeps/nptl/dl-setxid.h b/sysdeps/nptl/dl-setxid.h
new file mode 100644
index 0000000000..96160fdf04
--- /dev/null
+++ b/sysdeps/nptl/dl-setxid.h
@@ -0,0 +1,27 @@
+/* setxid definition for the dynamic loader.  NPTL version.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _DL_SETXID_H
+#define _DL_SETXID_H 1
+
+#if !IS_IN (rtld)
+# include <pthreadP.h>
+# define __rtld_setxid(CMDP) __nptl_setxid (CMDP)
+#endif
+
+#endif
diff --git a/sysdeps/nptl/pthreadP.h b/sysdeps/nptl/pthreadP.h
index 982705aee5..34dbf70d37 100644
--- a/sysdeps/nptl/pthreadP.h
+++ b/sysdeps/nptl/pthreadP.h
@@ -655,7 +655,8 @@ libc_hidden_proto (__nptl_deallocate_tsd)
 
 void __nptl_setxid_sighandler (int sig, siginfo_t *si, void *ctx);
 libc_hidden_proto (__nptl_setxid_sighandler)
-extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden;
+extern int __nptl_setxid (struct xid_command *cmdp);
+libc_hidden_proto (__nptl_setxid)
 
 extern void __wait_lookup_done (void) attribute_hidden;
 
diff --git a/sysdeps/x86_64/Makefile b/sysdeps/x86_64/Makefile
index ddbf05effa..0c5509164e 100644
--- a/sysdeps/x86_64/Makefile
+++ b/sysdeps/x86_64/Makefile
@@ -347,6 +347,7 @@ tests += \
   tst-cet-legacy-4d \
   tst-cet-legacy-5b \
   tst-cet-legacy-6b \
+  tst-cet-legacy-mt-dlopen \
 # tests
 modules-names += \
   tst-cet-legacy-mod-1 \
@@ -441,6 +442,10 @@ $(objpfx)tst-cet-legacy-4c.out: $(objpfx)tst-cet-legacy-mod-4.so
 tst-cet-legacy-4c-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK:glibc.cpu.x86_shstk=off
 $(objpfx)tst-cet-legacy-4d.out: $(objpfx)tst-cet-legacy-mod-4.so
 tst-cet-legacy-4d-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK
+CFLAGS-tst-cet-legacy-mt-dlopen.c += -fcf-protection
+$(objpfx)tst-cet-legacy-mt-dlopen.out: $(objpfx)tst-cet-legacy-mod-4.so
+$(objpfx)tst-cet-legacy-mt-dlopen: $(shared-thread-library)
+tst-cet-legacy-mt-dlopen-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK:glibc.cpu.x86_shstk=permissive
 $(objpfx)tst-cet-legacy-5b.out: $(objpfx)tst-cet-legacy-mod-5a.so \
 				$(objpfx)tst-cet-legacy-mod-5b.so
 tst-cet-legacy-5b-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=-IBT,-SHSTK
diff --git a/sysdeps/x86_64/dl-cet.c b/sysdeps/x86_64/dl-cet.c
index 0cf58b4468..accdd9f925 100644
--- a/sysdeps/x86_64/dl-cet.c
+++ b/sysdeps/x86_64/dl-cet.c
@@ -21,6 +21,7 @@
 #include <ldsodefs.h>
 #include <dl-cet.h>
 #include <sys/single_threaded.h>
+#include <dl-setxid.h>
 
 /* GNU_PROPERTY_X86_FEATURE_1_IBT and GNU_PROPERTY_X86_FEATURE_1_SHSTK
    are defined in <elf.h>, which are only available for C sources.
@@ -224,10 +225,7 @@ dl_cet_check_dlopen (struct link_map *m, struct dl_cet_info *info)
       && (info->feature_1_legacy
 	  & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0)
     {
-      /* Don't disable SHSTK if not single threaded since SHSTK may be
-         still enabled in other threads.  */
-      if (info->enable_shstk_type != cet_permissive
-	  || !SINGLE_THREAD_P)
+      if (info->enable_shstk_type != cet_permissive)
 	{
 	  legacy_obj = info->feature_1_legacy_shstk;
 	  msg = N_("rebuild shared object with SHSTK support enabled");
@@ -243,7 +241,23 @@ dl_cet_check_dlopen (struct link_map *m, struct dl_cet_info *info)
 
   if (disable_feature_1 != 0)
     {
-      int res = dl_cet_disable_cet (disable_feature_1);
+      int res;
+      if (!SINGLE_THREAD_P
+          && (disable_feature_1 & GNU_PROPERTY_X86_FEATURE_1_SHSTK))
+        {
+          /* When not single threaded, we use setxid to disable SHSTK in
+             all threads.  */
+          struct xid_command cmd;
+          cmd.syscall_no = __NR_arch_prctl;
+          cmd.id[0] = ARCH_SHSTK_DISABLE;
+          cmd.id[1] = ARCH_SHSTK_SHSTK;
+          cmd.id[2] = 0;
+          res = __rtld_setxid (&cmd);
+          res = (res == -1) ? -errno : 0;
+        }
+      else
+        res = dl_cet_disable_cet (disable_feature_1);
+
       if (res)
         {
 	  if ((disable_feature_1
diff --git a/sysdeps/x86_64/nptl/setxid-post.h b/sysdeps/x86_64/nptl/setxid-post.h
new file mode 100644
index 0000000000..a1fa1bd204
--- /dev/null
+++ b/sysdeps/x86_64/nptl/setxid-post.h
@@ -0,0 +1,29 @@
+/* Post-processing for setxid signal handler.  nptl/x86_64 version.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+static inline void __attribute__ ((always_inline))
+setxid_sighandler_post (struct xid_command *cmdp, int error)
+{
+  if (error == 0
+      && cmdp->syscall_no == __NR_arch_prctl
+      && cmdp->id[0] == ARCH_SHSTK_DISABLE
+      && cmdp->id[1] == ARCH_SHSTK_SHSTK)
+      THREAD_SETMEM (THREAD_SELF, header.feature_1,
+                     (THREAD_GETMEM (THREAD_SELF, header.feature_1)
+                      & ~GNU_PROPERTY_X86_FEATURE_1_SHSTK));
+}
diff --git a/sysdeps/x86_64/tst-cet-legacy-mt-dlopen.c b/sysdeps/x86_64/tst-cet-legacy-mt-dlopen.c
new file mode 100644
index 0000000000..05e6ed23f3
--- /dev/null
+++ b/sysdeps/x86_64/tst-cet-legacy-mt-dlopen.c
@@ -0,0 +1,116 @@
+/* Check multi-threaded SHSTK disable when dlopening a legacy object.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <dlfcn.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/platform/x86.h>
+
+#include <support/check.h>
+#include <support/xthread.h>
+
+static const char modname[] = "tst-cet-legacy-mod-4.so";
+static pthread_barrier_t barrier;
+
+static void *
+nop (void *arg)
+{
+  return NULL;
+}
+
+static void *
+worker (void *arg)
+{
+  int mode = (int) (intptr_t) arg;
+
+  TEST_VERIFY (CPU_FEATURE_ACTIVE (SHSTK));
+  /* This causes main to wait for our above test before it calls dlopen.  */
+  xpthread_barrier_wait (&barrier);
+
+  if (mode == 0)
+    {
+      /* Create/join threads while the dlopen transition may be in flight.  */
+      pthread_t threads[100];
+      for (int i = 0; i < 100; i++)
+	threads[i] = xpthread_create (NULL, nop, NULL);
+      for (int i = 0; i < 100; i++)
+	xpthread_join (threads[i]);
+    }
+  else
+    /* Now we race to a dlopen with main.  */
+    for (int i = 0; i < 100; i++)
+      {
+	void *h = dlopen (modname, RTLD_LAZY);
+	/* Whichever dlopen (ours or main's) goes through first turns off
+	   SHSTK for the other threads.  */
+	TEST_VERIFY_EXIT (h != NULL);
+	TEST_VERIFY (!CPU_FEATURE_ACTIVE (IBT)
+		     && !CPU_FEATURE_ACTIVE (SHSTK));
+	dlclose (h);
+      }
+
+  /* Wait for dlopens to finish before we test.  */
+  xpthread_barrier_wait (&barrier);
+  TEST_VERIFY (!CPU_FEATURE_ACTIVE (SHSTK));
+  return NULL;
+}
+
+static int
+do_test (void)
+{
+  int (*fp) (void);
+  void *h;
+  pthread_t workers[2];
+
+  if (!CPU_FEATURE_ACTIVE (SHSTK))
+    /* We are probably running on a CPU/system that does not support CET.  */
+    FAIL_UNSUPPORTED ("SHSTK not supported");
+
+  /* Set up workers and wait for them to be ready.  */
+  xpthread_barrier_init (&barrier, NULL, 3);
+  workers[0] = xpthread_create (NULL, worker, (void *) (intptr_t) 0);
+  workers[1] = xpthread_create (NULL, worker, (void *) (intptr_t) 1);
+  xpthread_barrier_wait (&barrier);
+
+  /* Race with thread 1's dlopen.  */
+  h = dlopen (modname, RTLD_LAZY);
+
+  TEST_VERIFY_EXIT (h != NULL);
+  TEST_VERIFY (!CPU_FEATURE_ACTIVE (IBT) && !CPU_FEATURE_ACTIVE (SHSTK));
+
+  xpthread_barrier_wait (&barrier);
+
+  xpthread_join (workers[0]);
+  xpthread_join (workers[1]);
+  xpthread_barrier_destroy (&barrier);
+
+  fp = dlsym (h, "test");
+  if (fp == NULL)
+    FAIL_EXIT1 ("cannot get symbol 'test': %s\n", dlerror ());
+
+  if (fp () != 0)
+    FAIL_EXIT1 ("test () != 0");
+
+  dlclose (h);
+
+  return 0;
+}
+
+#include <support/test-driver.c>
-- 
2.56.0



More information about the Libc-alpha mailing list