Disable mem leak checking for the self test lock
authorMatt Caswell <matt@openssl.org>
Thu, 19 Sep 2019 10:52:45 +0000 (11:52 +0100)
committerMatt Caswell <matt@openssl.org>
Fri, 29 Nov 2019 16:14:44 +0000 (16:14 +0000)
The fips self test lock is deallocated in platform specific ways that may
occur after we do mem leak checking. If we don't know how to free it for
a particular platform then we just leak it deliberately. So we
temporarily disable the mem leak checking while we allocate the lock.

Reviewed-by: Paul Dale <paul.dale@oracle.com>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9939)

crypto/provider_core.c
doc/man7/provider-base.pod
include/openssl/core_numbers.h
providers/fips/fipsprov.c
providers/fips/selftest.c

index 4f3123d..9e92e96 100644 (file)
@@ -889,6 +889,7 @@ static const OSSL_DISPATCH core_dispatch_[] = {
     { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
         (void (*)(void))CRYPTO_secure_allocated },
     { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
+    { OSSL_FUNC_CRYPTO_MEM_CTRL, (void (*)(void))CRYPTO_mem_ctrl },
 
     { 0, NULL }
 };
index 1b4b47d..08d807f 100644 (file)
@@ -108,6 +108,7 @@ provider):
  CRYPTO_secure_free             OSSL_FUNC_CRYPTO_SECURE_FREE
  CRYPTO_secure_clear_free       OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE
  CRYPTO_secure_allocated        OSSL_FUNC_CRYPTO_SECURE_ALLOCATED
+ CRYPTO_mem_ctrl                OSSL_FUNC_CRYPTO_MEM_CTRL
  BIO_new_file                   OSSL_FUNC_BIO_NEW_FILE
  BIO_new_mem_buf                OSSL_FUNC_BIO_NEW_MEMBUF
  BIO_read_ex                    OSSL_FUNC_BIO_READ_EX
@@ -181,7 +182,7 @@ CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_memdup(), CRYPTO_strdup(),
 CRYPTO_strndup(), CRYPTO_free(), CRYPTO_clear_free(),
 CRYPTO_realloc(), CRYPTO_clear_realloc(), CRYPTO_secure_malloc(),
 CRYPTO_secure_zalloc(), CRYPTO_secure_free(),
-CRYPTO_secure_clear_free(), CRYPTO_secure_allocated(),
+CRYPTO_secure_clear_free(), CRYPTO_secure_allocated(), CRYPTO_mem_ctrl(),
 BIO_new_file(), BIO_new_mem_buf(), BIO_read_ex(), BIO_free(),
 OPENSSL_cleanse(), and OPENSSL_hexstr2buf() correspond exactly to the
 public functions with the same name.
index 686944c..d07ef55 100644 (file)
@@ -119,12 +119,14 @@ OSSL_CORE_MAKE_FUNC(int,
 #define OSSL_FUNC_OPENSSL_CLEANSE             21
 OSSL_CORE_MAKE_FUNC(void,
         OPENSSL_cleanse, (void *ptr, size_t len))
+#define OSSL_FUNC_CRYPTO_MEM_CTRL             22
+OSSL_CORE_MAKE_FUNC(int, CRYPTO_mem_ctrl, (int mode))
 
 /* Bio functions provided by the core */
-#define OSSL_FUNC_BIO_NEW_FILE                22
-#define OSSL_FUNC_BIO_NEW_MEMBUF              23
-#define OSSL_FUNC_BIO_READ_EX                 24
-#define OSSL_FUNC_BIO_FREE                    25
+#define OSSL_FUNC_BIO_NEW_FILE                23
+#define OSSL_FUNC_BIO_NEW_MEMBUF              24
+#define OSSL_FUNC_BIO_READ_EX                 25
+#define OSSL_FUNC_BIO_FREE                    26
 
 OSSL_CORE_MAKE_FUNC(BIO *, BIO_new_file, (const char *filename, const char *mode))
 OSSL_CORE_MAKE_FUNC(BIO *, BIO_new_membuf, (const void *buf, int len))
index 12c471f..6a5ae3a 100644 (file)
@@ -60,6 +60,7 @@ static OSSL_CRYPTO_secure_zalloc_fn *c_CRYPTO_secure_zalloc;
 static OSSL_CRYPTO_secure_free_fn *c_CRYPTO_secure_free;
 static OSSL_CRYPTO_secure_clear_free_fn *c_CRYPTO_secure_clear_free;
 static OSSL_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated;
+static OSSL_CRYPTO_mem_ctrl_fn *c_CRYPTO_mem_ctrl;
 
 typedef struct fips_global_st {
     const OSSL_PROVIDER *prov;
@@ -515,6 +516,9 @@ int OSSL_provider_init(const OSSL_PROVIDER *provider,
         case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
             c_CRYPTO_secure_allocated = OSSL_get_CRYPTO_secure_allocated(in);
             break;
+        case OSSL_FUNC_CRYPTO_MEM_CTRL:
+            c_CRYPTO_mem_ctrl = OSSL_get_CRYPTO_mem_ctrl(in);
+            break;
         case OSSL_FUNC_BIO_NEW_FILE:
             selftest_params.bio_new_file_cb = OSSL_get_BIO_new_file(in);
             break;
@@ -700,3 +704,8 @@ int CRYPTO_secure_allocated(const void *ptr)
 {
     return c_CRYPTO_secure_allocated(ptr);
 }
+
+int CRYPTO_mem_ctrl(int mode)
+{
+    return c_CRYPTO_mem_ctrl(mode);
+}
index ad7dab2..369a6ba 100644 (file)
@@ -40,7 +40,15 @@ static unsigned char fixed_key[32] = { 0 };
 static CRYPTO_ONCE fips_self_test_init = CRYPTO_ONCE_STATIC_INIT;
 DEFINE_RUN_ONCE_STATIC(do_fips_self_test_init)
 {
+    /*
+     * This lock gets freed in platform specific ways that may occur after we
+     * do mem leak checking. If we don't know how to free it for a particular
+     * platform then we just leak it deliberately. So we temporarily disable the
+     * mem leak checking while we allocate this.
+     */
+    CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
     self_test_lock = CRYPTO_THREAD_lock_new();
+    CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
 
     return self_test_lock != NULL;
 }