Add EVP_PKEY_METHOD redirection test
[openssl.git] / crypto / mem.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 "internal/cryptlib_int.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <limits.h>
16 #include <openssl/crypto.h>
17 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
18 # include <execinfo.h>
19 #endif
20
21 /*
22  * the following pointers may be changed as long as 'allow_customize' is set
23  */
24 static int allow_customize = 1;
25
26 static void *(*malloc_impl)(size_t, const char *, int)
27     = CRYPTO_malloc;
28 static void *(*realloc_impl)(void *, size_t, const char *, int)
29     = CRYPTO_realloc;
30 static void (*free_impl)(void *, const char *, int)
31     = CRYPTO_free;
32
33 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
34 static char *md_failstring;
35 static long md_count;
36 static int md_fail_percent = 0;
37 static int md_tracefd = -1;
38 static int call_malloc_debug = 1;
39
40 static void parseit(void);
41 static int shouldfail(void);
42
43 # define FAILTEST() if (shouldfail()) return NULL
44
45 #else
46 static int call_malloc_debug = 0;
47
48 # define FAILTEST() /* empty */
49 #endif
50
51 int CRYPTO_set_mem_functions(
52         void *(*m)(size_t, const char *, int),
53         void *(*r)(void *, size_t, const char *, int),
54         void (*f)(void *, const char *, int))
55 {
56     if (!allow_customize)
57         return 0;
58     if (m)
59         malloc_impl = m;
60     if (r)
61         realloc_impl = r;
62     if (f)
63         free_impl = f;
64     return 1;
65 }
66
67 int CRYPTO_set_mem_debug(int flag)
68 {
69     if (!allow_customize)
70         return 0;
71     call_malloc_debug = flag;
72     return 1;
73 }
74
75 void CRYPTO_get_mem_functions(
76         void *(**m)(size_t, const char *, int),
77         void *(**r)(void *, size_t, const char *, int),
78         void (**f)(void *, const char *, int))
79 {
80     if (m != NULL)
81         *m = malloc_impl;
82     if (r != NULL)
83         *r = realloc_impl;
84     if (f != NULL)
85         *f = free_impl;
86 }
87
88 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
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     int len;
131     char buff[80];
132
133     if (md_tracefd > 0) {
134         BIO_snprintf(buff, sizeof(buff),
135                      "%c C%ld %%%d R%d\n",
136                      shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
137         len = strlen(buff);
138         if (write(md_tracefd, buff, len) != len)
139             perror("shouldfail write failed");
140 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
141         if (shoulditfail) {
142             void *addrs[30];
143             int num = backtrace(addrs, OSSL_NELEM(addrs));
144
145             backtrace_symbols_fd(addrs, num, md_tracefd);
146         }
147 #endif
148     }
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     void *ret = NULL;
173
174     if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
175         return malloc_impl(num, file, line);
176
177     if (num == 0)
178         return NULL;
179
180     FAILTEST();
181     allow_customize = 0;
182 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
183     if (call_malloc_debug) {
184         CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);
185         ret = malloc(num);
186         CRYPTO_mem_debug_malloc(ret, num, 1, file, line);
187     } else {
188         ret = malloc(num);
189     }
190 #else
191     (void)(file); (void)(line);
192     ret = malloc(num);
193 #endif
194
195     return ret;
196 }
197
198 void *CRYPTO_zalloc(size_t num, const char *file, int line)
199 {
200     void *ret = CRYPTO_malloc(num, file, line);
201
202     FAILTEST();
203     if (ret != NULL)
204         memset(ret, 0, num);
205     return ret;
206 }
207
208 void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
209 {
210     if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)
211         return realloc_impl(str, num, file, line);
212
213     FAILTEST();
214     if (str == NULL)
215         return CRYPTO_malloc(num, file, line);
216
217     if (num == 0) {
218         CRYPTO_free(str, file, line);
219         return NULL;
220     }
221
222     allow_customize = 0;
223 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
224     if (call_malloc_debug) {
225         void *ret;
226         CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
227         ret = realloc(str, num);
228         CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
229         return ret;
230     }
231 #else
232     (void)(file); (void)(line);
233 #endif
234     return realloc(str, num);
235
236 }
237
238 void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
239                            const char *file, int line)
240 {
241     void *ret = NULL;
242
243     if (str == NULL)
244         return CRYPTO_malloc(num, file, line);
245
246     if (num == 0) {
247         CRYPTO_clear_free(str, old_len, file, line);
248         return NULL;
249     }
250
251     /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
252     if (num < old_len) {
253         OPENSSL_cleanse((char*)str + num, old_len - num);
254         return str;
255     }
256
257     ret = CRYPTO_malloc(num, file, line);
258     if (ret != NULL) {
259         memcpy(ret, str, old_len);
260         CRYPTO_clear_free(str, old_len, file, line);
261     }
262     return ret;
263 }
264
265 void CRYPTO_free(void *str, const char *file, int line)
266 {
267     if (free_impl != NULL && free_impl != &CRYPTO_free) {
268         free_impl(str, file, line);
269         return;
270     }
271
272 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
273     if (call_malloc_debug) {
274         CRYPTO_mem_debug_free(str, 0, file, line);
275         free(str);
276         CRYPTO_mem_debug_free(str, 1, file, line);
277     } else {
278         free(str);
279     }
280 #else
281     free(str);
282 #endif
283 }
284
285 void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
286 {
287     if (str == NULL)
288         return;
289     if (num)
290         OPENSSL_cleanse(str, num);
291     CRYPTO_free(str, file, line);
292 }