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