sha/asm/keccak1600-avx512.pl: absorb bug-fix and minor optimization.
[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 <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_fail_percent = 0;
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@0;100@25;0@0
93  *    or    100;100@25;0
94  * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
95  * all remaining (count is zero) succeed.
96  */
97 static void parseit(void)
98 {
99     char *semi = strchr(md_failstring, ';');
100     char *atsign;
101
102     if (semi != NULL)
103         *semi++ = '\0';
104
105     /* Get the count (atol will stop at the @ if there), and percentage */
106     md_count = atol(md_failstring);
107     atsign = strchr(md_failstring, '@');
108     md_fail_percent = atsign == NULL ? 0 : atoi(atsign + 1);
109
110     if (semi != NULL)
111         md_failstring = semi;
112 }
113
114 /*
115  * Windows doesn't have random(), but it has rand()
116  * Some rand() implementations aren't good, but we're not
117  * dealing with secure randomness here.
118  */
119 #ifdef _WIN32
120 # define random() rand()
121 #endif
122 /*
123  * See if the current malloc should fail.
124  */
125 static int shouldfail(void)
126 {
127     int roll = (int)(random() % 100);
128     int shoulditfail = roll < md_fail_percent;
129     int len;
130     char buff[80];
131
132     if (md_tracefd > 0) {
133         BIO_snprintf(buff, sizeof(buff),
134                      "%c C%ld %%%d R%d\n",
135                      shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
136         len = strlen(buff);
137         if (write(md_tracefd, buff, len) != len)
138             perror("shouldfail write failed");
139 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
140         if (shoulditfail) {
141             void *addrs[30];
142             int num = backtrace(addrs, OSSL_NELEM(addrs));
143
144             backtrace_symbols_fd(addrs, num, md_tracefd);
145         }
146 #endif
147     }
148
149     if (md_count) {
150         /* If we used up this one, go to the next. */
151         if (--md_count == 0)
152             parseit();
153     }
154
155     return shoulditfail;
156 }
157
158 void ossl_malloc_setup_failures(void)
159 {
160     const char *cp = getenv("OPENSSL_MALLOC_FAILURES");
161
162     if (cp != NULL && (md_failstring = strdup(cp)) != NULL)
163         parseit();
164     if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
165         md_tracefd = atoi(cp);
166 }
167 #endif
168
169 void *CRYPTO_malloc(size_t num, const char *file, int line)
170 {
171     void *ret = NULL;
172
173     if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
174         return malloc_impl(num, file, line);
175
176     if (num == 0)
177         return NULL;
178
179     FAILTEST();
180     allow_customize = 0;
181 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
182     if (call_malloc_debug) {
183         CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);
184         ret = malloc(num);
185         CRYPTO_mem_debug_malloc(ret, num, 1, file, line);
186     } else {
187         ret = malloc(num);
188     }
189 #else
190     osslargused(file); osslargused(line);
191     ret = malloc(num);
192 #endif
193
194     return ret;
195 }
196
197 void *CRYPTO_zalloc(size_t num, const char *file, int line)
198 {
199     void *ret = CRYPTO_malloc(num, file, line);
200
201     FAILTEST();
202     if (ret != NULL)
203         memset(ret, 0, num);
204     return ret;
205 }
206
207 void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
208 {
209     if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)
210         return realloc_impl(str, num, file, line);
211
212     FAILTEST();
213     if (str == NULL)
214         return CRYPTO_malloc(num, file, line);
215
216     if (num == 0) {
217         CRYPTO_free(str, file, line);
218         return NULL;
219     }
220
221     allow_customize = 0;
222 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
223     if (call_malloc_debug) {
224         void *ret;
225         CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
226         ret = realloc(str, num);
227         CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
228         return ret;
229     }
230 #else
231     osslargused(file); osslargused(line);
232 #endif
233     return realloc(str, num);
234
235 }
236
237 void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
238                            const char *file, int line)
239 {
240     void *ret = NULL;
241
242     if (str == NULL)
243         return CRYPTO_malloc(num, file, line);
244
245     if (num == 0) {
246         CRYPTO_clear_free(str, old_len, file, line);
247         return NULL;
248     }
249
250     /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
251     if (num < old_len) {
252         OPENSSL_cleanse((char*)str + num, old_len - num);
253         return str;
254     }
255
256     ret = CRYPTO_malloc(num, file, line);
257     if (ret != NULL) {
258         memcpy(ret, str, old_len);
259         CRYPTO_clear_free(str, old_len, file, line);
260     }
261     return ret;
262 }
263
264 void CRYPTO_free(void *str, const char *file, int line)
265 {
266     if (free_impl != NULL && free_impl != &CRYPTO_free) {
267         free_impl(str, file, line);
268         return;
269     }
270
271 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
272     if (call_malloc_debug) {
273         CRYPTO_mem_debug_free(str, 0, file, line);
274         free(str);
275         CRYPTO_mem_debug_free(str, 1, file, line);
276     } else {
277         free(str);
278     }
279 #else
280     free(str);
281 #endif
282 }
283
284 void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
285 {
286     if (str == NULL)
287         return;
288     if (num)
289         OPENSSL_cleanse(str, num);
290     CRYPTO_free(str, file, line);
291 }