Coverage Report

Created: 2025-06-11 06:40

/src/boringssl/crypto/mem.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/mem.h>
16
17
#include <assert.h>
18
#include <errno.h>
19
#include <limits.h>
20
#include <stdarg.h>
21
#include <stdio.h>
22
#include <stdlib.h>
23
24
#include <openssl/err.h>
25
26
#if defined(OPENSSL_WINDOWS)
27
#include <windows.h>
28
#endif
29
30
#if defined(BORINGSSL_MALLOC_FAILURE_TESTING)
31
#include <errno.h>
32
#include <signal.h>
33
#include <unistd.h>
34
#endif
35
36
#include "internal.h"
37
38
39
3.37M
#define OPENSSL_MALLOC_PREFIX 8
40
static_assert(OPENSSL_MALLOC_PREFIX >= sizeof(size_t), "size_t too large");
41
42
#if defined(OPENSSL_ASAN)
43
extern "C" {
44
void __asan_poison_memory_region(const volatile void *addr, size_t size);
45
void __asan_unpoison_memory_region(const volatile void *addr, size_t size);
46
}
47
#else
48
484k
static void __asan_poison_memory_region(const void *addr, size_t size) {}
49
484k
static void __asan_unpoison_memory_region(const void *addr, size_t size) {}
50
#endif
51
52
// Windows doesn't really support weak symbols as of May 2019, and Clang on
53
// Windows will emit strong symbols instead. See
54
// https://bugs.llvm.org/show_bug.cgi?id=37598
55
//
56
// EDK2 targets UEFI but builds as ELF and then translates the binary to
57
// COFF(!). Thus it builds with __ELF__ defined but cannot actually cope with
58
// weak symbols.
59
#if !defined(__EDK2_BORINGSSL__) && defined(__ELF__) && defined(__GNUC__)
60
#define WEAK_SYMBOL_FUNC(rettype, name, args) \
61
  extern "C" {                                \
62
  rettype name args __attribute__((weak));    \
63
  }
64
#else
65
#define WEAK_SYMBOL_FUNC(rettype, name, args) \
66
  static rettype(*const name) args = NULL;
67
#endif
68
69
#if defined(BORINGSSL_DETECT_SDALLOCX)
70
// sdallocx is a sized |free| function. By passing the size (which we happen to
71
// always know in BoringSSL), the malloc implementation can save work. We cannot
72
// depend on |sdallocx| being available, however, so it's a weak symbol.
73
//
74
// This mechanism is kept opt-in because it assumes that, when |sdallocx| is
75
// defined, it is part of the same allocator as |malloc|. This is usually true
76
// but may break if |malloc| does not implement |sdallocx|, but some other
77
// allocator with |sdallocx| is imported which does.
78
WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags))
79
#else
80
static void (*const sdallocx)(void *ptr, size_t size, int flags) = NULL;
81
#endif
82
83
// The following three functions can be defined to override default heap
84
// allocation and freeing. If defined, it is the responsibility of
85
// |OPENSSL_memory_free| to zero out the memory before returning it to the
86
// system. |OPENSSL_memory_free| will not be passed NULL pointers.
87
//
88
// WARNING: These functions are called on every allocation and free in
89
// BoringSSL across the entire process. They may be called by any code in the
90
// process which calls BoringSSL, including in process initializers and thread
91
// destructors. When called, BoringSSL may hold pthreads locks. Any other code
92
// in the process which, directly or indirectly, calls BoringSSL may be on the
93
// call stack and may itself be using arbitrary synchronization primitives.
94
//
95
// As a result, these functions may not have the usual programming environment
96
// available to most C or C++ code. In particular, they may not call into
97
// BoringSSL, or any library which depends on BoringSSL. Any synchronization
98
// primitives used must tolerate every other synchronization primitive linked
99
// into the process, including pthreads locks. Failing to meet these constraints
100
// may result in deadlocks, crashes, or memory corruption.
101
WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_alloc, (size_t size))
102
WEAK_SYMBOL_FUNC(void, OPENSSL_memory_free, (void *ptr))
103
WEAK_SYMBOL_FUNC(size_t, OPENSSL_memory_get_size, (void *ptr))
104
105
#if defined(BORINGSSL_MALLOC_FAILURE_TESTING)
106
static CRYPTO_MUTEX malloc_failure_lock = CRYPTO_MUTEX_INIT;
107
static uint64_t current_malloc_count = 0;
108
static uint64_t malloc_number_to_fail = 0;
109
static int malloc_failure_enabled = 0, break_on_malloc_fail = 0,
110
           any_malloc_failed = 0, disable_malloc_failures = 0;
