components/openssl/common/patches/029-fork_safe.patch
changeset 6860 fbbec9b86c26
parent 6619 57ad7bed64ee
equal deleted inserted replaced
6859:569bef81e3c4 6860:fbbec9b86c26
     3 #	PSARC/2014/077
     3 #	PSARC/2014/077
     4 #	PSARC/2015/043
     4 #	PSARC/2015/043
     5 # This change was implemented in-house.  The issue was brought up to
     5 # This change was implemented in-house.  The issue was brought up to
     6 # the upstream engineers, but there was no commitment.
     6 # the upstream engineers, but there was no commitment.
     7 #
     7 #
     8 --- openssl-1.0.1f/crypto/cryptlib.c.~1~	Fri Feb  7 10:41:36 2014
     8 --- a/crypto/cryptlib.c	2016-05-03 06:44:42.000000000 -0700
     9 +++ openssl-1.0.1f/crypto/cryptlib.c	Thu Feb  6 16:03:58 2014
     9 +++ b/crypto/cryptlib.c	2016-09-02 08:47:23.640202700 -0700
    10 @@ -116,6 +116,7 @@
    10 @@ -116,6 +116,7 @@
    11  
    11  
    12  #include "cryptlib.h"
    12  #include "cryptlib.h"
    13  #include <openssl/safestack.h>
    13  #include <openssl/safestack.h>
    14 +#include <pthread.h>
    14 +#include <pthread.h>
    32 +    /*
    32 +    /*
    33 +     * we now setup our own dynamic locking callback, and disallow
    33 +     * we now setup our own dynamic locking callback, and disallow
    34 +     * setting of another locking callback.
    34 +     * setting of another locking callback.
    35 +     */
    35 +     */
    36  }
    36  }
    37 
    37  
    38  void CRYPTO_set_dynlock_lock_callback(void (*func) (int mode,
    38  void CRYPTO_set_dynlock_lock_callback(void (*func) (int mode,
    39 @@ -382,7 +388,10 @@
    39 @@ -382,7 +388,10 @@
    40                                                      const char *file,
    40                                                      const char *file,
    41                                                      int line))
    41                                                      int line))
    42  {
    42  {
    44 +    /*
    44 +    /*
    45 +     * we now setup our own dynamic locking callback, and disallow
    45 +     * we now setup our own dynamic locking callback, and disallow
    46 +     * setting of another locking callback.
    46 +     * setting of another locking callback.
    47 +     */
    47 +     */
    48  }
    48  }
    49 
    49  
    50  void CRYPTO_set_dynlock_destroy_callback(void (*func)
    50  void CRYPTO_set_dynlock_destroy_callback(void (*func)
    51 @@ -389,7 +398,10 @@
    51 @@ -389,7 +398,10 @@
    52                                            (struct CRYPTO_dynlock_value *l,
    52                                            (struct CRYPTO_dynlock_value *l,
    53                                             const char *file, int line))
    53                                             const char *file, int line))
    54  {
    54  {
    56 +    /*
    56 +    /*
    57 +     * we now setup our own dynamic locking callback, and disallow
    57 +     * we now setup our own dynamic locking callback, and disallow
    58 +     * setting of another locking callback.
    58 +     * setting of another locking callback.
    59 +     */
    59 +     */
    60  }
    60  }
    61 
    61  
    62  void (*CRYPTO_get_locking_callback(void)) (int mode, int type,
    62  void (*CRYPTO_get_locking_callback(void)) (int mode, int type,
    63 @@ -402,6 +414,128@@
    63 @@ -402,6 +414,127 @@
    64      return (add_lock_callback);
    64      return (add_lock_callback);
    65  }
    65  }
    66 
    66  
    67 +/*
    67 +/*
    68 + * This is the locking callback function which all applications will be
    68 + * This is the locking callback function which all applications will be
    69 + * using when CRYPTO_lock() is called.
    69 + * using when CRYPTO_lock() is called.
    70 + */ 
    70 + */ 
    71 +static void solaris_locking_callback(int mode, int type, const char *file,
    71 +static void solaris_locking_callback(int mode, int type, const char *file,
    72 +    int line)
    72 +    int line)
    73 +{
    73 +{
    74 +    if (mode & CRYPTO_LOCK) {
    74 +    if (mode & CRYPTO_LOCK) {
    75 +        pthread_mutex_lock(&solaris_openssl_locks[type]);
    75 +        (void) pthread_mutex_lock(&solaris_openssl_locks[type]);
    76 +    } else {
    76 +    } else {
    77 +        pthread_mutex_unlock(&solaris_openssl_locks[type]);
    77 +        (void) pthread_mutex_unlock(&solaris_openssl_locks[type]);
    78 +    }
    78 +    }
    79 +}
    79 +}
    80 +
    80 +
    81 +/*
    81 +/*
    82 + * Implement Solaris's own dynamic locking routines.
    82 + * Implement Solaris's own dynamic locking routines.
   121 +    ret = pthread_mutex_destroy((pthread_mutex_t *)dynlock);
   121 +    ret = pthread_mutex_destroy((pthread_mutex_t *)dynlock);
   122 +    OPENSSL_assert(ret == 0);
   122 +    OPENSSL_assert(ret == 0);
   123 +}
   123 +}
   124 +
   124 +
   125 +
   125 +
       
   126 +static void solaris_fork_prep(void)
       
   127 +{
       
   128 +    int i;
       
   129 +
       
   130 +    for (i = 0; i < CRYPTO_NUM_LOCKS; i++) {
       
   131 +        (void) pthread_mutex_lock(&solaris_openssl_locks[i]);
       
   132 +    }
       
   133 +}
       
   134 +
       
   135 +static void solaris_fork_post(void)
       
   136 +{
       
   137 +    int i;
       
   138 +
       
   139 +    for (i = CRYPTO_NUM_LOCKS - 1; i >= 0; i--) {
       
   140 +        (void) pthread_mutex_unlock(&solaris_openssl_locks[i]);
       
   141 +    }
       
   142 +
       
   143 +    OPENSSL_assert(dynlock_create_callback == solaris_dynlock_create);
       
   144 +    OPENSSL_assert(dynlock_lock_callback == solaris_dynlock_lock);
       
   145 +    OPENSSL_assert(dynlock_destroy_callback == solaris_dynlock_destroy);
       
   146 +    OPENSSL_assert(locking_callback == solaris_locking_callback);
       
   147 +}
       
   148 +
   126 +/*
   149 +/*
   127 + * This function is called when a child process is forked to setup its own
   150 + * This is called by the _init() function to setup locks used by OpenSSL
   128 + * global locking callback function ptr and mutexes.
   151 + * and locking callback functions.
   129 + */
   152 + */
   130 +static void solaris_fork_child(void)
   153 +void
   131 +{
   154 +solaris_locking_setup()
   132 +    /*
       
   133 +     * clear locking_callback to indicate that locks should
       
   134 +     * be reinitialized.
       
   135 +     */
       
   136 +    locking_callback = NULL;
       
   137 +    solaris_locking_setup();
       
   138 +}
       
   139 +
       
   140 +/*
       
   141 + * This function allocates and initializes the global mutex array, and
       
   142 + * sets the locking callback.
       
   143 + */
       
   144 +void solaris_locking_setup()
       
   145 +{
   155 +{
   146 +    int i;
   156 +    int i;
   147 +    int num_locks;
       
   148 +
   157 +
   149 +    /* setup the dynlock callback if not already */
   158 +    /* setup the dynlock callback if not already */
   150 +    if (dynlock_create_callback == NULL) {
   159 +    if (dynlock_create_callback == NULL) {
   151 +        dynlock_create_callback = solaris_dynlock_create;
   160 +        dynlock_create_callback = solaris_dynlock_create;
   152 +    }
   161 +    }
   154 +        dynlock_lock_callback = solaris_dynlock_lock;
   163 +        dynlock_lock_callback = solaris_dynlock_lock;
   155 +    }
   164 +    }
   156 +    if (dynlock_destroy_callback == NULL) {
   165 +    if (dynlock_destroy_callback == NULL) {
   157 +        dynlock_destroy_callback = solaris_dynlock_destroy;
   166 +        dynlock_destroy_callback = solaris_dynlock_destroy;
   158 +    }
   167 +    }
   159 +
   168 +    if (locking_callback == NULL) {
   160 +    /* locking callback is already setup. Nothing to do */
   169 +	    locking_callback = solaris_locking_callback;
   161 +    if (locking_callback != NULL) {
   170 +    }
   162 +        return;
   171 +
   163 +    }
   172 +    /* allocate and initialize locks needed by OpenSSL  */
   164 +
       
   165 +    /*
       
   166 +     * Set atfork handler so that child can setup its own mutexes and
       
   167 +     * locking callbacks when it is forked
       
   168 +     */
       
   169 +    (void) pthread_atfork(NULL, NULL, solaris_fork_child);
       
   170 +
       
   171 +    /* allocate locks needed by OpenSSL  */
       
   172 +    num_locks = CRYPTO_num_locks();
       
   173 +    solaris_openssl_locks =
   173 +    solaris_openssl_locks =
   174 +        OPENSSL_malloc(sizeof (pthread_mutex_t) * num_locks);
   174 +        OPENSSL_malloc(sizeof (pthread_mutex_t) * CRYPTO_NUM_LOCKS);
   175 +    if (solaris_openssl_locks == NULL) {
   175 +    if (solaris_openssl_locks == NULL) {
   176 +        fprintf(stderr,
   176 +        fprintf(stderr,
   177 +            "solaris_locking_setup: memory allocation failure.\n");
   177 +            "solaris_locking_setup: memory allocation failure.\n");
   178 +        abort();
   178 +        abort();
   179 +    }
   179 +    }
   180 +
   180 +    for (i = 0; i < CRYPTO_NUM_LOCKS; i++) {
   181 +    /* initialize openssl mutexes */
   181 +        (void) pthread_mutex_init(&solaris_openssl_locks[i], NULL);
   182 +    for (i = 0; i < num_locks; i++) {
   182 +    }
   183 +        pthread_mutex_init(&solaris_openssl_locks[i], NULL);
   183 +
   184 +    }
   184 +    (void) pthread_atfork(solaris_fork_prep, solaris_fork_post, solaris_fork_post);
   185 +    locking_callback = solaris_locking_callback;
   185 +}
   186 +
   186 +
   187 +}
       
   188 +
   187 +
   189  void CRYPTO_set_locking_callback(void (*func) (int mode, int type,
   188  void CRYPTO_set_locking_callback(void (*func) (int mode, int type,
   190                                                 const char *file, int line))
   189                                                 const char *file, int line))
   191  {
   190  {
   192 @@ -410,7 +486,11 @@
   191 @@ -410,7 +541,11 @@
   193       * started.
   192       * started.
   194       */
   193       */
   195      OPENSSL_init();
   194      OPENSSL_init();
   196 -    locking_callback = func;
   195 -    locking_callback = func;
   197 +
   196 +
   200 +     * setting of another locking callback.
   199 +     * setting of another locking callback.
   201 +     */
   200 +     */
   202  }
   201  }
   203  
   202  
   204  void CRYPTO_set_add_lock_callback(int (*func) (int *num, int mount, int type,
   203  void CRYPTO_set_add_lock_callback(int (*func) (int *num, int mount, int type,
   205 @@ -471,9 +551,10 @@
   204 @@ -471,9 +606,10 @@
   206  
   205  
   207  int CRYPTO_THREADID_set_callback(void (*func) (CRYPTO_THREADID *))
   206  int CRYPTO_THREADID_set_callback(void (*func) (CRYPTO_THREADID *))
   208  {
   207  {
   209 -    if (threadid_callback)
   208 -    if (threadid_callback)
   210 -        return 0;
   209 -        return 0;
   214 +     * thread and disallow setting the threadid callback.
   213 +     * thread and disallow setting the threadid callback.
   215 +     */
   214 +     */
   216      return 1;
   215      return 1;
   217  }
   216  }
   218  
   217  
   219 @@ -529,7 +669,10 @@
   218 @@ -529,7 +665,10 @@
   220  
   219  
   221  void CRYPTO_set_id_callback(unsigned long (*func) (void))
   220  void CRYPTO_set_id_callback(unsigned long (*func) (void))
   222  {
   221  {
   223 -    id_callback = func;
   222 -    id_callback = func;
   224 +    /*
   223 +    /*