Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-9]: src/libexec/ld.elf_so Pull up following revision(s) (requeste...



details:   https://anonhg.NetBSD.org/src/rev/b8c2d263f850
branches:  netbsd-9
changeset: 932686:b8c2d263f850
user:      martin <martin%NetBSD.org@localhost>
date:      Wed May 13 18:08:38 2020 +0000

description:
Pull up following revision(s) (requested by chs in ticket #907):

        libexec/ld.elf_so/rtld.c: revision 1.205
        libexec/ld.elf_so/rtld.h: revision 1.140
        libexec/ld.elf_so/symbols.map: revision 1.3
        libexec/ld.elf_so/symbols.map: revision 1.4
        lib/libc/gen/pthread_atfork.c: revision 1.13
        lib/libc/gen/pthread_atfork.c: revision 1.14
        libexec/ld.elf_so/rtld.h: revision 1.139
        libexec/ld.elf_so/rtld.c: revision 1.204

Introduce intermediate locking for fork, so that the dynamic linker is
in a consistent state. This most importantly avoids races between dlopen
and friends and fork, potentially resulting in dead locks in the child
when it itself tries to acquire locks.

Rename __atomic_fork to __locked_fork and give it &errno as argument.
rtld and libc use different storage, so the initial version would
incorrectly report the failure reason for fork().

There is still a small race condition inside ld.elf_so as it doesn't use
thread-safe errno internally, but that's a more contained internal
issue.

diffstat:

 lib/libc/gen/pthread_atfork.c |  13 ++++++++++---
 libexec/ld.elf_so/rtld.c      |  21 +++++++++++++++++++--
 libexec/ld.elf_so/rtld.h      |   4 +++-
 libexec/ld.elf_so/symbols.map |   1 +
 4 files changed, 33 insertions(+), 6 deletions(-)

diffs (111 lines):

diff -r 91f1a54a5d6f -r b8c2d263f850 lib/libc/gen/pthread_atfork.c
--- a/lib/libc/gen/pthread_atfork.c     Wed May 13 18:05:14 2020 +0000
+++ b/lib/libc/gen/pthread_atfork.c     Wed May 13 18:08:38 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_atfork.c,v 1.10 2015/01/20 18:31:25 christos Exp $     */
+/*     $NetBSD: pthread_atfork.c,v 1.10.18.1 2020/05/13 18:08:38 martin Exp $  */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: pthread_atfork.c,v 1.10 2015/01/20 18:31:25 christos Exp $");
+__RCSID("$NetBSD: pthread_atfork.c,v 1.10.18.1 2020/05/13 18:08:38 martin Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
@@ -48,6 +48,13 @@
 #endif /* __weak_alias */
 
 pid_t  __fork(void);   /* XXX */
+pid_t  __locked_fork(int *) __weak; /* XXX */
+
+pid_t
+__locked_fork(int *my_errno)
+{
+       return __fork();
+}
 
 struct atfork_callback {
        SIMPLEQ_ENTRY(atfork_callback) next;
@@ -157,7 +164,7 @@
        SIMPLEQ_FOREACH(iter, &prepareq, next)
                (*iter->fn)();
 
-       ret = __fork();
+       ret = __locked_fork(&errno);
 
        if (ret != 0) {
                /*
diff -r 91f1a54a5d6f -r b8c2d263f850 libexec/ld.elf_so/rtld.c
--- a/libexec/ld.elf_so/rtld.c  Wed May 13 18:05:14 2020 +0000
+++ b/libexec/ld.elf_so/rtld.c  Wed May 13 18:08:38 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rtld.c,v 1.197.2.3 2020/03/08 10:22:29 martin Exp $     */
+/*     $NetBSD: rtld.c,v 1.197.2.4 2020/05/13 18:08:38 martin Exp $     */
 
 /*
  * Copyright 1996 John D. Polstra.
@@ -40,7 +40,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: rtld.c,v 1.197.2.3 2020/03/08 10:22:29 martin Exp $");
+__RCSID("$NetBSD: rtld.c,v 1.197.2.4 2020/05/13 18:08:38 martin Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -1530,6 +1530,23 @@
        _rtld_exclusive_exit(&mask);
 }
 
+pid_t __fork(void);
+
+__dso_public pid_t
+__locked_fork(int *my_errno)
+{
+       sigset_t mask;
+       pid_t result;
+
+       _rtld_exclusive_enter(&mask);
+       result = __fork();
+       if (result == -1)
+               *my_errno = errno;
+       _rtld_exclusive_exit(&mask);
+
+       return result;
+}
+
 /*
  * Error reporting function.  Use it like printf.  If formats the message
  * into a buffer, and sets things up so that the next call to dlerror()
diff -r 91f1a54a5d6f -r b8c2d263f850 libexec/ld.elf_so/rtld.h
--- a/libexec/ld.elf_so/rtld.h  Wed May 13 18:05:14 2020 +0000
+++ b/libexec/ld.elf_so/rtld.h  Wed May 13 18:08:38 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rtld.h,v 1.136 2018/12/30 01:48:37 christos Exp $       */
+/*     $NetBSD: rtld.h,v 1.136.2.1 2020/05/13 18:08:38 martin Exp $     */
 
 /*
  * Copyright 1996 John D. Polstra.
@@ -347,6 +347,8 @@
 __dso_public void *_dlauxinfo(void) __pure;
 __dso_public void __dl_cxa_refcount(void *addr, ssize_t delta);
 
+__dso_public pid_t __locked_fork(int *);
+
 #if defined(__ARM_EABI__) && !defined(__ARM_DWARF_EH__)
 /*
  * This is used by libgcc to find the start and length of the exception table
diff -r 91f1a54a5d6f -r b8c2d263f850 libexec/ld.elf_so/symbols.map
--- a/libexec/ld.elf_so/symbols.map     Wed May 13 18:05:14 2020 +0000
+++ b/libexec/ld.elf_so/symbols.map     Wed May 13 18:08:38 2020 +0000
@@ -23,5 +23,6 @@
     ___tls_get_addr;
     __gnu_Unwind_Find_exidx;
     __dl_cxa_refcount;
+    __locked_fork;
   local: *;
 };



Home | Main Index | Thread Index | Old Index