Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/mem.c

raw
1 /*
2 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include "internal/e_os.h"
11 #include "internal/cryptlib.h"
12 #include "internal/mem_alloc_utils.h"
13 #include "crypto/cryptlib.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <limits.h>
17 #include <openssl/crypto.h>
18
19 /*
20 * the following pointers may be changed as long as 'allow_customize' is set
21 */
22 static int allow_customize = 1;
23 static CRYPTO_malloc_fn malloc_impl = CRYPTO_malloc;
24 static CRYPTO_realloc_fn realloc_impl = CRYPTO_realloc;
25 static CRYPTO_free_fn free_impl = CRYPTO_free;
26
27 #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODULE)
28 #include "internal/tsan_assist.h"
29
30 #ifdef TSAN_REQUIRES_LOCKING
31 #define INCREMENT(x) /* empty */
32 #define LOAD(x) 0
33 #else /* TSAN_REQUIRES_LOCKING */
34 static TSAN_QUALIFIER int malloc_count;
35 static TSAN_QUALIFIER int realloc_count;
36 static TSAN_QUALIFIER int free_count;
37
38 #define INCREMENT(x) tsan_counter(&(x))
39 #define LOAD(x) tsan_load(&x)
40 #endif /* TSAN_REQUIRES_LOCKING */
41
42 static char md_failbuf[CRYPTO_MEM_CHECK_MAX_FS + 1];
43 static char *md_failstring = NULL;
44 static long md_count;
45 static int md_fail_percent = 0;
46 static int md_tracefd = -1;
47
48 static void parseit(void);
49 static int shouldfail(void);
50
51 #define FAILTEST() \
52 if (shouldfail()) \
53 return NULL
54
55 #else
56
57 #define INCREMENT(x) /* empty */
58 #define FAILTEST() /* empty */
59 #endif
60
61 int CRYPTO_set_mem_functions(CRYPTO_malloc_fn malloc_fn,
62 CRYPTO_realloc_fn realloc_fn,
63 CRYPTO_free_fn free_fn)
64 {
65 if (!allow_customize)
66 return 0;
67 if (malloc_fn != NULL)
68 malloc_impl = malloc_fn;
69 if (realloc_fn != NULL)
70 realloc_impl = realloc_fn;
71 if (free_fn != NULL)
72 free_impl = free_fn;
73 return 1;
74 }
75
76 void CRYPTO_get_mem_functions(CRYPTO_malloc_fn *malloc_fn,
77 CRYPTO_realloc_fn *realloc_fn,
78 CRYPTO_free_fn *free_fn)
79 {
80 if (malloc_fn != NULL)
81 *malloc_fn = malloc_impl;
82 if (realloc_fn != NULL)
83 *realloc_fn = realloc_impl;
84 if (free_fn != NULL)
85 *free_fn = free_impl;
86 }
87
88 #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODULE)
89 void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount)
90 {
91 if (mcount != NULL)
92 *mcount = LOAD(malloc_count);
93 if (rcount != NULL)
94 *rcount = LOAD(realloc_count);
95 if (fcount != NULL)
96 *fcount = LOAD(free_count);
97 }
98
99 /*
100 * Parse a "malloc failure spec" string. This likes like a set of fields
101 * separated by semicolons. Each field has a count and an optional failure
102 * percentage. For example:
103 * 100@0;100@25;0@0
104 * or 100;100@25;0
105 * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
106 * all remaining (count is zero) succeed.
107 * The failure percentge can have 2 digits after the comma. For example:
108 * [email protected]
109 * This means 0.01% of all allocations will fail.
110 */
111 static void parseit(void)
112 {
113 char *semi = strchr(md_failstring, ';');
114 char *atsign;
115
116 if (semi != NULL)
117 *semi++ = '\0';
118
119 /* Get the count (atol will stop at the @ if there), and percentage */
120 md_count = atol(md_failstring);
121 atsign = strchr(md_failstring, '@');
122 md_fail_percent = atsign == NULL ? 0 : (int)(atof(atsign + 1) * 100 + 0.5);
123
124 if (semi != NULL)
125 md_failstring = semi;
126 }
127
128 /*
129 * Windows doesn't have random() and srandom(), but it has rand() and srand().
130 * Some rand() implementations aren't good, but we're not
131 * dealing with secure randomness here.
132 */
133 #ifdef _WIN32
134 #define random() rand()
135 #define srandom(seed) srand(seed)
136 #endif
137 /*
138 * See if the current malloc should fail.
139 */
140 static int shouldfail(void)
141 {
142 int roll = (int)(random() % 10000);
143 int shoulditfail = roll < md_fail_percent;
144 #ifndef _WIN32
145 /* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
146 int len;
147 char buff[80];
148
149 if (md_tracefd > 0) {
150 BIO_snprintf(buff, sizeof(buff),
151 "%c C%ld %%%d R%d\n",
152 shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
153 len = strlen(buff);
154 if (write(md_tracefd, buff, len) != len)
155 perror("shouldfail write failed");
156 }
157 #endif
158
159 if (md_count) {
160 /* If we used up this one, go to the next. */
161 if (--md_count == 0)
162 parseit();
163 }
164
165 return shoulditfail;
166 }
167
168 void ossl_malloc_setup_failures(void)
169 {
170 const char *cp = getenv("OPENSSL_MALLOC_FAILURES");
171 size_t cplen = 0;
172
173 if (cp != NULL) {
174 /* if the value is too long we'll just ignore it */
175 cplen = strlen(cp);
176 if (cplen <= CRYPTO_MEM_CHECK_MAX_FS) {
177 strncpy(md_failbuf, cp, CRYPTO_MEM_CHECK_MAX_FS);
178 md_failstring = md_failbuf;
179 parseit();
180 }
181 }
182 if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
183 md_tracefd = atoi(cp);
184 if ((cp = getenv("OPENSSL_MALLOC_SEED")) != NULL)
185 srandom(atoi(cp));
186 }
187 #endif
188
189 void *CRYPTO_malloc(size_t num, const char *file, int line)
190 {
191 void *ptr;
192
193 INCREMENT(malloc_count);
194 if (malloc_impl != CRYPTO_malloc) {
195 ptr = malloc_impl(num, file, line);
196 if (ptr != NULL || num == 0)
197 return ptr;
198 goto err;
199 }
200
201 if (ossl_unlikely(num == 0))
202 return NULL;
203
204 FAILTEST();
205 if (allow_customize) {
206 /*
207 * Disallow customization after the first allocation. We only set this
208 * if necessary to avoid a store to the same cache line on every
209 * allocation.
210 */
211 allow_customize = 0;
212 }
213
214 ptr = malloc(num);
215 if (ossl_likely(ptr != NULL))
216 return ptr;
217 err:
218 ossl_report_alloc_err(file, line);
219 return NULL;
220 }
221
222 void *CRYPTO_zalloc(size_t num, const char *file, int line)
223 {
224 void *ret;
225
226 ret = CRYPTO_malloc(num, file, line);
227 if (ret != NULL)
228 memset(ret, 0, num);
229
230 return ret;
231 }
232
233 void *CRYPTO_aligned_alloc(size_t num, size_t alignment, void **freeptr,
234 const char *file, int line)
235 {
236 size_t alloc_bytes;
237 void *ret;
238
239 *freeptr = NULL;
240
241 #if defined(OPENSSL_SMALL_FOOTPRINT)
242 return NULL;
243 #endif
244
245 /* Ensure that alignment is a power of two */
246 if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
247 ossl_report_alloc_err_inv(file, line);
248 return NULL;
249 }
250
251 /* Allow non-malloc() allocations as long as no malloc_impl is provided. */
252 if (malloc_impl == CRYPTO_malloc) {
253 #if defined(_BSD_SOURCE) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
254 int memalign_ret;
255
256 /* posix_memalign() requires alignment to be at least sizeof(void *) */
257 if (alignment < sizeof(void *))
258 alignment = sizeof(void *);
259
260 if ((memalign_ret = posix_memalign(&ret, alignment, num))) {
261 ret = NULL;
262 switch (memalign_ret) {
263 case EINVAL:
264 ossl_report_alloc_err_inv(file, line);
265 break;
266 case ENOMEM:
267 ossl_report_alloc_err(file, line);
268 break;
269 }
270 }
271 *freeptr = ret;
272 return ret;
273 #endif
274 }
275
276 /* we have to do this the hard way */
277
278 /*
279 * Note: Windows supports an _aligned_malloc call, but we choose
280 * not to use it here, because allocations from that function
281 * require that they be freed via _aligned_free. Given that
282 * we can't differentiate plain malloc blocks from blocks obtained
283 * via _aligned_malloc, just avoid its use entirely
284 */
285
286 if (ossl_unlikely(!ossl_size_add(num, alignment, &alloc_bytes, file, line)))
287 return NULL;
288
289 /*
290 * Step 1: Allocate an amount of memory that is <alignment>
291 * bytes bigger than requested
292 */
293 *freeptr = CRYPTO_malloc(alloc_bytes, file, line);
294 if (*freeptr == NULL)
295 return NULL;
296
297 /*
298 * Step 2: Add <alignment - 1> bytes to the pointer
299 * This will cross the alignment boundary that is
300 * requested
301 */
302 ret = (void *)((char *)*freeptr + (alignment - 1));
303
304 /*
305 * Step 3: Use the alignment as a mask to translate the
306 * least significant bits of the allocation at the alignment
307 * boundary to 0. ret now holds a pointer to the memory
308 * buffer at the requested alignment
309 * NOTE: It is a documented requirement that alignment be a
310 * power of 2, which is what allows this to work
311 */
312 ret = (void *)((uintptr_t)ret & (uintptr_t)(~(alignment - 1)));
313 return ret;
314 }
315
316 void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
317 {
318 void *ret;
319
320 INCREMENT(realloc_count);
321 if (realloc_impl != CRYPTO_realloc) {
322 ret = realloc_impl(str, num, file, line);
323
324 if (num == 0 || ret != NULL)
325 return ret;
326
327 goto err;
328 }
329
330 if (str == NULL)
331 return CRYPTO_malloc(num, file, line);
332
333 if (num == 0) {
334 CRYPTO_free(str, file, line);
335 return NULL;
336 }
337
338 FAILTEST();
339 ret = realloc(str, num);
340
341 err:
342 if (num != 0 && ret == NULL)
343 ossl_report_alloc_err(file, line);
344
345 return ret;
346 }
347
348 void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
349 const char *file, int line)
350 {
351 void *ret = NULL;
352
353 if (str == NULL)
354 return CRYPTO_malloc(num, file, line);
355
356 if (num == 0) {
357 CRYPTO_clear_free(str, old_len, file, line);
358 return NULL;
359 }
360
361 /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
362 if (num < old_len) {
363 OPENSSL_cleanse((char *)str + num, old_len - num);
364 return str;
365 }
366
367 ret = CRYPTO_malloc(num, file, line);
368 if (ret != NULL) {
369 memcpy(ret, str, old_len);
370 CRYPTO_clear_free(str, old_len, file, line);
371 }
372 return ret;
373 }
374
375 void CRYPTO_free(void *str, const char *file, int line)
376 {
377 INCREMENT(free_count);
378 if (free_impl != CRYPTO_free) {
379 free_impl(str, file, line);
380 return;
381 }
382
383 free(str);
384 }
385
386 void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
387 {
388 if (str == NULL)
389 return;
390 if (num)
391 OPENSSL_cleanse(str, num);
392 CRYPTO_free(str, file, line);
393 }
394
395 #if !defined(OPENSSL_NO_CRYPTO_MDEBUG)
396
397 #ifndef OPENSSL_NO_DEPRECATED_3_0
398 int CRYPTO_mem_ctrl(int mode)
399 {
400 (void)mode;
401 return -1;
402 }
403
404 int CRYPTO_set_mem_debug(int flag)
405 {
406 (void)flag;
407 return -1;
408 }
409
410 int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
411 {
412 (void)info;
413 (void)file;
414 (void)line;
415 return 0;
416 }
417
418 int CRYPTO_mem_debug_pop(void)
419 {
420 return 0;
421 }
422
423 void CRYPTO_mem_debug_malloc(void *addr, size_t num, int flag,
424 const char *file, int line)
425 {
426 (void)addr;
427 (void)num;
428 (void)flag;
429 (void)file;
430 (void)line;
431 }
432
433 void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num, int flag,
434 const char *file, int line)
435 {
436 (void)addr1;
437 (void)addr2;
438 (void)num;
439 (void)flag;
440 (void)file;
441 (void)line;
442 }
443
444 void CRYPTO_mem_debug_free(void *addr, int flag,
445 const char *file, int line)
446 {
447 (void)addr;
448 (void)flag;
449 (void)file;
450 (void)line;
451 }
452
453 int CRYPTO_mem_leaks(BIO *b)
454 {
455 (void)b;
456 return -1;
457 }
458
459 #ifndef OPENSSL_NO_STDIO
460 int CRYPTO_mem_leaks_fp(FILE *fp)
461 {
462 (void)fp;
463 return -1;
464 }
465 #endif
466
467 int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u),
468 void *u)
469 {
470 (void)cb;
471 (void)u;
472 return -1;
473 }
474
475 #endif
476
477 #endif