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