Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/mem.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2024 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 "crypto/cryptlib.h"
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <limits.h>
16
#include <openssl/crypto.h>
17
18
/*
19
 * the following pointers may be changed as long as 'allow_customize' is set
20
 */
21
static int allow_customize = 1;
22
static CRYPTO_malloc_fn malloc_impl = CRYPTO_malloc;
23
static CRYPTO_realloc_fn realloc_impl = CRYPTO_realloc;
24
static CRYPTO_free_fn free_impl = CRYPTO_free;
25
26
#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODULE)
27
#include "internal/tsan_assist.h"
28
29
#ifdef TSAN_REQUIRES_LOCKING
30
#define INCREMENT(x) /* empty */
31
#define LOAD(x) 0
32
#else /* TSAN_REQUIRES_LOCKING */
33
static TSAN_QUALIFIER int malloc_count;
34
static TSAN_QUALIFIER int realloc_count;
35
static TSAN_QUALIFIER int free_count;
36
37
#define INCREMENT(x) tsan_counter(&(x))
38
#define LOAD(x) tsan_load(&x)
39
#endif /* TSAN_REQUIRES_LOCKING */
40
41
static char *md_failstring;
42
static long md_count;
43
static int md_fail_percent = 0;
44
static int md_tracefd = -1;
45
46
static void parseit(void);
47
static int shouldfail(void);
48
49
#define FAILTEST()    \
50
    if (shouldfail()) \
51
    return NULL
52
53
#else
54
55
#define INCREMENT(x) /* empty */
56
#define FAILTEST() /* empty */
57
#endif
58
59
int CRYPTO_set_mem_functions(CRYPTO_malloc_fn malloc_fn,
60
    CRYPTO_realloc_fn realloc_fn,
61
    CRYPTO_free_fn free_fn)
62
0
{
63
0
    if (!allow_customize)
64
0
        return 0;
65
0
    if (malloc_fn != NULL)
66
0
        malloc_impl = malloc_fn;
67
0
    if (realloc_fn != NULL)
68
0
        realloc_impl = realloc_fn;
69
0
    if (free_fn != NULL)
70
0
        free_impl = free_fn;
71
0
    return 1;
72
0
}
73
74
void CRYPTO_get_mem_functions(CRYPTO_malloc_fn *malloc_fn,
75
    CRYPTO_realloc_fn *realloc_fn,
76
    CRYPTO_free_fn *free_fn)
77
0
{
78
0
    if (malloc_fn != NULL)
79
0
        *malloc_fn = malloc_impl;
80
0
    if (realloc_fn != NULL)
81
0
        *realloc_fn = realloc_impl;
82
0
    if (free_fn != NULL)
83
0
        *free_fn = free_impl;
84
0
}
85
86
#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODULE)
87
void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount)
88
{
89
    if (mcount != NULL)
90
        *mcount = LOAD(malloc_count);
91
    if (rcount != NULL)
92
        *rcount = LOAD(realloc_count);
93
    if (fcount != NULL)
94
        *fcount = LOAD(free_count);
95
}
96
97
/*
98
 * Parse a "malloc failure spec" string.  This likes like a set of fields
99
 * separated by semicolons.  Each field has a count and an optional failure
100
 * percentage.  For example:
101
 *          100@0;100@25;0@0
102
 *    or    100;100@25;0
103
 * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
104
 * all remaining (count is zero) succeed.
105
 * The failure percentge can have 2 digits after the comma.  For example:
106
 *          0@0.01
107
 * This means 0.01% of all allocations will fail.
108
 */
109
static void parseit(void)
110
{
111
    char *semi = strchr(md_failstring, ';');
112
    char *atsign;
113
114
    if (semi != NULL)
115
        *semi++ = '\0';
116
117
    /* Get the count (atol will stop at the @ if there), and percentage */
118
    md_count = atol(md_failstring);
119
    atsign = strchr(md_failstring, '@');
120
    md_fail_percent = atsign == NULL ? 0 : (int)(atof(atsign + 1) * 100 + 0.5);
121
122
    if (semi != NULL)
123
        md_failstring = semi;
124
}
125
126
/*
127
 * Windows doesn't have random() and srandom(), but it has rand() and srand().
128
 * Some rand() implementations aren't good, but we're not
129
 * dealing with secure randomness here.
130
 */
131
#ifdef _WIN32
132
#define random() rand()
133
#define srandom(seed) srand(seed)
134
#endif
135
/*
136
 * See if the current malloc should fail.
137
 */
