X-Git-Url: https://git.openssl.org/gitweb/?p=openssl.git;a=blobdiff_plain;f=crypto%2Fmem_sec.c;h=c4190bed33482e9a7f22935a9e9d863729654e62;hp=6fc1aca1e01ef9aa2aacf11adfaa66fad36cb1c9;hb=6258e244bf702dc981c8ad63ab61133b8bbf2ba3;hpb=c8e89d58a5d44b9dd657d6d13a5a10d1d4d30733 diff --git a/crypto/mem_sec.c b/crypto/mem_sec.c index 6fc1aca1e0..c4190bed33 100644 --- a/crypto/mem_sec.c +++ b/crypto/mem_sec.c @@ -1,5 +1,6 @@ /* - * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 2015-2018 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 * this file except in compliance with the License. You can obtain a copy @@ -7,11 +8,6 @@ * https://www.openssl.org/source/license.html */ -/* - * Copyright 2004-2014, Akamai Technologies. All Rights Reserved. - * This file is distributed under the terms of the OpenSSL license. - */ - /* * This file is in two halves. The first half implements the public API * to be used by external consumers, and to be used by OpenSSL to store @@ -19,12 +15,16 @@ * For details on that implementation, see below (look for uppercase * "SECURE HEAP IMPLEMENTATION"). */ +#include "e_os.h" #include -#include #include -#if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX) +/* 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 # include # include @@ -33,8 +33,10 @@ # include # if defined(OPENSSL_SYS_LINUX) # include -# include -# include +# if defined(SYS_mlock2) +# include +# include +# endif # endif # include # include @@ -45,6 +47,9 @@ #ifndef PAGE_SIZE # define PAGE_SIZE 4096 #endif +#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS) +# define MAP_ANON MAP_ANONYMOUS +#endif #ifdef IMPLEMENTED static size_t secure_mem_used; @@ -87,7 +92,7 @@ int CRYPTO_secure_malloc_init(size_t size, int minsize) #endif /* IMPLEMENTED */ } -int CRYPTO_secure_malloc_done() +int CRYPTO_secure_malloc_done(void) { #ifdef IMPLEMENTED if (secure_mem_used == 0) { @@ -101,7 +106,7 @@ int CRYPTO_secure_malloc_done() return 0; } -int CRYPTO_secure_malloc_initialized() +int CRYPTO_secure_malloc_initialized(void) { #ifdef IMPLEMENTED return secure_mem_initialized; @@ -132,11 +137,12 @@ void *CRYPTO_secure_malloc(size_t num, const char *file, int line) 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; +#ifdef IMPLEMENTED + 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) @@ -161,6 +167,33 @@ void CRYPTO_secure_free(void *ptr, const char *file, int line) #endif /* IMPLEMENTED */ } +void CRYPTO_secure_clear_free(void *ptr, size_t num, + const char *file, int line) +{ +#ifdef IMPLEMENTED + size_t actual_size; + + if (ptr == NULL) + return; + if (!CRYPTO_secure_allocated(ptr)) { + OPENSSL_cleanse(ptr, num); + CRYPTO_free(ptr, file, line); + return; + } + CRYPTO_THREAD_write_lock(sec_malloc_lock); + actual_size = sh_actual_size(ptr); + CLEAR(ptr, actual_size); + secure_mem_used -= actual_size; + sh_free(ptr); + CRYPTO_THREAD_unlock(sec_malloc_lock); +#else + if (ptr == NULL) + return; + OPENSSL_cleanse(ptr, num); + CRYPTO_free(ptr, file, line); +#endif /* IMPLEMENTED */ +} + int CRYPTO_secure_allocated(const void *ptr) { #ifdef IMPLEMENTED @@ -177,7 +210,7 @@ int CRYPTO_secure_allocated(const void *ptr) #endif /* IMPLEMENTED */ } -size_t CRYPTO_secure_used() +size_t CRYPTO_secure_used(void) { #ifdef IMPLEMENTED return secure_mem_used; @@ -351,7 +384,7 @@ static int sh_init(size_t size, int minsize) size_t pgsize; size_t aligned; - memset(&sh, 0, sizeof sh); + memset(&sh, 0, sizeof(sh)); /* make sure size and minsize are powers of 2 */ OPENSSL_assert(size > 0); @@ -378,7 +411,7 @@ static int sh_init(size_t size, int minsize) for (i = sh.bittable_size; i; i >>= 1) sh.freelist_size++; - sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof (char *)); + sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *)); OPENSSL_assert(sh.freelist != NULL); if (sh.freelist == NULL) goto err; @@ -468,14 +501,14 @@ static int sh_init(size_t size, int minsize) return 0; } -static void sh_done() +static void sh_done(void) { OPENSSL_free(sh.freelist); OPENSSL_free(sh.bittable); OPENSSL_free(sh.bitmalloc); if (sh.map_result != NULL && sh.map_size) munmap(sh.map_result, sh.map_size); - memset(&sh, 0, sizeof sh); + memset(&sh, 0, sizeof(sh)); } static int sh_allocated(const char *ptr) @@ -556,6 +589,9 @@ static void *sh_malloc(size_t size) OPENSSL_assert(WITHIN_ARENA(chunk)); + /* zero the free list header as a precaution against information leakage */ + memset(chunk, 0, sizeof(SH_LIST)); + return chunk; } @@ -588,6 +624,8 @@ static void sh_free(void *ptr) list--; + /* Zero the higher addressed block's free list pointers */ + memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST)); if (ptr > buddy) ptr = buddy;