Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/mem_sec.c
Line
Count
Source
1
/*
2
 * Copyright 2015-2024 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
220
{
132
220
#ifndef OPENSSL_NO_SECURE_MEMORY
133
220
    if (secure_mem_used == 0) {
134
220
        sh_done();
135
220
        secure_mem_initialized = 0;
136
220
        CRYPTO_THREAD_lock_free(sec_malloc_lock);
137
220
        sec_malloc_lock = NULL;
138
220
        return 1;
139
220
    }
140
0
#endif /* OPENSSL_NO_SECURE_MEMORY */
141
0
    return 0;
142
220
}
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
254k
{
155
254k
#ifndef OPENSSL_NO_SECURE_MEMORY
156
254k
    void *ret = NULL;
157
254k
    size_t actual_size;
158
254k
    int reason = CRYPTO_R_SECURE_MALLOC_FAILURE;
159
160
254k
    if (!secure_mem_initialized) {
161
254k
        return CRYPTO_malloc(num, file, line);
162
254k
    }
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
1.81M
{
185
1.81M
#ifndef OPENSSL_NO_SECURE_MEMORY
186
1.81M
    if (secure_mem_initialized)
187
        /* CRYPTO_secure_malloc() zeroes allocations when it is implemented */
188
0
        return CRYPTO_secure_malloc(num, file, line);
189
1.81M
#endif
190
1.81M
    return CRYPTO_zalloc(num, file, line);
191
1.81M
}
192
193
void CRYPTO_secure_free(void *ptr, const char *file, int line)
194
141k
{
195
141k
#ifndef OPENSSL_NO_SECURE_MEMORY
196
141k
    size_t actual_size;
197
198
141k
    if (ptr == NULL)
199
6.19k
        return;
200
135k
    if (!CRYPTO_secure_allocated(ptr)) {
201
135k
        CRYPTO_free(ptr, file, line);
202
135k
        return;
203
135k
    }
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
2.31M
{
219
2.31M
#ifndef OPENSSL_NO_SECURE_MEMORY
220
2.31M
    size_t actual_size;
221
222
2.31M
    if (ptr == NULL)
223
353k
        return;
224
1.95M
    if (!CRYPTO_secure_allocated(ptr)) {
225
1.95M
        OPENSSL_cleanse(ptr, num);
226
1.95M
        CRYPTO_free(ptr, file, line);
227
1.95M
        return;
228
1.95M
    }
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
2.71M
{
246
2.71M
#ifndef OPENSSL_NO_SECURE_MEMORY
247
2.71M
    if (!secure_mem_initialized)
248
2.71M
        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
2.71M
}
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 (!CRYPTO_THREAD_read_lock(sec_malloc_lock))
266
0
        return 0;
267
268
0
    ret = secure_mem_used;
269
270
0
    CRYPTO_THREAD_unlock(sec_malloc_lock);
271
0
#endif /* OPENSSL_NO_SECURE_MEMORY */
272
0
    return ret;
273
0
}
274
275
size_t CRYPTO_secure_actual_size(void *ptr)
276
0
{
277
0
#ifndef OPENSSL_NO_SECURE_MEMORY
278
0
    size_t actual_size;
279
280
0
    if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
281
0
        return 0;
282
0
    actual_size = sh_actual_size(ptr);
283
0
    CRYPTO_THREAD_unlock(sec_malloc_lock);
284
0
    return actual_size;
285
#else
286
    return 0;
287
#endif
288
0
}
289
290
/*
291
 * SECURE HEAP IMPLEMENTATION
292
 */
293
#ifndef OPENSSL_NO_SECURE_MEMORY
294
295
/*
296
 * The implementation provided here uses a fixed-sized mmap() heap,
297
 * which is locked into memory, not written to core files, and protected
298
 * on either side by an unmapped page, which will catch pointer overruns
299
 * (or underruns) and an attempt to read data out of the secure heap.
300
 * Free'd memory is zero'd or otherwise cleansed.
301
 *
302
 * This is a pretty standard buddy allocator.  We keep areas in a multiple
303
 * of "sh.minsize" units.  The freelist and bitmaps are kept separately,
304
 * so all (and only) data is kept in the mmap'd heap.
305
 *
306
 * This code assumes eight-bit bytes.  The numbers 3 and 7 are all over the
307
 * place.
308
 */
309
310
0
#define ONE ((size_t)1)
311
312
0
#define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
313
0
#define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
314
0
#define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
315
316
#define WITHIN_ARENA(p) \
317
0
    ((char *)(p) >= sh.arena && (char *)(p) < &sh.arena[sh.arena_size])
318
#define WITHIN_FREELIST(p) \
319
    ((char *)(p) >= (char *)sh.freelist && (char *)(p) < (char *)&sh.freelist[sh.freelist_size])
320
321
typedef struct sh_list_st {
322
    struct sh_list_st *next;
323
    struct sh_list_st **p_next;
324
} SH_LIST;
325
326
typedef struct sh_st {
327
    char *map_result;
328
    size_t map_size;
329
    char *arena;
330
    size_t arena_size;
331
    char **freelist;
332
    ossl_ssize_t freelist_size;
333
    size_t minsize;
334
    unsigned char *bittable;
335
    unsigned char *bitmalloc;
336
    size_t bittable_size; /* size in bits */
337
} SH;
338
339
static SH sh;
340
341
static size_t sh_getlist(char *ptr)
342
0
{
343
0
    ossl_ssize_t list = sh.freelist_size - 1;
344
0
    size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
345
346
0
    for (; bit; bit >>= 1, list--) {
347
0
        if (TESTBIT(sh.bittable, bit))
348
0
            break;
349
0
        OPENSSL_assert((bit & 1) == 0);
350
0
    }
351
352
0
    return list;
353
0
}
354
355
static int sh_testbit(char *ptr, int list, unsigned char *table)
356
0
{
357
0
    size_t bit;
358
359
0
    OPENSSL_assert(list >= 0 && list < sh.freelist_size);
360
0
    OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
361
0
    bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
362
0
    OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
363
0
    return TESTBIT(table, bit);
364
0
}
365
366
static void sh_clearbit(char *ptr, int list, unsigned char *table)
367
0
{
368
0
    size_t bit;
369
370
0
    OPENSSL_assert(list >= 0 && list < sh.freelist_size);
371
0
    OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
372
0
    bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
373
0
    OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
374
0
    OPENSSL_assert(TESTBIT(table, bit));
375
0
    CLEARBIT(table, bit);
376
0
}
377
378
static void sh_setbit(char *ptr, int list, unsigned char *table)
379
0
{
380
0
    size_t bit;
381
382
0
    OPENSSL_assert(list >= 0 && list < sh.freelist_size);
383
0
    OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
384
0
    bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
385
0
    OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
386
0
    OPENSSL_assert(!TESTBIT(table, bit));
387
0
    SETBIT(table, bit);
388
0
}
389
390
static void sh_add_to_list(char **list, char *ptr)
391
0
{
392
0
    SH_LIST *temp;
393
394
0
    OPENSSL_assert(WITHIN_FREELIST(list));
395
0
    OPENSSL_assert(WITHIN_ARENA(ptr));
396
397
0
    temp = (SH_LIST *)ptr;
398
0
    temp->next = *(SH_LIST **)list;
399
0
    OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
400
0
    temp->p_next = (SH_LIST **)list;
401
402
0
    if (temp->next != NULL) {
403
0
        OPENSSL_assert((char **)temp->next->p_next == list);
404
0
        temp->next->p_next = &(temp->next);
405
0
    }
406
407
0
    *list = ptr;
408
0
}
409
410
static void sh_remove_from_list(char *ptr)
411
0
{
412
0
    SH_LIST *temp, *temp2;
413
414
0
    temp = (SH_LIST *)ptr;
415
0
    if (temp->next != NULL)
416
0
        temp->next->p_next = temp->p_next;
417
0
    *temp->p_next = temp->next;
418
0
    if (temp->next == NULL)
419
0
        return;
420
421
0
    temp2 = temp->next;
422
0
    OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
423
0
}
424
425
static int sh_init(size_t size, size_t minsize)
426
0
{
427
0
    int ret;
428
0
    size_t i;
429
0
    size_t pgsize;
430
0
    size_t aligned;
431
#if defined(_WIN32)
432
    DWORD flOldProtect;
433
    SYSTEM_INFO systemInfo;
434
#endif
435
436
0
    memset(&sh, 0, sizeof(sh));
437
438
    /* make sure size is a powers of 2 */
439
0
    OPENSSL_assert(size > 0);
440
0
    OPENSSL_assert((size & (size - 1)) == 0);
441
0
    if (size == 0 || (size & (size - 1)) != 0)
442
0
        goto err;
443
444
0
    if (minsize <= sizeof(SH_LIST)) {
445
0
        OPENSSL_assert(sizeof(SH_LIST) <= 65536);
446
        /*
447
         * Compute the minimum possible allocation size.
448
         * This must be a power of 2 and at least as large as the SH_LIST
449
         * structure.
450
         */
451
0
        minsize = sizeof(SH_LIST) - 1;
452
0
        minsize |= minsize >> 1;
453
0
        minsize |= minsize >> 2;
454
0
        if (sizeof(SH_LIST) > 16)
455
0
            minsize |= minsize >> 4;
456
0
        if (sizeof(SH_LIST) > 256)
457
0
            minsize |= minsize >> 8;
458
0
        minsize++;
459
0
    } else {
460
        /* make sure minsize is a powers of 2 */
461
0
        OPENSSL_assert((minsize & (minsize - 1)) == 0);
462
0
        if ((minsize & (minsize - 1)) != 0)
463
0
            goto err;
464
0
    }
465
466
0
    sh.arena_size = size;
467
0
    sh.minsize = minsize;
468
0
    sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
469
470
    /* Prevent allocations of size 0 later on */
471
0
    if (sh.bittable_size >> 3 == 0)
472
0
        goto err;
473
474
0
    sh.freelist_size = -1;
475
0
    for (i = sh.bittable_size; i; i >>= 1)
476
0
        sh.freelist_size++;
477
478
0
    sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *));
479
0
    OPENSSL_assert(sh.freelist != NULL);
480
0
    if (sh.freelist == NULL)
481
0
        goto err;
482
483
0
    sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
484
0
    OPENSSL_assert(sh.bittable != NULL);
485
0
    if (sh.bittable == NULL)
486
0
        goto err;
487
488
0
    sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
489
0
    OPENSSL_assert(sh.bitmalloc != NULL);
490
0
    if (sh.bitmalloc == NULL)
491
0
        goto err;
492
493
    /* Allocate space for heap, and two extra pages as guards */
494
0
#if defined(_SC_PAGE_SIZE) || defined(_SC_PAGESIZE)
495
0
    {
496
0
#if defined(_SC_PAGE_SIZE)
497
0
        long tmppgsize = sysconf(_SC_PAGE_SIZE);
498
#else
499
        long tmppgsize = sysconf(_SC_PAGESIZE);
500
#endif
501
0
        if (tmppgsize < 1)
502
0
            pgsize = PAGE_SIZE;
503
0
        else
504
0
            pgsize = (size_t)tmppgsize;
505
0
    }
506
#elif defined(_WIN32)
507
    GetSystemInfo(&systemInfo);
508
    pgsize = (size_t)systemInfo.dwPageSize;
509
#else
510
    pgsize = PAGE_SIZE;
511
#endif
512
0
    sh.map_size = pgsize + sh.arena_size + pgsize;
513
514
0
#if !defined(_WIN32)
515
0
#ifdef MAP_ANON
516
0
    sh.map_result = mmap(NULL, sh.map_size,
517
0
        PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE | MAP_CONCEAL, -1, 0);
518
#else
519
    {
520
        int fd;
521
522
        sh.map_result = MAP_FAILED;
523
        if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
524
            sh.map_result = mmap(NULL, sh.map_size,
525
                PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
526
            close(fd);
527
        }
528
    }
529
#endif
530
0
    if (sh.map_result == MAP_FAILED)
531
0
        goto err;
532
#else
533
    sh.map_result = VirtualAlloc(NULL, sh.map_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
534
535
    if (sh.map_result == NULL)
536
        goto err;
537
#endif
538
539
0
    sh.arena = (char *)(sh.map_result + pgsize);
540
0
    sh_setbit(sh.arena, 0, sh.bittable);
541
0
    sh_add_to_list(&sh.freelist[0], sh.arena);
542
543
    /* Now try to add guard pages and lock into memory. */
544
0
    ret = 1;
545
546
0
#if !defined(_WIN32)
547
    /* Starting guard is already aligned from mmap. */
548
0
    if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
549
0
        ret = 2;
550
#else
551
    if (VirtualProtect(sh.map_result, pgsize, PAGE_NOACCESS, &flOldProtect) == FALSE)
552
        ret = 2;
553
#endif
554
555
    /* Ending guard page - need to round up to page boundary */
556
0
    aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
557
0
#if !defined(_WIN32)
558
0
    if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
559
0
        ret = 2;
560
#else
561
    if (VirtualProtect(sh.map_result + aligned, pgsize, PAGE_NOACCESS, &flOldProtect) == FALSE)
562
        ret = 2;
563
#endif
564
565
0
#if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
566
0
    if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
567
0
        if (errno == ENOSYS) {
568
0
            if (mlock(sh.arena, sh.arena_size) < 0)
569
0
                ret = 2;
570
0
        } else {
571
0
            ret = 2;
572
0
        }
573
0
    }
574
#elif defined(_WIN32)
575
    if (VirtualLock(sh.arena, sh.arena_size) == FALSE)
576
        ret = 2;
577
#else
578
    if (mlock(sh.arena, sh.arena_size) < 0)
579
        ret = 2;
580
#endif
581
0
#ifndef NO_MADVISE
582
0
    if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
583
0
        ret = 2;
584
0
#endif
585
586
0
    return ret;
587
588
0
err:
589
0
    sh_done();
590
0
    return 0;
591
0
}
592
593
static void sh_done(void)
594
220
{
595
220
    OPENSSL_free(sh.freelist);
596
220
    OPENSSL_free(sh.bittable);
597
220
    OPENSSL_free(sh.bitmalloc);
598
220
#if !defined(_WIN32)
599
220
    if (sh.map_result != MAP_FAILED && sh.map_size)
600
0
        munmap(sh.map_result, sh.map_size);
601
#else
602
    if (sh.map_result != NULL && sh.map_size)
603
        VirtualFree(sh.map_result, 0, MEM_RELEASE);
604
#endif
605
220
    memset(&sh, 0, sizeof(sh));
606
220
}
607
608
static int sh_allocated(const char *ptr)
609
0
{
610
0
    return WITHIN_ARENA(ptr) ? 1 : 0;
611
0
}
612
613
static char *sh_find_my_buddy(char *ptr, int list)
614
0
{
615
0
    size_t bit;
616
0
    char *chunk = NULL;
617
618
0
    bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
619
0
    bit ^= 1;
620
621
0
    if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
622
0
        chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
623
624
0
    return chunk;
625
0
}
626
627
static void *sh_malloc(size_t size)
628
0
{
629
0
    ossl_ssize_t list, slist;
630
0
    size_t i;
631
0
    char *chunk;
632
633
0
    if (size > sh.arena_size)
634
0
        return NULL;
635
636
0
    list = sh.freelist_size - 1;
637
0
    for (i = sh.minsize; i < size; i <<= 1)
638
0
        list--;
639
0
    if (list < 0)
640
0
        return NULL;
641
642
    /* try to find a larger entry to split */
643
0
    for (slist = list; slist >= 0; slist--)
644
0
        if (sh.freelist[slist] != NULL)
645
0
            break;
646
0
    if (slist < 0)
647
0
        return NULL;
648
649
    /* split larger entry */
650
0
    while (slist != list) {
651
0
        char *temp = sh.freelist[slist];
652
653
        /* remove from bigger list */
654
0
        OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
655
0
        sh_clearbit(temp, slist, sh.bittable);
656
0
        sh_remove_from_list(temp);
657
0
        OPENSSL_assert(temp != sh.freelist[slist]);
658
659
        /* done with bigger list */
660
0
        slist++;
661
662
        /* add to smaller list */
663
0
        OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
664
0
        sh_setbit(temp, slist, sh.bittable);
665
0
        sh_add_to_list(&sh.freelist[slist], temp);
666
0
        OPENSSL_assert(sh.freelist[slist] == temp);
667
668
        /* split in 2 */
669
0
        temp += sh.arena_size >> slist;
670
0
        OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
671
0
        sh_setbit(temp, slist, sh.bittable);
672
0
        sh_add_to_list(&sh.freelist[slist], temp);
673
0
        OPENSSL_assert(sh.freelist[slist] == temp);
674
675
0
        OPENSSL_assert(temp - (sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
676
0
    }
677
678
    /* peel off memory to hand back */
679
0
    chunk = sh.freelist[list];
680
0
    OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
681
0
    sh_setbit(chunk, list, sh.bitmalloc);
682
0
    sh_remove_from_list(chunk);
683
684
0
    OPENSSL_assert(WITHIN_ARENA(chunk));
685
686
    /* zero the free list header as a precaution against information leakage */
687
0
    memset(chunk, 0, sizeof(SH_LIST));
688
689
0
    return chunk;
690
0
}
691
692
static void sh_free(void *ptr)
693
0
{
694
0
    size_t list;
695
0
    void *buddy;
696
697
0
    if (ptr == NULL)
698
0
        return;
699
0
    OPENSSL_assert(WITHIN_ARENA(ptr));
700
0
    if (!WITHIN_ARENA(ptr))
701
0
        return;
702
703
0
    list = sh_getlist(ptr);
704
0
    OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
705
0
    sh_clearbit(ptr, list, sh.bitmalloc);
706
0
    sh_add_to_list(&sh.freelist[list], ptr);
707
708
    /* Try to coalesce two adjacent free areas. */
709
0
    while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
710
0
        OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
711
0
        OPENSSL_assert(ptr != NULL);
712
0
        OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
713
0
        sh_clearbit(ptr, list, sh.bittable);
714
0
        sh_remove_from_list(ptr);
715
0
        OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
716
0
        sh_clearbit(buddy, list, sh.bittable);
717
0
        sh_remove_from_list(buddy);
718
719
0
        list--;
720
721
        /* Zero the higher addressed block's free list pointers */
722
0
        memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST));
723
0
        if (ptr > buddy)
724
0
            ptr = buddy;
725
726
0
        OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
727
0
        sh_setbit(ptr, list, sh.bittable);
728
0
        sh_add_to_list(&sh.freelist[list], ptr);
729
0
        OPENSSL_assert(sh.freelist[list] == ptr);
730
0
    }
731
0
}
732
733
static size_t sh_actual_size(char *ptr)
734
0
{
735
0
    int list;
736
737
0
    OPENSSL_assert(WITHIN_ARENA(ptr));
738
0
    if (!WITHIN_ARENA(ptr))
739
0
        return 0;
740
0
    list = sh_getlist(ptr);
741
0
    OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
742
0
    return sh.arena_size / (ONE << list);
743
0
}
744
#endif /* OPENSSL_NO_SECURE_MEMORY */