Module Name:    src
Committed By:   joerg
Date:           Thu Apr 16 14:39:58 UTC 2020

Modified Files:
        src/lib/libc/gen: pthread_atfork.c
        src/libexec/ld.elf_so: rtld.c rtld.h symbols.map

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/lib/libc/gen/pthread_atfork.c
cvs rdiff -u -r1.203 -r1.204 src/libexec/ld.elf_so/rtld.c
cvs rdiff -u -r1.138 -r1.139 src/libexec/ld.elf_so/rtld.h
cvs rdiff -u -r1.2 -r1.3 src/libexec/ld.elf_so/symbols.map

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/gen/pthread_atfork.c
diff -u src/lib/libc/gen/pthread_atfork.c:1.12 src/lib/libc/gen/pthread_atfork.c:1.13
--- src/lib/libc/gen/pthread_atfork.c:1.12	Sat Feb  1 18:14:16 2020
+++ src/lib/libc/gen/pthread_atfork.c	Thu Apr 16 14:39:58 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_atfork.c,v 1.12 2020/02/01 18:14:16 kamil Exp $	*/
+/*	$NetBSD: pthread_atfork.c,v 1.13 2020/04/16 14:39:58 joerg 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.12 2020/02/01 18:14:16 kamil Exp $");
+__RCSID("$NetBSD: pthread_atfork.c,v 1.13 2020/04/16 14:39:58 joerg Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
@@ -48,6 +48,13 @@ __weak_alias(fork, _fork)
 #endif /* __weak_alias */
 
 pid_t	__fork(void);	/* XXX */
+pid_t	__atomic_fork(void) __weak; /* XXX */
+
+pid_t
+__atomic_fork(void)
+{
+	return __fork();
+}
 
 struct atfork_callback {
 	SIMPLEQ_ENTRY(atfork_callback) next;
@@ -157,7 +164,7 @@ fork(void)
 	SIMPLEQ_FOREACH(iter, &prepareq, next)
 		(*iter->fn)();
 
-	ret = __fork();
+	ret = __atomic_fork();
 
 	if (ret != 0) {
 		/*

Index: src/libexec/ld.elf_so/rtld.c
diff -u src/libexec/ld.elf_so/rtld.c:1.203 src/libexec/ld.elf_so/rtld.c:1.204
--- src/libexec/ld.elf_so/rtld.c:1.203	Wed Mar  4 01:21:17 2020
+++ src/libexec/ld.elf_so/rtld.c	Thu Apr 16 14:39:58 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: rtld.c,v 1.203 2020/03/04 01:21:17 thorpej Exp $	 */
+/*	$NetBSD: rtld.c,v 1.204 2020/04/16 14:39:58 joerg Exp $	 */
 
 /*
  * Copyright 1996 John D. Polstra.
@@ -40,7 +40,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: rtld.c,v 1.203 2020/03/04 01:21:17 thorpej Exp $");
+__RCSID("$NetBSD: rtld.c,v 1.204 2020/04/16 14:39:58 joerg Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -1532,6 +1532,21 @@ __dl_cxa_refcount(void *addr, ssize_t de
 	_rtld_exclusive_exit(&mask);
 }
 
+pid_t __fork(void);
+
+__dso_public pid_t
+__atomic_fork(void)
+{
+	sigset_t mask;
+	pid_t result;
+
+	_rtld_exclusive_enter(&mask);
+	result = __fork();
+	_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()

Index: src/libexec/ld.elf_so/rtld.h
diff -u src/libexec/ld.elf_so/rtld.h:1.138 src/libexec/ld.elf_so/rtld.h:1.139
--- src/libexec/ld.elf_so/rtld.h:1.138	Sat Feb 29 04:24:33 2020
+++ src/libexec/ld.elf_so/rtld.h	Thu Apr 16 14:39:58 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: rtld.h,v 1.138 2020/02/29 04:24:33 kamil Exp $	 */
+/*	$NetBSD: rtld.h,v 1.139 2020/04/16 14:39:58 joerg Exp $	 */
 
 /*
  * Copyright 1996 John D. Polstra.
@@ -370,6 +370,8 @@ __dso_public int dl_iterate_phdr(int (*)
 __dso_public void *_dlauxinfo(void) __pure;
 __dso_public void __dl_cxa_refcount(void *addr, ssize_t delta);
 
+__dso_public pid_t __atomic_fork(void);
+
 #if defined(__ARM_EABI__) && !defined(__ARM_DWARF_EH__)
 /*
  * This is used by libgcc to find the start and length of the exception table

Index: src/libexec/ld.elf_so/symbols.map
diff -u src/libexec/ld.elf_so/symbols.map:1.2 src/libexec/ld.elf_so/symbols.map:1.3
--- src/libexec/ld.elf_so/symbols.map:1.2	Tue Jul 11 15:21:35 2017
+++ src/libexec/ld.elf_so/symbols.map	Thu Apr 16 14:39:58 2020
@@ -23,5 +23,6 @@
     ___tls_get_addr;
     __gnu_Unwind_Find_exidx;
     __dl_cxa_refcount;
+    __atomic_fork;
   local: *;
 };

Reply via email to