111
112
static void malloc_exit_handler(void) {
113
  CRYPTO_MUTEX_lock_read(&malloc_failure_lock);
114
  if (any_malloc_failed) {
115
    // Signal to the test driver that some allocation failed, so it knows to
116
    // increment the counter and continue.
117
    _exit(88);
118
  }
119
  CRYPTO_MUTEX_unlock_read(&malloc_failure_lock);
120
}
121
122
static void init_malloc_failure(void) {
123
  const char *env = getenv("MALLOC_NUMBER_TO_FAIL");
124
  if (env != NULL && env[0] != 0) {
125
    char *endptr;
126
    malloc_number_to_fail = strtoull(env, &endptr, 10);
127
    if (*endptr == 0) {
128
      malloc_failure_enabled = 1;
129
      atexit(malloc_exit_handler);
130
    }
131
  }
132
  break_on_malloc_fail = getenv("MALLOC_BREAK_ON_FAIL") != NULL;
133
}
134
135
// should_fail_allocation returns one if the current allocation should fail and
136
// zero otherwise.
137
static int should_fail_allocation() {
138
  static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
139
  CRYPTO_once(&once, init_malloc_failure);
140
  if (!malloc_failure_enabled || disable_malloc_failures) {
141
    return 0;
142
  }
143
144
  // We lock just so multi-threaded tests are still correct, but we won't test
145
  // every malloc exhaustively.
146
  CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
147
  int should_fail = current_malloc_count == malloc_number_to_fail;
148
  current_malloc_count++;
149
  any_malloc_failed = any_malloc_failed || should_fail;
150
  CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
151
152
  if (should_fail && break_on_malloc_fail) {
153
    raise(SIGTRAP);
154
  }
155
  if (should_fail) {
156
    errno = ENOMEM;
157
  }
158
  return should_fail;
159
}
160
161
void OPENSSL_reset_malloc_counter_for_testing(void) {
162
  CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
163
  current_malloc_count = 0;
164
  CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
165
}
166
167
void OPENSSL_disable_malloc_failures_for_testing(void) {
168
  CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
169
  BSSL_CHECK(!disable_malloc_failures);
170
  disable_malloc_failures = 1;
171
  CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
172
}
173
174
void OPENSSL_enable_malloc_failures_for_testing(void) {
175
  CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
176
  BSSL_CHECK(disable_malloc_failures);
177
  disable_malloc_failures = 0;
178
  CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
179
}
180
181
#else
182
479k
static int should_fail_allocation(void) { return 0; }
183
#endif
184
185
479k
void *OPENSSL_malloc(size_t size) {
186
479k
  void *ptr = nullptr;
187
479k
  if (should_fail_allocation()) {
188
0
    goto err;
189
0
  }
190
191
479k
  if (OPENSSL_memory_alloc != NULL) {
192
0
    assert(OPENSSL_memory_free != NULL);
193
0
    assert(OPENSSL_memory_get_size != NULL);
194
0
    void *ptr2 = OPENSSL_memory_alloc(size);
195
0
    if (ptr2 == NULL && size != 0) {
196
0
      goto err;
197
0
    }
198
0
    return ptr2;
199
0
  }
200
201
479k
  if (size + OPENSSL_MALLOC_PREFIX < size) {
202
0
    goto err;
203
0
  }
204
205
479k
  ptr = malloc(size + OPENSSL_MALLOC_PREFIX);
206
479k
  if (ptr == NULL) {
207
0
    goto err;
208
0
  }
209
210
479k
  *(size_t *)ptr = size;
211
212
479k
  __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
213
479k
  return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX;
214
215
0
err:
216
  // This only works because ERR does not call OPENSSL_malloc.
217
0
  OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
218
0
  return NULL;
219
479k
}
220
221
214k
void *OPENSSL_zalloc(size_t size) {
222
214k
  void *ret = OPENSSL_malloc(size);
223
214k
  if (ret != NULL) {
224
214k
    OPENSSL_memset(ret, 0, size);
225
214k
  }
226
214k
  return ret;
227
214k
}
228
229
41.0k
void *OPENSSL_calloc(size_t num, size_t size) {
230
41.0k
  if (size != 0 && num > SIZE_MAX / size) {
231
0
    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
232
0
    return NULL;
233
0
  }
234
235
41.0k
  return OPENSSL_zalloc(num * size);
236
41.0k
}
237
238
610k
void OPENSSL_free(void *orig_ptr) {
239
610k
  if (orig_ptr == NULL) {
240
131k
    return;
241
131k
  }
242
243
479k
  if (OPENSSL_memory_free != NULL) {
244
0
    OPENSSL_memory_free(orig_ptr);
245
0
    return;
246
0
  }
247
248
479k
  void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
249
479k
  __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
250
251
479k
  size_t size = *(size_t *)ptr;
252
479k
  OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX);
