1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
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
57 #include <openssl/mem.h>
58
59 #include <assert.h>
60 #include <stdarg.h>
61 #include <stdio.h>
62
63 #include <openssl/err.h>
64
65 #if defined(OPENSSL_WINDOWS)
66 OPENSSL_MSVC_PRAGMA(warning(push, 3))
67 #include <windows.h>
68 OPENSSL_MSVC_PRAGMA(warning(pop))
69 #endif
70
71 #include "internal.h"
72
73
74 #define OPENSSL_MALLOC_PREFIX 8
75
76 #if defined(OPENSSL_ASAN)
77 void __asan_poison_memory_region(const volatile void *addr, size_t size);
78 void __asan_unpoison_memory_region(const volatile void *addr, size_t size);
79 #else
80 static void __asan_poison_memory_region(const void *addr, size_t size) {}
81 static void __asan_unpoison_memory_region(const void *addr, size_t size) {}
82 #endif
83
84 // Windows doesn't really support weak symbols as of May 2019, and Clang on
85 // Windows will emit strong symbols instead. See
86 // https://bugs.llvm.org/show_bug.cgi?id=37598
87 #if defined(__ELF__) && defined(__GNUC__)
88 #define WEAK_SYMBOL_FUNC(rettype, name, args) \
89 rettype name args __attribute__((weak));
90 #else
91 #define WEAK_SYMBOL_FUNC(rettype, name, args) static rettype(*name) args = NULL;
92 #endif
93
94 // sdallocx is a sized |free| function. By passing the size (which we happen to
95 // always know in BoringSSL), the malloc implementation can save work. We cannot
96 // depend on |sdallocx| being available, however, so it's a weak symbol.
97 //
98 // This will always be safe, but will only be overridden if the malloc
99 // implementation is statically linked with BoringSSL. So, if |sdallocx| is
100 // provided in, say, libc.so, we still won't use it because that's dynamically
101 // linked. This isn't an ideal result, but its helps in some cases.
102 WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags));
103
104 // The following two functions are for memory tracking. They are no-ops by
105 // default but can be overridden at link time if the application needs to
106 // observe heap operations.
107 WEAK_SYMBOL_FUNC(void, OPENSSL_track_memory_alloc, (void *ptr, size_t size));
108 WEAK_SYMBOL_FUNC(void, OPENSSL_track_memory_free, (void *ptr, size_t size));
109
OPENSSL_malloc(size_t size)110 void *OPENSSL_malloc(size_t size) {
111 if (size + OPENSSL_MALLOC_PREFIX < size) {
112 return NULL;
113 }
114
115 void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX);
116 if (ptr == NULL) {
117 return NULL;
118 }
119
120 *(size_t *)ptr = size;
121
122 __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
123 if (OPENSSL_track_memory_alloc) {
124 OPENSSL_track_memory_alloc(ptr, size + OPENSSL_MALLOC_PREFIX);
125 }
126 return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX;
127 }
128
OPENSSL_free(void * orig_ptr)129 void OPENSSL_free(void *orig_ptr) {
130 if (orig_ptr == NULL) {
131 return;
132 }
133
134 void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
135 __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
136
137 size_t size = *(size_t *)ptr;
138 if (OPENSSL_track_memory_free) {
139 OPENSSL_track_memory_free(ptr, size + OPENSSL_MALLOC_PREFIX);
140 }
141 OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX);
142 if (sdallocx) {
143 sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */);
144 } else {
145 free(ptr);
146 }
147 }
148
OPENSSL_realloc(void * orig_ptr,size_t new_size)149 void *OPENSSL_realloc(void *orig_ptr, size_t new_size) {
150 if (orig_ptr == NULL) {
151 return OPENSSL_malloc(new_size);
152 }
153
154 void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
155 __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
156 size_t old_size = *(size_t *)ptr;
157 __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
158
159 void *ret = OPENSSL_malloc(new_size);
160 if (ret == NULL) {
161 return NULL;
162 }
163
164 size_t to_copy = new_size;
165 if (old_size < to_copy) {
166 to_copy = old_size;
167 }
168
169 memcpy(ret, orig_ptr, to_copy);
170 OPENSSL_free(orig_ptr);
171
172 return ret;
173 }
174
OPENSSL_cleanse(void * ptr,size_t len)175 void OPENSSL_cleanse(void *ptr, size_t len) {
176 #if defined(OPENSSL_WINDOWS)
177 SecureZeroMemory(ptr, len);
178 #else
179 OPENSSL_memset(ptr, 0, len);
180
181 #if !defined(OPENSSL_NO_ASM)
182 /* As best as we can tell, this is sufficient to break any optimisations that
183 might try to eliminate "superfluous" memsets. If there's an easy way to
184 detect memset_s, it would be better to use that. */
185 __asm__ __volatile__("" : : "r"(ptr) : "memory");
186 #endif
187 #endif // !OPENSSL_NO_ASM
188 }
189
OPENSSL_clear_free(void * ptr,size_t unused)190 void OPENSSL_clear_free(void *ptr, size_t unused) {
191 OPENSSL_free(ptr);
192 }
193
CRYPTO_memcmp(const void * in_a,const void * in_b,size_t len)194 int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) {
195 const uint8_t *a = in_a;
196 const uint8_t *b = in_b;
197 uint8_t x = 0;
198
199 for (size_t i = 0; i < len; i++) {
200 x |= a[i] ^ b[i];
201 }
202
203 return x;
204 }
205
OPENSSL_hash32(const void * ptr,size_t len)206 uint32_t OPENSSL_hash32(const void *ptr, size_t len) {
207 // These are the FNV-1a parameters for 32 bits.
208 static const uint32_t kPrime = 16777619u;
209 static const uint32_t kOffsetBasis = 2166136261u;
210
211 const uint8_t *in = ptr;
212 uint32_t h = kOffsetBasis;
213
214 for (size_t i = 0; i < len; i++) {
215 h ^= in[i];
216 h *= kPrime;
217 }
218
219 return h;
220 }
221
OPENSSL_strnlen(const char * s,size_t len)222 size_t OPENSSL_strnlen(const char *s, size_t len) {
223 for (size_t i = 0; i < len; i++) {
224 if (s[i] == 0) {
225 return i;
226 }
227 }
228
229 return len;
230 }
231
OPENSSL_strdup(const char * s)232 char *OPENSSL_strdup(const char *s) {
233 if (s == NULL) {
234 return NULL;
235 }
236 const size_t len = strlen(s) + 1;
237 char *ret = OPENSSL_malloc(len);
238 if (ret == NULL) {
239 return NULL;
240 }
241 OPENSSL_memcpy(ret, s, len);
242 return ret;
243 }
244
OPENSSL_tolower(int c)245 int OPENSSL_tolower(int c) {
246 if (c >= 'A' && c <= 'Z') {
247 return c + ('a' - 'A');
248 }
249 return c;
250 }
251
OPENSSL_strcasecmp(const char * a,const char * b)252 int OPENSSL_strcasecmp(const char *a, const char *b) {
253 for (size_t i = 0;; i++) {
254 const int aa = OPENSSL_tolower(a[i]);
255 const int bb = OPENSSL_tolower(b[i]);
256
257 if (aa < bb) {
258 return -1;
259 } else if (aa > bb) {
260 return 1;
261 } else if (aa == 0) {
262 return 0;
263 }
264 }
265 }
266
OPENSSL_strncasecmp(const char * a,const char * b,size_t n)267 int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) {
268 for (size_t i = 0; i < n; i++) {
269 const int aa = OPENSSL_tolower(a[i]);
270 const int bb = OPENSSL_tolower(b[i]);
271
272 if (aa < bb) {
273 return -1;
274 } else if (aa > bb) {
275 return 1;
276 } else if (aa == 0) {
277 return 0;
278 }
279 }
280
281 return 0;
282 }
283
BIO_snprintf(char * buf,size_t n,const char * format,...)284 int BIO_snprintf(char *buf, size_t n, const char *format, ...) {
285 va_list args;
286 va_start(args, format);
287 int ret = BIO_vsnprintf(buf, n, format, args);
288 va_end(args);
289 return ret;
290 }
291
BIO_vsnprintf(char * buf,size_t n,const char * format,va_list args)292 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) {
293 return vsnprintf(buf, n, format, args);
294 }
295
OPENSSL_strndup(const char * str,size_t size)296 char *OPENSSL_strndup(const char *str, size_t size) {
297 char *ret;
298 size_t alloc_size;
299
300 if (str == NULL) {
301 return NULL;
302 }
303
304 size = OPENSSL_strnlen(str, size);
305
306 alloc_size = size + 1;
307 if (alloc_size < size) {
308 // overflow
309 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
310 return NULL;
311 }
312 ret = OPENSSL_malloc(alloc_size);
313 if (ret == NULL) {
314 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
315 return NULL;
316 }
317
318 OPENSSL_memcpy(ret, str, size);
319 ret[size] = '\0';
320 return ret;
321 }
322
OPENSSL_strlcpy(char * dst,const char * src,size_t dst_size)323 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) {
324 size_t l = 0;
325
326 for (; dst_size > 1 && *src; dst_size--) {
327 *dst++ = *src++;
328 l++;
329 }
330
331 if (dst_size) {
332 *dst = 0;
333 }
334
335 return l + strlen(src);
336 }
337
OPENSSL_strlcat(char * dst,const char * src,size_t dst_size)338 size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) {
339 size_t l = 0;
340 for (; dst_size > 0 && *dst; dst_size--, dst++) {
341 l++;
342 }
343 return l + OPENSSL_strlcpy(dst, src, dst_size);
344 }
345
OPENSSL_memdup(const void * data,size_t size)346 void *OPENSSL_memdup(const void *data, size_t size) {
347 if (size == 0) {
348 return NULL;
349 }
350
351 void *ret = OPENSSL_malloc(size);
352 if (ret == NULL) {
353 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
354 return NULL;
355 }
356
357 OPENSSL_memcpy(ret, data, size);
358 return ret;
359 }
360