This is the mail archive of the libc-hacker@sourceware.org mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix setxid race with thread creation


When setxid runs in the time window between allocating a thread stack
and cloning the new thread this thread does not receive the setxid
signal and remains at the wrong xid.

Andreas.

2010-03-02  Andreas Schwab  <schwab@redhat.com>

	* allocatestack.c (get_cached_stack): Initialize setxid_futex to -1.
	(allocate_stack): Likewise.
	(setxid_mark_thread): Wait for setxid_futex != -1.
	* sysdeps/pthread/createthread.c (do_clone): Set setxid_futex
	after cloning.

diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c
index 3c3585f..88f9574 100644
--- a/nptl/allocatestack.c
+++ b/nptl/allocatestack.c
@@ -213,6 +213,9 @@ get_cached_stack (size_t *sizep, void **memp)
       return NULL;
     }
 
+  /* Don't allow setxid until cloned.  */
+  result->setxid_futex = -1;
+
   /* Dequeue the entry.  */
   stack_list_del (&result->list);
 
@@ -418,6 +421,9 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
       /* The process ID is also the same as that of the caller.  */
       pd->pid = THREAD_GETMEM (THREAD_SELF, pid);
 
+      /* Don't allow setxid until cloned.  */
+      pd->setxid_futex = -1;
+
       /* Allocate the DTV for this thread.  */
       if (_dl_allocate_tls (TLS_TPADJ (pd)) == NULL)
 	{
@@ -557,6 +563,9 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
 	  /* The process ID is also the same as that of the caller.  */
 	  pd->pid = THREAD_GETMEM (THREAD_SELF, pid);
 
+	  /* Don't allow setxid until cloned.  */
+	  pd->setxid_futex = -1;
+
 	  /* Allocate the DTV for this thread.  */
 	  if (_dl_allocate_tls (TLS_TPADJ (pd)) == NULL)
 	    {
@@ -969,6 +978,10 @@ setxid_mark_thread (struct xid_command *cmdp, struct pthread *t)
 {
   int ch;
 
+  /* Wait until this thread is cloned.  */
+  while (t->setxid_futex == -1)
+    lll_futex_wait (&t->setxid_futex, -1, LLL_PRIVATE);
+
   /* Don't let the thread exit before the setxid handler runs.  */
   t->setxid_futex = 0;
 
diff --git a/nptl/sysdeps/pthread/createthread.c b/nptl/sysdeps/pthread/createthread.c
index 66fafe8..d176bad 100644
--- a/nptl/sysdeps/pthread/createthread.c
+++ b/nptl/sysdeps/pthread/createthread.c
@@ -52,6 +52,8 @@ do_clone (struct pthread *pd, const struct pthread_attr *attr,
 	  int clone_flags, int (*fct) (void *), STACK_VARIABLES_PARMS,
 	  int stopped)
 {
+  int rc;
+
 #ifdef PREPARE_CREATE
   PREPARE_CREATE;
 #endif
@@ -72,8 +74,14 @@ do_clone (struct pthread *pd, const struct pthread_attr *attr,
      that cares whether the thread count is correct.  */
   atomic_increment (&__nptl_nthreads);
 
-  if (ARCH_CLONE (fct, STACK_VARIABLES_ARGS, clone_flags,
-		  pd, &pd->tid, TLS_VALUE, &pd->tid) == -1)
+  rc = ARCH_CLONE (fct, STACK_VARIABLES_ARGS, clone_flags,
+		   pd, &pd->tid, TLS_VALUE, &pd->tid);
+
+  /* Allow setxid from now onwards.  */
+  pd->setxid_futex = 0;
+  lll_futex_wake (&pd->setxid_futex, 1, LLL_PRIVATE);
+  
+  if (rc == -1)
     {
       atomic_decrement (&__nptl_nthreads); /* Oops, we lied for a second.  */
 

-- 
Andreas Schwab, schwab@redhat.com
GPG Key fingerprint = D4E8 DBE3 3813 BB5D FA84  5EC7 45C6 250E 6F00 984E
"And now for something completely different."


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]