Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/mem_sec.c
Line
Count
Source
1
/*
2
 * Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
/*
12
 * This file is in two halves. The first half implements the public API
13
 * to be used by external consumers, and to be used by OpenSSL to store
14
 * data in a "secure arena." The second half implements the secure arena.
15
 * For details on that implementation, see below (look for uppercase
16
 * "SECURE HEAP IMPLEMENTATION").
17
 */
18
#include "internal/e_os.h"
19
#include <openssl/crypto.h>
20
#include <openssl/err.h>
21
22
#include <string.h>
23
24
#ifndef OPENSSL_NO_SECURE_MEMORY
25
#if defined(_WIN32)
26
#include <windows.h>
27
#if defined(WINAPI_FAMILY_PARTITION)
28
#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
29
/*
30
 * While VirtualLock is available under the app partition (e.g. UWP),
31
 * the headers do not define the API. Define it ourselves instead.
32
 */
33
WINBASEAPI
34
BOOL
35
    WINAPI
36
    VirtualLock(
37
        _In_ LPVOID lpAddress,
38
        _In_ SIZE_T dwSize);
39
#endif
40
#endif
41
#endif
42
#include <stdlib.h>
43
#include <assert.h>
44
#if defined(OPENSSL_SYS_UNIX)
45
#include <unistd.h>
46
#endif
47
#include <sys/types.h>
48
#if defined(OPENSSL_SYS_UNIX)
49
#include <sys/mman.h>
50
#if defined(__FreeBSD__)
51
#define MADV_DONTDUMP MADV_NOCORE
52
#endif
53
#if !defined(MAP_CONCEAL)
54
0
#define MAP_CONCEAL 0
55
#endif
56
#endif
57
#if defined(OPENSSL_SYS_LINUX)
58
#include <sys/syscall.h>
59
#if defined(SYS_mlock2)
60
#include <linux/mman.h>
61
#include <errno.h>
62
#endif
63
#include <sys/param.h>
64
#endif
65
#include <sys/stat.h>
66
#include <fcntl.h>
67
#endif
68
#ifndef HAVE_MADVISE
69
#if defined(MADV_DONTDUMP)
70
#define HAVE_MADVISE 1
71
#else
72
#define HAVE_MADVISE 0
73
#endif
74
#endif
75
#if HAVE_MADVISE
76
#undef NO_MADVISE
77
#else
78
#define NO_MADVISE
79
#endif
80
81
0
#define CLEAR(p, s) OPENSSL_cleanse(p, s)
82
#ifndef PAGE_SIZE
83
0
#define PAGE_SIZE 4096
84
#endif
85
#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
86
#define MAP_ANON MAP_ANONYMOUS
87
#endif
88
89
#ifndef OPENSSL_NO_SECURE_MEMORY
90
static size_t secure_mem_used;
91
92
static int secure_mem_initialized;
93
94
static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
95
96
/*
97
 * These are the functions that must be implemented by a secure heap (sh).
98
 */
