VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.3/crypto/mem.c@ 102427

Last change on this file since 102427 was 101211, checked in by vboxsync, 17 months ago

openssl-3.1.3: Applied and adjusted our OpenSSL changes to 3.1.2. bugref:10527

File size: 7.9 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 */
103static void parseit(void)
104{
105 char *semi = strchr(md_failstring, ';');
106 char *atsign;
107
108 if (semi != NULL)
109 *semi++ = '\0';
110
111 /* Get the count (atol will stop at the @ if there), and percentage */
112 md_count = atol(md_failstring);
113 atsign = strchr(md_failstring, '@');
114 md_fail_percent = atsign == NULL ? 0 : atoi(atsign + 1);
115
116 if (semi != NULL)
117 md_failstring = semi;
118}
119
120/*
121 * Windows doesn't have random(), but it has rand()
122 * Some rand() implementations aren't good, but we're not
123 * dealing with secure randomness here.
124 */
125# ifdef _WIN32
126# define random() rand()
127# endif
128/*
129 * See if the current malloc should fail.
130 */
131static int shouldfail(void)
132{
133 int roll = (int)(random() % 100);
134 int shoulditfail = roll < md_fail_percent;
135# ifndef _WIN32
136/* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
137 int len;
138 char buff[80];
139
140 if (md_tracefd > 0) {
141 BIO_snprintf(buff, sizeof(buff),
142 "%c C%ld %%%d R%d\n",
143 shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
144 len = strlen(buff);
145 if (write(md_tracefd, buff, len) != len)
146 perror("shouldfail write failed");
147 }
148# endif
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
159void 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
170void *CRYPTO_malloc(size_t num, const char *file, int line)
171{
172 INCREMENT(malloc_count);
173 if (malloc_impl != CRYPTO_malloc)
174 return malloc_impl(num, file, line);
175
176 if (num == 0)
177 return NULL;
178
179 FAILTEST();
180 if (allow_customize) {
181 /*
182 * Disallow customization after the first allocation. We only set this
183 * if necessary to avoid a store to the same cache line on every
184 * allocation.
185 */
186 allow_customize = 0;
187 }
188
189 return malloc(num);
190}
191
192void *CRYPTO_zalloc(size_t num, const char *file, int line)
193{
194 void *ret;
195
196 ret = CRYPTO_malloc(num, file, line);
197 if (ret != NULL)
198 memset(ret, 0, num);
199
200 return ret;
201}
202
203void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
204{
205 INCREMENT(realloc_count);
206 if (realloc_impl != CRYPTO_realloc)
207 return realloc_impl(str, num, file, line);
208
209 if (str == NULL)
210 return CRYPTO_malloc(num, file, line);
211
212 if (num == 0) {
213 CRYPTO_free(str, file, line);
214 return NULL;
215 }
216
217 FAILTEST();
218 return realloc(str, num);
219}
220
221void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
222 const char *file, int line)
223{
224 void *ret = NULL;
225
226 if (str == NULL)
227 return CRYPTO_malloc(num, file, line);
228
229 if (num == 0) {
230 CRYPTO_clear_free(str, old_len, file, line);
231 return NULL;
232 }
233
234 /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
235 if (num < old_len) {
236 OPENSSL_cleanse((char*)str + num, old_len - num);
237 return str;
238 }
239
240 ret = CRYPTO_malloc(num, file, line);
241 if (ret != NULL) {
242 memcpy(ret, str, old_len);
243 CRYPTO_clear_free(str, old_len, file, line);
244 }
245 return ret;
246}
247
248void CRYPTO_free(void *str, const char *file, int line)
249{
250 INCREMENT(free_count);
251 if (free_impl != CRYPTO_free) {
252 free_impl(str, file, line);
253 return;
254 }
255
256 free(str);
257}
258
259void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
260{
261 if (str == NULL)
262 return;
263 if (num)
264 OPENSSL_cleanse(str, num);
265 CRYPTO_free(str, file, line);
266}
267
268#if !defined(OPENSSL_NO_CRYPTO_MDEBUG)
269
270# ifndef OPENSSL_NO_DEPRECATED_3_0
271int CRYPTO_mem_ctrl(int mode)
272{
273 (void)mode;
274 return -1;
275}
276
277int CRYPTO_set_mem_debug(int flag)
278{
279 (void)flag;
280 return -1;
281}
282
283int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
284{
285 (void)info; (void)file; (void)line;
286 return 0;
287}
288
289int CRYPTO_mem_debug_pop(void)
290{
291 return 0;
292}
293
294void CRYPTO_mem_debug_malloc(void *addr, size_t num, int flag,
295 const char *file, int line)
296{
297 (void)addr; (void)num; (void)flag; (void)file; (void)line;
298}
299
300void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num, int flag,
301 const char *file, int line)
302{
303 (void)addr1; (void)addr2; (void)num; (void)flag; (void)file; (void)line;
304}
305
306void CRYPTO_mem_debug_free(void *addr, int flag,
307 const char *file, int line)
308{
309 (void)addr; (void)flag; (void)file; (void)line;
310}
311
312int CRYPTO_mem_leaks(BIO *b)
313{
314 (void)b;
315 return -1;
316}
317
318# ifndef OPENSSL_NO_STDIO
319int CRYPTO_mem_leaks_fp(FILE *fp)
320{
321 (void)fp;
322 return -1;
323}
324# endif
325
326int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u),
327 void *u)
328{
329 (void)cb; (void)u;
330 return -1;
331}
332
333# endif
334
335#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