]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/mem_sec.c
Use RAND_bytes_ex in crypto/rsa
[thirdparty/openssl.git] / crypto / mem_sec.c
CommitLineData
4f22f405 1/*
48e5119a 2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
624265c6 3 * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
4f22f405 4 *
0e9725bc 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
4f22f405
RS
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
74924dcb
RS
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 */
bef7a815 18#include "e_os.h"
07016a8a 19#include <openssl/crypto.h>
74924dcb 20
183733f8
RL
21#include <string.h>
22
6943335e 23#ifndef OPENSSL_NO_SECURE_MEMORY
d4dfb0ba 24# include <stdlib.h>
d4dfb0ba
RS
25# include <assert.h>
26# include <unistd.h>
27186da7 27# include <sys/types.h>
74924dcb 28# include <sys/mman.h>
9dfc5b96
TS
29# if defined(OPENSSL_SYS_LINUX)
30# include <sys/syscall.h>
f1c00b93
AP
31# if defined(SYS_mlock2)
32# include <linux/mman.h>
33# include <errno.h>
34# endif
61783db5 35# include <sys/param.h>
9dfc5b96 36# endif
27186da7
AP
37# include <sys/stat.h>
38# include <fcntl.h>
74924dcb
RS
39#endif
40
74924dcb 41#define CLEAR(p, s) OPENSSL_cleanse(p, s)
34750dc2
BL
42#ifndef PAGE_SIZE
43# define PAGE_SIZE 4096
44#endif
014cc4b2
AP
45#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
46# define MAP_ANON MAP_ANONYMOUS
47#endif
74924dcb 48
6943335e 49#ifndef OPENSSL_NO_SECURE_MEMORY
df2ee0e2 50static size_t secure_mem_used;
74924dcb
RS
51
52static int secure_mem_initialized;
74924dcb 53
9471f776
MC
54static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
55
74924dcb
RS
56/*
57 * These are the functions that must be implemented by a secure heap (sh).
58 */
34b16762 59static int sh_init(size_t size, size_t minsize);
332dc4fa
RS
60static void *sh_malloc(size_t size);
61static void sh_free(void *ptr);
74924dcb 62static void sh_done(void);
e8408681 63static size_t sh_actual_size(char *ptr);
74924dcb
RS
64static int sh_allocated(const char *ptr);
65#endif
66
34b16762 67int CRYPTO_secure_malloc_init(size_t size, size_t minsize)
74924dcb 68{
6943335e 69#ifndef OPENSSL_NO_SECURE_MEMORY
74924dcb
RS
70 int ret = 0;
71
74924dcb 72 if (!secure_mem_initialized) {
63ab5ea1 73 sec_malloc_lock = CRYPTO_THREAD_lock_new();
9471f776
MC
74 if (sec_malloc_lock == NULL)
75 return 0;
7031ddac
TS
76 if ((ret = sh_init(size, minsize)) != 0) {
77 secure_mem_initialized = 1;
78 } else {
79 CRYPTO_THREAD_lock_free(sec_malloc_lock);
80 sec_malloc_lock = NULL;
81 }
74924dcb 82 }
9471f776 83
74924dcb
RS
84 return ret;
85#else
86 return 0;
6943335e 87#endif /* OPENSSL_NO_SECURE_MEMORY */
74924dcb
RS
88}
89
3cb7c5cf 90int CRYPTO_secure_malloc_done(void)
74924dcb 91{
6943335e 92#ifndef OPENSSL_NO_SECURE_MEMORY
e8408681
TS
93 if (secure_mem_used == 0) {
94 sh_done();
95 secure_mem_initialized = 0;
96 CRYPTO_THREAD_lock_free(sec_malloc_lock);
7031ddac 97 sec_malloc_lock = NULL;
e8408681
TS
98 return 1;
99 }
6943335e 100#endif /* OPENSSL_NO_SECURE_MEMORY */
e8408681 101 return 0;
74924dcb
RS
102}
103
3cb7c5cf 104int CRYPTO_secure_malloc_initialized(void)
74924dcb 105{
6943335e 106#ifndef OPENSSL_NO_SECURE_MEMORY
74924dcb
RS
107 return secure_mem_initialized;
108#else
109 return 0;
6943335e 110#endif /* OPENSSL_NO_SECURE_MEMORY */
74924dcb
RS
111}
112
ff842856 113void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
74924dcb 114{
6943335e 115#ifndef OPENSSL_NO_SECURE_MEMORY
74924dcb
RS
116 void *ret;
117 size_t actual_size;
118
119 if (!secure_mem_initialized) {
74924dcb
RS
120 return CRYPTO_malloc(num, file, line);
121 }
9471f776 122 CRYPTO_THREAD_write_lock(sec_malloc_lock);
74924dcb
RS
123 ret = sh_malloc(num);
124 actual_size = ret ? sh_actual_size(ret) : 0;
125 secure_mem_used += actual_size;
9471f776 126 CRYPTO_THREAD_unlock(sec_malloc_lock);
74924dcb
RS
127 return ret;
128#else
129 return CRYPTO_malloc(num, file, line);
6943335e 130#endif /* OPENSSL_NO_SECURE_MEMORY */
74924dcb
RS
131}
132
3538c7da
RS
133void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
134{
6943335e 135#ifndef OPENSSL_NO_SECURE_MEMORY
3b8e97ab
P
136 if (secure_mem_initialized)
137 /* CRYPTO_secure_malloc() zeroes allocations when it is implemented */
138 return CRYPTO_secure_malloc(num, file, line);
139#endif
140 return CRYPTO_zalloc(num, file, line);
3538c7da
RS
141}
142
05c7b163 143void CRYPTO_secure_free(void *ptr, const char *file, int line)
74924dcb 144{
6943335e 145#ifndef OPENSSL_NO_SECURE_MEMORY
74924dcb
RS
146 size_t actual_size;
147
148 if (ptr == NULL)
149 return;
e8408681 150 if (!CRYPTO_secure_allocated(ptr)) {
05c7b163 151 CRYPTO_free(ptr, file, line);
74924dcb
RS
152 return;
153 }
9471f776 154 CRYPTO_THREAD_write_lock(sec_malloc_lock);
74924dcb
RS
155 actual_size = sh_actual_size(ptr);
156 CLEAR(ptr, actual_size);
157 secure_mem_used -= actual_size;
158 sh_free(ptr);
9471f776 159 CRYPTO_THREAD_unlock(sec_malloc_lock);
74924dcb 160#else
6a78ae28 161 CRYPTO_free(ptr, file, line);
6943335e 162#endif /* OPENSSL_NO_SECURE_MEMORY */
74924dcb
RS
163}
164
4dae7cd3
BE
165void CRYPTO_secure_clear_free(void *ptr, size_t num,
166 const char *file, int line)
167{
6943335e 168#ifndef OPENSSL_NO_SECURE_MEMORY
4dae7cd3
BE
169 size_t actual_size;
170
171 if (ptr == NULL)
172 return;
173 if (!CRYPTO_secure_allocated(ptr)) {
174 OPENSSL_cleanse(ptr, num);
175 CRYPTO_free(ptr, file, line);
176 return;
177 }
178 CRYPTO_THREAD_write_lock(sec_malloc_lock);
179 actual_size = sh_actual_size(ptr);
180 CLEAR(ptr, actual_size);
181 secure_mem_used -= actual_size;
182 sh_free(ptr);
183 CRYPTO_THREAD_unlock(sec_malloc_lock);
184#else
185 if (ptr == NULL)
186 return;
187 OPENSSL_cleanse(ptr, num);
188 CRYPTO_free(ptr, file, line);
6943335e 189#endif /* OPENSSL_NO_SECURE_MEMORY */
4dae7cd3
BE
190}
191
74924dcb
RS
192int CRYPTO_secure_allocated(const void *ptr)
193{
6943335e 194#ifndef OPENSSL_NO_SECURE_MEMORY
74924dcb
RS
195 int ret;
196
197 if (!secure_mem_initialized)
198 return 0;
9471f776 199 CRYPTO_THREAD_write_lock(sec_malloc_lock);
74924dcb 200 ret = sh_allocated(ptr);
9471f776 201 CRYPTO_THREAD_unlock(sec_malloc_lock);
74924dcb
RS
202 return ret;
203#else
204 return 0;
6943335e 205#endif /* OPENSSL_NO_SECURE_MEMORY */
74924dcb
RS
206}
207
3cb7c5cf 208size_t CRYPTO_secure_used(void)
bbd86bf5 209{
6943335e 210#ifndef OPENSSL_NO_SECURE_MEMORY
bbd86bf5
RS
211 return secure_mem_used;
212#else
213 return 0;
6943335e 214#endif /* OPENSSL_NO_SECURE_MEMORY */
bbd86bf5
RS
215}
216
d594199b
RS
217size_t CRYPTO_secure_actual_size(void *ptr)
218{
6943335e 219#ifndef OPENSSL_NO_SECURE_MEMORY
d594199b
RS
220 size_t actual_size;
221
9471f776 222 CRYPTO_THREAD_write_lock(sec_malloc_lock);
d594199b 223 actual_size = sh_actual_size(ptr);
9471f776 224 CRYPTO_THREAD_unlock(sec_malloc_lock);
d594199b
RS
225 return actual_size;
226#else
227 return 0;
228#endif
229}
74924dcb
RS
230
231/*
232 * SECURE HEAP IMPLEMENTATION
233 */
6943335e 234#ifndef OPENSSL_NO_SECURE_MEMORY
74924dcb
RS
235
236
237/*
238 * The implementation provided here uses a fixed-sized mmap() heap,
239 * which is locked into memory, not written to core files, and protected
240 * on either side by an unmapped page, which will catch pointer overruns
241 * (or underruns) and an attempt to read data out of the secure heap.
242 * Free'd memory is zero'd or otherwise cleansed.
243 *
244 * This is a pretty standard buddy allocator. We keep areas in a multiple
245 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
246 * so all (and only) data is kept in the mmap'd heap.
247 *
248 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
249 * place.
250 */
251
e8408681
TS
252#define ONE ((size_t)1)
253
254# define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
255# define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
256# define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
74924dcb
RS
257
258#define WITHIN_ARENA(p) \
259 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
260#define WITHIN_FREELIST(p) \
261 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
262
263
264typedef struct sh_list_st
265{
266 struct sh_list_st *next;
267 struct sh_list_st **p_next;
268} SH_LIST;
269
270typedef struct sh_st
271{
272 char* map_result;
273 size_t map_size;
274 char *arena;
e8408681 275 size_t arena_size;
74924dcb 276 char **freelist;
e8408681
TS
277 ossl_ssize_t freelist_size;
278 size_t minsize;
74924dcb
RS
279 unsigned char *bittable;
280 unsigned char *bitmalloc;
e8408681 281 size_t bittable_size; /* size in bits */
74924dcb
RS
282} SH;
283
284static SH sh;
285
e8408681 286static size_t sh_getlist(char *ptr)
74924dcb 287{
e8408681
TS
288 ossl_ssize_t list = sh.freelist_size - 1;
289 size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
74924dcb
RS
290
291 for (; bit; bit >>= 1, list--) {
292 if (TESTBIT(sh.bittable, bit))
293 break;
294 OPENSSL_assert((bit & 1) == 0);
295 }
296
297 return list;
298}
299
300
301static int sh_testbit(char *ptr, int list, unsigned char *table)
302{
e8408681 303 size_t bit;
74924dcb
RS
304
305 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
306 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
e8408681 307 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
74924dcb
RS
308 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
309 return TESTBIT(table, bit);
310}
311
312static void sh_clearbit(char *ptr, int list, unsigned char *table)
313{
e8408681 314 size_t bit;
74924dcb
RS
315
316 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
317 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
e8408681 318 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
74924dcb
RS
319 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
320 OPENSSL_assert(TESTBIT(table, bit));
321 CLEARBIT(table, bit);
322}
323
324static void sh_setbit(char *ptr, int list, unsigned char *table)
325{
e8408681 326 size_t bit;
74924dcb
RS
327
328 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
329 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
e8408681 330 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
74924dcb
RS
331 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
332 OPENSSL_assert(!TESTBIT(table, bit));
333 SETBIT(table, bit);
334}
335
336static void sh_add_to_list(char **list, char *ptr)
337{
338 SH_LIST *temp;
339
340 OPENSSL_assert(WITHIN_FREELIST(list));
341 OPENSSL_assert(WITHIN_ARENA(ptr));
342
343 temp = (SH_LIST *)ptr;
344 temp->next = *(SH_LIST **)list;
345 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
346 temp->p_next = (SH_LIST **)list;
347
348 if (temp->next != NULL) {
349 OPENSSL_assert((char **)temp->next->p_next == list);
350 temp->next->p_next = &(temp->next);
351 }
352
353 *list = ptr;
354}
355
a773b52a 356static void sh_remove_from_list(char *ptr)
74924dcb
RS
357{
358 SH_LIST *temp, *temp2;
359
360 temp = (SH_LIST *)ptr;
361 if (temp->next != NULL)
362 temp->next->p_next = temp->p_next;
363 *temp->p_next = temp->next;
364 if (temp->next == NULL)
365 return;
366
367 temp2 = temp->next;
368 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
369}
370
371
34b16762 372static int sh_init(size_t size, size_t minsize)
74924dcb 373{
7031ddac
TS
374 int ret;
375 size_t i;
74924dcb
RS
376 size_t pgsize;
377 size_t aligned;
378
cbe29648 379 memset(&sh, 0, sizeof(sh));
74924dcb 380
a998ec0e 381 /* make sure size is a powers of 2 */
74924dcb
RS
382 OPENSSL_assert(size > 0);
383 OPENSSL_assert((size & (size - 1)) == 0);
d27fd991 384 if (size == 0 || (size & (size - 1)) != 0)
74924dcb 385 goto err;
74924dcb 386
a998ec0e
P
387 if (minsize <= sizeof(SH_LIST)) {
388 OPENSSL_assert(sizeof(SH_LIST) <= 65536);
389 /*
390 * Compute the minimum possible allocation size.
391 * This must be a power of 2 and at least as large as the SH_LIST
392 * structure.
393 */
394 minsize = sizeof(SH_LIST) - 1;
395 minsize |= minsize >> 1;
396 minsize |= minsize >> 2;
397 if (sizeof(SH_LIST) > 16)
398 minsize |= minsize >> 4;
399 if (sizeof(SH_LIST) > 256)
400 minsize |= minsize >> 8;
401 minsize++;
402 } else {
403 /* make sure minsize is a powers of 2 */
404 OPENSSL_assert((minsize & (minsize - 1)) == 0);
405 if ((minsize & (minsize - 1)) != 0)
406 goto err;
407 }
70e14ffb 408
74924dcb
RS
409 sh.arena_size = size;
410 sh.minsize = minsize;
411 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
412
7f07149d
GV
413 /* Prevent allocations of size 0 later on */
414 if (sh.bittable_size >> 3 == 0)
415 goto err;
416
74924dcb
RS
417 sh.freelist_size = -1;
418 for (i = sh.bittable_size; i; i >>= 1)
419 sh.freelist_size++;
420
cbe29648 421 sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *));
74924dcb
RS
422 OPENSSL_assert(sh.freelist != NULL);
423 if (sh.freelist == NULL)
424 goto err;
74924dcb 425
b51bce94 426 sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
74924dcb
RS
427 OPENSSL_assert(sh.bittable != NULL);
428 if (sh.bittable == NULL)
429 goto err;
74924dcb 430
b51bce94 431 sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
74924dcb
RS
432 OPENSSL_assert(sh.bitmalloc != NULL);
433 if (sh.bitmalloc == NULL)
434 goto err;
74924dcb
RS
435
436 /* Allocate space for heap, and two extra pages as guards */
9ae720b4
MC
437#if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
438 {
439# if defined(_SC_PAGE_SIZE)
440 long tmppgsize = sysconf(_SC_PAGE_SIZE);
441# else
442 long tmppgsize = sysconf(_SC_PAGESIZE);
443# endif
444 if (tmppgsize < 1)
445 pgsize = PAGE_SIZE;
446 else
447 pgsize = (size_t)tmppgsize;
448 }
74924dcb
RS
449#else
450 pgsize = PAGE_SIZE;
451#endif
452 sh.map_size = pgsize + sh.arena_size + pgsize;
d27fd991 453
27186da7 454#ifdef MAP_ANON
d27fd991
DG
455 sh.map_result = mmap(NULL, sh.map_size,
456 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
457#else
458 {
27186da7
AP
459 int fd;
460
461 sh.map_result = MAP_FAILED;
462 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
463 sh.map_result = mmap(NULL, sh.map_size,
464 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
465 close(fd);
466 }
467 }
d27fd991 468#endif
74924dcb
RS
469 if (sh.map_result == MAP_FAILED)
470 goto err;
471 sh.arena = (char *)(sh.map_result + pgsize);
472 sh_setbit(sh.arena, 0, sh.bittable);
473 sh_add_to_list(&sh.freelist[0], sh.arena);
474
475 /* Now try to add guard pages and lock into memory. */
476 ret = 1;
477
478 /* Starting guard is already aligned from mmap. */
479 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
480 ret = 2;
481
482 /* Ending guard page - need to round up to page boundary */
483 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
484 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
485 ret = 2;
486
9dfc5b96
TS
487#if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
488 if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
489 if (errno == ENOSYS) {
490 if (mlock(sh.arena, sh.arena_size) < 0)
491 ret = 2;
492 } else {
493 ret = 2;
494 }
495 }
496#else
74924dcb
RS
497 if (mlock(sh.arena, sh.arena_size) < 0)
498 ret = 2;
9dfc5b96 499#endif
74924dcb
RS
500#ifdef MADV_DONTDUMP
501 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
502 ret = 2;
503#endif
504
505 return ret;
506
507 err:
508 sh_done();
509 return 0;
510}
511
3cb7c5cf 512static void sh_done(void)
74924dcb
RS
513{
514 OPENSSL_free(sh.freelist);
515 OPENSSL_free(sh.bittable);
516 OPENSSL_free(sh.bitmalloc);
517 if (sh.map_result != NULL && sh.map_size)
518 munmap(sh.map_result, sh.map_size);
cbe29648 519 memset(&sh, 0, sizeof(sh));
74924dcb
RS
520}
521
522static int sh_allocated(const char *ptr)
523{
524 return WITHIN_ARENA(ptr) ? 1 : 0;
525}
526
527static char *sh_find_my_buddy(char *ptr, int list)
528{
e8408681 529 size_t bit;
74924dcb
RS
530 char *chunk = NULL;
531
e8408681 532 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
74924dcb
RS
533 bit ^= 1;
534
535 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
e8408681 536 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
74924dcb
RS
537
538 return chunk;
539}
540
332dc4fa 541static void *sh_malloc(size_t size)
74924dcb 542{
e8408681 543 ossl_ssize_t list, slist;
74924dcb
RS
544 size_t i;
545 char *chunk;
546
7031ddac
TS
547 if (size > sh.arena_size)
548 return NULL;
549
74924dcb
RS
550 list = sh.freelist_size - 1;
551 for (i = sh.minsize; i < size; i <<= 1)
552 list--;
553 if (list < 0)
554 return NULL;
555
556 /* try to find a larger entry to split */
557 for (slist = list; slist >= 0; slist--)
558 if (sh.freelist[slist] != NULL)
559 break;
560 if (slist < 0)
561 return NULL;
562
563 /* split larger entry */
564 while (slist != list) {
565 char *temp = sh.freelist[slist];
566
567 /* remove from bigger list */
568 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
569 sh_clearbit(temp, slist, sh.bittable);
a773b52a 570 sh_remove_from_list(temp);
74924dcb
RS
571 OPENSSL_assert(temp != sh.freelist[slist]);
572
573 /* done with bigger list */
574 slist++;
575
576 /* add to smaller list */
577 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
578 sh_setbit(temp, slist, sh.bittable);
579 sh_add_to_list(&sh.freelist[slist], temp);
580 OPENSSL_assert(sh.freelist[slist] == temp);
581
582 /* split in 2 */
583 temp += sh.arena_size >> slist;
584 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
585 sh_setbit(temp, slist, sh.bittable);
586 sh_add_to_list(&sh.freelist[slist], temp);
587 OPENSSL_assert(sh.freelist[slist] == temp);
588
589 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
590 }
591
592 /* peel off memory to hand back */
593 chunk = sh.freelist[list];
594 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
595 sh_setbit(chunk, list, sh.bitmalloc);
a773b52a 596 sh_remove_from_list(chunk);
74924dcb
RS
597
598 OPENSSL_assert(WITHIN_ARENA(chunk));
599
3b8e97ab
P
600 /* zero the free list header as a precaution against information leakage */
601 memset(chunk, 0, sizeof(SH_LIST));
602
74924dcb
RS
603 return chunk;
604}
605
332dc4fa 606static void sh_free(void *ptr)
74924dcb 607{
e8408681 608 size_t list;
332dc4fa 609 void *buddy;
74924dcb
RS
610
611 if (ptr == NULL)
612 return;
613 OPENSSL_assert(WITHIN_ARENA(ptr));
614 if (!WITHIN_ARENA(ptr))
615 return;
616
617 list = sh_getlist(ptr);
618 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
619 sh_clearbit(ptr, list, sh.bitmalloc);
620 sh_add_to_list(&sh.freelist[list], ptr);
621
622 /* Try to coalesce two adjacent free areas. */
623 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
624 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
625 OPENSSL_assert(ptr != NULL);
626 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
627 sh_clearbit(ptr, list, sh.bittable);
a773b52a 628 sh_remove_from_list(ptr);
74924dcb
RS
629 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
630 sh_clearbit(buddy, list, sh.bittable);
a773b52a 631 sh_remove_from_list(buddy);
74924dcb
RS
632
633 list--;
634
3b8e97ab
P
635 /* Zero the higher addressed block's free list pointers */
636 memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST));
74924dcb
RS
637 if (ptr > buddy)
638 ptr = buddy;
639
640 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
641 sh_setbit(ptr, list, sh.bittable);
642 sh_add_to_list(&sh.freelist[list], ptr);
643 OPENSSL_assert(sh.freelist[list] == ptr);
644 }
645}
646
e8408681 647static size_t sh_actual_size(char *ptr)
74924dcb
RS
648{
649 int list;
650
651 OPENSSL_assert(WITHIN_ARENA(ptr));
652 if (!WITHIN_ARENA(ptr))
653 return 0;
654 list = sh_getlist(ptr);
655 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
e8408681 656 return sh.arena_size / (ONE << list);
74924dcb 657}
6943335e 658#endif /* OPENSSL_NO_SECURE_MEMORY */