mem_sec.c 17.0 KB
Newer Older
R
Rich Salz 已提交
1
/*
H
HJ 已提交
2
 * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
R
Rich Salz 已提交
3
 * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
R
Rich Salz 已提交
4 5 6 7 8 9 10
 *
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

R
Rich Salz 已提交
11 12 13 14 15 16 17
/*
 * This file is in two halves. The first half implements the public API
 * to be used by external consumers, and to be used by OpenSSL to store
 * data in a "secure arena." The second half implements the secure arena.
 * For details on that implementation, see below (look for uppercase
 * "SECURE HEAP IMPLEMENTATION").
 */
R
Rich Salz 已提交
18
#include "e_os.h"
19
#include <openssl/crypto.h>
R
Rich Salz 已提交
20

21 22
#include <string.h>

23 24
/* e_os.h defines OPENSSL_SECURE_MEMORY if secure memory can be implemented */
#ifdef OPENSSL_SECURE_MEMORY
R
Rich Salz 已提交
25 26 27
# include <stdlib.h>
# include <assert.h>
# include <unistd.h>
28
# include <sys/types.h>
R
Rich Salz 已提交
29
# include <sys/mman.h>
30 31
# if defined(OPENSSL_SYS_LINUX)
#  include <sys/syscall.h>
A
Andy Polyakov 已提交
32 33 34 35
#  if defined(SYS_mlock2)
#   include <linux/mman.h>
#   include <errno.h>
#  endif
36
# endif
H
HJ 已提交
37 38 39 40 41 42
# if defined(__FreeBSD__)
#  define MADV_DONTDUMP MADV_NOCORE
# endif
# if !defined(MAP_CONCEAL)
#  define MAP_CONCEAL 0
# endif
R
Rich Salz 已提交
43
# include <sys/param.h>
44 45
# include <sys/stat.h>
# include <fcntl.h>
R
Rich Salz 已提交
46 47 48
#endif

#define CLEAR(p, s) OPENSSL_cleanse(p, s)
49 50 51
#ifndef PAGE_SIZE
# define PAGE_SIZE    4096
#endif
A
Andy Polyakov 已提交
52 53 54
#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
# define MAP_ANON MAP_ANONYMOUS
#endif
R
Rich Salz 已提交
55

56
#ifdef OPENSSL_SECURE_MEMORY
57
static size_t secure_mem_used;
R
Rich Salz 已提交
58 59 60

static int secure_mem_initialized;

61 62
static CRYPTO_RWLOCK *sec_malloc_lock = NULL;

R
Rich Salz 已提交
63 64 65 66
/*
 * These are the functions that must be implemented by a secure heap (sh).
 */
static int sh_init(size_t size, int minsize);
67 68
static void *sh_malloc(size_t size);
static void sh_free(void *ptr);
R
Rich Salz 已提交
69
static void sh_done(void);
T
Todd Short 已提交
70
static size_t sh_actual_size(char *ptr);
R
Rich Salz 已提交
71 72 73 74 75
static int sh_allocated(const char *ptr);
#endif

int CRYPTO_secure_malloc_init(size_t size, int minsize)
{
76
#ifdef OPENSSL_SECURE_MEMORY
R
Rich Salz 已提交
77 78 79
    int ret = 0;

    if (!secure_mem_initialized) {
80
        sec_malloc_lock = CRYPTO_THREAD_lock_new();
81 82
        if (sec_malloc_lock == NULL)
            return 0;
83 84 85 86 87 88
        if ((ret = sh_init(size, minsize)) != 0) {
            secure_mem_initialized = 1;
        } else {
            CRYPTO_THREAD_lock_free(sec_malloc_lock);
            sec_malloc_lock = NULL;
        }
R
Rich Salz 已提交
89
    }
90

R
Rich Salz 已提交
91 92 93
    return ret;
#else
    return 0;
94
#endif /* OPENSSL_SECURE_MEMORY */
R
Rich Salz 已提交
95 96
}

