Coverage Report

Created: 2026-05-06 07:07

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