/*
- * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* For details on that implementation, see below (look for uppercase
* "SECURE HEAP IMPLEMENTATION").
*/
-#include "e_os.h"
+#include "internal/e_os.h"
#include <openssl/crypto.h>
+#include <openssl/err.h>
#include <string.h>
#ifndef OPENSSL_NO_SECURE_MEMORY
# if defined(_WIN32)
# include <windows.h>
+# if defined(WINAPI_FAMILY_PARTITION)
+# if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
+/*
+ * While VirtualLock is available under the app partition (e.g. UWP),
+ * the headers do not define the API. Define it ourselves instead.
+ */
+WINBASEAPI
+BOOL
+WINAPI
+VirtualLock(
+ _In_ LPVOID lpAddress,
+ _In_ SIZE_T dwSize
+ );
+# endif
+# endif
# endif
# include <stdlib.h>
# include <assert.h>
# if defined(__FreeBSD__)
# define MADV_DONTDUMP MADV_NOCORE
# endif
+# if !defined(MAP_CONCEAL)
+# define MAP_CONCEAL 0
+# endif
# endif
# if defined(OPENSSL_SYS_LINUX)
# include <sys/syscall.h>
# include <sys/stat.h>
# include <fcntl.h>
#endif
+#ifndef HAVE_MADVISE
+# if defined(MADV_DONTDUMP)
+# define HAVE_MADVISE 1
+# else
+# define HAVE_MADVISE 0
+# endif
+#endif
+#if HAVE_MADVISE
+# undef NO_MADVISE
+#else
+# define NO_MADVISE
+#endif
#define CLEAR(p, s) OPENSSL_cleanse(p, s)
#ifndef PAGE_SIZE
void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
{
#ifndef OPENSSL_NO_SECURE_MEMORY
- void *ret;
+ void *ret = NULL;
size_t actual_size;
+ int reason = CRYPTO_R_SECURE_MALLOC_FAILURE;
if (!secure_mem_initialized) {
return CRYPTO_malloc(num, file, line);
}
- CRYPTO_THREAD_write_lock(sec_malloc_lock);
+ if (!CRYPTO_THREAD_write_lock(sec_malloc_lock)) {
+ reason = ERR_R_CRYPTO_LIB;
+ goto err;
+ }
ret = sh_malloc(num);
actual_size = ret ? sh_actual_size(ret) : 0;
secure_mem_used += actual_size;
CRYPTO_THREAD_unlock(sec_malloc_lock);
+ err:
+ if (ret == NULL && (file != NULL || line != 0)) {
+ ERR_new();
+ ERR_set_debug(file, line, NULL);
+ ERR_set_error(ERR_LIB_CRYPTO, reason, NULL);
+ }
return ret;
#else
return CRYPTO_malloc(num, file, line);
CRYPTO_free(ptr, file, line);
return;
}
- CRYPTO_THREAD_write_lock(sec_malloc_lock);
+ if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
+ return;
actual_size = sh_actual_size(ptr);
CLEAR(ptr, actual_size);
secure_mem_used -= actual_size;
CRYPTO_free(ptr, file, line);
return;
}
- CRYPTO_THREAD_write_lock(sec_malloc_lock);
+ if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
+ return;
actual_size = sh_actual_size(ptr);
CLEAR(ptr, actual_size);
secure_mem_used -= actual_size;
int CRYPTO_secure_allocated(const void *ptr)
{
#ifndef OPENSSL_NO_SECURE_MEMORY
- int ret;
-
if (!secure_mem_initialized)
return 0;
- CRYPTO_THREAD_write_lock(sec_malloc_lock);
- ret = sh_allocated(ptr);
- CRYPTO_THREAD_unlock(sec_malloc_lock);
- return ret;
+ /*
+ * Only read accesses to the arena take place in sh_allocated() and this
+ * is only changed by the sh_init() and sh_done() calls which are not
+ * locked. Hence, it is safe to make this check without a lock too.
+ */
+ return sh_allocated(ptr);
#else
return 0;
#endif /* OPENSSL_NO_SECURE_MEMORY */
size_t CRYPTO_secure_used(void)
{
+ size_t ret = 0;
+
#ifndef OPENSSL_NO_SECURE_MEMORY
- return secure_mem_used;
-#else
- return 0;
+ if (!CRYPTO_THREAD_read_lock(sec_malloc_lock))
+ return 0;
+
+ ret = secure_mem_used;
+
+ CRYPTO_THREAD_unlock(sec_malloc_lock);
#endif /* OPENSSL_NO_SECURE_MEMORY */
+ return ret;
}
size_t CRYPTO_secure_actual_size(void *ptr)
#ifndef OPENSSL_NO_SECURE_MEMORY
size_t actual_size;
- CRYPTO_THREAD_write_lock(sec_malloc_lock);
+ if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
+ return 0;
actual_size = sh_actual_size(ptr);
CRYPTO_THREAD_unlock(sec_malloc_lock);
return actual_size;
#if !defined(_WIN32)
# ifdef MAP_ANON
sh.map_result = mmap(NULL, sh.map_size,
- PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
+ PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|MAP_CONCEAL, -1, 0);
# else
{
int fd;
if (mlock(sh.arena, sh.arena_size) < 0)
ret = 2;
#endif
-#ifdef MADV_DONTDUMP
+#ifndef NO_MADVISE
if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
ret = 2;
#endif