Coverage Report

Created: 2025-12-10 06:24

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