97
int CRYPTO_secure_malloc_done(void)
R
Rich Salz 已提交
98
{
99
#ifdef OPENSSL_SECURE_MEMORY
T
Todd Short 已提交
100 101 102 103
    if (secure_mem_used == 0) {
        sh_done();
        secure_mem_initialized = 0;
        CRYPTO_THREAD_lock_free(sec_malloc_lock);
104
        sec_malloc_lock = NULL;
T
Todd Short 已提交
105 106
        return 1;
    }
107
#endif /* OPENSSL_SECURE_MEMORY */
T
Todd Short 已提交
108
    return 0;
R
Rich Salz 已提交
109 110
}

111
int CRYPTO_secure_malloc_initialized(void)
R
Rich Salz 已提交
112
{
113
#ifdef OPENSSL_SECURE_MEMORY
R
Rich Salz 已提交
114 115 116
    return secure_mem_initialized;
#else
    return 0;
117
#endif /* OPENSSL_SECURE_MEMORY */
R
Rich Salz 已提交
118 119
}

120
void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
R
Rich Salz 已提交
121
{
122
#ifdef OPENSSL_SECURE_MEMORY
R
Rich Salz 已提交
123 124 125 126 127 128
    void *ret;
    size_t actual_size;

    if (!secure_mem_initialized) {
        return CRYPTO_malloc(num, file, line);
    }
129
    CRYPTO_THREAD_write_lock(sec_malloc_lock);
R
Rich Salz 已提交
130 131 132
    ret = sh_malloc(num);
    actual_size = ret ? sh_actual_size(ret) : 0;
    secure_mem_used += actual_size;
133
    CRYPTO_THREAD_unlock(sec_malloc_lock);
R
Rich Salz 已提交
134 135 136
    return ret;
#else
    return CRYPTO_malloc(num, file, line);
137
#endif /* OPENSSL_SECURE_MEMORY */
R
Rich Salz 已提交
138 139
}

R
Rich Salz 已提交
140 141
void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
{
142
#ifdef OPENSSL_SECURE_MEMORY
P
Pauli 已提交
143 144 145 146 147
    if (secure_mem_initialized)
        /* CRYPTO_secure_malloc() zeroes allocations when it is implemented */
        return CRYPTO_secure_malloc(num, file, line);
#endif
    return CRYPTO_zalloc(num, file, line);
R
Rich Salz 已提交
148 149
}

150
void CRYPTO_secure_free(void *ptr, const char *file, int line)
R
Rich Salz 已提交
151
{
152
#ifdef OPENSSL_SECURE_MEMORY
R
Rich Salz 已提交
153 154 155 156
    size_t actual_size;

    if (ptr == NULL)
        return;
T
Todd Short 已提交
157
    if (!CRYPTO_secure_allocated(ptr)) {
158
        CRYPTO_free(ptr, file, line);
R
Rich Salz 已提交
159 160
        return;
    }
161
    CRYPTO_THREAD_write_lock(sec_malloc_lock);
R
Rich Salz 已提交
162 163 164 165
    actual_size = sh_actual_size(ptr);
    CLEAR(ptr, actual_size);
    secure_mem_used -= actual_size;
    sh_free(ptr);
166
    CRYPTO_THREAD_unlock(sec_malloc_lock);
R
Rich Salz 已提交
167
#else
168
    CRYPTO_free(ptr, file, line);
169
#endif /* OPENSSL_SECURE_MEMORY */
R
Rich Salz 已提交
170 171
}

172 173 174
void CRYPTO_secure_clear_free(void *ptr, size_t num,
                              const char *file, int line)
{
175
#ifdef OPENSSL_SECURE_MEMORY
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    size_t actual_size;

    if (ptr == NULL)
        return;
    if (!CRYPTO_secure_allocated(ptr)) {
        OPENSSL_cleanse(ptr, num);
        CRYPTO_free(ptr, file, line);
        return;
    }
    CRYPTO_THREAD_write_lock(sec_malloc_lock);
    actual_size = sh_actual_size(ptr);
    CLEAR(ptr, actual_size);
    secure_mem_used -= actual_size;
    sh_free(ptr);
    CRYPTO_THREAD_unlock(sec_malloc_lock);
#else
    if (ptr == NULL)
        return;
    OPENSSL_cleanse(ptr, num);
    CRYPTO_free(ptr, file, line);
196
#endif /* OPENSSL_SECURE_MEMORY */
197 198
}

