components/openssl/openssl-1.0.1-fips-140/patches/29_fork_safe.patch
branchs11-update
changeset 4461 68eb2fdf9b3a
parent 4006 c737cefdce54
child 4626 d5dbb6652eec
equal deleted inserted replaced
4458:6e71221d175a 4461:68eb2fdf9b3a
     1 #
     1 #
     2 # This file adds the code to setup internal mutexes and callback function.
     2 # This file adds the code to setup internal mutexes and callback function.
     3 #	PSARC/2014/077
     3 #       PSARC/2014/077
       
     4 #	PSARC/2015/043
     4 # 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
     5 # the upstream engineers, but there was no commitment.
     6 # the upstream engineers, but there was no commitment.
     6 #
     7 #
     7 --- openssl-1.0.1f/crypto/cryptlib.c.~1~	Fri Feb  7 10:41:36 2014
     8 --- openssl-1.0.1f/crypto/cryptlib.c.~1~	Fri Feb  7 10:41:36 2014
     8 +++ openssl-1.0.1f/crypto/cryptlib.c	Thu Feb  6 16:03:58 2014
     9 +++ openssl-1.0.1f/crypto/cryptlib.c    Thu Feb  6 16:03:58 2014
     9 @@ -116,6 +116,7 @@
    10 @@ -116,6 +116,7 @@
    10  
    11  
    11  #include "cryptlib.h"
    12  #include "cryptlib.h"
    12  #include <openssl/safestack.h>
    13  #include <openssl/safestack.h>
    13 +#include <pthread.h>
    14 +#include <pthread.h>
    19  static STACK_OF(CRYPTO_dynlock) *dyn_locks = NULL;
    20  static STACK_OF(CRYPTO_dynlock) *dyn_locks = NULL;
    20  
    21  
    21 +static pthread_mutex_t *solaris_openssl_locks;
    22 +static pthread_mutex_t *solaris_openssl_locks;
    22 +
    23 +
    23  static void (MS_FAR *locking_callback) (int mode, int type,
    24  static void (MS_FAR *locking_callback) (int mode, int type,
    24                                          const char *file, int line) = 0;
    25 		                         const char *file, int line) = 0;
    25  static int (MS_FAR *add_lock_callback) (int *pointer, int amount,
    26  static int (MS_FAR *add_lock_callback) (int *pointer, int amount,
    26 @@ -402,6 +405,79 @@
    27 @@ -373,7 +376,10 @@
       
    28  void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*func)
       
    29 		                          (const char *file, int line))
       
    30  {
       
    31 -    dynlock_create_callback = func;
       
    32 +	/*
       
    33 +	 * We now setup our own dynamic locking callback, and disallow
       
    34 +	 * setting of another locking callback.
       
    35 +	 */
       
    36  }
       
    37  
       
    38  void CRYPTO_set_dynlock_lock_callback(void (*func) (int mode,
       
    39 @@ -382,7 +388,10 @@
       
    40 		                                     const char *file,
       
    41 		                                     int line))
       
    42  {
       
    43 -    dynlock_lock_callback = func;
       
    44 +	/*
       
    45 +	 * We now setup our own dynamic locking callback, and disallow
       
    46 +	 * setting of another locking callback.
       
    47 +	 */
       
    48  }
       
    49  
       
    50  void CRYPTO_set_dynlock_destroy_callback(void (*func)
       
    51 @@ -389,7 +398,10 @@
       
    52 		                           (struct CRYPTO_dynlock_value *l,
       
    53 		                            const char *file, int line))
       
    54  {
       
    55 -    dynlock_destroy_callback = func;
       
    56 +	/*
       
    57 +	 * We now setup our own dynamic locking callback, and disallow
       
    58 +	 * setting of another locking callback.
       
    59 +	 */
       
    60  }
       
    61  
       
    62  void (*CRYPTO_get_locking_callback(void)) (int mode, int type,
       
    63 @@ -402,6 +414,127 @@
    27      return (add_lock_callback);
    64      return (add_lock_callback);
    28  }
    65  }
    29 
    66  
    30 +/*
    67 +/*
    31 + * This is the locking callback function which all applications will be
    68 + * This is the locking callback function which all applications will be
    32 + * using when CRYPTO_lock() is called.
    69 + * using when CRYPTO_lock() is called.
    33 + */ 
    70 + */ 
    34 +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,
    35 +    int line)
    72 +    int line)
    36 +	{
    73 +{
    37 +	if (mode & CRYPTO_LOCK)
    74 +	if (mode & CRYPTO_LOCK) {
    38 +		{
       
    39 +		pthread_mutex_lock(&solaris_openssl_locks[type]);
    75 +		pthread_mutex_lock(&solaris_openssl_locks[type]);
    40 +		}
    76 +	} else {
    41 +	else
       
    42 +		{
       
    43 +		pthread_mutex_unlock(&solaris_openssl_locks[type]);
    77 +		pthread_mutex_unlock(&solaris_openssl_locks[type]);
    44 +		}
    78 +	}
    45 +	}
    79 +}
    46 +
    80 +
       
    81 +/*
       
    82 + * Implement Solaris's own dynamic locking routines.
       
    83 + */
       
    84 +static struct CRYPTO_dynlock_value *
       
    85 +solaris_dynlock_create(const char *file, int line)
       
    86 +{
       
    87 +	int		ret;
       
    88 +	pthread_mutex_t	*dynlock;
       
    89 +
       
    90 +	dynlock = OPENSSL_malloc(sizeof(pthread_mutex_t));
       
    91 +	if (dynlock == NULL) {
       
    92 +		return (NULL);
       
    93 +	}
       
    94 +
       
    95 +	ret = pthread_mutex_init(dynlock, NULL);
       
    96 +	OPENSSL_assert(ret);
       
    97 +
       
    98 +	return ((struct CRYPTO_dynlock_value *)dynlock);
       
    99 +}
       
   100 +
       
   101 +static void
       
   102 +solaris_dynlock_lock(int mode, struct CRYPTO_dynlock_value *dynlock,
       
   103 +    const char *file, int line)
       
   104 +{
       
   105 +	int	ret;
       
   106 +
       
   107 +	if (mode & CRYPTO_LOCK) {
       
   108 +		ret = pthread_mutex_lock((pthread_mutex_t *)dynlock);
       
   109 +	} else {
       
   110 +		ret = pthread_mutex_unlock((pthread_mutex_t *)dynlock);
       
   111 +	}
       
   112 +
       
   113 +	OPENSSL_assert(ret == 0);
       
   114 +}
       
   115 +
       
   116 +static void
       
   117 +solaris_dynlock_destroy(struct CRYPTO_dynlock_value *dynlock,
       
   118 +    const char *file, int line)
       
   119 +{
       
   120 +	int	ret;
       
   121 +	ret = pthread_mutex_destroy((pthread_mutex_t *)dynlock);
       
   122 +	OPENSSL_assert(ret);
       
   123 +}
    47 +
   124 +
    48 +/*
   125 +/*
    49 + * This function is called when a child process is forked to setup its own
   126 + * This function is called when a child process is forked to setup its own
    50 + * global locking callback function ptr and mutexes.
   127 + * global locking callback function ptr and mutexes.
    51 + */
   128 + */
    52 +static void solaris_fork_child(void)
   129 +static void solaris_fork_child(void)
    53 +	{
   130 +{
    54 +		/*
   131 +	/*
    55 +		 * clear locking_callback to indicate that locks should
   132 +	 * clear locking_callback to indicate that locks should
    56 +		 * be reinitialized.
   133 +	 * be reinitialized.
    57 +		 */
   134 +	 */
    58 +		locking_callback = NULL;
   135 +	locking_callback = NULL;
    59 +		solaris_locking_setup();
   136 +	solaris_locking_setup();
    60 +	}
   137 +}
    61 +
   138 +
    62 +/*
   139 +/*
    63 + * This function allocates and initializes the global mutex array, and
   140 + * This function allocates and initializes the global mutex array, and
    64 + * sets the locking callback.
   141 + * sets the locking callback.
    65 + */
   142 + */
    66 +void solaris_locking_setup()
   143 +void solaris_locking_setup()
    67 +	{
   144 +{
    68 +	int i;
   145 +	int i;
    69 +	int num_locks;
   146 +	int num_locks;
    70 +
   147 +
       
   148 +	/* setup the dynlock callback if not already */
       
   149 +	if (dynlock_create_callback == NULL) {
       
   150 +		dynlock_create_callback = solaris_dynlock_create;
       
   151 +	}
       
   152 +	if (dynlock_lock_callback == NULL) {
       
   153 +		dynlock_lock_callback = solaris_dynlock_lock;
       
   154 +	}
       
   155 +	if (dynlock_destroy_callback == NULL) {
       
   156 +		dynlock_destroy_callback = solaris_dynlock_destroy;
       
   157 +	}
       
   158 +
    71 +	/* locking callback is already setup. Nothing to do */
   159 +	/* locking callback is already setup. Nothing to do */
    72 +	if (locking_callback != NULL)
   160 +	if (locking_callback != NULL) {
    73 +		{
       
    74 +		return;
   161 +		return;
    75 +		}
   162 +	}
    76 +
   163 +
    77 +	/*
   164 +	/*
    78 +	 * Set atfork handler so that child can setup its own mutexes and
   165 +	 * Set atfork handler so that child can setup its own mutexes and
    79 +	 * locking callbacks when it is forked
   166 +	 * locking callbacks when it is forked
    80 +	 */
   167 +	 */
    82 +
   169 +
    83 +	/* allocate locks needed by OpenSSL  */
   170 +	/* allocate locks needed by OpenSSL  */
    84 +	num_locks = CRYPTO_num_locks();
   171 +	num_locks = CRYPTO_num_locks();
    85 +	solaris_openssl_locks =
   172 +	solaris_openssl_locks =
    86 +	    OPENSSL_malloc(sizeof (pthread_mutex_t) * num_locks);
   173 +	    OPENSSL_malloc(sizeof (pthread_mutex_t) * num_locks);
    87 +	if (solaris_openssl_locks == NULL)
   174 +	if (solaris_openssl_locks == NULL) {
    88 +		{
       
    89 +		fprintf(stderr,
   175 +		fprintf(stderr,
    90 +			"solaris_locking_setup: memory allocation failure.\n");
   176 +			"solaris_locking_setup: memory allocation failure.\n");
    91 +		abort();
   177 +		abort();
    92 +		}
   178 +	}
    93 +
   179 +
    94 +	/* initialize openssl mutexes */
   180 +	/* initialize openssl mutexes */
    95 +	for (i = 0; i < num_locks; i++)
   181 +	for (i = 0; i < num_locks; i++) {
    96 +		{
       
    97 +		pthread_mutex_init(&solaris_openssl_locks[i], NULL);
   182 +		pthread_mutex_init(&solaris_openssl_locks[i], NULL);
    98 +		}
   183 +	}
    99 +	locking_callback = solaris_locking_callback;
   184 +	locking_callback = solaris_locking_callback;
   100 +
   185 +
   101 +}
   186 +}
   102 +
   187 +
   103  void CRYPTO_set_locking_callback(void (*func) (int mode, int type,
   188  void CRYPTO_set_locking_callback(void (*func) (int mode, int type,
   104                                                 const char *file, int line))
   189 		                                const char *file, int line))
   105  {
   190  {
   106 @@ -410,7 +486,11 @@
   191 @@ -410,7 +543,11 @@
   107       * started.
   192       * started.
   108       */
   193       */
   109      OPENSSL_init();
   194      OPENSSL_init();
   110 -    locking_callback = func;
   195 -    locking_callback = func;
   111 +
   196 +
   114 +	 * setting of another locking callback.
   199 +	 * setting of another locking callback.
   115 +	 */
   200 +	 */
   116  }
   201  }
   117  
   202  
   118  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,
       
   204 @@ -471,9 +608,10 @@
       
   205  
       
   206  int CRYPTO_THREADID_set_callback(void (*func) (CRYPTO_THREADID *))
       
   207  {
       
   208 -    if (threadid_callback)
       
   209 -        return 0;
       
   210 -    threadid_callback = func;
       
   211 +    /*
       
   212 +     * Use the backup method (the address of 'errno') to identify the
       
   213 +     * thread and disallow setting the threadid callback.
       
   214 +     */
       
   215      return 1;
       
   216  }
       
   217  
       
   218 @@ -529,7 +667,10 @@
       
   219  
       
   220  void CRYPTO_set_id_callback(unsigned long (*func) (void))
       
   221  {
       
   222 -    id_callback = func;
       
   223 +    /*
       
   224 +     * Use the backup method (the address of 'errno') to identify the
       
   225 +     * thread and disallow setting the threadid callback.
       
   226 +     */
       
   227  }
       
   228  
       
   229  unsigned long CRYPTO_thread_id(void)
       
   230 
   119 --- openssl-1.0.1f/crypto/cryptlib.h.~1~	Fri Feb  7 10:41:42 2014
   231 --- openssl-1.0.1f/crypto/cryptlib.h.~1~	Fri Feb  7 10:41:42 2014
   120 +++ openssl-1.0.1f/crypto/cryptlib.h	Thu Feb  6 16:04:16 2014
   232 +++ openssl-1.0.1f/crypto/cryptlib.h    Thu Feb  6 16:04:16 2014
   121 @@ -104,6 +104,8 @@
   233 @@ -104,6 +104,8 @@
   122  void *OPENSSL_stderr(void);
   234  void *OPENSSL_stderr(void);
   123  extern int OPENSSL_NONPIC_relocated;
   235  extern int OPENSSL_NONPIC_relocated;
   124  
   236 
   125 +void solaris_locking_setup();
   237 +void solaris_locking_setup();
   126 +
   238 +
   127  #ifdef  __cplusplus
   239  #ifdef  __cplusplus
   128  }
   240  }
   129  #endif
   241  #endif
   130 --- openssl-1.0.1f/crypto/sparccpuid.S.~1~	Fri Feb  7 10:41:37 2014
   242 --- openssl-1.0.1f/crypto/sparccpuid.S.~1~      Fri Feb  7 10:41:37 2014
   131 +++ openssl-1.0.1f/crypto/sparccpuid.S	Thu Feb  6 16:04:14 2014
   243 +++ openssl-1.0.1f/crypto/sparccpuid.S  Thu Feb  6 16:04:14 2014
   132 @@ -398,5 +398,7 @@
   244 @@ -398,5 +398,7 @@
   133  .size	OPENSSL_cleanse,.-OPENSSL_cleanse
   245  .size  OPENSSL_cleanse,.-OPENSSL_cleanse
   134 
   246 
   135  .section	".init",#alloc,#execinstr
   247  .section	".init",#alloc,#execinstr
   136 +	call	solaris_locking_setup
   248 +	call	solaris_locking_setup
   137 +	nop
   249 +	nop
   138  	call	OPENSSL_cpuid_setup
   250 	call	OPENSSL_cpuid_setup
   139  	nop
   251 	nop
   140 --- openssl-1.0.1f/crypto/x86_64cpuid.pl.~1~	Wed Feb 12 13:20:09 2014
   252 --- openssl-1.0.1f/crypto/x86_64cpuid.pl.~1~    Wed Feb 12 13:20:09 2014
   141 +++ openssl-1.0.1f/crypto/x86_64cpuid.pl	Wed Feb 12 13:21:20 2014
   253 +++ openssl-1.0.1f/crypto/x86_64cpuid.pl	Wed Feb 12 13:21:20 2014
   142 @@ -20,7 +20,10 @@
   254 @@ -20,7 +20,10 @@
   143  print<<___;
   255  print<<___;
   144  .extern		OPENSSL_cpuid_setup
   256  .extern		OPENSSL_cpuid_setup
   145  .hidden		OPENSSL_cpuid_setup
   257  .hidden		OPENSSL_cpuid_setup
   146 +.extern		solaris_locking_setup
   258 +.extern		solaris_locking_setup
   147 +.hidden		solaris_locking_setup
   259 +.hidden		solaris_locking_setup
   148  .section	.init
   260  .section	.init
   149 +	call	solaris_locking_setup
   261 +	call	solaris_locking_setup
   150  	call	OPENSSL_cpuid_setup
   262 	call	OPENSSL_cpuid_setup
   151  
   263 
   152  .hidden	OPENSSL_ia32cap_P
   264  .hidden	OPENSSL_ia32cap_P
   153 --- openssl-1.0.1f/crypto/x86cpuid.pl.~1~	Wed Feb 12 13:38:03 2014
   265 --- openssl-1.0.1f/crypto/x86cpuid.pl.~1~       Wed Feb 12 13:38:03 2014
   154 +++ openssl-1.0.1f/crypto/x86cpuid.pl	Wed Feb 12 13:38:31 2014
   266 +++ openssl-1.0.1f/crypto/x86cpuid.pl   Wed Feb 12 13:38:31 2014
   155 @@ -353,6 +353,7 @@
   267 @@ -353,6 +353,7 @@
   156  	&ret	();
   268 	&ret    ();
   157  &function_end_B("OPENSSL_ia32_rdrand");
   269  &function_end_B("OPENSSL_ia32_rdrand");
   158  
   270 
   159 +&initseg("solaris_locking_setup");
   271 +&initseg("solaris_locking_setup");
   160  &initseg("OPENSSL_cpuid_setup");
   272  &initseg("OPENSSL_cpuid_setup");
   161  
   273 
   162  &asm_finish();
   274  &asm_finish();