Skip to content

Commit

Permalink
crypto/provider_core.c: Allocate activatecnt_lock
Browse files Browse the repository at this point in the history
CRYPTO_atomic_add has a lock as a parameter, which is often ignored, but in
some cases (for example, when BROKEN_CLANG_ATOMICS is defined) it is required.

There is no easy way to determine if the lock is needed or not. The current
logic looks like this:

    if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
      if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
        - It works without the lock, but in general the need for the
          lock depends on __atomic_is_lock_free results
      elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
        - The lock is not needed (unless ret is NULL, which should never
          happen?)
      else
        - The lock is required
      endif
    else
      - The lock is not needed
    endif

Adding such conditions outside of crypto.h is error-prone, so it is better to
always allocate the lock, otherwise CRYPTO_atomic_add may silently fail.

Fixes #23376.

Fixes: fc570b2 ("Avoid taking a write lock in ossl_provider_doall_activated()")
Signed-off-by: Oleg Bulatov <oleg@bulatov.me>
  • Loading branch information
dmage committed Apr 10, 2024
1 parent 309c7ff commit 19b3ef4
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions crypto/provider_core.c
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
Expand Down Expand Up @@ -446,13 +446,11 @@ static OSSL_PROVIDER *provider_new(const char *name,
OPENSSL_free(prov);
return NULL;
}
#ifndef HAVE_ATOMICS
if ((prov->activatecnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
ossl_provider_free(prov);
ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
return NULL;
}
#endif

if ((prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
|| (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
Expand Down Expand Up @@ -742,9 +740,7 @@ void ossl_provider_free(OSSL_PROVIDER *prov)
sk_INFOPAIR_pop_free(prov->parameters, infopair_free);
CRYPTO_THREAD_lock_free(prov->opbits_lock);
CRYPTO_THREAD_lock_free(prov->flag_lock);
#ifndef HAVE_ATOMICS
CRYPTO_THREAD_lock_free(prov->activatecnt_lock);
#endif
CRYPTO_FREE_REF(&prov->refcnt);
OPENSSL_free(prov);
}
Expand Down

0 comments on commit 19b3ef4

Please sign in to comment.