253
254
// ASan knows to intercept malloc and free, but not sdallocx.
255
#if defined(OPENSSL_ASAN)
256
  (void)sdallocx;
257
  free(ptr);
258
#else
259
479k
  if (sdallocx) {
260
0
    sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */);
261
479k
  } else {
262
479k
    free(ptr);
263
479k
  }
264
479k
#endif
265
479k
}
266
267
7.46k
void *OPENSSL_realloc(void *orig_ptr, size_t new_size) {
268
7.46k
  if (orig_ptr == NULL) {
269
2.50k
    return OPENSSL_malloc(new_size);
270
2.50k
  }
271
272
4.96k
  size_t old_size;
273
4.96k
  if (OPENSSL_memory_get_size != NULL) {
274
0
    old_size = OPENSSL_memory_get_size(orig_ptr);
275
4.96k
  } else {
276
4.96k
    void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
277
4.96k
    __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
278
4.96k
    old_size = *(size_t *)ptr;
279
4.96k
    __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
280
4.96k
  }
281
282
4.96k
  void *ret = OPENSSL_malloc(new_size);
283
4.96k
  if (ret == NULL) {
284
0
    return NULL;
285
0
  }
286
287
4.96k
  size_t to_copy = new_size;
288
4.96k
  if (old_size < to_copy) {
289
4.96k
    to_copy = old_size;
290
4.96k
  }
291
292
4.96k
  memcpy(ret, orig_ptr, to_copy);
293
4.96k
  OPENSSL_free(orig_ptr);
294
295
4.96k
  return ret;
296
4.96k
}
297
298
479k
void OPENSSL_cleanse(void *ptr, size_t len) {
299
#if defined(OPENSSL_WINDOWS)
300
  SecureZeroMemory(ptr, len);
301
#else
302
479k
  OPENSSL_memset(ptr, 0, len);
303
304
479k
#if !defined(OPENSSL_NO_ASM)
305
  /* As best as we can tell, this is sufficient to break any optimisations that
306
     might try to eliminate "superfluous" memsets. If there's an easy way to
307
     detect memset_s, it would be better to use that. */
308
479k
  __asm__ __volatile__("" : : "r"(ptr) : "memory");
309
479k
#endif
310
479k
#endif  // !OPENSSL_NO_ASM
311
479k
}
312
313
0
void OPENSSL_clear_free(void *ptr, size_t unused) { OPENSSL_free(ptr); }
314
315
0
int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { return 0; }
316
317
0
int CRYPTO_secure_malloc_initialized(void) { return 0; }
318
319
0
size_t CRYPTO_secure_used(void) { return 0; }
320
321
0
void *OPENSSL_secure_malloc(size_t size) { return OPENSSL_malloc(size); }
322
323
0
void OPENSSL_secure_clear_free(void *ptr, size_t len) {
324
0
  OPENSSL_clear_free(ptr, len);
325
0
}
326
327
0
int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) {
328
0
  const uint8_t *a = reinterpret_cast<const uint8_t *>(in_a);
329
0
  const uint8_t *b = reinterpret_cast<const uint8_t *>(in_b);
330
0
  uint8_t x = 0;
331
332
0
  for (size_t i = 0; i < len; i++) {
333
0
    x |= a[i] ^ b[i];
334
0
  }
335
336
0
  return x;
337
0
}
338
339
0
uint32_t OPENSSL_hash32(const void *ptr, size_t len) {
340
  // These are the FNV-1a parameters for 32 bits.
341
0
  static const uint32_t kPrime = 16777619u;
342
0
  static const uint32_t kOffsetBasis = 2166136261u;
343
344
0
  const uint8_t *in = reinterpret_cast<const uint8_t *>(ptr);
345
0
  uint32_t h = kOffsetBasis;
346
347
0
  for (size_t i = 0; i < len; i++) {
348
0
    h ^= in[i];
349
0
    h *= kPrime;
350
0
  }
351
352
0
  return h;
353
0
}
354
355
0
uint32_t OPENSSL_strhash(const char *s) { return OPENSSL_hash32(s, strlen(s)); }
356
357
46
size_t OPENSSL_strnlen(const char *s, size_t len) {
358
2.18k
  for (size_t i = 0; i < len; i++) {
359
2.13k
    if (s[i] == 0) {
360
0
      return i;
361
0
    }
362
2.13k
  }
363
364
46
  return len;
365
46
}
366
367
0
char *OPENSSL_strdup(const char *s) {
368
0
  if (s == NULL) {
369
0
    return NULL;
370
0
  }
371
  // Copy the NUL terminator.
372
0
  return reinterpret_cast<char *>(OPENSSL_memdup(s, strlen(s) + 1));
373
0
}
374
375
8
int OPENSSL_isalpha(int c) {
376
8
  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
377
8
}
378
379
28.9k
int OPENSSL_isdigit(int c) { return c >= '0' && c <= '9'; }
380
381
0
int OPENSSL_isxdigit(int c) {
382
0
  return OPENSSL_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
383
0
}
384
385
0
int OPENSSL_fromxdigit(uint8_t *out, int c) {
386
0
  if (OPENSSL_isdigit(c)) {
387
0
    *out = c - '0';
388
0
    return 1;
389
0
  }
390
0
  if ('a' <= c && c <= 'f') {
391
0
    *out = c - 'a' + 10;
392
0
    return 1;
393
0
  }
394
0
  if ('A' <= c && c <= 'F') {
395
0
    *out = c - 'A' + 10;
396
0
    return 1;
397
0
  }
398
0
  return 0;
399
0
}
400
401
8
int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); }
402
403
55.5k
int OPENSSL_tolower(int c) {
404
55.5k
  if (c >= 'A' && c <= 'Z') {
405
6.59k
    return c + ('a' - 'A');
406
6.59k
  }
407
48.9k
  return c;
408
55.5k
}
409
410
74.2k
int OPENSSL_isspace(int c) {
411
74.2k
  return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' ||
412
74.2k
         c == ' ';
413
74.2k
}
414
415
0
int OPENSSL_strcasecmp(const char *a, const char *b) {
416
0
  for (size_t i = 0;; i++) {
417
0
    const int aa = OPENSSL_tolower(a[i]);
418
0
    const int bb = OPENSSL_tolower(b[i]);
419
420
0
    if (aa < bb) {
421
0
      return -1;
422
0
    } else if (aa > bb) {
423
0
      return 1;
424
0
    } else if (aa == 0) {
425
0
      return 0;
426
0
    }
427
0
  }
428
0
}
429
430
0
int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) {
431
0
  for (size_t i = 0; i < n; i++) {
432
0
    const int aa = OPENSSL_tolower(a[i]);
433
0
    const int bb = OPENSSL_tolower(b[i]);
434
435
0
    if (aa < bb) {
436
0
      return -1;
437
0
    } else if (aa > bb) {
438
0
      return 1;
439
0
    } else if (aa == 0) {
440
0
      return 0;
441
0
    }
442
0
  }
443
444
0
  return 0;
445
0
}
446
447
0
int BIO_snprintf(char *buf, size_t n, const char *format, ...) {
448
0
  va_list args;
449
0
  va_start(args, format);
450
0
  int ret = BIO_vsnprintf(buf, n, format, args);
451
0
  va_end(args);
452
0
  return ret;
453
0
}
454
455
0
int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) {
456
0
  return vsnprintf(buf, n, format, args);
457
0
}
458
459
int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args,
460
0
                               int system_malloc) {
461
0
  void *(*allocate)(size_t) = system_malloc ? malloc : OPENSSL_malloc;
462
0
  void (*deallocate)(void *) = system_malloc ? free : OPENSSL_free;
463
0
  void *(*reallocate)(void *, size_t) =
464
0
      system_malloc ? realloc : OPENSSL_realloc;
465
0
  char *candidate = NULL;
466
0
  size_t candidate_len = 64;  // TODO(bbe) what's the best initial size?
467
0
  int ret;
468
469
0
  if ((candidate = reinterpret_cast<char *>(allocate(candidate_len))) == NULL) {
470
0
    goto err;
471
0
  }
472
0
  va_list args_copy;
473
0
  va_copy(args_copy, args);
474
0
  ret = vsnprintf(candidate, candidate_len, format, args_copy);
475
0
  va_end(args_copy);
476
0
  if (ret < 0) {
477
0
    goto err;
478
0
  }
479
0
  if ((size_t)ret >= candidate_len) {
480
    // Too big to fit in allocation.
481
0
    char *tmp;
482
483
0
    candidate_len = (size_t)ret + 1;
484
0
    if ((tmp = reinterpret_cast<char *>(
485
0
             reallocate(candidate, candidate_len))) == NULL) {
486
0
      goto err;
487
0
    }
488
0
    candidate = tmp;
489
0
    ret = vsnprintf(candidate, candidate_len, format, args);
490
0
  }
491
  // At this point this should not happen unless vsnprintf is insane.
492
0
  if (ret < 0 || (size_t)ret >= candidate_len) {
493
0
    goto err;
494
0
  }
495
0
  *str = candidate;
496
0
  return ret;
497
498
0
err:
499
0
  deallocate(candidate);
500
0
  *str = NULL;
501
0
  errno = ENOMEM;
502
0
  return -1;
503
0
}
504
505
0
int OPENSSL_vasprintf(char **str, const char *format, va_list args) {
506
0
  return OPENSSL_vasprintf_internal(str, format, args, /*system_malloc=*/0);
507
0
}
508
509
0
int OPENSSL_asprintf(char **str, const char *format, ...) {
510
0
  va_list args;
511
0
  va_start(args, format);
512
0
  int ret = OPENSSL_vasprintf(str, format, args);
513
0
  va_end(args);
514
0
  return ret;
515
0
}
516
517
46
char *OPENSSL_strndup(const char *str, size_t size) {
518
46
  size = OPENSSL_strnlen(str, size);
519
520
46
  size_t alloc_size = size + 1;
521
46
  if (alloc_size < size) {
522
    // overflow
523
0
    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
524
0
    return NULL;
525
0
  }
526
46
  char *ret = reinterpret_cast<char *>(OPENSSL_malloc(alloc_size));
527
46
  if (ret == NULL) {
528
0
    return NULL;
529
0
  }
530
531
46
  OPENSSL_memcpy(ret, str, size);
532
46
  ret[size] = '\0';
533
46
  return ret;
534
46
}
535
536
9.66k
size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) {
537
9.66k
  size_t l = 0;
538
539
83.5k
  for (; dst_size > 1 && *src; dst_size--) {
540
73.8k
    *dst++ = *src++;
541
73.8k
    l++;
542
73.8k
  }
543
544
9.66k
  if (dst_size) {
545
9.66k
    *dst = 0;
546
9.66k
  }
547
548
9.66k
  return l + strlen(src);
549
9.66k
}
550
551
9.66k
size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) {
552
9.66k
  size_t l = 0;
553
101k
  for (; dst_size > 0 && *dst; dst_size--, dst++) {
554
92.3k
    l++;
555
92.3k
  }
556
9.66k
  return l + OPENSSL_strlcpy(dst, src, dst_size);
557
9.66k
}
558
559
117k
void *OPENSSL_memdup(const void *data, size_t size) {
560
117k
  if (size == 0) {
561
11
    return NULL;
562
11
  }
563
564
117k
  void *ret = OPENSSL_malloc(size);
565
117k
  if (ret == NULL) {
566
0
    return NULL;
567
0
  }
568
569
117k
  OPENSSL_memcpy(ret, data, size);
570
117k
  return ret;
571
117k
}
572
573
0
void *CRYPTO_malloc(size_t size, const char *file, int line) {
574
0
  return OPENSSL_malloc(size);
575
0
}
576
577
0
void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) {
578
0
  return OPENSSL_realloc(ptr, new_size);
579
0
}
580
581
0
void CRYPTO_free(void *ptr, const char *file, int line) { OPENSSL_free(ptr); }