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