David Benjamin | 820731a | 2015-07-23 20:01:51 -0400 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] */ |
| 56 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 57 | #include <openssl/mem.h> |
| 58 | |
| 59 | #include <assert.h> |
Bob Beck | 350f854 | 2023-02-07 16:11:58 -0700 | [diff] [blame] | 60 | #include <errno.h> |
| 61 | #include <limits.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 62 | #include <stdarg.h> |
| 63 | #include <stdio.h> |
Bob Beck | 350f854 | 2023-02-07 16:11:58 -0700 | [diff] [blame] | 64 | #include <stdlib.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 65 | |
David Benjamin | 3ba9586 | 2019-10-21 16:14:33 -0400 | [diff] [blame] | 66 | #include <openssl/err.h> |
| 67 | |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 68 | #if defined(OPENSSL_WINDOWS) |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 69 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
Adam Langley | 3e71931 | 2015-03-20 16:32:23 -0700 | [diff] [blame] | 70 | #include <windows.h> |
David Benjamin | 054e597 | 2016-06-16 12:08:26 -0400 | [diff] [blame] | 71 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 72 | #endif |
| 73 | |
David Benjamin | 582904f | 2023-02-04 18:30:36 -0500 | [diff] [blame] | 74 | #if defined(BORINGSSL_MALLOC_FAILURE_TESTING) |
| 75 | #include <errno.h> |
| 76 | #include <signal.h> |
| 77 | #include <unistd.h> |
| 78 | #endif |
| 79 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 80 | #include "internal.h" |
| 81 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 82 | |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 83 | #define OPENSSL_MALLOC_PREFIX 8 |
David Benjamin | b7d6320 | 2022-07-26 13:25:02 -0700 | [diff] [blame] | 84 | static_assert(OPENSSL_MALLOC_PREFIX >= sizeof(size_t), "size_t too large"); |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 85 | |
David Benjamin | da8bb84 | 2019-02-26 22:13:28 -0600 | [diff] [blame] | 86 | #if defined(OPENSSL_ASAN) |
| 87 | void __asan_poison_memory_region(const volatile void *addr, size_t size); |
| 88 | void __asan_unpoison_memory_region(const volatile void *addr, size_t size); |
| 89 | #else |
| 90 | static void __asan_poison_memory_region(const void *addr, size_t size) {} |
| 91 | static void __asan_unpoison_memory_region(const void *addr, size_t size) {} |
| 92 | #endif |
| 93 | |
John Sheu | 787b26c | 2019-05-03 12:08:12 -0700 | [diff] [blame] | 94 | // Windows doesn't really support weak symbols as of May 2019, and Clang on |
| 95 | // Windows will emit strong symbols instead. See |
| 96 | // https://bugs.llvm.org/show_bug.cgi?id=37598 |
Adam Langley | 0cf14d3 | 2020-03-30 09:24:45 -0700 | [diff] [blame] | 97 | #if defined(__ELF__) && defined(__GNUC__) |
| 98 | #define WEAK_SYMBOL_FUNC(rettype, name, args) \ |
| 99 | rettype name args __attribute__((weak)); |
Wiktor Garbacz | 9ae40ce | 2020-02-05 18:14:20 +0100 | [diff] [blame] | 100 | #else |
Adam Langley | 0cf14d3 | 2020-03-30 09:24:45 -0700 | [diff] [blame] | 101 | #define WEAK_SYMBOL_FUNC(rettype, name, args) static rettype(*name) args = NULL; |
Wiktor Garbacz | 9ae40ce | 2020-02-05 18:14:20 +0100 | [diff] [blame] | 102 | #endif |
| 103 | |
Chris Kennelly | b5e4a22 | 2018-09-10 11:47:15 -0400 | [diff] [blame] | 104 | // sdallocx is a sized |free| function. By passing the size (which we happen to |
Adam Langley | b49b78e | 2021-09-02 14:57:02 -0700 | [diff] [blame] | 105 | // always know in BoringSSL), the malloc implementation can save work. We cannot |
| 106 | // depend on |sdallocx| being available, however, so it's a weak symbol. |
Chris Kennelly | b5e4a22 | 2018-09-10 11:47:15 -0400 | [diff] [blame] | 107 | // |
Adam Langley | b49b78e | 2021-09-02 14:57:02 -0700 | [diff] [blame] | 108 | // This will always be safe, but will only be overridden if the malloc |
| 109 | // implementation is statically linked with BoringSSL. So, if |sdallocx| is |
| 110 | // provided in, say, libc.so, we still won't use it because that's dynamically |
| 111 | // linked. This isn't an ideal result, but its helps in some cases. |
| 112 | WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags)); |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 113 | |
Adam Langley | 0313b59 | 2020-06-10 14:38:02 -0700 | [diff] [blame] | 114 | // The following three functions can be defined to override default heap |
| 115 | // allocation and freeing. If defined, it is the responsibility of |
| 116 | // |OPENSSL_memory_free| to zero out the memory before returning it to the |
| 117 | // system. |OPENSSL_memory_free| will not be passed NULL pointers. |
David Benjamin | 20f7bba | 2021-03-24 02:31:33 -0400 | [diff] [blame] | 118 | // |
| 119 | // WARNING: These functions are called on every allocation and free in |
| 120 | // BoringSSL across the entire process. They may be called by any code in the |
| 121 | // process which calls BoringSSL, including in process initializers and thread |
| 122 | // destructors. When called, BoringSSL may hold pthreads locks. Any other code |
| 123 | // in the process which, directly or indirectly, calls BoringSSL may be on the |
| 124 | // call stack and may itself be using arbitrary synchronization primitives. |
| 125 | // |
| 126 | // As a result, these functions may not have the usual programming environment |
| 127 | // available to most C or C++ code. In particular, they may not call into |
| 128 | // BoringSSL, or any library which depends on BoringSSL. Any synchronization |
| 129 | // primitives used must tolerate every other synchronization primitive linked |
| 130 | // into the process, including pthreads locks. Failing to meet these constraints |
| 131 | // may result in deadlocks, crashes, or memory corruption. |
Bob Beck | 350f854 | 2023-02-07 16:11:58 -0700 | [diff] [blame] | 132 | WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_alloc, (size_t size)); |
Adam Langley | 0313b59 | 2020-06-10 14:38:02 -0700 | [diff] [blame] | 133 | WEAK_SYMBOL_FUNC(void, OPENSSL_memory_free, (void *ptr)); |
| 134 | WEAK_SYMBOL_FUNC(size_t, OPENSSL_memory_get_size, (void *ptr)); |
Wiktor Garbacz | 9ae40ce | 2020-02-05 18:14:20 +0100 | [diff] [blame] | 135 | |
Adam Langley | 89386ac | 2021-10-12 12:43:14 -0700 | [diff] [blame] | 136 | // kBoringSSLBinaryTag is a distinctive byte sequence to identify binaries that |
| 137 | // are linking in BoringSSL and, roughly, what version they are using. |
| 138 | static const uint8_t kBoringSSLBinaryTag[18] = { |
| 139 | // 16 bytes of magic tag. |
Bob Beck | 350f854 | 2023-02-07 16:11:58 -0700 | [diff] [blame] | 140 | 0x8c, |
| 141 | 0x62, |
| 142 | 0x20, |
| 143 | 0x0b, |
| 144 | 0xd2, |
| 145 | 0xa0, |
| 146 | 0x72, |
| 147 | 0x58, |
| 148 | 0x44, |
| 149 | 0xa8, |
| 150 | 0x96, |
| 151 | 0x69, |
| 152 | 0xad, |
| 153 | 0x55, |
| 154 | 0x7e, |
| 155 | 0xec, |
Adam Langley | 89386ac | 2021-10-12 12:43:14 -0700 | [diff] [blame] | 156 | // Current source iteration. Incremented ~monthly. |
Bob Beck | 350f854 | 2023-02-07 16:11:58 -0700 | [diff] [blame] | 157 | 3, |
| 158 | 0, |
Adam Langley | 89386ac | 2021-10-12 12:43:14 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
David Benjamin | 582904f | 2023-02-04 18:30:36 -0500 | [diff] [blame] | 161 | #if defined(BORINGSSL_MALLOC_FAILURE_TESTING) |
David Benjamin | 04c3d40 | 2023-06-03 01:26:29 -0400 | [diff] [blame] | 162 | static CRYPTO_MUTEX malloc_failure_lock = CRYPTO_MUTEX_INIT; |
David Benjamin | 582904f | 2023-02-04 18:30:36 -0500 | [diff] [blame] | 163 | static uint64_t current_malloc_count = 0; |
| 164 | static uint64_t malloc_number_to_fail = 0; |
David Benjamin | 5e356a8 | 2023-02-04 19:44:34 -0500 | [diff] [blame] | 165 | static int malloc_failure_enabled = 0, break_on_malloc_fail = 0, |
| 166 | any_malloc_failed = 0; |
David Benjamin | 582904f | 2023-02-04 18:30:36 -0500 | [diff] [blame] | 167 | |
| 168 | static void malloc_exit_handler(void) { |
David Benjamin | 04c3d40 | 2023-06-03 01:26:29 -0400 | [diff] [blame] | 169 | CRYPTO_MUTEX_lock_read(&malloc_failure_lock); |
David Benjamin | 5e356a8 | 2023-02-04 19:44:34 -0500 | [diff] [blame] | 170 | if (any_malloc_failed) { |
| 171 | // Signal to the test driver that some allocation failed, so it knows to |
| 172 | // increment the counter and continue. |
David Benjamin | 582904f | 2023-02-04 18:30:36 -0500 | [diff] [blame] | 173 | _exit(88); |
| 174 | } |
David Benjamin | 04c3d40 | 2023-06-03 01:26:29 -0400 | [diff] [blame] | 175 | CRYPTO_MUTEX_unlock_read(&malloc_failure_lock); |
David Benjamin | 582904f | 2023-02-04 18:30:36 -0500 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static void init_malloc_failure(void) { |
| 179 | const char *env = getenv("MALLOC_NUMBER_TO_FAIL"); |
| 180 | if (env != NULL && env[0] != 0) { |
| 181 | char *endptr; |
| 182 | malloc_number_to_fail = strtoull(env, &endptr, 10); |
| 183 | if (*endptr == 0) { |
| 184 | malloc_failure_enabled = 1; |
| 185 | atexit(malloc_exit_handler); |
| 186 | } |
| 187 | } |
| 188 | break_on_malloc_fail = getenv("MALLOC_BREAK_ON_FAIL") != NULL; |
| 189 | } |
| 190 | |
| 191 | // should_fail_allocation returns one if the current allocation should fail and |
| 192 | // zero otherwise. |
| 193 | static int should_fail_allocation() { |
| 194 | static CRYPTO_once_t once = CRYPTO_ONCE_INIT; |
| 195 | CRYPTO_once(&once, init_malloc_failure); |
| 196 | if (!malloc_failure_enabled) { |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | // We lock just so multi-threaded tests are still correct, but we won't test |
| 201 | // every malloc exhaustively. |
David Benjamin | 04c3d40 | 2023-06-03 01:26:29 -0400 | [diff] [blame] | 202 | CRYPTO_MUTEX_lock_write(&malloc_failure_lock); |
David Benjamin | 582904f | 2023-02-04 18:30:36 -0500 | [diff] [blame] | 203 | int should_fail = current_malloc_count == malloc_number_to_fail; |
| 204 | current_malloc_count++; |
David Benjamin | 5e356a8 | 2023-02-04 19:44:34 -0500 | [diff] [blame] | 205 | any_malloc_failed = any_malloc_failed || should_fail; |
David Benjamin | 04c3d40 | 2023-06-03 01:26:29 -0400 | [diff] [blame] | 206 | CRYPTO_MUTEX_unlock_write(&malloc_failure_lock); |
David Benjamin | 582904f | 2023-02-04 18:30:36 -0500 | [diff] [blame] | 207 | |
| 208 | if (should_fail && break_on_malloc_fail) { |
| 209 | raise(SIGTRAP); |
| 210 | } |
| 211 | if (should_fail) { |
| 212 | errno = ENOMEM; |
| 213 | } |
| 214 | return should_fail; |
| 215 | } |
| 216 | |
David Benjamin | 5e356a8 | 2023-02-04 19:44:34 -0500 | [diff] [blame] | 217 | void OPENSSL_reset_malloc_counter_for_testing(void) { |
David Benjamin | 04c3d40 | 2023-06-03 01:26:29 -0400 | [diff] [blame] | 218 | CRYPTO_MUTEX_lock_write(&malloc_failure_lock); |
David Benjamin | 5e356a8 | 2023-02-04 19:44:34 -0500 | [diff] [blame] | 219 | current_malloc_count = 0; |
David Benjamin | 04c3d40 | 2023-06-03 01:26:29 -0400 | [diff] [blame] | 220 | CRYPTO_MUTEX_unlock_write(&malloc_failure_lock); |
David Benjamin | 5e356a8 | 2023-02-04 19:44:34 -0500 | [diff] [blame] | 221 | } |
| 222 | |
David Benjamin | 582904f | 2023-02-04 18:30:36 -0500 | [diff] [blame] | 223 | #else |
| 224 | static int should_fail_allocation(void) { return 0; } |
| 225 | #endif |
| 226 | |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 227 | void *OPENSSL_malloc(size_t size) { |
David Benjamin | 582904f | 2023-02-04 18:30:36 -0500 | [diff] [blame] | 228 | if (should_fail_allocation()) { |
Bob Beck | dcabfe2 | 2023-02-07 19:06:08 -0700 | [diff] [blame] | 229 | goto err; |
David Benjamin | 582904f | 2023-02-04 18:30:36 -0500 | [diff] [blame] | 230 | } |
| 231 | |
Adam Langley | 0313b59 | 2020-06-10 14:38:02 -0700 | [diff] [blame] | 232 | if (OPENSSL_memory_alloc != NULL) { |
| 233 | assert(OPENSSL_memory_free != NULL); |
| 234 | assert(OPENSSL_memory_get_size != NULL); |
Bob Beck | dcabfe2 | 2023-02-07 19:06:08 -0700 | [diff] [blame] | 235 | void *ptr = OPENSSL_memory_alloc(size); |
| 236 | if (ptr == NULL && size != 0) { |
| 237 | goto err; |
| 238 | } |
| 239 | return ptr; |
Adam Langley | 0313b59 | 2020-06-10 14:38:02 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Adam Langley | 7964a1d | 2020-02-05 15:23:07 -0800 | [diff] [blame] | 242 | if (size + OPENSSL_MALLOC_PREFIX < size) { |
Adam Langley | 89386ac | 2021-10-12 12:43:14 -0700 | [diff] [blame] | 243 | // |OPENSSL_malloc| is a central function in BoringSSL thus a reference to |
| 244 | // |kBoringSSLBinaryTag| is created here so that the tag isn't discarded by |
| 245 | // the linker. The following is sufficient to stop GCC, Clang, and MSVC |
| 246 | // optimising away the reference at the time of writing. Since this |
| 247 | // probably results in an actual memory reference, it is put in this very |
| 248 | // rare code path. |
| 249 | uint8_t unused = *(volatile uint8_t *)kBoringSSLBinaryTag; |
| 250 | (void) unused; |
Bob Beck | dcabfe2 | 2023-02-07 19:06:08 -0700 | [diff] [blame] | 251 | goto err; |
Adam Langley | 7964a1d | 2020-02-05 15:23:07 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 254 | void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 255 | if (ptr == NULL) { |
Bob Beck | dcabfe2 | 2023-02-07 19:06:08 -0700 | [diff] [blame] | 256 | goto err; |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | *(size_t *)ptr = size; |
| 260 | |
David Benjamin | da8bb84 | 2019-02-26 22:13:28 -0600 | [diff] [blame] | 261 | __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 262 | return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX; |
Bob Beck | dcabfe2 | 2023-02-07 19:06:08 -0700 | [diff] [blame] | 263 | |
| 264 | err: |
| 265 | // This only works because ERR does not call OPENSSL_malloc. |
| 266 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
| 267 | return NULL; |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void OPENSSL_free(void *orig_ptr) { |
| 271 | if (orig_ptr == NULL) { |
| 272 | return; |
| 273 | } |
| 274 | |
Adam Langley | 0313b59 | 2020-06-10 14:38:02 -0700 | [diff] [blame] | 275 | if (OPENSSL_memory_free != NULL) { |
| 276 | OPENSSL_memory_free(orig_ptr); |
| 277 | return; |
| 278 | } |
| 279 | |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 280 | void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
David Benjamin | da8bb84 | 2019-02-26 22:13:28 -0600 | [diff] [blame] | 281 | __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 282 | |
| 283 | size_t size = *(size_t *)ptr; |
| 284 | OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX); |
niewei | f94a7ce | 2022-03-16 10:02:19 +0800 | [diff] [blame] | 285 | |
| 286 | // ASan knows to intercept malloc and free, but not sdallocx. |
| 287 | #if defined(OPENSSL_ASAN) |
David Benjamin | 28883d4 | 2022-07-19 19:39:53 -0400 | [diff] [blame] | 288 | (void)sdallocx; |
niewei | f94a7ce | 2022-03-16 10:02:19 +0800 | [diff] [blame] | 289 | free(ptr); |
| 290 | #else |
Adam Langley | b49b78e | 2021-09-02 14:57:02 -0700 | [diff] [blame] | 291 | if (sdallocx) { |
| 292 | sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */); |
| 293 | } else { |
| 294 | free(ptr); |
| 295 | } |
niewei | f94a7ce | 2022-03-16 10:02:19 +0800 | [diff] [blame] | 296 | #endif |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void *OPENSSL_realloc(void *orig_ptr, size_t new_size) { |
| 300 | if (orig_ptr == NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 301 | return OPENSSL_malloc(new_size); |
| 302 | } |
| 303 | |
Adam Langley | 0313b59 | 2020-06-10 14:38:02 -0700 | [diff] [blame] | 304 | size_t old_size; |
| 305 | if (OPENSSL_memory_get_size != NULL) { |
| 306 | old_size = OPENSSL_memory_get_size(orig_ptr); |
| 307 | } else { |
| 308 | void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
| 309 | __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
| 310 | old_size = *(size_t *)ptr; |
| 311 | __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
| 312 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 313 | |
David Benjamin | 0ee3193 | 2016-07-11 19:38:56 -0400 | [diff] [blame] | 314 | void *ret = OPENSSL_malloc(new_size); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | if (ret == NULL) { |
| 316 | return NULL; |
| 317 | } |
| 318 | |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 319 | size_t to_copy = new_size; |
| 320 | if (old_size < to_copy) { |
| 321 | to_copy = old_size; |
| 322 | } |
| 323 | |
| 324 | memcpy(ret, orig_ptr, to_copy); |
| 325 | OPENSSL_free(orig_ptr); |
| 326 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 327 | return ret; |
| 328 | } |
| 329 | |
Adam Langley | ad1907f | 2014-07-30 11:55:17 -0700 | [diff] [blame] | 330 | void OPENSSL_cleanse(void *ptr, size_t len) { |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 331 | #if defined(OPENSSL_WINDOWS) |
David Benjamin | c3774c1 | 2015-12-30 21:37:50 -0500 | [diff] [blame] | 332 | SecureZeroMemory(ptr, len); |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 333 | #else |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 334 | OPENSSL_memset(ptr, 0, len); |
Adam Langley | ad1907f | 2014-07-30 11:55:17 -0700 | [diff] [blame] | 335 | |
Adam Langley | cf052cf | 2014-07-31 18:46:35 -0700 | [diff] [blame] | 336 | #if !defined(OPENSSL_NO_ASM) |
Adam Langley | ad1907f | 2014-07-30 11:55:17 -0700 | [diff] [blame] | 337 | /* As best as we can tell, this is sufficient to break any optimisations that |
| 338 | might try to eliminate "superfluous" memsets. If there's an easy way to |
| 339 | detect memset_s, it would be better to use that. */ |
Adam Langley | ad1907f | 2014-07-30 11:55:17 -0700 | [diff] [blame] | 340 | __asm__ __volatile__("" : : "r"(ptr) : "memory"); |
| 341 | #endif |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 342 | #endif // !OPENSSL_NO_ASM |
Adam Langley | ad1907f | 2014-07-30 11:55:17 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Bob Beck | 350f854 | 2023-02-07 16:11:58 -0700 | [diff] [blame] | 345 | void OPENSSL_clear_free(void *ptr, size_t unused) { OPENSSL_free(ptr); } |
Jeremy Apthorp | 1fa5abc | 2019-03-04 11:09:13 -0800 | [diff] [blame] | 346 | |
David Benjamin | 8a1542f | 2022-09-06 12:40:08 -0400 | [diff] [blame] | 347 | int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { return 0; } |
| 348 | |
| 349 | int CRYPTO_secure_malloc_initialized(void) { return 0; } |
| 350 | |
| 351 | size_t CRYPTO_secure_used(void) { return 0; } |
| 352 | |
| 353 | void *OPENSSL_secure_malloc(size_t size) { return OPENSSL_malloc(size); } |
| 354 | |
| 355 | void OPENSSL_secure_clear_free(void *ptr, size_t len) { |
| 356 | OPENSSL_clear_free(ptr, len); |
| 357 | } |
| 358 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 359 | int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 360 | const uint8_t *a = in_a; |
| 361 | const uint8_t *b = in_b; |
| 362 | uint8_t x = 0; |
| 363 | |
David Benjamin | 2e8ba2d | 2016-06-09 16:22:26 -0400 | [diff] [blame] | 364 | for (size_t i = 0; i < len; i++) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 365 | x |= a[i] ^ b[i]; |
| 366 | } |
| 367 | |
| 368 | return x; |
| 369 | } |
| 370 | |
| 371 | uint32_t OPENSSL_hash32(const void *ptr, size_t len) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 372 | // These are the FNV-1a parameters for 32 bits. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 373 | static const uint32_t kPrime = 16777619u; |
| 374 | static const uint32_t kOffsetBasis = 2166136261u; |
| 375 | |
| 376 | const uint8_t *in = ptr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 377 | uint32_t h = kOffsetBasis; |
| 378 | |
David Benjamin | 0ee3193 | 2016-07-11 19:38:56 -0400 | [diff] [blame] | 379 | for (size_t i = 0; i < len; i++) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 380 | h ^= in[i]; |
| 381 | h *= kPrime; |
| 382 | } |
| 383 | |
| 384 | return h; |
| 385 | } |
| 386 | |
David Benjamin | ec8c67d | 2021-06-21 17:10:53 -0400 | [diff] [blame] | 387 | uint32_t OPENSSL_strhash(const char *s) { return OPENSSL_hash32(s, strlen(s)); } |
| 388 | |
Adam Langley | 01797e3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 389 | size_t OPENSSL_strnlen(const char *s, size_t len) { |
David Benjamin | 0ee3193 | 2016-07-11 19:38:56 -0400 | [diff] [blame] | 390 | for (size_t i = 0; i < len; i++) { |
Adam Langley | 01797e3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 391 | if (s[i] == 0) { |
| 392 | return i; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | return len; |
| 397 | } |
| 398 | |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 399 | char *OPENSSL_strdup(const char *s) { |
David Benjamin | 3ba9586 | 2019-10-21 16:14:33 -0400 | [diff] [blame] | 400 | if (s == NULL) { |
| 401 | return NULL; |
| 402 | } |
Martin Kreichgauer | c0e15d1 | 2017-08-18 14:24:36 -0700 | [diff] [blame] | 403 | const size_t len = strlen(s) + 1; |
| 404 | char *ret = OPENSSL_malloc(len); |
| 405 | if (ret == NULL) { |
| 406 | return NULL; |
| 407 | } |
| 408 | OPENSSL_memcpy(ret, s, len); |
| 409 | return ret; |
| 410 | } |
Adam Langley | ccf8057 | 2017-07-25 14:49:30 -0700 | [diff] [blame] | 411 | |
Bob Beck | 00c70b8 | 2023-02-01 12:41:49 -0700 | [diff] [blame] | 412 | int OPENSSL_isalpha(int c) { |
| 413 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); |
| 414 | } |
| 415 | |
Bob Beck | 350f854 | 2023-02-07 16:11:58 -0700 | [diff] [blame] | 416 | int OPENSSL_isdigit(int c) { return c >= '0' && c <= '9'; } |
Bob Beck | f86a63c | 2023-01-30 12:17:39 -0700 | [diff] [blame] | 417 | |
Bob Beck | 00c70b8 | 2023-02-01 12:41:49 -0700 | [diff] [blame] | 418 | int OPENSSL_isxdigit(int c) { |
| 419 | return OPENSSL_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); |
| 420 | } |
| 421 | |
| 422 | int OPENSSL_fromxdigit(uint8_t *out, int c) { |
| 423 | if (OPENSSL_isdigit(c)) { |
| 424 | *out = c - '0'; |
| 425 | return 1; |
| 426 | } |
| 427 | if ('a' <= c && c <= 'f') { |
| 428 | *out = c - 'a' + 10; |
| 429 | return 1; |
| 430 | } |
| 431 | if ('A' <= c && c <= 'F') { |
| 432 | *out = c - 'A' + 10; |
| 433 | return 1; |
| 434 | } |
| 435 | return 0; |
| 436 | } |
| 437 | |
Bob Beck | 350f854 | 2023-02-07 16:11:58 -0700 | [diff] [blame] | 438 | int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); } |
Bob Beck | 00c70b8 | 2023-02-01 12:41:49 -0700 | [diff] [blame] | 439 | |
Adam Langley | ccf8057 | 2017-07-25 14:49:30 -0700 | [diff] [blame] | 440 | int OPENSSL_tolower(int c) { |
| 441 | if (c >= 'A' && c <= 'Z') { |
| 442 | return c + ('a' - 'A'); |
| 443 | } |
| 444 | return c; |
| 445 | } |
| 446 | |
David Benjamin | 42b7b35 | 2023-01-27 21:02:34 -0500 | [diff] [blame] | 447 | int OPENSSL_isspace(int c) { |
| 448 | return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || |
| 449 | c == ' '; |
| 450 | } |
| 451 | |
Adam Langley | b0d5fb6 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 452 | int OPENSSL_strcasecmp(const char *a, const char *b) { |
Adam Langley | ccf8057 | 2017-07-25 14:49:30 -0700 | [diff] [blame] | 453 | for (size_t i = 0;; i++) { |
| 454 | const int aa = OPENSSL_tolower(a[i]); |
| 455 | const int bb = OPENSSL_tolower(b[i]); |
| 456 | |
| 457 | if (aa < bb) { |
| 458 | return -1; |
| 459 | } else if (aa > bb) { |
| 460 | return 1; |
| 461 | } else if (aa == 0) { |
| 462 | return 0; |
| 463 | } |
| 464 | } |
Adam Langley | b0d5fb6 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) { |
Adam Langley | ccf8057 | 2017-07-25 14:49:30 -0700 | [diff] [blame] | 468 | for (size_t i = 0; i < n; i++) { |
| 469 | const int aa = OPENSSL_tolower(a[i]); |
| 470 | const int bb = OPENSSL_tolower(b[i]); |
Adam Langley | b0d5fb6 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 471 | |
Adam Langley | ccf8057 | 2017-07-25 14:49:30 -0700 | [diff] [blame] | 472 | if (aa < bb) { |
| 473 | return -1; |
| 474 | } else if (aa > bb) { |
| 475 | return 1; |
| 476 | } else if (aa == 0) { |
| 477 | return 0; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | } |
Adam Langley | b0d5fb6 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 483 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 484 | int BIO_snprintf(char *buf, size_t n, const char *format, ...) { |
| 485 | va_list args; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 486 | va_start(args, format); |
David Benjamin | 0ee3193 | 2016-07-11 19:38:56 -0400 | [diff] [blame] | 487 | int ret = BIO_vsnprintf(buf, n, format, args); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 488 | va_end(args); |
| 489 | return ret; |
| 490 | } |
| 491 | |
| 492 | int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) { |
| 493 | return vsnprintf(buf, n, format, args); |
| 494 | } |
David Benjamin | 3ba9586 | 2019-10-21 16:14:33 -0400 | [diff] [blame] | 495 | |
Bob Beck | 350f854 | 2023-02-07 16:11:58 -0700 | [diff] [blame] | 496 | int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args, |
| 497 | int system_malloc) { |
| 498 | void *(*allocate)(size_t) = system_malloc ? malloc : OPENSSL_malloc; |
| 499 | void (*deallocate)(void *) = system_malloc ? free : OPENSSL_free; |
| 500 | void *(*reallocate)(void *, size_t) = |
| 501 | system_malloc ? realloc : OPENSSL_realloc; |
| 502 | char *candidate = NULL; |
| 503 | size_t candidate_len = 64; // TODO(bbe) what's the best initial size? |
| 504 | |
| 505 | if ((candidate = allocate(candidate_len)) == NULL) { |
| 506 | goto err; |
| 507 | } |
| 508 | va_list args_copy; |
| 509 | va_copy(args_copy, args); |
| 510 | int ret = vsnprintf(candidate, candidate_len, format, args_copy); |
| 511 | va_end(args_copy); |
Bob Beck | dcabfe2 | 2023-02-07 19:06:08 -0700 | [diff] [blame] | 512 | if (ret < 0) { |
Bob Beck | 350f854 | 2023-02-07 16:11:58 -0700 | [diff] [blame] | 513 | goto err; |
| 514 | } |
| 515 | if ((size_t)ret >= candidate_len) { |
| 516 | // Too big to fit in allocation. |
| 517 | char *tmp; |
| 518 | |
Bob Beck | dcabfe2 | 2023-02-07 19:06:08 -0700 | [diff] [blame] | 519 | candidate_len = (size_t)ret + 1; |
Bob Beck | 350f854 | 2023-02-07 16:11:58 -0700 | [diff] [blame] | 520 | if ((tmp = reallocate(candidate, candidate_len)) == NULL) { |
| 521 | goto err; |
| 522 | } |
| 523 | candidate = tmp; |
Bob Beck | dcabfe2 | 2023-02-07 19:06:08 -0700 | [diff] [blame] | 524 | ret = vsnprintf(candidate, candidate_len, format, args); |
Bob Beck | 350f854 | 2023-02-07 16:11:58 -0700 | [diff] [blame] | 525 | } |
Bob Beck | dcabfe2 | 2023-02-07 19:06:08 -0700 | [diff] [blame] | 526 | // At this point this should not happen unless vsnprintf is insane. |
Bob Beck | 350f854 | 2023-02-07 16:11:58 -0700 | [diff] [blame] | 527 | if (ret < 0 || (size_t)ret >= candidate_len) { |
| 528 | goto err; |
| 529 | } |
| 530 | *str = candidate; |
| 531 | return ret; |
| 532 | |
| 533 | err: |
| 534 | deallocate(candidate); |
| 535 | *str = NULL; |
| 536 | errno = ENOMEM; |
| 537 | return -1; |
| 538 | } |
| 539 | |
| 540 | int OPENSSL_vasprintf(char **str, const char *format, va_list args) { |
| 541 | return OPENSSL_vasprintf_internal(str, format, args, /*system_malloc=*/0); |
| 542 | } |
| 543 | |
| 544 | int OPENSSL_asprintf(char **str, const char *format, ...) { |
| 545 | va_list args; |
| 546 | va_start(args, format); |
| 547 | int ret = OPENSSL_vasprintf(str, format, args); |
| 548 | va_end(args); |
| 549 | return ret; |
| 550 | } |
| 551 | |
David Benjamin | 3ba9586 | 2019-10-21 16:14:33 -0400 | [diff] [blame] | 552 | char *OPENSSL_strndup(const char *str, size_t size) { |
David Benjamin | 3ba9586 | 2019-10-21 16:14:33 -0400 | [diff] [blame] | 553 | size = OPENSSL_strnlen(str, size); |
| 554 | |
David Benjamin | 5984cfe | 2021-08-24 16:03:34 -0400 | [diff] [blame] | 555 | size_t alloc_size = size + 1; |
David Benjamin | 3ba9586 | 2019-10-21 16:14:33 -0400 | [diff] [blame] | 556 | if (alloc_size < size) { |
| 557 | // overflow |
| 558 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
| 559 | return NULL; |
| 560 | } |
David Benjamin | 5984cfe | 2021-08-24 16:03:34 -0400 | [diff] [blame] | 561 | char *ret = OPENSSL_malloc(alloc_size); |
David Benjamin | 3ba9586 | 2019-10-21 16:14:33 -0400 | [diff] [blame] | 562 | if (ret == NULL) { |
David Benjamin | 3ba9586 | 2019-10-21 16:14:33 -0400 | [diff] [blame] | 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | OPENSSL_memcpy(ret, str, size); |
| 567 | ret[size] = '\0'; |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) { |
| 572 | size_t l = 0; |
| 573 | |
| 574 | for (; dst_size > 1 && *src; dst_size--) { |
| 575 | *dst++ = *src++; |
| 576 | l++; |
| 577 | } |
| 578 | |
| 579 | if (dst_size) { |
| 580 | *dst = 0; |
| 581 | } |
| 582 | |
| 583 | return l + strlen(src); |
| 584 | } |
| 585 | |
| 586 | size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) { |
| 587 | size_t l = 0; |
| 588 | for (; dst_size > 0 && *dst; dst_size--, dst++) { |
| 589 | l++; |
| 590 | } |
| 591 | return l + OPENSSL_strlcpy(dst, src, dst_size); |
| 592 | } |
| 593 | |
| 594 | void *OPENSSL_memdup(const void *data, size_t size) { |
| 595 | if (size == 0) { |
| 596 | return NULL; |
| 597 | } |
| 598 | |
| 599 | void *ret = OPENSSL_malloc(size); |
| 600 | if (ret == NULL) { |
David Benjamin | 3ba9586 | 2019-10-21 16:14:33 -0400 | [diff] [blame] | 601 | return NULL; |
| 602 | } |
| 603 | |
| 604 | OPENSSL_memcpy(ret, data, size); |
| 605 | return ret; |
| 606 | } |
David Benjamin | 551ccd7 | 2021-09-28 11:55:10 -0400 | [diff] [blame] | 607 | |
| 608 | void *CRYPTO_malloc(size_t size, const char *file, int line) { |
| 609 | return OPENSSL_malloc(size); |
| 610 | } |
| 611 | |
| 612 | void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) { |
| 613 | return OPENSSL_realloc(ptr, new_size); |
| 614 | } |
| 615 | |
| 616 | void CRYPTO_free(void *ptr, const char *file, int line) { OPENSSL_free(ptr); } |