99
static int sh_init(size_t size, size_t minsize);
100
static void *sh_malloc(size_t size);
101
static void sh_free(void *ptr);
102
static void sh_done(void);
103
static size_t sh_actual_size(char *ptr);
104
static int sh_allocated(const char *ptr);
105
#endif
106
107
int CRYPTO_secure_malloc_init(size_t size, size_t minsize)
108
0
{
109
0
#ifndef OPENSSL_NO_SECURE_MEMORY
110
0
    int ret = 0;
111
112
0
    if (!secure_mem_initialized) {
113
0
        sec_malloc_lock = CRYPTO_THREAD_lock_new();
114
0
        if (sec_malloc_lock == NULL)
115
0
            return 0;
116
0
        if ((ret = sh_init(size, minsize)) != 0) {
117
0
            secure_mem_initialized = 1;
118
0
        } else {
119
0
            CRYPTO_THREAD_lock_free(sec_malloc_lock);
120
0
            sec_malloc_lock = NULL;
121
0
        }
122
0
    }
123
124
0
    return ret;
125
#else
126
    return 0;
127
#endif /* OPENSSL_NO_SECURE_MEMORY */
128
0
}
129
130
int CRYPTO_secure_malloc_done(void)
131
3
{
132
3
#ifndef OPENSSL_NO_SECURE_MEMORY
133
3
    if (secure_mem_used == 0) {
134
3
        sh_done();
135
3
        secure_mem_initialized = 0;
136
3
        CRYPTO_THREAD_lock_free(sec_malloc_lock);
137
3
        sec_malloc_lock = NULL;
138
3
        return 1;
139
3
    }
140
0
#endif /* OPENSSL_NO_SECURE_MEMORY */
141
0
    return 0;
142
3
}
143
144
int CRYPTO_secure_malloc_initialized(void)
145
0
{
146
0
#ifndef OPENSSL_NO_SECURE_MEMORY
147
0
    return secure_mem_initialized;
148
#else
149
    return 0;
150
#endif /* OPENSSL_NO_SECURE_MEMORY */
151
0
}
152
153
void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
154
0
{
155
0
#ifndef OPENSSL_NO_SECURE_MEMORY
156
0
    void *ret = NULL;
157
0
    size_t actual_size;
158
0
    int reason = CRYPTO_R_SECURE_MALLOC_FAILURE;
159
160
0
    if (!secure_mem_initialized) {
161
0
        return CRYPTO_malloc(num, file, line);
162
0
    }
163
0
    if (!CRYPTO_THREAD_write_lock(sec_malloc_lock)) {
164
0
        reason = ERR_R_CRYPTO_LIB;
165
0
        goto err;
166
0
    }
167
0
    ret = sh_malloc(num);
168
0
    actual_size = ret ? sh_actual_size(ret) : 0;
169
0
    secure_mem_used += actual_size;
170
0
    CRYPTO_THREAD_unlock(sec_malloc_lock);
171
0
err:
172
0
    if (ret == NULL && (file != NULL || line != 0)) {
173
0
        ERR_new();
174
0
        ERR_set_debug(file, line, NULL);
175
0
        ERR_set_error(ERR_LIB_CRYPTO, reason, NULL);
176
0
    }
177
0
    return ret;
178
#else
179
    return CRYPTO_malloc(num, file, line);
180
#endif /* OPENSSL_NO_SECURE_MEMORY */
181
0
}
182
183
void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
184
0
{
185
0
#ifndef OPENSSL_NO_SECURE_MEMORY
186
0
    if (secure_mem_initialized)
187
        /* CRYPTO_secure_malloc() zeroes allocations when it is implemented */
188
0
        return CRYPTO_secure_malloc(num, file, line);
189
0
#endif
190
0
    return CRYPTO_zalloc(num, file, line);
191
0
}
192
193
void CRYPTO_secure_free(void *ptr, const char *file, int line)
194
0
{
195
0
#ifndef OPENSSL_NO_SECURE_MEMORY
196
0
    size_t actual_size;
197
198
0
    if (ptr == NULL)
199
0
        return;
200
0
    if (!CRYPTO_secure_allocated(ptr)) {
201
0
        CRYPTO_free(ptr, file, line);
202
0
        return;
203
0
    }
204
0
    if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
205
0
        return;
206
0
    actual_size = sh_actual_size(ptr);
207
0
    CLEAR(ptr, actual_size);
208
0
    secure_mem_used -= actual_size;
209
0
    sh_free(ptr);
210
0
    CRYPTO_THREAD_unlock(sec_malloc_lock);
211
#else
212
    CRYPTO_free(ptr, file, line);
213
#endif /* OPENSSL_NO_SECURE_MEMORY */
214
0
}
215
216
void CRYPTO_secure_clear_free(void *ptr, size_t num,
217
    const char *file, int line)
