029-fork_safe.patch revision 6617
2505N/A#
2505N/A# This file adds the code to setup internal mutexes and callback function.
2505N/A# PSARC/2014/077
4364N/A# PSARC/2015/043
2505N/A# This change was implemented in-house. The issue was brought up to
2505N/A# the upstream engineers, but there was no commitment.
2505N/A#
2505N/A--- openssl-1.0.1f/crypto/cryptlib.c.~1~ Fri Feb 7 10:41:36 2014
2505N/A+++ openssl-1.0.1f/crypto/cryptlib.c Thu Feb 6 16:03:58 2014
2505N/A@@ -116,6 +116,7 @@
2505N/A
2505N/A #include "cryptlib.h"
2505N/A #include <openssl/safestack.h>
2505N/A+#include <pthread.h>
2505N/A
2505N/A #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
4002N/A static double SSLeay_MSVC5_hack = 0.0; /* and for VC1.5 */
4002N/A@@ -184,6 +185,8 @@
4002N/A */
4002N/A static STACK_OF(CRYPTO_dynlock) *dyn_locks = NULL;
2505N/A
2505N/A+static pthread_mutex_t *solaris_openssl_locks;
4002N/A+
4002N/A static void (MS_FAR *locking_callback) (int mode, int type,
4002N/A const char *file, int line) = 0;
4002N/A static int (MS_FAR *add_lock_callback) (int *pointer, int amount,
4364N/A@@ -373,7 +376,10 @@
4364N/A void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*func)
4364N/A (const char *file, int line))
4364N/A {
4364N/A- dynlock_create_callback = func;
4364N/A+ /*
4364N/A+ * we now setup our own dynamic locking callback, and disallow
4364N/A+ * setting of another locking callback.
4364N/A+ */
4364N/A }
4364N/A
4364N/A void CRYPTO_set_dynlock_lock_callback(void (*func) (int mode,
4364N/A@@ -382,7 +388,10 @@
4364N/A const char *file,
4364N/A int line))
4364N/A {
4364N/A- dynlock_lock_callback = func;
4364N/A+ /*
4364N/A+ * we now setup our own dynamic locking callback, and disallow
4364N/A+ * setting of another locking callback.
4364N/A+ */
4364N/A }
4364N/A
4364N/A void CRYPTO_set_dynlock_destroy_callback(void (*func)
4364N/A@@ -389,7 +398,10 @@
4364N/A (struct CRYPTO_dynlock_value *l,
4364N/A const char *file, int line))
4364N/A {
4364N/A- dynlock_destroy_callback = func;
4364N/A+ /*
4364N/A+ * we now setup our own dynamic locking callback, and disallow
4364N/A+ * setting of another locking callback.
4364N/A+ */
4364N/A }
4364N/A
4364N/A void (*CRYPTO_get_locking_callback(void)) (int mode, int type,
4820N/A@@ -402,6 +414,128@@
4002N/A return (add_lock_callback);
4002N/A }
2505N/A
2505N/A+/*
2505N/A+ * This is the locking callback function which all applications will be
2505N/A+ * using when CRYPTO_lock() is called.
2505N/A+ */
2505N/A+static void solaris_locking_callback(int mode, int type, const char *file,
2505N/A+ int line)
4364N/A+{
4364N/A+ if (mode & CRYPTO_LOCK) {
4364N/A+ pthread_mutex_lock(&solaris_openssl_locks[type]);
4364N/A+ } else {
4364N/A+ pthread_mutex_unlock(&solaris_openssl_locks[type]);
4364N/A+ }
4364N/A+}
4364N/A+
4364N/A+/*
4364N/A+ * Implement Solaris's own dynamic locking routines.
4364N/A+ */
4364N/A+static struct CRYPTO_dynlock_value *
4364N/A+solaris_dynlock_create(const char *file, int line)
4364N/A+{
4364N/A+ int ret;
4364N/A+ pthread_mutex_t *dynlock;
4364N/A+
4364N/A+ dynlock = OPENSSL_malloc(sizeof(pthread_mutex_t));
4364N/A+ if (dynlock == NULL) {
4364N/A+ return (NULL);
4364N/A+ }
4364N/A+
4364N/A+ ret = pthread_mutex_init(dynlock, NULL);
6617N/A+ OPENSSL_assert(ret == 0);
4364N/A+
4364N/A+ return ((struct CRYPTO_dynlock_value *)dynlock);
4364N/A+}
4364N/A+
4364N/A+static void
4423N/A+solaris_dynlock_lock(int mode, struct CRYPTO_dynlock_value *dynlock,
4364N/A+ const char *file, int line)
4364N/A+{
4364N/A+ int ret;
4364N/A+
4364N/A+ if (mode & CRYPTO_LOCK) {
4364N/A+ ret = pthread_mutex_lock((pthread_mutex_t *)dynlock);
4364N/A+ } else {
4364N/A+ ret = pthread_mutex_unlock((pthread_mutex_t *)dynlock);
4364N/A+ }
4364N/A+
4364N/A+ OPENSSL_assert(ret == 0);
4364N/A+}
4364N/A+
4364N/A+static void
4364N/A+solaris_dynlock_destroy(struct CRYPTO_dynlock_value *dynlock,
4364N/A+ const char *file, int line)
4364N/A+{
4364N/A+ int ret;
4364N/A+ ret = pthread_mutex_destroy((pthread_mutex_t *)dynlock);
6617N/A+ OPENSSL_assert(ret == 0);
4364N/A+}
2505N/A+
2505N/A+
2505N/A+/*
2505N/A+ * This function is called when a child process is forked to setup its own
2505N/A+ * global locking callback function ptr and mutexes.
2505N/A+ */
2505N/A+static void solaris_fork_child(void)
4364N/A+{
4364N/A+ /*
4364N/A+ * clear locking_callback to indicate that locks should
4364N/A+ * be reinitialized.
4364N/A+ */
4364N/A+ locking_callback = NULL;
4364N/A+ solaris_locking_setup();
4364N/A+}
2505N/A+
2505N/A+/*
2505N/A+ * This function allocates and initializes the global mutex array, and
2505N/A+ * sets the locking callback.
2505N/A+ */
2505N/A+void solaris_locking_setup()
4364N/A+{
4364N/A+ int i;
4364N/A+ int num_locks;
2505N/A+
4364N/A+ /* setup the dynlock callback if not already */
4364N/A+ if (dynlock_create_callback == NULL) {
4364N/A+ dynlock_create_callback = solaris_dynlock_create;
4364N/A+ }
4364N/A+ if (dynlock_lock_callback == NULL) {
4364N/A+ dynlock_lock_callback = solaris_dynlock_lock;
4364N/A+ }
4364N/A+ if (dynlock_destroy_callback == NULL) {
4364N/A+ dynlock_destroy_callback = solaris_dynlock_destroy;
4364N/A+ }
2505N/A+
4364N/A+ /* locking callback is already setup. Nothing to do */
4364N/A+ if (locking_callback != NULL) {
4364N/A+ return;
4364N/A+ }
2505N/A+
4364N/A+ /*
4364N/A+ * Set atfork handler so that child can setup its own mutexes and
4364N/A+ * locking callbacks when it is forked
4364N/A+ */
4364N/A+ (void) pthread_atfork(NULL, NULL, solaris_fork_child);
2505N/A+
4364N/A+ /* allocate locks needed by OpenSSL */
4364N/A+ num_locks = CRYPTO_num_locks();
4364N/A+ solaris_openssl_locks =
4364N/A+ OPENSSL_malloc(sizeof (pthread_mutex_t) * num_locks);
4364N/A+ if (solaris_openssl_locks == NULL) {
4364N/A+ fprintf(stderr,
4364N/A+ "solaris_locking_setup: memory allocation failure.\n");
4364N/A+ abort();
4364N/A+ }
4364N/A+
4364N/A+ /* initialize openssl mutexes */
4364N/A+ for (i = 0; i < num_locks; i++) {
4364N/A+ pthread_mutex_init(&solaris_openssl_locks[i], NULL);
4364N/A+ }
4364N/A+ locking_callback = solaris_locking_callback;
2505N/A+
4002N/A+}
2505N/A+
4002N/A void CRYPTO_set_locking_callback(void (*func) (int mode, int type,
4002N/A const char *file, int line))
4002N/A {
4002N/A@@ -410,7 +486,11 @@
4002N/A * started.
4002N/A */
4002N/A OPENSSL_init();
4002N/A- locking_callback = func;
2505N/A+
4364N/A+ /*
4364N/A+ * we now setup our own locking callback and mutexes, and disallow
4364N/A+ * setting of another locking callback.
4364N/A+ */
4002N/A }
2505N/A
4002N/A void CRYPTO_set_add_lock_callback(int (*func) (int *num, int mount, int type,
4364N/A@@ -471,9 +551,10 @@
4364N/A
4364N/A int CRYPTO_THREADID_set_callback(void (*func) (CRYPTO_THREADID *))
4364N/A {
4364N/A- if (threadid_callback)
4364N/A- return 0;
4364N/A- threadid_callback = func;
4364N/A+ /*
4364N/A+ * Use the backup method (the address of 'errno') to identify the
4364N/A+ * thread and disallow setting the threadid callback.
4364N/A+ */
4364N/A return 1;
4364N/A }
4364N/A
4820N/A@@ -529,7 +669,10 @@
4364N/A
4364N/A void CRYPTO_set_id_callback(unsigned long (*func) (void))
4364N/A {
4364N/A- id_callback = func;
4364N/A+ /*
4364N/A+ * Use the backup method to identify the thread/process.
4364N/A+ * Setting the id callback is disallowed.
4364N/A+ */
4364N/A }
4364N/A
4364N/A unsigned long CRYPTO_thread_id(void)
2505N/A--- openssl-1.0.1f/crypto/cryptlib.h.~1~ Fri Feb 7 10:41:42 2014
2505N/A+++ openssl-1.0.1f/crypto/cryptlib.h Thu Feb 6 16:04:16 2014
2505N/A@@ -104,6 +104,8 @@
2505N/A void *OPENSSL_stderr(void);
2505N/A extern int OPENSSL_NONPIC_relocated;
2505N/A
2505N/A+void solaris_locking_setup();
2505N/A+
2505N/A #ifdef __cplusplus
2505N/A }
2505N/A #endif
2505N/A--- openssl-1.0.1f/crypto/sparccpuid.S.~1~ Fri Feb 7 10:41:37 2014
2505N/A+++ openssl-1.0.1f/crypto/sparccpuid.S Thu Feb 6 16:04:14 2014
4820N/A@@ -525,5 +525,7 @@
4820N/A .size _sparcv9_vis1_instrument_bus2,.-_sparcv9_vis1_instrument_bus2
2505N/A
2505N/A .section ".init",#alloc,#execinstr
2505N/A+ call solaris_locking_setup
2505N/A+ nop
2505N/A call OPENSSL_cpuid_setup
2505N/A nop
2505N/A--- openssl-1.0.1f/crypto/x86_64cpuid.pl.~1~ Wed Feb 12 13:20:09 2014
2505N/A+++ openssl-1.0.1f/crypto/x86_64cpuid.pl Wed Feb 12 13:21:20 2014
2505N/A@@ -20,7 +20,10 @@
2505N/A print<<___;
2505N/A .extern OPENSSL_cpuid_setup
2505N/A .hidden OPENSSL_cpuid_setup
2505N/A+.extern solaris_locking_setup
2505N/A+.hidden solaris_locking_setup
2505N/A .section .init
2505N/A+ call solaris_locking_setup
2505N/A call OPENSSL_cpuid_setup
2505N/A
2505N/A .hidden OPENSSL_ia32cap_P
2505N/A--- openssl-1.0.1f/crypto/x86cpuid.pl.~1~ Wed Feb 12 13:38:03 2014
2505N/A+++ openssl-1.0.1f/crypto/x86cpuid.pl Wed Feb 12 13:38:31 2014
4820N/A@@ -379,8 +379,10 @@
2505N/A &ret ();
4820N/A &function_end_B("OPENSSL_ia32_rdseed");
2505N/A
2505N/A+&initseg("solaris_locking_setup");
2505N/A &initseg("OPENSSL_cpuid_setup");
2505N/A
4820N/A+&hidden("solaris_locking_setup");
4820N/A &hidden("OPENSSL_cpuid_setup");
4820N/A &hidden("OPENSSL_ia32cap_P");
4820N/A