138
static int shouldfail(void)
139
{
140
    int roll = (int)(random() % 10000);
141
    int shoulditfail = roll < md_fail_percent;
142
#ifndef _WIN32
143
    /* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
144
    int len;
145
    char buff[80];
146
147
    if (md_tracefd > 0) {
148
        BIO_snprintf(buff, sizeof(buff),
149
            "%c C%ld %%%d R%d\n",
150
            shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
151
        len = strlen(buff);
152
        if (write(md_tracefd, buff, len) != len)
153
            perror("shouldfail write failed");
154
    }
155
#endif
156
157
    if (md_count) {
158
        /* If we used up this one, go to the next. */
159
        if (--md_count == 0)
160
            parseit();
161
    }
162
163
    return shoulditfail;
164
}
165
166
void ossl_malloc_setup_failures(void)
167
{
168
    const char *cp = getenv("OPENSSL_MALLOC_FAILURES");
169
170
    if (cp != NULL && (md_failstring = strdup(cp)) != NULL)
171
        parseit();
172
    if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
173
        md_tracefd = atoi(cp);
174
    if ((cp = getenv("OPENSSL_MALLOC_SEED")) != NULL)
175
        srandom(atoi(cp));
176
}
177
#endif
178
179
void *CRYPTO_malloc(size_t num, const char *file, int line)
180
949M
{
181
949M
    void *ptr;
182
183
949M
    INCREMENT(malloc_count);
184
949M
    if (malloc_impl != CRYPTO_malloc) {
185
0
        ptr = malloc_impl(num, file, line);
186
0
        if (ptr != NULL || num == 0)
187
0
            return ptr;
188
0
        goto err;
189
0
    }
190
191
949M
    if (num == 0)
192
66
        return NULL;
193
194
949M
    FAILTEST();
195
949M
    if (allow_customize) {
196
        /*
197
         * Disallow customization after the first allocation. We only set this
198
         * if necessary to avoid a store to the same cache line on every
199
         * allocation.
200
         */
201
146
        allow_customize = 0;
202
146
    }
203
204
949M
    ptr = malloc(num);
205
949M
    if (ptr != NULL)
206
949M
        return ptr;
207
0
err:
208
    /*
209
     * ossl_err_get_state_int() in err.c uses CRYPTO_zalloc(num, NULL, 0) for
210
     * ERR_STATE allocation. Prevent mem alloc error loop while reporting error.
211
     */
212
0
    if (file != NULL || line != 0) {
213
0
        ERR_new();
214
0
        ERR_set_debug(file, line, NULL);
215
0
        ERR_set_error(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE, NULL);
216
0
    }
217
0
    return NULL;
218
949M
}
219
220
void *CRYPTO_zalloc(size_t num, const char *file, int line)
221
776M
{
222
776M
    void *ret;
223
224
776M
    ret = CRYPTO_malloc(num, file, line);
225
776M
    if (ret != NULL)
226
776M
        memset(ret, 0, num);
227
228
776M
    return ret;
229
776M
}
230
231
void *CRYPTO_aligned_alloc(size_t num, size_t alignment, void **freeptr,
232
    const char *file, int line)
233
276
{
234
276
    void *ret;
235
236
276
    *freeptr = NULL;
237
238
#if defined(OPENSSL_SMALL_FOOTPRINT)
239
    ret = freeptr = NULL;
240
    return ret;
241
#endif
242
243
    /* Allow non-malloc() allocations as long as no malloc_impl is provided. */
244
276
    if (malloc_impl == CRYPTO_malloc) {
245
276
#if defined(_BSD_SOURCE) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
246
276
        if (posix_memalign(&ret, alignment, num))
247
0
            return NULL;
248
276
        *freeptr = ret;
249
276
        return ret;
250
#elif defined(_ISOC11_SOURCE)
251
        ret = *freeptr = aligned_alloc(alignment, num);
252
        return ret;
253
#endif
254
276
    }
255
256
    /* we have to do this the hard way */
257
258
    /*
259
     * Note: Windows supports an _aligned_malloc call, but we choose
260
     * not to use it here, because allocations from that function
261
     * require that they be freed via _aligned_free.  Given that
262
     * we can't differentiate plain malloc blocks from blocks obtained
263
     * via _aligned_malloc, just avoid its use entirely
264
     */
265
266
    /*
267
     * Step 1: Allocate an amount of memory that is <alignment>
268
     * bytes bigger than requested
269
     */
270
0
    *freeptr = CRYPTO_malloc(num + alignment, file, line);
271
0
    if (*freeptr == NULL)
272
0
        return NULL;
273
274
    /*
275
     * Step 2: Add <alignment - 1> bytes to the pointer
276
     * This will cross the alignment boundary that is
277
     * requested
278
     */
279
0
    ret = (void *)((char *)*freeptr + (alignment - 1));
280
281
    /*
282
     * Step 3: Use the alignment as a mask to translate the
283
     * least significant bits of the allocation at the alignment
284
     * boundary to 0.  ret now holds a pointer to the memory
285
     * buffer at the requested alignment
286
     * NOTE: It is a documented requirement that alignment be a
287
     * power of 2, which is what allows this to work
288
     */
289
0
    ret = (void *)((uintptr_t)ret & (uintptr_t)(~(alignment - 1)));
290
0
    return ret;
291
0
}
292
293
void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
294
70.0M
{
295
70.0M
    INCREMENT(realloc_count);
296
70.0M
    if (realloc_impl != CRYPTO_realloc)
297
0
        return realloc_impl(str, num, file, line);
298
299
70.0M
    if (str == NULL)
300
57.5M
        return CRYPTO_malloc(num, file, line);
301
302
12.5M
    if (num == 0) {
303
0
        CRYPTO_free(str, file, line);
304
0
        return NULL;
305
0
    }
306
307
12.5M
    FAILTEST();
308
12.5M
    return realloc(str, num);
309
12.5M
}
310
311
void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
312
    const char *file, int line)
