VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.7/crypto/mem.c@ 107935

Last change on this file since 107935 was 104078, checked in by vboxsync, 11 months ago

openssl-3.1.5: Applied and adjusted our OpenSSL changes to 3.1.4. bugref:10638

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette