/src/openssl/crypto/mem.c
Line | Count | Source |
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 | 0 | { |
65 | 0 | if (!allow_customize) |
66 | 0 | return 0; |
67 | 0 | if (malloc_fn != NULL) |
68 | 0 | malloc_impl = malloc_fn; |
69 | 0 | if (realloc_fn != NULL) |
70 | 0 | realloc_impl = realloc_fn; |
71 | 0 | if (free_fn != NULL) |
72 | 0 | free_impl = free_fn; |
73 | 0 | return 1; |
74 | 0 | } |
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 | 0 | { |
80 | 0 | if (malloc_fn != NULL) |
81 | 0 | *malloc_fn = malloc_impl; |
82 | 0 | if (realloc_fn != NULL) |
83 | 0 | *realloc_fn = realloc_impl; |
84 | 0 | if (free_fn != NULL) |
85 | 0 | *free_fn = free_impl; |
86 | 0 | } |
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 | | * 0@0.01 |
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 | | char *end; |
116 | | |
117 | | if (semi != NULL) |
118 | | *semi++ = '\0'; |
119 | | |
120 | | /* |
121 | | * Get the count (parsing stops at the '@' if present), and percentage. |
122 | | * Ignore an unparsable/overflowing count rather than acting on garbage. |
123 | | * Validate that the count is followed by '@' or end-of-string. |
124 | | */ |
125 | | if (!ossl_strtol(md_failstring, &end, 10, &md_count) |
126 | | || (*end != '\0' && *end != '@')) |
127 | | md_count = 0; |
128 | | atsign = strchr(md_failstring, '@'); |
129 | | md_fail_percent = atsign == NULL ? 0 : (int)(atof(atsign + 1) * 100 + 0.5); |
130 | | |
131 | | if (semi != NULL) |
132 | | md_failstring = semi; |
133 | | } |
134 | | |
135 | | /* |
136 | | * Windows doesn't have random() and srandom(), but it has rand() and srand(). |
137 | | * Some rand() implementations aren't good, but we're not |
138 | | * dealing with secure randomness here. |
139 | | */ |
140 | | #ifdef _WIN32 |
141 | | #define random() rand() |
142 | | #define srandom(seed) srand(seed) |
143 | | #endif |
144 | | /* |
145 | | * See if the current malloc should fail. |
146 | | */ |
147 | | static int shouldfail(void) |
148 | | { |
149 | | int roll = (int)(random() % 10000); |
150 | | int shoulditfail = roll < md_fail_percent; |
151 | | #ifndef _WIN32 |
152 | | /* suppressed on Windows as POSIX-like file descriptors are non-inheritable */ |
153 | | int len; |
154 | | char buff[80]; |
155 | | |
156 | | if (md_tracefd > 0) { |
157 | | BIO_snprintf(buff, sizeof(buff), |
158 | | "%c C%ld %%%d R%d\n", |
159 | | shoulditfail ? '-' : '+', md_count, md_fail_percent, roll); |
160 | | len = strlen(buff); |
161 | | if (write(md_tracefd, buff, len) != len) |
162 | | perror("shouldfail write failed"); |
163 | | } |
164 | | #endif |
165 | | |
166 | | if (md_count) { |
167 | | /* If we used up this one, go to the next. */ |
168 | | if (--md_count == 0) |
169 | | parseit(); |
170 | | } |
171 | | |
172 | | return shoulditfail; |
173 | | } |
174 | | |
175 | | void ossl_malloc_setup_failures(void) |
176 | | { |
177 | | const char *cp = getenv("OPENSSL_MALLOC_FAILURES"); |
178 | | size_t cplen = 0; |
179 | | |
180 | | if (cp != NULL) { |
181 | | /* if the value is too long we'll just ignore it */ |
182 | | cplen = strlen(cp); |
183 | | if (cplen <= CRYPTO_MEM_CHECK_MAX_FS) { |
184 | | strncpy(md_failbuf, cp, CRYPTO_MEM_CHECK_MAX_FS); |
185 | | md_failstring = md_failbuf; |
186 | | parseit(); |
187 | | } |
188 | | } |
189 | | if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL) { |
190 | | int fd; |
191 | | |
192 | | if (ossl_strtoint(cp, NULL, 10, &fd) && fd >= 0) |
193 | | md_tracefd = fd; |
194 | | } |
195 | | if ((cp = getenv("OPENSSL_MALLOC_SEED")) != NULL) { |
196 | | unsigned long seed; |
197 | | |
198 | | /* Any value is a usable seed; just truncate it to the srandom() type. */ |
199 | | if (OPENSSL_strtoul(cp, NULL, 10, &seed)) |
200 | | srandom((unsigned int)seed); |
201 | | } |
202 | | } |
203 | | #endif |
204 | | |
205 | | void *CRYPTO_malloc(size_t num, const char *file, int line) |
206 | 719k | { |
207 | 719k | void *ptr; |
208 | | |
209 | 719k | INCREMENT(malloc_count); |
210 | 719k | FAILTEST(); |
211 | 719k | if (malloc_impl != CRYPTO_malloc) { |
212 | 0 | ptr = malloc_impl(num, file, line); |
213 | 0 | if (ptr != NULL || num == 0) |
214 | 0 | return ptr; |
215 | 0 | goto err; |
216 | 0 | } |
217 | | |
218 | 719k | if (ossl_unlikely(num == 0)) |
219 | 0 | return NULL; |
220 | | |
221 | 719k | if (allow_customize) { |
222 | | /* |
223 | | * Disallow customization after the first allocation. We only set this |
224 | | * if necessary to avoid a store to the same cache line on every |
225 | | * allocation. |
226 | | */ |
227 | 16 | allow_customize = 0; |
228 | 16 | } |
229 | | |
230 | 719k | ptr = malloc(num); |
231 | 719k | if (ossl_likely(ptr != NULL)) |
232 | 719k | return ptr; |
233 | 0 | err: |
234 | 0 | ossl_report_alloc_err(file, line); |
235 | 0 | return NULL; |
236 | 719k | } |
237 | | |
238 | | void *CRYPTO_zalloc(size_t num, const char *file, int line) |
239 | 540k | { |
240 | 540k | void *ret; |
241 | | |
242 | 540k | ret = CRYPTO_malloc(num, file, line); |
243 | 540k | if (ret != NULL) |
244 | 540k | memset(ret, 0, num); |
245 | | |
246 | 540k | return ret; |
247 | 540k | } |
248 | | |
249 | | void *CRYPTO_aligned_alloc(size_t num, size_t alignment, void **freeptr, |
250 | | const char *file, int line) |
251 | 16 | { |
252 | 16 | *freeptr = NULL; |
253 | | |
254 | | /* Ensure that alignment is a power of two no larger than 65536 */ |
255 | 16 | if (alignment == 0 || (alignment & (alignment - 1)) != 0 |
256 | 16 | || alignment > 65536) { |
257 | 0 | ossl_report_alloc_err_inv(file, line); |
258 | 0 | return NULL; |
259 | 0 | } |
260 | | |
261 | | /* Allow non-malloc() allocations as long as no malloc_impl is provided. */ |
262 | 16 | if (malloc_impl == CRYPTO_malloc) { |
263 | 16 | #if defined(_BSD_SOURCE) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) |
264 | 16 | void *ret; |
265 | | |
266 | | /* posix_memalign() requires alignment to be at least sizeof(void *) */ |
267 | 16 | if (alignment < sizeof(void *)) |
268 | 0 | alignment = sizeof(void *); |
269 | | |
270 | 16 | if (posix_memalign(&ret, alignment, num) == 0) { |
271 | 16 | *freeptr = ret; |
272 | 16 | return ret; |
273 | 16 | } |
274 | 16 | #endif |
275 | 16 | } |
276 | | |
277 | 0 | return ossl_malloc_align(num, alignment, freeptr, file, line); |
278 | 16 | } |
279 | | |
280 | | void *CRYPTO_realloc(void *str, size_t num, const char *file, int line) |
281 | 416 | { |
282 | 416 | void *ret; |
283 | | |
284 | 416 | INCREMENT(realloc_count); |
285 | 416 | FAILTEST(); |
286 | 416 | if (realloc_impl != CRYPTO_realloc) { |
287 | 0 | ret = realloc_impl(str, num, file, line); |
288 | |
|
289 | 0 | if (num == 0 || ret != NULL) |
290 | 0 | return ret; |
291 | | |
292 | 0 | goto err; |
293 | 0 | } |
294 | | |
295 | 416 | if (str == NULL) |
296 | 32 | return CRYPTO_malloc(num, file, line); |
297 | | |
298 | 384 | if (num == 0) { |
299 | 0 | CRYPTO_free(str, file, line); |
300 | 0 | return NULL; |
301 | 0 | } |
302 | | |
303 | 384 | ret = realloc(str, num); |
304 | | |
305 | 384 | err: |
306 | 384 | if (num != 0 && ret == NULL) |
307 | 0 | ossl_report_alloc_err(file, line); |
308 | | |
309 | 384 | return ret; |
310 | 384 | } |
311 | | |
312 | | void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num, |
313 | | const char *file, int line) |
314 | 0 | { |
315 | 0 | void *ret = NULL; |
316 | |
|
317 | 0 | if (str == NULL) |
318 | 0 | return CRYPTO_malloc(num, file, line); |
319 | | |
320 | 0 | if (num == 0) { |
321 | 0 | CRYPTO_clear_free(str, old_len, file, line); |
322 | 0 | return NULL; |
323 | 0 | } |
324 | | |
325 | | /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */ |
326 | 0 | if (num < old_len) { |
327 | 0 | OPENSSL_cleanse((char *)str + num, old_len - num); |
328 | 0 | return str; |
329 | 0 | } |
330 | | |
331 | 0 | ret = CRYPTO_malloc(num, file, line); |
332 | 0 | if (ret != NULL) { |
333 | 0 | memcpy(ret, str, old_len); |
334 | 0 | CRYPTO_clear_free(str, old_len, file, line); |
335 | 0 | } |
336 | 0 | return ret; |
337 | 0 | } |
338 | | |
339 | | void CRYPTO_free(void *str, const char *file, int line) |
340 | 871k | { |
341 | 871k | INCREMENT(free_count); |
342 | 871k | if (free_impl != CRYPTO_free) { |
343 | 0 | free_impl(str, file, line); |
344 | 0 | return; |
345 | 0 | } |
346 | | |
347 | 871k | free(str); |
348 | 871k | } |
349 | | |
350 | | void CRYPTO_clear_free(void *str, size_t num, const char *file, int line) |
351 | 105k | { |
352 | 105k | if (str == NULL) |
353 | 0 | return; |
354 | 105k | if (num) |
355 | 105k | OPENSSL_cleanse(str, num); |
356 | 105k | CRYPTO_free(str, file, line); |
357 | 105k | } |
358 | | |
359 | | #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) |
360 | | |
361 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
362 | | int CRYPTO_mem_ctrl(int mode) |
363 | | { |
364 | | (void)mode; |
365 | | return -1; |
366 | | } |
367 | | |
368 | | int CRYPTO_set_mem_debug(int flag) |
369 | | { |
370 | | (void)flag; |
371 | | return -1; |
372 | | } |
373 | | |
374 | | int CRYPTO_mem_debug_push(const char *info, const char *file, int line) |
375 | | { |
376 | | (void)info; |
377 | | (void)file; |
378 | | (void)line; |
379 | | return 0; |
380 | | } |
381 | | |
382 | | int CRYPTO_mem_debug_pop(void) |
383 | | { |
384 | | return 0; |
385 | | } |
386 | | |
387 | | void CRYPTO_mem_debug_malloc(void *addr, size_t num, int flag, |
388 | | const char *file, int line) |
389 | | { |
390 | | (void)addr; |
391 | | (void)num; |
392 | | (void)flag; |
393 | | (void)file; |
394 | | (void)line; |
395 | | } |
396 | | |
397 | | void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num, int flag, |
398 | | const char *file, int line) |
399 | | { |
400 | | (void)addr1; |
401 | | (void)addr2; |
402 | | (void)num; |
403 | | (void)flag; |
404 | | (void)file; |
405 | | (void)line; |
406 | | } |
407 | | |
408 | | void CRYPTO_mem_debug_free(void *addr, int flag, |
409 | | const char *file, int line) |
410 | | { |
411 | | (void)addr; |
412 | | (void)flag; |
413 | | (void)file; |
414 | | (void)line; |
415 | | } |
416 | | |
417 | | int CRYPTO_mem_leaks(BIO *b) |
418 | | { |
419 | | (void)b; |
420 | | return -1; |
421 | | } |
422 | | |
423 | | #ifndef OPENSSL_NO_STDIO |
424 | | int CRYPTO_mem_leaks_fp(FILE *fp) |
425 | | { |
426 | | (void)fp; |
427 | | return -1; |
428 | | } |
429 | | #endif |
430 | | |
431 | | int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u), |
432 | | void *u) |
433 | | { |
434 | | (void)cb; |
435 | | (void)u; |
436 | | return -1; |
437 | | } |
438 | | |
439 | | #endif |
440 | | |
441 | | #endif |