218
0
{
219
0
#ifndef OPENSSL_NO_SECURE_MEMORY
220
0
    size_t actual_size;
221
222
0
    if (ptr == NULL)
223
0
        return;
224
0
    if (!CRYPTO_secure_allocated(ptr)) {
225
0
        OPENSSL_cleanse(ptr, num);
226
0
        CRYPTO_free(ptr, file, line);
227
0
        return;
228
0
    }
229
0
    if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
230
0
        return;
231
0
    actual_size = sh_actual_size(ptr);
232
0
    CLEAR(ptr, actual_size);
233
0
    secure_mem_used -= actual_size;
234
0
    sh_free(ptr);
235
0
    CRYPTO_THREAD_unlock(sec_malloc_lock);
236
#else
237
    if (ptr == NULL)
238
        return;
239
    OPENSSL_cleanse(ptr, num);
240
    CRYPTO_free(ptr, file, line);
241
#endif /* OPENSSL_NO_SECURE_MEMORY */
242
0
}
243
244
int CRYPTO_secure_allocated(const void *ptr)
245
0
{
246
0
#ifndef OPENSSL_NO_SECURE_MEMORY
247
0
    if (!secure_mem_initialized)
248
0
        return 0;
249
    /*
250
     * Only read accesses to the arena take place in sh_allocated() and this
251
     * is only changed by the sh_init() and sh_done() calls which are not
252
     * locked.  Hence, it is safe to make this check without a lock too.
253
     */
254
0
    return sh_allocated(ptr);
255
#else
256
    return 0;
257
#endif /* OPENSSL_NO_SECURE_MEMORY */
258
0
}
259
260
size_t CRYPTO_secure_used(void)
261
0
{
262
0
    size_t ret = 0;
263
264
0
#ifndef OPENSSL_NO_SECURE_MEMORY
265
0
    if (!secure_mem_initialized)
266
0
        return 0;
267
0
    if (!CRYPTO_THREAD_read_lock(sec_malloc_lock))
268
0
        return 0;
269
270
0
    ret = secure_mem_used;
271
272
0
    CRYPTO_THREAD_unlock(sec_malloc_lock);
273
0
#endif /* OPENSSL_NO_SECURE_MEMORY */
274
0
    return ret;
275
0
}
276
277
size_t CRYPTO_secure_actual_size(void *ptr)
278
0
{
279
0
#ifndef OPENSSL_NO_SECURE_MEMORY
280
0
    size_t actual_size;
281
282
0
    if (!secure_mem_initialized)
283
0
        return 0;
284
0
    if (!CRYPTO_THREAD_read_lock(sec_malloc_lock))
285
0
        return 0;
286
0
    actual_size = sh_actual_size(ptr);
287
0
    CRYPTO_THREAD_unlock(sec_malloc_lock);
288
0
    return actual_size;
289
#else
290
    return 0;
291
#endif
292
0
}
293
294
/*
295
 * SECURE HEAP IMPLEMENTATION
296
 */
297
#ifndef OPENSSL_NO_SECURE_MEMORY
298
299
/*
300
 * The implementation provided here uses a fixed-sized mmap() heap,
301
 * which is locked into memory, not written to core files, and protected
302
 * on either side by an unmapped page, which will catch pointer overruns
303
 * (or underruns) and an attempt to read data out of the secure heap.
304
 * Free'd memory is zero'd or otherwise cleansed.
305
 *
306
 * This is a pretty standard buddy allocator.  We keep areas in a multiple
307
 * of "sh.minsize" units.  The freelist and bitmaps are kept separately,
308
 * so all (and only) data is kept in the mmap'd heap.
309
 *
310
 * This code assumes eight-bit bytes.  The numbers 3 and 7 are all over the
311
 * place.
312
 */
313
314
0
#define ONE ((size_t)1)
315
316
0
#define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
317
0
#define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
318
0
#define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
319
320
#define WITHIN_ARENA(p) \
321
0
    ((char *)(p) >= sh.arena && (char *)(p) < &sh.arena[sh.arena_size])
322
#define WITHIN_FREELIST(p) \
323
    ((char *)(p) >= (char *)sh.freelist && (char *)(p) < (char *)&sh.freelist[sh.freelist_size])