R
Rich Salz 已提交
199 200
int CRYPTO_secure_allocated(const void *ptr)
{
201
#ifdef OPENSSL_SECURE_MEMORY
R
Rich Salz 已提交
202 203 204 205
    int ret;

    if (!secure_mem_initialized)
        return 0;
206
    CRYPTO_THREAD_write_lock(sec_malloc_lock);
R
Rich Salz 已提交
207
    ret = sh_allocated(ptr);
208
    CRYPTO_THREAD_unlock(sec_malloc_lock);
R
Rich Salz 已提交
209 210 211
    return ret;
#else
    return 0;
212
#endif /* OPENSSL_SECURE_MEMORY */
R
Rich Salz 已提交
213 214
}

215
size_t CRYPTO_secure_used(void)
R
Rich Salz 已提交
216
{
217
#ifdef OPENSSL_SECURE_MEMORY
R
Rich Salz 已提交
218 219 220
    return secure_mem_used;
#else
    return 0;
221
#endif /* OPENSSL_SECURE_MEMORY */
R
Rich Salz 已提交
222 223
}

224 225
size_t CRYPTO_secure_actual_size(void *ptr)
{
226
#ifdef OPENSSL_SECURE_MEMORY
227 228
    size_t actual_size;

229
    CRYPTO_THREAD_write_lock(sec_malloc_lock);
230
    actual_size = sh_actual_size(ptr);
231
    CRYPTO_THREAD_unlock(sec_malloc_lock);
232 233 234 235 236
    return actual_size;
#else
    return 0;
#endif
}
R
Rich Salz 已提交
237 238 239 240 241 242 243
/* END OF PAGE ...

   ... START OF PAGE */

/*
 * SECURE HEAP IMPLEMENTATION
 */
244
#ifdef OPENSSL_SECURE_MEMORY
R
Rich Salz 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261


/*
 * The implementation provided here uses a fixed-sized mmap() heap,
 * which is locked into memory, not written to core files, and protected
 * on either side by an unmapped page, which will catch pointer overruns
 * (or underruns) and an attempt to read data out of the secure heap.
 * Free'd memory is zero'd or otherwise cleansed.
 *
 * This is a pretty standard buddy allocator.  We keep areas in a multiple
 * of "sh.minsize" units.  The freelist and bitmaps are kept separately,
 * so all (and only) data is kept in the mmap'd heap.
 *
 * This code assumes eight-bit bytes.  The numbers 3 and 7 are all over the
 * place.
 */

T
Todd Short 已提交
262 263 264 265 266
#define ONE ((size_t)1)

# define TESTBIT(t, b)  (t[(b) >> 3] &  (ONE << ((b) & 7)))
# define SETBIT(t, b)   (t[(b) >> 3] |= (ONE << ((b) & 7)))
# define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
R
Rich Salz 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

#define WITHIN_ARENA(p) \
    ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
#define WITHIN_FREELIST(p) \
    ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])


typedef struct sh_list_st
{
    struct sh_list_st *next;
    struct sh_list_st **p_next;
} SH_LIST;

typedef struct sh_st
{
    char* map_result;
    size_t map_size;
    char *arena;
T
Todd Short 已提交
285
    size_t arena_size;
R
Rich Salz 已提交
286
    char **freelist;
T
Todd Short 已提交
287 288
    ossl_ssize_t freelist_size;
    size_t minsize;
R
Rich Salz 已提交
289 290
    unsigned char *bittable;
    unsigned char *bitmalloc;
T
Todd Short 已提交
291
    size_t bittable_size; /* size in bits */
R
Rich Salz 已提交
292 293 294 295
} SH;

static SH sh;

