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