From: Rich Felker Date: Sat, 29 Jun 2013 06:24:02 +0000 (-0400) Subject: fix missing synchronization in calls from dynamic linker to global ctors X-Git-Tag: v0.9.11~5 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=509b50eda8ea7d4a28f738e4cf8ea98d25959f00;p=oweals%2Fmusl.git fix missing synchronization in calls from dynamic linker to global ctors this change is needed to correctly handle the case where a constructor creates a new thread which calls dlopen. previously, the lock was not held in this case. the reason for the complex logic to avoid locking whenever possible is that, since the mutex is recursive, it will need to inspect the thread pointer to get the current thread's tid, and this requires initializing the thread pointer. we do not want non-multi-threaded programs to attempt to access the thread pointer unnecessarily; doing so could make them crash on ancient kernels that don't support threads but which may otherwise be capable of running the program. --- diff --git a/src/ldso/dynlink.c b/src/ldso/dynlink.c index d1974736..ac4b669f 100644 --- a/src/ldso/dynlink.c +++ b/src/ldso/dynlink.c @@ -692,6 +692,10 @@ static void do_init_fini(struct dso *p) } if (dyn[0] & (1<base + dyn[DT_INIT]))(); + if (!need_locking && libc.threads_minus_1) { + need_locking = 1; + pthread_mutex_lock(&init_fini_lock); + } } if (need_locking) pthread_mutex_unlock(&init_fini_lock); }