]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
nptl: Avoid fork handler lock for async-signal-safe fork [BZ #24161]
authorFlorian Weimer <fweimer@redhat.com>
Fri, 8 Feb 2019 11:55:21 +0000 (12:55 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Fri, 8 Feb 2019 11:55:21 +0000 (12:55 +0100)
Commit 27761a1042daf01987e7d79636d0c41511c6df3c ("Refactor atfork
handlers") introduced a lock, atfork_lock, around fork handler list
accesses.  It turns out that this lock occasionally results in
self-deadlocks in malloc/tst-mallocfork2:

(gdb) bt
#0  __lll_lock_wait_private ()
    at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:63
#1  0x00007f160c6f927a in __run_fork_handlers (who=(unknown: 209394016),
    who@entry=atfork_run_prepare) at register-atfork.c:116
#2  0x00007f160c6b7897 in __libc_fork () at ../sysdeps/nptl/fork.c:58
#3  0x00000000004027d6 in sigusr1_handler (signo=<optimized out>)
    at tst-mallocfork2.c:80
#4  sigusr1_handler (signo=<optimized out>) at tst-mallocfork2.c:64
#5  <signal handler called>
#6  0x00007f160c6f92e4 in __run_fork_handlers (who=who@entry=atfork_run_parent)
    at register-atfork.c:136
#7  0x00007f160c6b79a2 in __libc_fork () at ../sysdeps/nptl/fork.c:152
#8  0x0000000000402567 in do_test () at tst-mallocfork2.c:156
#9  0x0000000000402dd2 in support_test_main (argc=1, argv=0x7ffc81ef1ab0,
    config=config@entry=0x7ffc81ef1970) at support_test_main.c:350
#10 0x0000000000402362 in main (argc=<optimized out>, argv=<optimized out>)
    at ../support/test-driver.c:168

If no locking happens in the single-threaded case (where fork is
expected to be async-signal-safe), this deadlock is avoided.
(pthread_atfork is not required to be async-signal-safe, so a fork
call from a signal handler interrupting pthread_atfork is not
a problem.)

(cherry picked from commit 669ff911e2571f74a2668493e326ac9a505776bd)

ChangeLog
NEWS
nptl/register-atfork.c
sysdeps/nptl/fork.c
sysdeps/nptl/fork.h

index 82802f33675bdde44488c9f550fa554e43df6b01..f3477bbff9e044f53140d301e8b0993c761239d8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2019-02-08  Florian Weimer  <fweimer@redhat.com>
+
+       [BZ #24161]
+       * sysdeps/nptl/fork.h (__run_fork_handlers): Add multiple_threads
+       argument.
+       * nptl/register-atfork.c (__run_fork_handlers): Only perform
+       locking if the new do_locking argument is true.
+       * sysdeps/nptl/fork.c (__libc_fork): Pass multiple_threads to
+       __run_fork_handlers.
+
 2019-02-07  Stefan Liebler  <stli@linux.ibm.com>
 
        [BZ #24180]
diff --git a/NEWS b/NEWS
index 5aa15afb9dc13efafd95f399a0b0b92466d55d89..51a173ce7bcca103045f3080dc09589ef564de46 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -37,6 +37,7 @@ The following bugs are resolved with this release:
   [24034] tst-cancel21-static fails with SIGBUS on pre-ARMv7 when using GCC 8
   [24097] Can't use 64-bit register for size_t in assembly codes for x32 (CVE-2019-6488)
   [24155] x32 memcmp can treat positive length as 0 (if sign bit in RDX is set) (CVE-2019-7309)
+  [24161] __run_fork_handlers self-deadlocks in malloc/tst-mallocfork2
 
 Security related changes:
 
index 5ff1c1be8cd0432c45f9726625922e44c1c1b173..9edb7d4bbb49fbed7d37793c827d12cb01cb0a43 100644 (file)
@@ -107,13 +107,14 @@ __unregister_atfork (void *dso_handle)
 }
 
 void
-__run_fork_handlers (enum __run_fork_handler_type who)
+__run_fork_handlers (enum __run_fork_handler_type who, _Bool do_locking)
 {
   struct fork_handler *runp;
 
   if (who == atfork_run_prepare)
     {
-      lll_lock (atfork_lock, LLL_PRIVATE);
+      if (do_locking)
+       lll_lock (atfork_lock, LLL_PRIVATE);
       size_t sl = fork_handler_list_size (&fork_handlers);
       for (size_t i = sl; i > 0; i--)
        {
@@ -133,7 +134,8 @@ __run_fork_handlers (enum __run_fork_handler_type who)
          else if (who == atfork_run_parent && runp->parent_handler)
            runp->parent_handler ();
        }
-      lll_unlock (atfork_lock, LLL_PRIVATE);
+      if (do_locking)
+       lll_unlock (atfork_lock, LLL_PRIVATE);
     }
 }
 
index ec56a827eba483b6a6e3226a8c4ba7e1199d10a5..1a9429b579cd346ebf9ec7c08d5a5bb1ab69cc05 100644 (file)
@@ -55,7 +55,7 @@ __libc_fork (void)
      but our current fork implementation is not.  */
   bool multiple_threads = THREAD_GETMEM (THREAD_SELF, header.multiple_threads);
 
-  __run_fork_handlers (atfork_run_prepare);
+  __run_fork_handlers (atfork_run_prepare, multiple_threads);
 
   /* If we are not running multiple threads, we do not have to
      preserve lock state.  If fork runs from a signal handler, only
@@ -134,7 +134,7 @@ __libc_fork (void)
       __rtld_lock_initialize (GL(dl_load_lock));
 
       /* Run the handlers registered for the child.  */
-      __run_fork_handlers (atfork_run_child);
+      __run_fork_handlers (atfork_run_child, multiple_threads);
     }
   else
     {
@@ -149,7 +149,7 @@ __libc_fork (void)
        }
 
       /* Run the handlers registered for the parent.  */
-      __run_fork_handlers (atfork_run_parent);
+      __run_fork_handlers (atfork_run_parent, multiple_threads);
     }
 
   return pid;
index 6eab61c12154dc9ca173a859528c9e45b84b8afd..bef2b7a8a6af86353da82a169ff9e3ae9d33f89b 100644 (file)
@@ -52,9 +52,11 @@ enum __run_fork_handler_type
    - atfork_run_child: run all the CHILD_HANDLER and unlocks the internal
                       lock.
    - atfork_run_parent: run all the PARENT_HANDLER and unlocks the internal
-                       lock.  */
-extern void __run_fork_handlers (enum __run_fork_handler_type who)
-  attribute_hidden;
+                       lock.
+
+   Perform locking only if DO_LOCKING.  */
+extern void __run_fork_handlers (enum __run_fork_handler_type who,
+                                _Bool do_locking) attribute_hidden;
 
 /* C library side function to register new fork handlers.  */
 extern int __register_atfork (void (*__prepare) (void),