Browse code

tls: add preloaded library to force process-shared locks

This is a hacky workaround for OpenSSL >= 1.1 which uses internal
locking and doesn't support locks in memory shared across multiple
processes.

Richard Fuchs authored on 11/04/2019 18:29:48
Showing 3 changed files
... ...
@@ -33,6 +33,8 @@ LIBS+= $(TLS_EXTRA_LIBS)
33 33
 # dcm: tls.cfg installed via local 'install-cfg' to update paths
34 34
 #MOD_INSTALL_CFGS=tls.cfg
35 35
 
36
+MOD_INSTALL_UTILS=utils/openssl_mutex_shared
37
+
36 38
 include ../../Makefile.modules
37 39
 
38 40
 install-tls-cert: $(cfg_prefix)/$(cfg_dir)
39 41
new file mode 100644
... ...
@@ -0,0 +1,26 @@
1
+COREPATH=../../../../../src
2
+include $(COREPATH)/Makefile.defs
3
+include $(COREPATH)/Makefile.targets
4
+
5
+
6
+.PHONY: all
7
+all: openssl_mutex_shared.so
8
+
9
+.PHONY: install-if-newer
10
+install-if-newer: install
11
+
12
+.PHONY: install
13
+install: install-modules
14
+
15
+.PHONY: install-modules
16
+install-modules: openssl_mutex_shared.so
17
+	mkdir -p $(modules_prefix)/$(lib_dir)/openssl_mutex_shared
18
+	$(INSTALL_TOUCH) $(modules_prefix)/$(lib_dir)/openssl_mutex_shared/openssl_mutex_shared.so
19
+	$(INSTALL_BIN) openssl_mutex_shared.so $(modules_prefix)/$(lib_dir)/openssl_mutex_shared
20
+
21
+openssl_mutex_shared.so: openssl_mutex_shared.c
22
+	$(CC) -g -D_GNU_SOURCE -std=c99 -fvisibility=hidden -pthread -o $@ -O3 -Wall -shared -fPIC -ldl $<
23
+
24
+.PHONY: clean
25
+clean:
26
+	rm -f openssl_mutex_shared.so
0 27
new file mode 100644
... ...
@@ -0,0 +1,46 @@
1
+#include <pthread.h>
2
+#include <dlfcn.h>
3
+
4
+#define SYMBOL_EXPORT __attribute__((visibility("default")))
5
+
6
+int SYMBOL_EXPORT pthread_mutex_init (pthread_mutex_t *__mutex, const pthread_mutexattr_t *__mutexattr)
7
+{
8
+	static int (*real_pthread_mutex_init)(pthread_mutex_t *__mutex, const pthread_mutexattr_t *__mutexattr);
9
+	if (!real_pthread_mutex_init)
10
+		real_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
11
+
12
+	if (__mutexattr) {
13
+		pthread_mutexattr_t attr = *__mutexattr;
14
+		pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
15
+		return real_pthread_mutex_init(__mutex, &attr);
16
+	}
17
+
18
+	pthread_mutexattr_t attr;
19
+	pthread_mutexattr_init(&attr);
20
+	pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
21
+	int ret = real_pthread_mutex_init(__mutex, &attr);
22
+	pthread_mutexattr_destroy(&attr);
23
+	return ret;
24
+}
25
+
26
+int SYMBOL_EXPORT pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
27
+				const pthread_rwlockattr_t *__restrict __attr)
28
+{
29
+	static int (*real_pthread_rwlock_init)(pthread_rwlock_t *__restrict __rwlock,
30
+				const pthread_rwlockattr_t *__restrict __attr);
31
+	if (!real_pthread_rwlock_init)
32
+		real_pthread_rwlock_init = dlsym(RTLD_NEXT, "pthread_rwlock_init");
33
+
34
+	if (__attr) {
35
+		pthread_rwlockattr_t attr = *__attr;
36
+		pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
37
+		return real_pthread_rwlock_init(__rwlock, &attr);
38
+	}
39
+
40
+	pthread_rwlockattr_t attr;
41
+	pthread_rwlockattr_init(&attr);
42
+	pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
43
+	int ret = real_pthread_rwlock_init(__rwlock, &attr);
44
+	pthread_rwlockattr_destroy(&attr);
45
+	return ret;
46
+}