Browse Source

CRYPTO_THREAD_lock_new(): Avoid infinite recursion on allocation error

Fixes #19334

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19335)
Tomas Mraz 1 year ago
parent
commit
894f2166ef
3 changed files with 6 additions and 9 deletions
  1. 1 2
      crypto/threads_none.c
  2. 2 4
      crypto/threads_pthread.c
  3. 3 3
      crypto/threads_win.c

+ 1 - 2
crypto/threads_none.c

@@ -21,10 +21,9 @@ CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
 {
     CRYPTO_RWLOCK *lock;
 
-    if ((lock = OPENSSL_zalloc(sizeof(unsigned int))) == NULL) {
+    if ((lock = CRYPTO_zalloc(sizeof(unsigned int), NULL, 0)) == NULL)
         /* Don't set error, to avoid recursion blowup. */
         return NULL;
-    }
 
     *(unsigned int *)lock = 1;
 

+ 2 - 4
crypto/threads_pthread.c

@@ -47,10 +47,9 @@ CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
 # ifdef USE_RWLOCK
     CRYPTO_RWLOCK *lock;
 
-    if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL) {
+    if ((lock = CRYPTO_zalloc(sizeof(pthread_rwlock_t), NULL, 0)) == NULL)
         /* Don't set error, to avoid recursion blowup. */
         return NULL;
-    }
 
     if (pthread_rwlock_init(lock, NULL) != 0) {
         OPENSSL_free(lock);
@@ -60,10 +59,9 @@ CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
     pthread_mutexattr_t attr;
     CRYPTO_RWLOCK *lock;
 
-    if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL) {
+    if ((lock = CRYPTO_zalloc(sizeof(pthread_mutex_t), NULL, 0)) == NULL)
         /* Don't set error, to avoid recursion blowup. */
         return NULL;
-    }
 
     /*
      * We don't use recursive mutexes, but try to catch errors if we do.

+ 3 - 3
crypto/threads_win.c

@@ -43,16 +43,16 @@ CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
 # ifdef USE_RWLOCK
     CRYPTO_win_rwlock *rwlock;
 
-    if ((lock = OPENSSL_zalloc(sizeof(CRYPTO_win_rwlock))) == NULL)
+    if ((lock = CRYPTO_zalloc(sizeof(CRYPTO_win_rwlock), NULL, 0)) == NULL)
+        /* Don't set error, to avoid recursion blowup. */
         return NULL;
     rwlock = lock;
     InitializeSRWLock(&rwlock->lock);
 # else
 
-    if ((lock = OPENSSL_zalloc(sizeof(CRITICAL_SECTION))) == NULL) {
+    if ((lock = CRYPTO_zalloc(sizeof(CRITICAL_SECTION), NULL, 0)) == NULL)
         /* Don't set error, to avoid recursion blowup. */
         return NULL;
-    }
 
 #  if !defined(_WIN32_WCE)
     /* 0x400 is the spin count value suggested in the documentation */