T
Todd Short 已提交
296
static size_t sh_getlist(char *ptr)
R
Rich Salz 已提交
297
{
T
Todd Short 已提交
298 299
    ossl_ssize_t list = sh.freelist_size - 1;
    size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
R
Rich Salz 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312

    for (; bit; bit >>= 1, list--) {
        if (TESTBIT(sh.bittable, bit))
            break;
        OPENSSL_assert((bit & 1) == 0);
    }

    return list;
}


static int sh_testbit(char *ptr, int list, unsigned char *table)
{
T
Todd Short 已提交
313
    size_t bit;
R
Rich Salz 已提交
314 315 316

    OPENSSL_assert(list >= 0 && list < sh.freelist_size);
    OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
T
Todd Short 已提交
317
    bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
R
Rich Salz 已提交
318 319 320 321 322 323
    OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
    return TESTBIT(table, bit);
}

static void sh_clearbit(char *ptr, int list, unsigned char *table)
{
T
Todd Short 已提交
324
    size_t bit;
R
Rich Salz 已提交
325 326 327

    OPENSSL_assert(list >= 0 && list < sh.freelist_size);
    OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
T
Todd Short 已提交
328
    bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
R
Rich Salz 已提交
329 330 331 332 333 334 335
    OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
    OPENSSL_assert(TESTBIT(table, bit));
    CLEARBIT(table, bit);
}

static void sh_setbit(char *ptr, int list, unsigned char *table)
{
T
Todd Short 已提交
336
    size_t bit;
R
Rich Salz 已提交
337 338 339

    OPENSSL_assert(list >= 0 && list < sh.freelist_size);
    OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
T
Todd Short 已提交
340
    bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
R
Rich Salz 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
    OPENSSL_assert(!TESTBIT(table, bit));
    SETBIT(table, bit);
}

static void sh_add_to_list(char **list, char *ptr)
{
    SH_LIST *temp;

    OPENSSL_assert(WITHIN_FREELIST(list));
    OPENSSL_assert(WITHIN_ARENA(ptr));

    temp = (SH_LIST *)ptr;
    temp->next = *(SH_LIST **)list;
    OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
    temp->p_next = (SH_LIST **)list;

    if (temp->next != NULL) {
        OPENSSL_assert((char **)temp->next->p_next == list);
        temp->next->p_next = &(temp->next);
    }

    *list = ptr;
}

366
static void sh_remove_from_list(char *ptr)
R
Rich Salz 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
{
    SH_LIST *temp, *temp2;

    temp = (SH_LIST *)ptr;
    if (temp->next != NULL)
        temp->next->p_next = temp->p_next;
    *temp->p_next = temp->next;
    if (temp->next == NULL)
        return;

    temp2 = temp->next;
    OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
}


static int sh_init(size_t size, int minsize)
{
384 385
    int ret;
    size_t i;
R
Rich Salz 已提交
386 387 388
    size_t pgsize;
    size_t aligned;

R
Rich Salz 已提交
389
    memset(&sh, 0, sizeof(sh));
R
Rich Salz 已提交
390 391 392 393 394 395 396 397 398 399 400

    /* make sure size and minsize are powers of 2 */
    OPENSSL_assert(size > 0);
    OPENSSL_assert((size & (size - 1)) == 0);
    OPENSSL_assert(minsize > 0);
    OPENSSL_assert((minsize & (minsize - 1)) == 0);
    if (size <= 0 || (size & (size - 1)) != 0)
        goto err;
    if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
        goto err;

P
Pauli 已提交
401 402 403
    while (minsize < (int)sizeof(SH_LIST))
        minsize *= 2;

R
Rich Salz 已提交
404 405 406 407
    sh.arena_size = size;
    sh.minsize = minsize;
    sh.bittable_size = (sh.arena_size / sh.minsize) * 2;

408 409 410 411
    /* Prevent allocations of size 0 later on */
    if (sh.bittable_size >> 3 == 0)
        goto err;

R
Rich Salz 已提交
412 413 414 415
    sh.freelist_size = -1;
    for (i = sh.bittable_size; i; i >>= 1)
        sh.freelist_size++;

R
Rich Salz 已提交
416
    sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *));