313
34.7M
{
314
34.7M
    void *ret = NULL;
315
316
34.7M
    if (str == NULL)
317
14.8M
        return CRYPTO_malloc(num, file, line);
318
319
19.8M
    if (num == 0) {
320
0
        CRYPTO_clear_free(str, old_len, file, line);
321
0
        return NULL;
322
0
    }
323
324
    /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
325
19.8M
    if (num < old_len) {
326
0
        OPENSSL_cleanse((char *)str + num, old_len - num);
327
0
        return str;
328
0
    }
329
330
19.8M
    ret = CRYPTO_malloc(num, file, line);
331
19.8M
    if (ret != NULL) {
332
19.8M
        memcpy(ret, str, old_len);
333
19.8M
        CRYPTO_clear_free(str, old_len, file, line);
334
19.8M
    }
335
19.8M
    return ret;
336
19.8M
}
337
338
void CRYPTO_free(void *str, const char *file, int line)
339
5.91G
{
340
5.91G
    INCREMENT(free_count);
341
5.91G
    if (free_impl != CRYPTO_free) {
342
0
        free_impl(str, file, line);
343
0
        return;
344
0
    }
345
346
5.91G
    free(str);
347
5.91G
}
348
349
void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
350
98.5M
{
351
98.5M
    if (str == NULL)
352
10.4M
        return;
353
88.0M
    if (num)
354
88.0M
        OPENSSL_cleanse(str, num);
355
88.0M
    CRYPTO_free(str, file, line);
356
88.0M
}
357
358
#if !defined(OPENSSL_NO_CRYPTO_MDEBUG)
359
360
#ifndef OPENSSL_NO_DEPRECATED_3_0
361
int CRYPTO_mem_ctrl(int mode)
362
{
363
    (void)mode;
364
    return -1;
365
}
366
367
int CRYPTO_set_mem_debug(int flag)
368
{
369
    (void)flag;
370
    return -1;
371
}
372
373
int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
374
{
375
    (void)info;
376
    (void)file;
377
    (void)line;
378
    return 0;
379
}
380
381
int CRYPTO_mem_debug_pop(void)
382
{
383
    return 0;
384
}
385
386
void CRYPTO_mem_debug_malloc(void *addr, size_t num, int flag,
387
    const char *file, int line)
388
{
389
    (void)addr;
390
    (void)num;
391
    (void)flag;
392
    (void)file;
393
    (void)line;
394
}
395
396
void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num, int flag,
397
    const char *file, int line)
398
{
399
    (void)addr1;
400
    (void)addr2;
401
    (void)num;
402
    (void)flag;
403
    (void)file;
404
    (void)line;
405
}
406
407
void CRYPTO_mem_debug_free(void *addr, int flag,
408
    const char *file, int line)
409
{
410
    (void)addr;
411
    (void)flag;
412
    (void)file;
413
    (void)line;
414
}
415
416
int CRYPTO_mem_leaks(BIO *b)
417
{
418
    (void)b;
419
    return -1;
420
}
421
422
#ifndef OPENSSL_NO_STDIO
423
int CRYPTO_mem_leaks_fp(FILE *fp)
424
{
425
    (void)fp;
426
    return -1;
427
}
428
#endif
429
430
int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u),
431
    void *u)
432
{
433
    (void)cb;
434
    (void)u;
435
    return -1;
436
}
437
438
#endif
439
440
#endif