/src/openssl33/crypto/mem.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2023 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 "crypto/cryptlib.h" |
13 | | #include <stdio.h> |
14 | | #include <stdlib.h> |
15 | | #include <limits.h> |
16 | | #include <openssl/crypto.h> |
17 | | |
18 | | /* |
19 | | * the following pointers may be changed as long as 'allow_customize' is set |
20 | | */ |
21 | | static int allow_customize = 1; |
22 | | static CRYPTO_malloc_fn malloc_impl = CRYPTO_malloc; |
23 | | static CRYPTO_realloc_fn realloc_impl = CRYPTO_realloc; |
24 | | static CRYPTO_free_fn free_impl = CRYPTO_free; |
25 | | |
26 | | #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODULE) |
27 | | #include "internal/tsan_assist.h" |
28 | | |
29 | | #ifdef TSAN_REQUIRES_LOCKING |
30 | | #define INCREMENT(x) /* empty */ |
31 | | #define LOAD(x) 0 |
32 | | #else /* TSAN_REQUIRES_LOCKING */ |
33 | | static TSAN_QUALIFIER int malloc_count; |
34 | | static TSAN_QUALIFIER int realloc_count; |
35 | | static TSAN_QUALIFIER int free_count; |
36 | | |
37 | | #define INCREMENT(x) tsan_counter(&(x)) |
38 | | #define LOAD(x) tsan_load(&x) |
39 | | #endif /* TSAN_REQUIRES_LOCKING */ |
40 | | |
41 | | static char *md_failstring; |
42 | | static long md_count; |
43 | | static int md_fail_percent = 0; |
44 | | static int md_tracefd = -1; |
45 | | |
46 | | static void parseit(void); |
47 | | static int shouldfail(void); |
48 | | |
49 | | #define FAILTEST() \ |
50 | | if (shouldfail()) \ |
51 | | 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 | | |
170 | | if (cp != NULL && (md_failstring = strdup(cp)) != NULL) |
171 | | parseit(); |
172 | | if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL) |
173 | | md_tracefd = atoi(cp); |
174 | | if ((cp = getenv("OPENSSL_MALLOC_SEED")) != NULL) |
175 | | srandom(atoi(cp)); |
176 | | } |
177 | | #endif |
178 | | |
179 | | void *CRYPTO_malloc(size_t num, const char *file, int line) |
180 | 949M | { |
181 | 949M | void *ptr; |
182 | | |
183 | 949M | INCREMENT(malloc_count); |
184 | 949M | if (malloc_impl != CRYPTO_malloc) { |
185 | 0 | ptr = malloc_impl(num, file, line); |
186 | 0 | if (ptr != NULL || num == 0) |
187 | 0 | return ptr; |
188 | 0 | goto err; |
189 | 0 | } |
190 | | |
191 | 949M | if (num == 0) |
192 | 66 | return NULL; |
193 | | |
194 | 949M | FAILTEST(); |
195 | 949M | if (allow_customize) { |
196 | | /* |
197 | | * Disallow customization after the first allocation. We only set this |
198 | | * if necessary to avoid a store to the same cache line on every |
199 | | * allocation. |
200 | | */ |
201 | 146 | allow_customize = 0; |
202 | 146 | } |
203 | | |
204 | 949M | ptr = malloc(num); |
205 | 949M | if (ptr != NULL) |
206 | 949M | return ptr; |
207 | 0 | err: |
208 | | /* |
209 | | * ossl_err_get_state_int() in err.c uses CRYPTO_zalloc(num, NULL, 0) for |
210 | | * ERR_STATE allocation. Prevent mem alloc error loop while reporting error. |
211 | | */ |
212 | 0 | if (file != NULL || line != 0) { |
213 | 0 | ERR_new(); |
214 | 0 | ERR_set_debug(file, line, NULL); |
215 | 0 | ERR_set_error(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE, NULL); |
216 | 0 | } |
217 | 0 | return NULL; |
218 | 949M | } |
219 | | |
220 | | void *CRYPTO_zalloc(size_t num, const char *file, int line) |
221 | 776M | { |
222 | 776M | void *ret; |
223 | | |
224 | 776M | ret = CRYPTO_malloc(num, file, line); |
225 | 776M | if (ret != NULL) |
226 | 776M | memset(ret, 0, num); |
227 | | |
228 | 776M | return ret; |
229 | 776M | } |
230 | | |
231 | | void *CRYPTO_realloc(void *str, size_t num, const char *file, int line) |
232 | 70.0M | { |
233 | 70.0M | INCREMENT(realloc_count); |
234 | 70.0M | if (realloc_impl != CRYPTO_realloc) |
235 | 0 | return realloc_impl(str, num, file, line); |
236 | | |
237 | 70.0M | if (str == NULL) |
238 | 57.5M | return CRYPTO_malloc(num, file, line); |
239 | | |
240 | 12.5M | if (num == 0) { |
241 | 0 | CRYPTO_free(str, file, line); |
242 | 0 | return NULL; |
243 | 0 | } |
244 | | |
245 | 12.5M | FAILTEST(); |
246 | 12.5M | return realloc(str, num); |
247 | 12.5M | } |
248 | | |
249 | | void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num, |
250 | | const char *file, int line) |
251 | 34.7M | { |
252 | 34.7M | void *ret = NULL; |
253 | | |
254 | 34.7M | if (str == NULL) |
255 | 14.8M | return CRYPTO_malloc(num, file, line); |
256 | | |
257 | 19.8M | if (num == 0) { |
258 | 0 | CRYPTO_clear_free(str, old_len, file, line); |
259 | 0 | return NULL; |
260 | 0 | } |
261 | | |
262 | | /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */ |
263 | 19.8M | if (num < old_len) { |
264 | 0 | OPENSSL_cleanse((char *)str + num, old_len - num); |
265 | 0 | return str; |
266 | 0 | } |
267 | | |
268 | 19.8M | ret = CRYPTO_malloc(num, file, line); |
269 | 19.8M | if (ret != NULL) { |
270 | 19.8M | memcpy(ret, str, old_len); |
271 | 19.8M | CRYPTO_clear_free(str, old_len, file, line); |
272 | 19.8M | } |
273 | 19.8M | return ret; |
274 | 19.8M | } |
275 | | |
276 | | void CRYPTO_free(void *str, const char *file, int line) |
277 | 5.91G | { |
278 | 5.91G | INCREMENT(free_count); |
279 | 5.91G | if (free_impl != CRYPTO_free) { |
280 | 0 | free_impl(str, file, line); |
281 | 0 | return; |
282 | 0 | } |
283 | | |
284 | 5.91G | free(str); |
285 | 5.91G | } |
286 | | |
287 | | void CRYPTO_clear_free(void *str, size_t num, const char *file, int line) |
288 | 98.5M | { |
289 | 98.5M | if (str == NULL) |
290 | 10.4M | return; |
291 | 88.0M | if (num) |
292 | 88.0M | OPENSSL_cleanse(str, num); |
293 | 88.0M | CRYPTO_free(str, file, line); |
294 | 88.0M | } |
295 | | |
296 | | #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) |
297 | | |
298 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
299 | | int CRYPTO_mem_ctrl(int mode) |
300 | | { |
301 | | (void)mode; |
302 | | return -1; |
303 | | } |
304 | | |
305 | | int CRYPTO_set_mem_debug(int flag) |
306 | | { |
307 | | (void)flag; |
308 | | return -1; |
309 | | } |
310 | | |
311 | | int CRYPTO_mem_debug_push(const char *info, const char *file, int line) |
312 | | { |
313 | | (void)info; |
314 | | (void)file; |
315 | | (void)line; |
316 | | return 0; |
317 | | } |
318 | | |
319 | | int CRYPTO_mem_debug_pop(void) |
320 | | { |
321 | | return 0; |
322 | | } |
323 | | |
324 | | void CRYPTO_mem_debug_malloc(void *addr, size_t num, int flag, |
325 | | const char *file, int line) |
326 | | { |
327 | | (void)addr; |
328 | | (void)num; |
329 | | (void)flag; |
330 | | (void)file; |
331 | | (void)line; |
332 | | } |
333 | | |
334 | | void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num, int flag, |
335 | | const char *file, int line) |
336 | | { |
337 | | (void)addr1; |
338 | | (void)addr2; |
339 | | (void)num; |
340 | | (void)flag; |
341 | | (void)file; |
342 | | (void)line; |
343 | | } |
344 | | |
345 | | void CRYPTO_mem_debug_free(void *addr, int flag, |
346 | | const char *file, int line) |
347 | | { |
348 | | (void)addr; |
349 | | (void)flag; |
350 | | (void)file; |
351 | | (void)line; |
352 | | } |
353 | | |
354 | | int CRYPTO_mem_leaks(BIO *b) |
355 | | { |
356 | | (void)b; |
357 | | return -1; |
358 | | } |
359 | | |
360 | | #ifndef OPENSSL_NO_STDIO |
361 | | int CRYPTO_mem_leaks_fp(FILE *fp) |
362 | | { |
363 | | (void)fp; |
364 | | return -1; |
365 | | } |
366 | | #endif |
367 | | |
368 | | int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u), |
369 | | void *u) |
370 | | { |
371 | | (void)cb; |
372 | | (void)u; |
373 | | return -1; |
374 | | } |
375 | | |
376 | | #endif |
377 | | |
378 | | #endif |