324
325
typedef struct sh_list_st {
326
    struct sh_list_st *next;
327
    struct sh_list_st **p_next;
328
} SH_LIST;
329
330
typedef struct sh_st {
331
    char *map_result;
332
    size_t map_size;
333
    char *arena;
334
    size_t arena_size;
335
    char **freelist;
336
    ossl_ssize_t freelist_size;
337
    size_t minsize;
338
    unsigned char *bittable;
339
    unsigned char *bitmalloc;
340
    size_t bittable_size; /* size in bits */
341
} SH;
342
343
static SH sh;
344
345
static size_t sh_getlist(char *ptr)
346
0
{
347
0
    ossl_ssize_t list = sh.freelist_size - 1;
348
0
    size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
349
350
0
    for (; bit; bit >>= 1, list--) {
351
0
        if (TESTBIT(sh.bittable, bit))
352
0
            break;
353
0
        OPENSSL_assert((bit & 1) == 0);
354
0
    }
355
356
0
    return list;
357
0
}
358
359
static int sh_testbit(char *ptr, int list, unsigned char *table)
360
0
{
361
0
    size_t bit;
362
363
0
    OPENSSL_assert(list >= 0 && list < sh.freelist_size);
364
0
    OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
365
0
    bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
366
0
    OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
367
0
    return TESTBIT(table, bit);
368
0
}
369
370
static void sh_clearbit(char *ptr, int list, unsigned char *table)
371
0
{
372
0
    size_t bit;
373
374
0
    OPENSSL_assert(list >= 0 && list < sh.freelist_size);
375
0
    OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
376
0
    bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
377
0
    OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
378
0
    OPENSSL_assert(TESTBIT(table, bit));
379
0
    CLEARBIT(table, bit);
380
0
}
381
382
static void sh_setbit(char *ptr, int list, unsigned char *table)
383
0
{
384
0
    size_t bit;
385
386
0
    OPENSSL_assert(list >= 0 && list < sh.freelist_size);
387
0
    OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
388
0
    bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
389
0
    OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
390
0
    OPENSSL_assert(!TESTBIT(table, bit));
391
0
    SETBIT(table, bit);
392
0
}
393
394
static void sh_add_to_list(char **list, char *ptr)
395
0
{
396
0
    SH_LIST *temp;
397
398
0
    OPENSSL_assert(WITHIN_FREELIST(list));
399
0
    OPENSSL_assert(WITHIN_ARENA(ptr));
400
401
0
    temp = (SH_LIST *)ptr;
402
0
    temp->next = *(SH_LIST **)list;
403
0
    OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
404
0
    temp->p_next = (SH_LIST **)list;
405
406
0
    if (temp->next != NULL) {
407
0
        OPENSSL_assert((char **)temp->next->p_next == list);
408
0
        temp->next->p_next = &(temp->next);
409
0
    }
410
411
0
    *list = ptr;
412
0
}
413
414
static void sh_remove_from_list(char *ptr)
415
0
{
416
0
    SH_LIST *temp, *temp2;
417
418
0
    temp = (SH_LIST *)ptr;
419
0
    if (temp->next != NULL)
420
0
        temp->next->p_next = temp->p_next;
421
0
    *temp->p_next = temp->next;
422
0
    if (temp->next == NULL)
423
0
        return;
424
425
0
    temp2 = temp->next;
426
0
    OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
427
0
}
428
429
static int sh_init(size_t size, size_t minsize)
430
0
{
431
0
    int ret;
432
0
    size_t i;
433
0
    size_t pgsize;
434
0
    size_t aligned;
435
#if defined(_WIN32)
436
    DWORD flOldProtect;
437
    SYSTEM_INFO systemInfo;
438
#endif
439
440
0
    memset(&sh, 0, sizeof(sh));
441
442
    /* make sure size is a powers of 2 */
443
0
    OPENSSL_assert(size > 0);
444
0
    OPENSSL_assert((size & (size - 1)) == 0);
445
0
    if (size == 0 || (size & (size - 1)) != 0)
446
0
        goto err;
447
448
0
    if (minsize <= sizeof(SH_LIST)) {
449
0
        OPENSSL_assert(sizeof(SH_LIST) <= 65536);
450
        /*
451
         * Compute the minimum possible allocation size.
452
         * This must be a power of 2 and at least as large as the SH_LIST
453
         * structure.
454
         */
455
0
        minsize = sizeof(SH_LIST) - 1;
456
0
        minsize |= minsize >> 1;
457
0
        minsize |= minsize >> 2;
458
0
        if (sizeof(SH_LIST) > 16)
459
0
            minsize |= minsize >> 4;
460
0
        if (sizeof(SH_LIST) > 256)
461
0
            minsize |= minsize >> 8;
462
0
        minsize++;
463
0
    } else {
464
        /* make sure minsize is a powers of 2 */
465
0
        OPENSSL_assert((minsize & (minsize - 1)) == 0);
466
0
        if ((minsize & (minsize - 1)) != 0)
467
0
            goto err;
468
0
    }
469
470
0
    sh.arena_size = size;
471
0
    sh.minsize = minsize;
472
0
    sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
473
474
    /* Prevent allocations of size 0 later on */
475
0
    if (sh.bittable_size >> 3 == 0)
476
0
        goto err;
477
478
0
    sh.freelist_size = -1;
479
0
    for (i = sh.bittable_size; i; i >>= 1)
480
0
        sh.freelist_size++;
481
482
0
    sh.freelist = OPENSSL_calloc(sh.freelist_size, sizeof(char *));
483
0
    OPENSSL_assert(sh.freelist != NULL);
484
0
    if (sh.freelist == NULL)
485
0
        goto err;
486
487
0
    sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
488
0
    OPENSSL_assert(sh.bittable != NULL);
489
0
    if (sh.bittable == NULL)
490
0
        goto err;
491
492
0
    sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
493
0
    OPENSSL_assert(sh.bitmalloc != NULL);
494
0
    if (sh.bitmalloc == NULL)
495
0
        goto err;
496
497
    /* Allocate space for heap, and two extra pages as guards */
498
0
#if defined(_SC_PAGE_SIZE) || defined(_SC_PAGESIZE)
499
0
    {
500
0
#if defined(_SC_PAGE_SIZE)
501
0
        long tmppgsize = sysconf(_SC_PAGE_SIZE);
502
#else
503
        long tmppgsize = sysconf(_SC_PAGESIZE);
504
#endif
505
0
        if (tmppgsize < 1)
506
0
            pgsize = PAGE_SIZE;
507
0
        else
508
0
            pgsize = (size_t)tmppgsize;
509
0
    }
510
#elif defined(_WIN32)
511
    GetSystemInfo(&systemInfo);
512
    pgsize = (size_t)systemInfo.dwPageSize;
513
#else
514
    pgsize = PAGE_SIZE;
515
#endif
516
0
    sh.map_size = pgsize + sh.arena_size + pgsize;
517
518
0
#if !defined(_WIN32)
519
0
#ifdef MAP_ANON
520
0
    sh.map_result = mmap(NULL, sh.map_size,
521
0
        PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE | MAP_CONCEAL, -1, 0);
522
#else
523
    {
524
        int fd;
525
526
        sh.map_result = MAP_FAILED;
527
        if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
528
            sh.map_result = mmap(NULL, sh.map_size,
529
                PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
530
            close(fd);
531
        }
532
    }
533
#endif
534
0
    if (sh.map_result == MAP_FAILED)
535
0
        goto err;
536
#else
537
    sh.map_result = VirtualAlloc(NULL, sh.map_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
538
539
    if (sh.map_result == NULL)
540
        goto err;
541
#endif
542
543
0
    sh.arena = (char *)(sh.map_result + pgsize);
544
0
    sh_setbit(sh.arena, 0, sh.bittable);
545
0
    sh_add_to_list(&sh.freelist[0], sh.arena);
546
547
    /* Now try to add guard pages and lock into memory. */
548
0
    ret = 1;
549
550
0
#if !defined(_WIN32)
551
    /* Starting guard is already aligned from mmap. */
552
0
    if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
553
0
        ret = 2;
554
#else
555
    if (VirtualProtect(sh.map_result, pgsize, PAGE_NOACCESS, &flOldProtect) == FALSE)
556
        ret = 2;
557
#endif
558
559
    /* Ending guard page - need to round up to page boundary */
560
0
    aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
561
0
#if !defined(_WIN32)
562
0
    if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
563
0
        ret = 2;
564
#else
565
    if (VirtualProtect(sh.map_result + aligned, pgsize, PAGE_NOACCESS, &flOldProtect) == FALSE)
566
        ret = 2;
567
#endif
568
569
0
#if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
570
0
    if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
571
0
        if (errno == ENOSYS) {
572
0
            if (mlock(sh.arena, sh.arena_size) < 0)
573
0
                ret = 2;
574
0
        } else {
575
0
            ret = 2;
576
0
        }
577
0
    }
578
#elif defined(_WIN32)
579
    if (VirtualLock(sh.arena, sh.arena_size) == FALSE)
580
        ret = 2;
581
#else
582
    if (mlock(sh.arena, sh.arena_size) < 0)
583
        ret = 2;
584
#endif
585
0
#ifndef NO_MADVISE
586
0
    if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
587
0
        ret = 2;
588
0
#endif
589
590
0
    return ret;
591
592
0
err:
593
0
    sh_done();
594
0
    return 0;
595
0
}
596
597
static void sh_done(void)
598
3
{
599
3
    OPENSSL_free(sh.freelist);
600
3
    OPENSSL_free(sh.bittable);
601
3
    OPENSSL_free(sh.bitmalloc);
602
3
#if !defined(_WIN32)
603
3
    if (sh.map_result != MAP_FAILED && sh.map_size)
604
0
        munmap(sh.map_result, sh.map_size);
605
#else
606
    if (sh.map_result != NULL && sh.map_size)
607
        VirtualFree(sh.map_result, 0, MEM_RELEASE);
608
#endif
609
3
    memset(&sh, 0, sizeof(sh));
610
3
}
611
612
static int sh_allocated(const char *ptr)
613
0
{
614
0
    return WITHIN_ARENA(ptr) ? 1 : 0;
615
0
}
616
617
static char *sh_find_my_buddy(char *ptr, int list)
618
0
{
619
0
    size_t bit;
620
0
    char *chunk = NULL;
621
622
0
    bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
623
0
    bit ^= 1;
624
625
0
    if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
626
0
        chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
627
628
0
    return chunk;
629
0
}
630
631
static void *sh_malloc(size_t size)
632
0
{
633
0
    ossl_ssize_t list, slist;
634
0
    size_t i;
635
0
    char *chunk;
636
637
0
    if (size > sh.arena_size)
638
0
        return NULL;
639
640
0
    list = sh.freelist_size - 1;
641
0
    for (i = sh.minsize; i < size; i <<= 1)
642
0
        list--;
643
0
    if (list < 0)
644
0
        return NULL;
645
646
    /* try to find a larger entry to split */
647
0
    for (slist = list; slist >= 0; slist--)
648
0
        if (sh.freelist[slist] != NULL)
649
0
            break;
650
0
    if (slist < 0)
651
0
        return NULL;
652
653
    /* split larger entry */
654
0
    while (slist != list) {
655
0
        char *temp = sh.freelist[slist];
656
657
        /* remove from bigger list */
658
0
        OPENSSL_assert(!sh_testbit(temp, (int)slist, sh.bitmalloc));
659
0
        sh_clearbit(temp, (int)slist, sh.bittable);
660
0
        sh_remove_from_list(temp);
661
0
        OPENSSL_assert(temp != sh.freelist[slist]);
662
663
        /* done with bigger list */
664
0
        slist++;
665
666
        /* add to smaller list */
667
0
        OPENSSL_assert(!sh_testbit(temp, (int)slist, sh.bitmalloc));
668
0
        sh_setbit(temp, (int)slist, sh.bittable);
669
0
        sh_add_to_list(&sh.freelist[slist], temp);
670
0
        OPENSSL_assert(sh.freelist[slist] == temp);
671
672
        /* split in 2 */
673
0
        temp += sh.arena_size >> slist;
674
0
        OPENSSL_assert(!sh_testbit(temp, (int)slist, sh.bitmalloc));
675
0
        sh_setbit(temp, (int)slist, sh.bittable);
676
0
        sh_add_to_list(&sh.freelist[slist], temp);
677
0
        OPENSSL_assert(sh.freelist[slist] == temp);
678
679
0
        OPENSSL_assert(temp - (sh.arena_size >> slist) == sh_find_my_buddy(temp, (int)slist));
680
0
    }
681
682
    /* peel off memory to hand back */
683
0
    chunk = sh.freelist[list];
684
0
    OPENSSL_assert(sh_testbit(chunk, (int)list, sh.bittable));
685
0
    sh_setbit(chunk, (int)list, sh.bitmalloc);
686
0
    sh_remove_from_list(chunk);
687
688
0
    OPENSSL_assert(WITHIN_ARENA(chunk));
689
690
    /* zero the free list header as a precaution against information leakage */
691
0
    memset(chunk, 0, sizeof(SH_LIST));
692
693
0
    return chunk;
694
0
}
695
696
static void sh_free(void *ptr)
697
0
{
698
0
    size_t list;
699
0
    void *buddy;
700
701
0
    if (ptr == NULL)
702
0
        return;
703
0
    OPENSSL_assert(WITHIN_ARENA(ptr));
704
0
    if (!WITHIN_ARENA(ptr))
705
0
        return;
706
707
0
    list = sh_getlist(ptr);
708
0
    OPENSSL_assert(sh_testbit(ptr, (int)list, sh.bittable));
709
0
    sh_clearbit(ptr, (int)list, sh.bitmalloc);
710
0
    sh_add_to_list(&sh.freelist[list], ptr);
711
712
    /* Try to coalesce two adjacent free areas. */
713
0
    while ((buddy = sh_find_my_buddy(ptr, (int)list)) != NULL) {
714
0
        OPENSSL_assert(ptr == sh_find_my_buddy(buddy, (int)list));
715
0
        OPENSSL_assert(ptr != NULL);
716
0
        OPENSSL_assert(!sh_testbit(ptr, (int)list, sh.bitmalloc));
717
0
        sh_clearbit(ptr, (int)list, sh.bittable);
718
0
        sh_remove_from_list(ptr);
719
0
        OPENSSL_assert(!sh_testbit(ptr, (int)list, sh.bitmalloc));
720
0
        sh_clearbit(buddy, (int)list, sh.bittable);
721
0
        sh_remove_from_list(buddy);
722
723
0
        list--;
724
725
        /* Zero the higher addressed block's free list pointers */
726
0
        memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST));
727
0
        if (ptr > buddy)
728
0
            ptr = buddy;
729
730
0
        OPENSSL_assert(!sh_testbit(ptr, (int)list, sh.bitmalloc));
731
0
        sh_setbit(ptr, (int)list, sh.bittable);
732
0
        sh_add_to_list(&sh.freelist[list], ptr);
733
0
        OPENSSL_assert(sh.freelist[list] == ptr);
734
0
    }
735
0
}
736
737
static size_t sh_actual_size(char *ptr)
738
0
{
739
0
    int list;
740
741
0
    OPENSSL_assert(WITHIN_ARENA(ptr));
742
0
    if (!WITHIN_ARENA(ptr))
743
0
        return 0;
744
0
    list = (int)sh_getlist(ptr);
745
0
    OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
746
0
    return sh.arena_size / (ONE << list);
747
0
}
748
#endif /* OPENSSL_NO_SECURE_MEMORY */