/* Copyright (C) 1995-1998 Eric Young ([email protected]) * All rights reserved. * * This package is an SSL implementation written * by Eric Young ([email protected]). * The implementation was written so as to conform with Netscapes SSL. * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson ([email protected]). * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * "This product includes cryptographic software written by * Eric Young ([email protected])" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson ([email protected])" * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */ #include <openssl/mem.h> #include <assert.h> #include <errno.h> #include <limits.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <openssl/err.h> #if defined(OPENSSL_WINDOWS) OPENSSL_MSVC_PRAGMA(warning(push, 3)) #include <windows.h> OPENSSL_MSVC_PRAGMA(warning(pop)) #endif #if defined(BORINGSSL_MALLOC_FAILURE_TESTING) #include <errno.h> #include <signal.h> #include <unistd.h> #endif #include "internal.h" #define OPENSSL_MALLOC_PREFIX … static_assert; #if defined(OPENSSL_ASAN) void __asan_poison_memory_region(const volatile void *addr, size_t size); void __asan_unpoison_memory_region(const volatile void *addr, size_t size); #else static void __asan_poison_memory_region(const void *addr, size_t size) { … } static void __asan_unpoison_memory_region(const void *addr, size_t size) { … } #endif // Windows doesn't really support weak symbols as of May 2019, and Clang on // Windows will emit strong symbols instead. See // https://bugs.llvm.org/show_bug.cgi?id=37598 // // EDK2 targets UEFI but builds as ELF and then translates the binary to // COFF(!). Thus it builds with __ELF__ defined but cannot actually cope with // weak symbols. #if !defined(__EDK2_BORINGSSL__) && defined(__ELF__) && defined(__GNUC__) #define WEAK_SYMBOL_FUNC(rettype, name, args) … #else #define WEAK_SYMBOL_FUNC … #endif // sdallocx is a sized |free| function. By passing the size (which we happen to // always know in BoringSSL), the malloc implementation can save work. We cannot // depend on |sdallocx| being available, however, so it's a weak symbol. // // This will always be safe, but will only be overridden if the malloc // implementation is statically linked with BoringSSL. So, if |sdallocx| is // provided in, say, libc.so, we still won't use it because that's dynamically // linked. This isn't an ideal result, but its helps in some cases. WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags)); // The following three functions can be defined to override default heap // allocation and freeing. If defined, it is the responsibility of // |OPENSSL_memory_free| to zero out the memory before returning it to the // system. |OPENSSL_memory_free| will not be passed NULL pointers. // // WARNING: These functions are called on every allocation and free in // BoringSSL across the entire process. They may be called by any code in the // process which calls BoringSSL, including in process initializers and thread // destructors. When called, BoringSSL may hold pthreads locks. Any other code // in the process which, directly or indirectly, calls BoringSSL may be on the // call stack and may itself be using arbitrary synchronization primitives. // // As a result, these functions may not have the usual programming environment // available to most C or C++ code. In particular, they may not call into // BoringSSL, or any library which depends on BoringSSL. Any synchronization // primitives used must tolerate every other synchronization primitive linked // into the process, including pthreads locks. Failing to meet these constraints // may result in deadlocks, crashes, or memory corruption. WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_alloc, (size_t size)); WEAK_SYMBOL_FUNC(void, OPENSSL_memory_free, (void *ptr)); WEAK_SYMBOL_FUNC(size_t, OPENSSL_memory_get_size, (void *ptr)); #if defined(BORINGSSL_MALLOC_FAILURE_TESTING) static CRYPTO_MUTEX malloc_failure_lock = CRYPTO_MUTEX_INIT; static uint64_t current_malloc_count = 0; static uint64_t malloc_number_to_fail = 0; static int malloc_failure_enabled = 0, break_on_malloc_fail = 0, any_malloc_failed = 0, disable_malloc_failures = 0; static void malloc_exit_handler(void) { CRYPTO_MUTEX_lock_read(&malloc_failure_lock); if (any_malloc_failed) { // Signal to the test driver that some allocation failed, so it knows to // increment the counter and continue. _exit(88); } CRYPTO_MUTEX_unlock_read(&malloc_failure_lock); } static void init_malloc_failure(void) { const char *env = getenv("MALLOC_NUMBER_TO_FAIL"); if (env != NULL && env[0] != 0) { char *endptr; malloc_number_to_fail = strtoull(env, &endptr, 10); if (*endptr == 0) { malloc_failure_enabled = 1; atexit(malloc_exit_handler); } } break_on_malloc_fail = getenv("MALLOC_BREAK_ON_FAIL") != NULL; } // should_fail_allocation returns one if the current allocation should fail and // zero otherwise. static int should_fail_allocation() { static CRYPTO_once_t once = CRYPTO_ONCE_INIT; CRYPTO_once(&once, init_malloc_failure); if (!malloc_failure_enabled || disable_malloc_failures) { return 0; } // We lock just so multi-threaded tests are still correct, but we won't test // every malloc exhaustively. CRYPTO_MUTEX_lock_write(&malloc_failure_lock); int should_fail = current_malloc_count == malloc_number_to_fail; current_malloc_count++; any_malloc_failed = any_malloc_failed || should_fail; CRYPTO_MUTEX_unlock_write(&malloc_failure_lock); if (should_fail && break_on_malloc_fail) { raise(SIGTRAP); } if (should_fail) { errno = ENOMEM; } return should_fail; } void OPENSSL_reset_malloc_counter_for_testing(void) { CRYPTO_MUTEX_lock_write(&malloc_failure_lock); current_malloc_count = 0; CRYPTO_MUTEX_unlock_write(&malloc_failure_lock); } void OPENSSL_disable_malloc_failures_for_testing(void) { CRYPTO_MUTEX_lock_write(&malloc_failure_lock); BSSL_CHECK(!disable_malloc_failures); disable_malloc_failures = 1; CRYPTO_MUTEX_unlock_write(&malloc_failure_lock); } void OPENSSL_enable_malloc_failures_for_testing(void) { CRYPTO_MUTEX_lock_write(&malloc_failure_lock); BSSL_CHECK(disable_malloc_failures); disable_malloc_failures = 0; CRYPTO_MUTEX_unlock_write(&malloc_failure_lock); } #else static int should_fail_allocation(void) { … } #endif void *OPENSSL_malloc(size_t size) { … } void *OPENSSL_zalloc(size_t size) { … } void *OPENSSL_calloc(size_t num, size_t size) { … } void OPENSSL_free(void *orig_ptr) { … } void *OPENSSL_realloc(void *orig_ptr, size_t new_size) { … } void OPENSSL_cleanse(void *ptr, size_t len) { … } void OPENSSL_clear_free(void *ptr, size_t unused) { … } int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { … } int CRYPTO_secure_malloc_initialized(void) { … } size_t CRYPTO_secure_used(void) { … } void *OPENSSL_secure_malloc(size_t size) { … } void OPENSSL_secure_clear_free(void *ptr, size_t len) { … } int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) { … } uint32_t OPENSSL_hash32(const void *ptr, size_t len) { … } uint32_t OPENSSL_strhash(const char *s) { … } size_t OPENSSL_strnlen(const char *s, size_t len) { … } char *OPENSSL_strdup(const char *s) { … } int OPENSSL_isalpha(int c) { … } int OPENSSL_isdigit(int c) { … } int OPENSSL_isxdigit(int c) { … } int OPENSSL_fromxdigit(uint8_t *out, int c) { … } int OPENSSL_isalnum(int c) { … } int OPENSSL_tolower(int c) { … } int OPENSSL_isspace(int c) { … } int OPENSSL_strcasecmp(const char *a, const char *b) { … } int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) { … } int BIO_snprintf(char *buf, size_t n, const char *format, ...) { … } int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) { … } int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args, int system_malloc) { … } int OPENSSL_vasprintf(char **str, const char *format, va_list args) { … } int OPENSSL_asprintf(char **str, const char *format, ...) { … } char *OPENSSL_strndup(const char *str, size_t size) { … } size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) { … } size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) { … } void *OPENSSL_memdup(const void *data, size_t size) { … } void *CRYPTO_malloc(size_t size, const char *file, int line) { … } void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) { … } void CRYPTO_free(void *ptr, const char *file, int line) { … }