R
Rich Salz 已提交
417 418 419 420
    OPENSSL_assert(sh.freelist != NULL);
    if (sh.freelist == NULL)
        goto err;

R
Rich Salz 已提交
421
    sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
R
Rich Salz 已提交
422 423 424 425
    OPENSSL_assert(sh.bittable != NULL);
    if (sh.bittable == NULL)
        goto err;

R
Rich Salz 已提交
426
    sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
R
Rich Salz 已提交
427 428 429 430 431
    OPENSSL_assert(sh.bitmalloc != NULL);
    if (sh.bitmalloc == NULL)
        goto err;

    /* Allocate space for heap, and two extra pages as guards */
432 433 434 435 436 437 438 439 440 441 442 443
#if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
    {
# if defined(_SC_PAGE_SIZE)
        long tmppgsize = sysconf(_SC_PAGE_SIZE);
# else
        long tmppgsize = sysconf(_SC_PAGESIZE);
# endif
        if (tmppgsize < 1)
            pgsize = PAGE_SIZE;
        else
            pgsize = (size_t)tmppgsize;
    }
R
Rich Salz 已提交
444 445 446 447
#else
    pgsize = PAGE_SIZE;
#endif
    sh.map_size = pgsize + sh.arena_size + pgsize;
448 449 450
    if (1) {
#ifdef MAP_ANON
        sh.map_result = mmap(NULL, sh.map_size,
H
HJ 已提交
451
                             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|MAP_CONCEAL, -1, 0);
452 453 454 455 456 457 458 459 460 461 462
    } else {
#endif
        int fd;

        sh.map_result = MAP_FAILED;
        if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
            sh.map_result = mmap(NULL, sh.map_size,
                                 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
            close(fd);
        }
    }
R
Rich Salz 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
    if (sh.map_result == MAP_FAILED)
        goto err;
    sh.arena = (char *)(sh.map_result + pgsize);
    sh_setbit(sh.arena, 0, sh.bittable);
    sh_add_to_list(&sh.freelist[0], sh.arena);

    /* Now try to add guard pages and lock into memory. */
    ret = 1;

    /* Starting guard is already aligned from mmap. */
    if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
        ret = 2;

    /* Ending guard page - need to round up to page boundary */
    aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
    if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
        ret = 2;

481 482 483 484 485 486 487 488 489 490
#if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
    if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
        if (errno == ENOSYS) {
            if (mlock(sh.arena, sh.arena_size) < 0)
                ret = 2;
        } else {
            ret = 2;
        }
    }
#else
R
Rich Salz 已提交
491 492
    if (mlock(sh.arena, sh.arena_size) < 0)
        ret = 2;
493
#endif
R
Rich Salz 已提交
494 495 496 497 498 499 500 501 502 503 504 505
#ifdef MADV_DONTDUMP
    if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
        ret = 2;
#endif

    return ret;

 err:
    sh_done();
    return 0;
}

506
static void sh_done(void)
R
Rich Salz 已提交
507 508 509 510
{
    OPENSSL_free(sh.freelist);
    OPENSSL_free(sh.bittable);
    OPENSSL_free(sh.bitmalloc);
H
HJ 已提交
511
    if (sh.map_result != MAP_FAILED && sh.map_size)
R
Rich Salz 已提交
512
        munmap(sh.map_result, sh.map_size);
R
Rich Salz 已提交
513
    memset(&sh, 0, sizeof(sh));
R
Rich Salz 已提交
514 515 516 517 518 519 520 521 522
}

static int sh_allocated(const char *ptr)
{
    return WITHIN_ARENA(ptr) ? 1 : 0;
}

static char *sh_find_my_buddy(char *ptr, int list)
{
T
Todd Short 已提交
523
    size_t bit;
R
Rich Salz 已提交
524 525
    char *chunk = NULL;

T
Todd Short 已提交
526
    bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
R
Rich Salz 已提交
527 528 529
    bit ^= 1;

    if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
T
Todd Short 已提交
530
        chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
R
Rich Salz 已提交
531 532 533 534

    return chunk;
}

