1 | /*
|
---|
2 | * Copyright 1995-2022 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 <stdio.h>
|
---|
13 | #include <stdlib.h>
|
---|
14 | #include <limits.h>
|
---|
15 | #include <openssl/crypto.h>
|
---|
16 |
|
---|
17 | /*
|
---|
18 | * the following pointers may be changed as long as 'allow_customize' is set
|
---|
19 | */
|
---|
20 | static int allow_customize = 1;
|
---|
21 | static CRYPTO_malloc_fn malloc_impl = CRYPTO_malloc;
|
---|
22 | static CRYPTO_realloc_fn realloc_impl = CRYPTO_realloc;
|
---|
23 | static CRYPTO_free_fn free_impl = CRYPTO_free;
|
---|
24 |
|
---|
25 | #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODULE)
|
---|
26 | # include "internal/tsan_assist.h"
|
---|
27 |
|
---|
28 | # ifdef TSAN_REQUIRES_LOCKING
|
---|
29 | # define INCREMENT(x) /* empty */
|
---|
30 | # define LOAD(x) 0
|
---|
31 | # else /* TSAN_REQUIRES_LOCKING */
|
---|
32 | static TSAN_QUALIFIER int malloc_count;
|
---|
33 | static TSAN_QUALIFIER int realloc_count;
|
---|
34 | static TSAN_QUALIFIER int free_count;
|
---|
35 |
|
---|
36 | # define INCREMENT(x) tsan_counter(&(x))
|
---|
37 | # define LOAD(x) tsan_load(&x)
|
---|
38 | # endif /* TSAN_REQUIRES_LOCKING */
|
---|
39 |
|
---|
40 | static char *md_failstring;
|
---|
41 | static long md_count;
|
---|
42 | static int md_fail_percent = 0;
|
---|
43 | static int md_tracefd = -1;
|
---|
44 |
|
---|
45 | static void parseit(void);
|
---|
46 | static int shouldfail(void);
|
---|
47 |
|
---|
48 | # define FAILTEST() if (shouldfail()) return NULL
|
---|
49 |
|
---|
50 | #else
|
---|
51 |
|
---|
52 | # define INCREMENT(x) /* empty */
|
---|
53 | # define FAILTEST() /* empty */
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | int CRYPTO_set_mem_functions(CRYPTO_malloc_fn malloc_fn,
|
---|
57 | CRYPTO_realloc_fn realloc_fn,
|
---|
58 | CRYPTO_free_fn free_fn)
|
---|
59 | {
|
---|
60 | if (!allow_customize)
|
---|
61 | return 0;
|
---|
62 | if (malloc_fn != NULL)
|
---|
63 | malloc_impl = malloc_fn;
|
---|
64 | if (realloc_fn != NULL)
|
---|
65 | realloc_impl = realloc_fn;
|
---|
66 | if (free_fn != NULL)
|
---|
67 | free_impl = free_fn;
|
---|
68 | return 1;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void CRYPTO_get_mem_functions(CRYPTO_malloc_fn *malloc_fn,
|
---|
72 | CRYPTO_realloc_fn *realloc_fn,
|
---|
73 | CRYPTO_free_fn *free_fn)
|
---|
74 | {
|
---|
75 | if (malloc_fn != NULL)
|
---|
76 | *malloc_fn = malloc_impl;
|
---|
77 | if (realloc_fn != NULL)
|
---|
78 | *realloc_fn = realloc_impl;
|
---|
79 | if (free_fn != NULL)
|
---|
80 | *free_fn = free_impl;
|
---|
81 | }
|
---|
82 |
|
---|
83 | #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODULE)
|
---|
84 | void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount)
|
---|
85 | {
|
---|
86 | if (mcount != NULL)
|
---|
87 | *mcount = LOAD(malloc_count);
|
---|
88 | if (rcount != NULL)
|
---|
89 | *rcount = LOAD(realloc_count);
|
---|
90 | if (fcount != NULL)
|
---|
91 | *fcount = LOAD(free_count);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * Parse a "malloc failure spec" string. This likes like a set of fields
|
---|
96 | * separated by semicolons. Each field has a count and an optional failure
|
---|
97 | * percentage. For example:
|
---|
98 | * 100@0;100@25;0@0
|
---|
99 | * or 100;100@25;0
|
---|
100 | * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
|
---|
101 | * all remaining (count is zero) succeed.
|
---|
102 | */
|
---|
103 | static void parseit(void)
|
---|
104 | {
|
---|
105 | char *semi = strchr(md_failstring, ';');
|
---|
106 | char *atsign;
|
---|
107 |
|
---|
108 | if (semi != NULL)
|
---|
109 | *semi++ = '\0';
|
---|
110 |
|
---|
111 | /* Get the count (atol will stop at the @ if there), and percentage */
|
---|
112 | md_count = atol(md_failstring);
|
---|
113 | atsign = strchr(md_failstring, '@');
|
---|
114 | md_fail_percent = atsign == NULL ? 0 : atoi(atsign + 1);
|
---|
115 |
|
---|
116 | if (semi != NULL)
|
---|
117 | md_failstring = semi;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * Windows doesn't have random(), but it has rand()
|
---|
122 | * Some rand() implementations aren't good, but we're not
|
---|
123 | * dealing with secure randomness here.
|
---|
124 | */
|
---|
125 | # ifdef _WIN32
|
---|
126 | # define random() rand()
|
---|
127 | # endif
|
---|
128 | /*
|
---|
129 | * See if the current malloc should fail.
|
---|
130 | */
|
---|
131 | static int shouldfail(void)
|
---|
132 | {
|
---|
133 | int roll = (int)(random() % 100);
|
---|
134 | int shoulditfail = roll < md_fail_percent;
|
---|
135 | # ifndef _WIN32
|
---|
136 | /* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
|
---|
137 | int len;
|
---|
138 | char buff[80];
|
---|
139 |
|
---|
140 | if (md_tracefd > 0) {
|
---|
141 | BIO_snprintf(buff, sizeof(buff),
|
---|
142 | "%c C%ld %%%d R%d\n",
|
---|
143 | shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
|
---|
144 | len = strlen(buff);
|
---|
145 | if (write(md_tracefd, buff, len) != len)
|
---|
146 | perror("shouldfail write failed");
|
---|
147 | }
|
---|
148 | # endif
|
---|
149 |
|
---|
150 | if (md_count) {
|
---|
151 | /* If we used up this one, go to the next. */
|
---|
152 | if (--md_count == 0)
|
---|
153 | parseit();
|
---|
154 | }
|
---|
155 |
|
---|
156 | return shoulditfail;
|
---|
157 | }
|
---|
158 |
|
---|
159 | void ossl_malloc_setup_failures(void)
|
---|
160 | {
|
---|
161 | const char *cp = getenv("OPENSSL_MALLOC_FAILURES");
|
---|
162 |
|
---|
163 | if (cp != NULL && (md_failstring = strdup(cp)) != NULL)
|
---|
164 | parseit();
|
---|
165 | if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
|
---|
166 | md_tracefd = atoi(cp);
|
---|
167 | }
|
---|
168 | #endif
|
---|
169 |
|
---|
170 | void *CRYPTO_malloc(size_t num, const char *file, int line)
|
---|
171 | {
|
---|
172 | INCREMENT(malloc_count);
|
---|
173 | if (malloc_impl != CRYPTO_malloc)
|
---|
174 | return malloc_impl(num, file, line);
|
---|
175 |
|
---|
176 | if (num == 0)
|
---|
177 | return NULL;
|
---|
178 |
|
---|
179 | FAILTEST();
|
---|
180 | if (allow_customize) {
|
---|
181 | /*
|
---|
182 | * Disallow customization after the first allocation. We only set this
|
---|
183 | * if necessary to avoid a store to the same cache line on every
|
---|
184 | * allocation.
|
---|
185 | */
|
---|
186 | allow_customize = 0;
|
---|
187 | }
|
---|
188 |
|
---|
189 | return malloc(num);
|
---|
190 | }
|
---|
191 |
|
---|
192 | void *CRYPTO_zalloc(size_t num, const char *file, int line)
|
---|
193 | {
|
---|
194 | void *ret;
|
---|
195 |
|
---|
196 | ret = CRYPTO_malloc(num, file, line);
|
---|
197 | FAILTEST();
|
---|
198 | if (ret != NULL)
|
---|
199 | memset(ret, 0, num);
|
---|
200 |
|
---|
201 | return ret;
|
---|
202 | }
|
---|
203 |
|
---|
204 | void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
|
---|
205 | {
|
---|
206 | INCREMENT(realloc_count);
|
---|
207 | if (realloc_impl != CRYPTO_realloc)
|
---|
208 | return realloc_impl(str, num, file, line);
|
---|
209 |
|
---|
210 | FAILTEST();
|
---|
211 | if (str == NULL)
|
---|
212 | return CRYPTO_malloc(num, file, line);
|
---|
213 |
|
---|
214 | if (num == 0) {
|
---|
215 | CRYPTO_free(str, file, line);
|
---|
216 | return NULL;
|
---|
217 | }
|
---|
218 |
|
---|
219 | return realloc(str, num);
|
---|
220 | }
|
---|
221 |
|
---|
222 | void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
|
---|
223 | const char *file, int line)
|
---|
224 | {
|
---|
225 | void *ret = NULL;
|
---|
226 |
|
---|
227 | if (str == NULL)
|
---|
228 | return CRYPTO_malloc(num, file, line);
|
---|
229 |
|
---|
230 | if (num == 0) {
|
---|
231 | CRYPTO_clear_free(str, old_len, file, line);
|
---|
232 | return NULL;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
|
---|
236 | if (num < old_len) {
|
---|
237 | OPENSSL_cleanse((char*)str + num, old_len - num);
|
---|
238 | return str;
|
---|
239 | }
|
---|
240 |
|
---|
241 | ret = CRYPTO_malloc(num, file, line);
|
---|
242 | if (ret != NULL) {
|
---|
243 | memcpy(ret, str, old_len);
|
---|
244 | CRYPTO_clear_free(str, old_len, file, line);
|
---|
245 | }
|
---|
246 | return ret;
|
---|
247 | }
|
---|
248 |
|
---|
249 | void CRYPTO_free(void *str, const char *file, int line)
|
---|
250 | {
|
---|
251 | INCREMENT(free_count);
|
---|
252 | if (free_impl != CRYPTO_free) {
|
---|
253 | free_impl(str, file, line);
|
---|
254 | return;
|
---|
255 | }
|
---|
256 |
|
---|
257 | free(str);
|
---|
258 | }
|
---|
259 |
|
---|
260 | void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
|
---|
261 | {
|
---|
262 | if (str == NULL)
|
---|
263 | return;
|
---|
264 | if (num)
|
---|
265 | OPENSSL_cleanse(str, num);
|
---|
266 | CRYPTO_free(str, file, line);
|
---|
267 | }
|
---|
268 |
|
---|
269 | #if !defined(OPENSSL_NO_CRYPTO_MDEBUG)
|
---|
270 |
|
---|
271 | # ifndef OPENSSL_NO_DEPRECATED_3_0
|
---|
272 | int CRYPTO_mem_ctrl(int mode)
|
---|
273 | {
|
---|
274 | (void)mode;
|
---|
275 | return -1;
|
---|
276 | }
|
---|
277 |
|
---|
278 | int CRYPTO_set_mem_debug(int flag)
|
---|
279 | {
|
---|
280 | (void)flag;
|
---|
281 | return -1;
|
---|
282 | }
|
---|
283 |
|
---|
284 | int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
|
---|
285 | {
|
---|
286 | (void)info; (void)file; (void)line;
|
---|
287 | return 0;
|
---|
288 | }
|
---|
289 |
|
---|
290 | int CRYPTO_mem_debug_pop(void)
|
---|
291 | {
|
---|
292 | return 0;
|
---|
293 | }
|
---|
294 |
|
---|
295 | void CRYPTO_mem_debug_malloc(void *addr, size_t num, int flag,
|
---|
296 | const char *file, int line)
|
---|
297 | {
|
---|
298 | (void)addr; (void)num; (void)flag; (void)file; (void)line;
|
---|
299 | }
|
---|
300 |
|
---|
301 | void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num, int flag,
|
---|
302 | const char *file, int line)
|
---|
303 | {
|
---|
304 | (void)addr1; (void)addr2; (void)num; (void)flag; (void)file; (void)line;
|
---|
305 | }
|
---|
306 |
|
---|
307 | void CRYPTO_mem_debug_free(void *addr, int flag,
|
---|
308 | const char *file, int line)
|
---|
309 | {
|
---|
310 | (void)addr; (void)flag; (void)file; (void)line;
|
---|
311 | }
|
---|
312 |
|
---|
313 | int CRYPTO_mem_leaks(BIO *b)
|
---|
314 | {
|
---|
315 | (void)b;
|
---|
316 | return -1;
|
---|
317 | }
|
---|
318 |
|
---|
319 | # ifndef OPENSSL_NO_STDIO
|
---|
320 | int CRYPTO_mem_leaks_fp(FILE *fp)
|
---|
321 | {
|
---|
322 | (void)fp;
|
---|
323 | return -1;
|
---|
324 | }
|
---|
325 | # endif
|
---|
326 |
|
---|
327 | int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u),
|
---|
328 | void *u)
|
---|
329 | {
|
---|
330 | (void)cb; (void)u;
|
---|
331 | return -1;
|
---|
332 | }
|
---|
333 |
|
---|
334 | # endif
|
---|
335 |
|
---|
336 | #endif
|
---|