/*
- * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
*
- * Licensed under the OpenSSL license (the "License"). You may not use
+ * 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
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
#include <string.h>
-/* e_os.h includes unistd.h, which defines _POSIX_VERSION */
-#if !defined(OPENSSL_NO_SECURE_MEMORY) && defined(OPENSSL_SYS_UNIX) \
- && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) \
- || defined(__sun) || defined(__hpux) || defined(__sgi) \
- || defined(__osf__) )
-# define IMPLEMENTED
+#ifndef OPENSSL_NO_SECURE_MEMORY
# include <stdlib.h>
# include <assert.h>
# include <unistd.h>
# include <linux/mman.h>
# include <errno.h>
# endif
+# include <sys/param.h>
# endif
-# include <sys/param.h>
# include <sys/stat.h>
# include <fcntl.h>
#endif
# define MAP_ANON MAP_ANONYMOUS
#endif
-#ifdef IMPLEMENTED
+#ifndef OPENSSL_NO_SECURE_MEMORY
static size_t secure_mem_used;
static int secure_mem_initialized;
/*
* These are the functions that must be implemented by a secure heap (sh).
*/
-static int sh_init(size_t size, int minsize);
+static int sh_init(size_t size, size_t minsize);
static void *sh_malloc(size_t size);
static void sh_free(void *ptr);
static void sh_done(void);
static int sh_allocated(const char *ptr);
#endif
-int CRYPTO_secure_malloc_init(size_t size, int minsize)
+int CRYPTO_secure_malloc_init(size_t size, size_t minsize)
{
-#ifdef IMPLEMENTED
+#ifndef OPENSSL_NO_SECURE_MEMORY
int ret = 0;
if (!secure_mem_initialized) {
return ret;
#else
return 0;
-#endif /* IMPLEMENTED */
+#endif /* OPENSSL_NO_SECURE_MEMORY */
}
int CRYPTO_secure_malloc_done(void)
{
-#ifdef IMPLEMENTED
+#ifndef OPENSSL_NO_SECURE_MEMORY
if (secure_mem_used == 0) {
sh_done();
secure_mem_initialized = 0;
sec_malloc_lock = NULL;
return 1;
}
-#endif /* IMPLEMENTED */
+#endif /* OPENSSL_NO_SECURE_MEMORY */
return 0;
}
int CRYPTO_secure_malloc_initialized(void)
{
-#ifdef IMPLEMENTED
+#ifndef OPENSSL_NO_SECURE_MEMORY
return secure_mem_initialized;
#else
return 0;
-#endif /* IMPLEMENTED */
+#endif /* OPENSSL_NO_SECURE_MEMORY */
}
void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
{
-#ifdef IMPLEMENTED
+#ifndef OPENSSL_NO_SECURE_MEMORY
void *ret;
size_t actual_size;
return ret;
#else
return CRYPTO_malloc(num, file, line);
-#endif /* IMPLEMENTED */
+#endif /* OPENSSL_NO_SECURE_MEMORY */
}
void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
{
- void *ret = CRYPTO_secure_malloc(num, file, line);
-
- if (ret != NULL)
- memset(ret, 0, num);
- return ret;
+#ifndef OPENSSL_NO_SECURE_MEMORY
+ if (secure_mem_initialized)
+ /* CRYPTO_secure_malloc() zeroes allocations when it is implemented */
+ return CRYPTO_secure_malloc(num, file, line);
+#endif
+ return CRYPTO_zalloc(num, file, line);
}
void CRYPTO_secure_free(void *ptr, const char *file, int line)
{
-#ifdef IMPLEMENTED
+#ifndef OPENSSL_NO_SECURE_MEMORY
size_t actual_size;
if (ptr == NULL)
CRYPTO_THREAD_unlock(sec_malloc_lock);
#else
CRYPTO_free(ptr, file, line);
-#endif /* IMPLEMENTED */
+#endif /* OPENSSL_NO_SECURE_MEMORY */
}
void CRYPTO_secure_clear_free(void *ptr, size_t num,
const char *file, int line)
{
-#ifdef IMPLEMENTED
+#ifndef OPENSSL_NO_SECURE_MEMORY
size_t actual_size;
if (ptr == NULL)
return;
OPENSSL_cleanse(ptr, num);
CRYPTO_free(ptr, file, line);
-#endif /* IMPLEMENTED */
+#endif /* OPENSSL_NO_SECURE_MEMORY */
}
int CRYPTO_secure_allocated(const void *ptr)
{
-#ifdef IMPLEMENTED
+#ifndef OPENSSL_NO_SECURE_MEMORY
int ret;
if (!secure_mem_initialized)
return ret;
#else
return 0;
-#endif /* IMPLEMENTED */
+#endif /* OPENSSL_NO_SECURE_MEMORY */
}
size_t CRYPTO_secure_used(void)
{
-#ifdef IMPLEMENTED
+#ifndef OPENSSL_NO_SECURE_MEMORY
return secure_mem_used;
#else
return 0;
-#endif /* IMPLEMENTED */
+#endif /* OPENSSL_NO_SECURE_MEMORY */
}
size_t CRYPTO_secure_actual_size(void *ptr)
{
-#ifdef IMPLEMENTED
+#ifndef OPENSSL_NO_SECURE_MEMORY
size_t actual_size;
CRYPTO_THREAD_write_lock(sec_malloc_lock);
return 0;
#endif
}
-/* END OF PAGE ...
-
- ... START OF PAGE */
/*
* SECURE HEAP IMPLEMENTATION
*/
-#ifdef IMPLEMENTED
+#ifndef OPENSSL_NO_SECURE_MEMORY
/*
}
-static int sh_init(size_t size, int minsize)
+static int sh_init(size_t size, size_t minsize)
{
int ret;
size_t i;
memset(&sh, 0, sizeof(sh));
- /* make sure size and minsize are powers of 2 */
+ /* make sure size is a powers of 2 */
OPENSSL_assert(size > 0);
OPENSSL_assert((size & (size - 1)) == 0);
- OPENSSL_assert(minsize > 0);
- OPENSSL_assert((minsize & (minsize - 1)) == 0);
- if (size <= 0 || (size & (size - 1)) != 0)
- goto err;
- if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
+ if (size == 0 || (size & (size - 1)) != 0)
goto err;
- while (minsize < (int)sizeof(SH_LIST))
- minsize *= 2;
+ if (minsize <= sizeof(SH_LIST)) {
+ OPENSSL_assert(sizeof(SH_LIST) <= 65536);
+ /*
+ * Compute the minimum possible allocation size.
+ * This must be a power of 2 and at least as large as the SH_LIST
+ * structure.
+ */
+ minsize = sizeof(SH_LIST) - 1;
+ minsize |= minsize >> 1;
+ minsize |= minsize >> 2;
+ if (sizeof(SH_LIST) > 16)
+ minsize |= minsize >> 4;
+ if (sizeof(SH_LIST) > 256)
+ minsize |= minsize >> 8;
+ minsize++;
+ } else {
+ /* make sure minsize is a powers of 2 */
+ OPENSSL_assert((minsize & (minsize - 1)) == 0);
+ if ((minsize & (minsize - 1)) != 0)
+ goto err;
+ }
sh.arena_size = size;
sh.minsize = minsize;
pgsize = PAGE_SIZE;
#endif
sh.map_size = pgsize + sh.arena_size + pgsize;
- if (1) {
+
#ifdef MAP_ANON
- sh.map_result = mmap(NULL, sh.map_size,
- PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
- } else {
-#endif
+ sh.map_result = mmap(NULL, sh.map_size,
+ PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
+#else
+ {
int fd;
sh.map_result = MAP_FAILED;
close(fd);
}
}
+#endif
if (sh.map_result == MAP_FAILED)
goto err;
sh.arena = (char *)(sh.map_result + pgsize);
OPENSSL_assert(WITHIN_ARENA(chunk));
+ /* zero the free list header as a precaution against information leakage */
+ memset(chunk, 0, sizeof(SH_LIST));
+
return chunk;
}
list--;
+ /* Zero the higher addressed block's free list pointers */
+ memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST));
if (ptr > buddy)
ptr = buddy;
OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
return sh.arena_size / (ONE << list);
}
-#endif /* IMPLEMENTED */
+#endif /* OPENSSL_NO_SECURE_MEMORY */