535
static void *sh_malloc(size_t size)
R
Rich Salz 已提交
536
{
T
Todd Short 已提交
537
    ossl_ssize_t list, slist;
R
Rich Salz 已提交
538 539 540
    size_t i;
    char *chunk;

541 542 543
    if (size > sh.arena_size)
        return NULL;

R
Rich Salz 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
    list = sh.freelist_size - 1;
    for (i = sh.minsize; i < size; i <<= 1)
        list--;
    if (list < 0)
        return NULL;

    /* try to find a larger entry to split */
    for (slist = list; slist >= 0; slist--)
        if (sh.freelist[slist] != NULL)
            break;
    if (slist < 0)
        return NULL;

    /* split larger entry */
    while (slist != list) {
        char *temp = sh.freelist[slist];

        /* remove from bigger list */
        OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
        sh_clearbit(temp, slist, sh.bittable);
564
        sh_remove_from_list(temp);
R
Rich Salz 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
        OPENSSL_assert(temp != sh.freelist[slist]);

        /* done with bigger list */
        slist++;

        /* add to smaller list */
        OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
        sh_setbit(temp, slist, sh.bittable);
        sh_add_to_list(&sh.freelist[slist], temp);
        OPENSSL_assert(sh.freelist[slist] == temp);

        /* split in 2 */
        temp += sh.arena_size >> slist;
        OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
        sh_setbit(temp, slist, sh.bittable);
        sh_add_to_list(&sh.freelist[slist], temp);
        OPENSSL_assert(sh.freelist[slist] == temp);

        OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
    }

    /* peel off memory to hand back */
    chunk = sh.freelist[list];
    OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
    sh_setbit(chunk, list, sh.bitmalloc);
590
    sh_remove_from_list(chunk);
R
Rich Salz 已提交
591 592 593

    OPENSSL_assert(WITHIN_ARENA(chunk));

P
Pauli 已提交
594 595 596
    /* zero the free list header as a precaution against information leakage */
    memset(chunk, 0, sizeof(SH_LIST));

R
Rich Salz 已提交
597 598 599
    return chunk;
}

600
static void sh_free(void *ptr)
R
Rich Salz 已提交
601
{
T
Todd Short 已提交
602
    size_t list;
603
    void *buddy;
R
Rich Salz 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

    if (ptr == NULL)
        return;
    OPENSSL_assert(WITHIN_ARENA(ptr));
    if (!WITHIN_ARENA(ptr))
        return;

    list = sh_getlist(ptr);
    OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
    sh_clearbit(ptr, list, sh.bitmalloc);
    sh_add_to_list(&sh.freelist[list], ptr);

    /* Try to coalesce two adjacent free areas. */
    while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
        OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
        OPENSSL_assert(ptr != NULL);
        OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
        sh_clearbit(ptr, list, sh.bittable);
622
        sh_remove_from_list(ptr);
R
Rich Salz 已提交
623 624
        OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
        sh_clearbit(buddy, list, sh.bittable);
625
        sh_remove_from_list(buddy);
R
Rich Salz 已提交
626 627 628

        list--;

P
Pauli 已提交
629 630
        /* Zero the higher addressed block's free list pointers */
        memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST));
R
Rich Salz 已提交
631 632 633 634 635 636 637 638 639 640
        if (ptr > buddy)
            ptr = buddy;

        OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
        sh_setbit(ptr, list, sh.bittable);
        sh_add_to_list(&sh.freelist[list], ptr);
        OPENSSL_assert(sh.freelist[list] == ptr);
    }
}

T
Todd Short 已提交
641
static size_t sh_actual_size(char *ptr)
R
Rich Salz 已提交
642 643 644 645 646 647 648 649
{
    int list;

    OPENSSL_assert(WITHIN_ARENA(ptr));
    if (!WITHIN_ARENA(ptr))
        return 0;
    list = sh_getlist(ptr);
    OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
T
Todd Short 已提交
650
    return sh.arena_size / (ONE << list);
R
Rich Salz 已提交
651
}
652
#endif /* OPENSSL_SECURE_MEMORY */