Coverage Report

Created: 2025-12-31 06:58

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