Add "random malloc failure" tooling
[openssl.git] / crypto / mem.c
1 /*
2  * Copyright 1995-2016 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 <stdio.h>
11 #include <stdlib.h>
12 #include <limits.h>
13 #include <openssl/crypto.h>
14 #include "internal/cryptlib.h"
15 #include "internal/cryptlib_int.h"
16 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
17 # include <execinfo.h>
18 #endif
19
20 /*
21  * the following pointers may be changed as long as 'allow_customize' is set
22  */
23 static int allow_customize = 1;
24
25 static void *(*malloc_impl)(size_t, const char *, int)
26     = CRYPTO_malloc;
27 static void *(*realloc_impl)(void *, size_t, const char *, int)
28     = CRYPTO_realloc;
29 static void (*free_impl)(void *, const char *, int)
30     = CRYPTO_free;
31
32 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
33 static char *md_failstring;
34 static long md_count;
35 static int md_percent = 100;
36 static int md_tracefd = -1;
37 static int call_malloc_debug = 1;
38
39 static void parseit(void);
40 static int shouldfail(void);
41
42 # define FAILTEST() if (shouldfail()) return NULL
43
44 #else
45 static int call_malloc_debug = 0;
46
47 # define FAILTEST() /* empty */
48 #endif
49
50 int CRYPTO_set_mem_functions(
51         void *(*m)(size_t, const char *, int),
52         void *(*r)(void *, size_t, const char *, int),
53         void (*f)(void *, const char *, int))
54 {
55     if (!allow_customize)
56         return 0;
57     if (m)
58         malloc_impl = m;
59     if (r)
60         realloc_impl = r;
61     if (f)
62         free_impl = f;
63     return 1;
64 }
65
66 int CRYPTO_set_mem_debug(int flag)
67 {
68     if (!allow_customize)
69         return 0;
70     call_malloc_debug = flag;
71     return 1;
72 }
73
74 void CRYPTO_get_mem_functions(
75         void *(**m)(size_t, const char *, int),
76         void *(**r)(void *, size_t, const char *, int),
77         void (**f)(void *, const char *, int))
78 {
79     if (m != NULL)
80         *m = malloc_impl;
81     if (r != NULL)
82         *r = realloc_impl;
83     if (f != NULL)
84         *f = free_impl;
85 }
86
87 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
88 /*
89  * Parse a "malloc failure spec" string.  This likes like a set of fields
90  * separated by semicolons.  Each field has a count and an optional failure
91  * percentage.  For example:
92  *          100;100@25;@100
93  * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
94  * all remaining (count is zero) succeed.
95  */
96 static void parseit(void)
97 {
98     char *semi = strchr(md_failstring, ';');
99     char *atsign;
100
101     if (semi != NULL)
102         *semi++ = '\0';
103
104     /* Get the count (atol will stop at the @ if there), and percentage */
105     md_count = atol(md_failstring);
106     atsign = strchr(md_failstring, '@');
107     md_percent = atsign == NULL ? 100 : atoi(atsign + 1);
108
109     if (semi != NULL)
110         md_failstring = semi;
111 }
112
113 /*
114  * See if the current malloc should fail.
115  */
116 static int shouldfail(void)
117 {
118     int roll = (int)(random() % 100);
119     int shouldfail = roll > md_percent;
120     char buff[80];
121
122     if (md_tracefd > 0) {
123         BIO_snprintf(buff, sizeof(buff),
124                      "%c C%ld %%%d R%d\n",
125                      shouldfail ? '-' : '+', md_count, md_percent, roll);
126         write(md_tracefd, buff, strlen(buff));
127 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
128         if (shouldfail) {
129             void *addrs[30];
130             int num = backtrace(addrs, OSSL_NELEM(addrs));
131
132             backtrace_symbols_fd(addrs, num, md_tracefd);
133         }
134 #endif
135     }
136
137     if (md_count) {
138         /* If we used up this one, go to the next. */
139         if (--md_count == 0)
140             parseit();
141     }
142
143     return shouldfail;
144 }
145
146 void ossl_malloc_setup_failures(void)
147 {
148     const char *cp = getenv("OPENSSL_MALLOC_FAILURES");
149
150     if (cp != NULL && (md_failstring = strdup(cp)) != NULL)
151         parseit();
152     if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
153         md_tracefd = atoi(cp);
154 }
155 #endif
156
157 void *CRYPTO_malloc(size_t num, const char *file, int line)
158 {
159     void *ret = NULL;
160
161     if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
162         return malloc_impl(num, file, line);
163
164     if (num <= 0)
165         return NULL;
166
167     FAILTEST();
168     allow_customize = 0;
169 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
170     if (call_malloc_debug) {
171         CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);
172         ret = malloc(num);
173         CRYPTO_mem_debug_malloc(ret, num, 1, file, line);
174     } else {
175         ret = malloc(num);
176     }
177 #else
178     osslargused(file); osslargused(line);
179     ret = malloc(num);
180 #endif
181
182     return ret;
183 }
184
185 void *CRYPTO_zalloc(size_t num, const char *file, int line)
186 {
187     void *ret = CRYPTO_malloc(num, file, line);
188
189     FAILTEST();
190     if (ret != NULL)
191         memset(ret, 0, num);
192     return ret;
193 }
194
195 void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
196 {
197     if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)
198         return realloc_impl(str, num, file, line);
199
200     FAILTEST();
201     if (str == NULL)
202         return CRYPTO_malloc(num, file, line);
203
204     if (num == 0) {
205         CRYPTO_free(str, file, line);
206         return NULL;
207     }
208
209     allow_customize = 0;
210 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
211     if (call_malloc_debug) {
212         void *ret;
213         CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
214         ret = realloc(str, num);
215         CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
216         return ret;
217     }
218 #else
219     osslargused(file); osslargused(line);
220 #endif
221     return realloc(str, num);
222
223 }
224
225 void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
226                            const char *file, int line)
227 {
228     void *ret = NULL;
229
230     if (str == NULL)
231         return CRYPTO_malloc(num, file, line);
232
233     if (num == 0) {
234         CRYPTO_clear_free(str, old_len, file, line);
235         return NULL;
236     }
237
238     /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
239     if (num < old_len) {
240         OPENSSL_cleanse((char*)str + num, old_len - num);
241         return str;
242     }
243
244     ret = CRYPTO_malloc(num, file, line);
245     if (ret != NULL) {
246         memcpy(ret, str, old_len);
247         CRYPTO_clear_free(str, old_len, file, line);
248     }
249     return ret;
250 }
251
252 void CRYPTO_free(void *str, const char *file, int line)
253 {
254     if (free_impl != NULL && free_impl != &CRYPTO_free) {
255         free_impl(str, file, line);
256         return;
257     }
258
259 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
260     if (call_malloc_debug) {
261         CRYPTO_mem_debug_free(str, 0, file, line);
262         free(str);
263         CRYPTO_mem_debug_free(str, 1, file, line);
264     } else {
265         free(str);
266     }
267 #else
268     free(str);
269 #endif
270 }
271
272 void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
273 {
274     if (str == NULL)
275         return;
276     if (num)
277         OPENSSL_cleanse(str, num);
278     CRYPTO_free(str, file, line);
279 }