Coverage Report

Created: 2025-11-25 07:00

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