2 * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
5 * Licensed under the OpenSSL license (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
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").
19 #include <openssl/crypto.h>
23 /* e_os.h defines OPENSSL_SECURE_MEMORY if secure memory can be implemented */
24 #ifdef OPENSSL_SECURE_MEMORY
28 # include <sys/types.h>
29 # include <sys/mman.h>
30 # if defined(OPENSSL_SYS_LINUX)
31 # include <sys/syscall.h>
32 # if defined(SYS_mlock2)
33 # include <linux/mman.h>
37 # if defined(__FreeBSD__)
38 # define MADV_DONTDUMP MADV_NOCORE
40 # if !defined(MAP_CONCEAL)
41 # define MAP_CONCEAL 0
43 # include <sys/param.h>
44 # include <sys/stat.h>
48 #define CLEAR(p, s) OPENSSL_cleanse(p, s)
50 # define PAGE_SIZE 4096
52 #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
53 # define MAP_ANON MAP_ANONYMOUS
56 #ifdef OPENSSL_SECURE_MEMORY
57 static size_t secure_mem_used;
59 static int secure_mem_initialized;
61 static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
64 * These are the functions that must be implemented by a secure heap (sh).
66 static int sh_init(size_t size, int minsize);
67 static void *sh_malloc(size_t size);
68 static void sh_free(void *ptr);
69 static void sh_done(void);
70 static size_t sh_actual_size(char *ptr);
71 static int sh_allocated(const char *ptr);
74 int CRYPTO_secure_malloc_init(size_t size, int minsize)
76 #ifdef OPENSSL_SECURE_MEMORY
79 if (!secure_mem_initialized) {
80 sec_malloc_lock = CRYPTO_THREAD_lock_new();
81 if (sec_malloc_lock == NULL)
83 if ((ret = sh_init(size, minsize)) != 0) {
84 secure_mem_initialized = 1;
86 CRYPTO_THREAD_lock_free(sec_malloc_lock);
87 sec_malloc_lock = NULL;
94 #endif /* OPENSSL_SECURE_MEMORY */
97 int CRYPTO_secure_malloc_done(void)
99 #ifdef OPENSSL_SECURE_MEMORY
100 if (secure_mem_used == 0) {
102 secure_mem_initialized = 0;
103 CRYPTO_THREAD_lock_free(sec_malloc_lock);
104 sec_malloc_lock = NULL;
107 #endif /* OPENSSL_SECURE_MEMORY */
111 int CRYPTO_secure_malloc_initialized(void)
113 #ifdef OPENSSL_SECURE_MEMORY
114 return secure_mem_initialized;
117 #endif /* OPENSSL_SECURE_MEMORY */
120 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
122 #ifdef OPENSSL_SECURE_MEMORY
126 if (!secure_mem_initialized) {
127 return CRYPTO_malloc(num, file, line);
129 CRYPTO_THREAD_write_lock(sec_malloc_lock);
130 ret = sh_malloc(num);
131 actual_size = ret ? sh_actual_size(ret) : 0;
132 secure_mem_used += actual_size;
133 CRYPTO_THREAD_unlock(sec_malloc_lock);
136 return CRYPTO_malloc(num, file, line);
137 #endif /* OPENSSL_SECURE_MEMORY */
140 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
142 #ifdef OPENSSL_SECURE_MEMORY
143 if (secure_mem_initialized)
144 /* CRYPTO_secure_malloc() zeroes allocations when it is implemented */
145 return CRYPTO_secure_malloc(num, file, line);
147 return CRYPTO_zalloc(num, file, line);
150 void CRYPTO_secure_free(void *ptr, const char *file, int line)
152 #ifdef OPENSSL_SECURE_MEMORY
157 if (!CRYPTO_secure_allocated(ptr)) {
158 CRYPTO_free(ptr, file, line);
161 CRYPTO_THREAD_write_lock(sec_malloc_lock);
162 actual_size = sh_actual_size(ptr);
163 CLEAR(ptr, actual_size);
164 secure_mem_used -= actual_size;
166 CRYPTO_THREAD_unlock(sec_malloc_lock);
168 CRYPTO_free(ptr, file, line);
169 #endif /* OPENSSL_SECURE_MEMORY */
172 void CRYPTO_secure_clear_free(void *ptr, size_t num,
173 const char *file, int line)
175 #ifdef OPENSSL_SECURE_MEMORY
180 if (!CRYPTO_secure_allocated(ptr)) {
181 OPENSSL_cleanse(ptr, num);
182 CRYPTO_free(ptr, file, line);
185 CRYPTO_THREAD_write_lock(sec_malloc_lock);
186 actual_size = sh_actual_size(ptr);
187 CLEAR(ptr, actual_size);
188 secure_mem_used -= actual_size;
190 CRYPTO_THREAD_unlock(sec_malloc_lock);
194 OPENSSL_cleanse(ptr, num);
195 CRYPTO_free(ptr, file, line);
196 #endif /* OPENSSL_SECURE_MEMORY */
199 int CRYPTO_secure_allocated(const void *ptr)
201 #ifdef OPENSSL_SECURE_MEMORY
204 if (!secure_mem_initialized)
206 CRYPTO_THREAD_write_lock(sec_malloc_lock);
207 ret = sh_allocated(ptr);
208 CRYPTO_THREAD_unlock(sec_malloc_lock);
212 #endif /* OPENSSL_SECURE_MEMORY */
215 size_t CRYPTO_secure_used(void)
217 #ifdef OPENSSL_SECURE_MEMORY
218 return secure_mem_used;
221 #endif /* OPENSSL_SECURE_MEMORY */
224 size_t CRYPTO_secure_actual_size(void *ptr)
226 #ifdef OPENSSL_SECURE_MEMORY
229 CRYPTO_THREAD_write_lock(sec_malloc_lock);
230 actual_size = sh_actual_size(ptr);
231 CRYPTO_THREAD_unlock(sec_malloc_lock);
242 * SECURE HEAP IMPLEMENTATION
244 #ifdef OPENSSL_SECURE_MEMORY
248 * The implementation provided here uses a fixed-sized mmap() heap,
249 * which is locked into memory, not written to core files, and protected
250 * on either side by an unmapped page, which will catch pointer overruns
251 * (or underruns) and an attempt to read data out of the secure heap.
252 * Free'd memory is zero'd or otherwise cleansed.
254 * This is a pretty standard buddy allocator. We keep areas in a multiple
255 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
256 * so all (and only) data is kept in the mmap'd heap.
258 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
262 #define ONE ((size_t)1)
264 # define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
265 # define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
266 # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
268 #define WITHIN_ARENA(p) \
269 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
270 #define WITHIN_FREELIST(p) \
271 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
274 typedef struct sh_list_st
276 struct sh_list_st *next;
277 struct sh_list_st **p_next;
287 ossl_ssize_t freelist_size;
289 unsigned char *bittable;
290 unsigned char *bitmalloc;
291 size_t bittable_size; /* size in bits */
296 static size_t sh_getlist(char *ptr)
298 ossl_ssize_t list = sh.freelist_size - 1;
299 size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
301 for (; bit; bit >>= 1, list--) {
302 if (TESTBIT(sh.bittable, bit))
304 OPENSSL_assert((bit & 1) == 0);
311 static int sh_testbit(char *ptr, int list, unsigned char *table)
315 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
316 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
317 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
318 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
319 return TESTBIT(table, bit);
322 static void sh_clearbit(char *ptr, int list, unsigned char *table)
326 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
327 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
328 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
329 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
330 OPENSSL_assert(TESTBIT(table, bit));
331 CLEARBIT(table, bit);
334 static void sh_setbit(char *ptr, int list, unsigned char *table)
338 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
339 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
340 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
341 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
342 OPENSSL_assert(!TESTBIT(table, bit));
346 static void sh_add_to_list(char **list, char *ptr)
350 OPENSSL_assert(WITHIN_FREELIST(list));
351 OPENSSL_assert(WITHIN_ARENA(ptr));
353 temp = (SH_LIST *)ptr;
354 temp->next = *(SH_LIST **)list;
355 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
356 temp->p_next = (SH_LIST **)list;
358 if (temp->next != NULL) {
359 OPENSSL_assert((char **)temp->next->p_next == list);
360 temp->next->p_next = &(temp->next);
366 static void sh_remove_from_list(char *ptr)
368 SH_LIST *temp, *temp2;
370 temp = (SH_LIST *)ptr;
371 if (temp->next != NULL)
372 temp->next->p_next = temp->p_next;
373 *temp->p_next = temp->next;
374 if (temp->next == NULL)
378 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
382 static int sh_init(size_t size, int minsize)
389 memset(&sh, 0, sizeof(sh));
391 /* make sure size and minsize are powers of 2 */
392 OPENSSL_assert(size > 0);
393 OPENSSL_assert((size & (size - 1)) == 0);
394 OPENSSL_assert(minsize > 0);
395 OPENSSL_assert((minsize & (minsize - 1)) == 0);
396 if (size <= 0 || (size & (size - 1)) != 0)
398 if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
401 while (minsize < (int)sizeof(SH_LIST))
404 sh.arena_size = size;
405 sh.minsize = minsize;
406 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
408 /* Prevent allocations of size 0 later on */
409 if (sh.bittable_size >> 3 == 0)
412 sh.freelist_size = -1;
413 for (i = sh.bittable_size; i; i >>= 1)
416 sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *));
417 OPENSSL_assert(sh.freelist != NULL);
418 if (sh.freelist == NULL)
421 sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
422 OPENSSL_assert(sh.bittable != NULL);
423 if (sh.bittable == NULL)
426 sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
427 OPENSSL_assert(sh.bitmalloc != NULL);
428 if (sh.bitmalloc == NULL)
431 /* Allocate space for heap, and two extra pages as guards */
432 #if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
434 # if defined(_SC_PAGE_SIZE)
435 long tmppgsize = sysconf(_SC_PAGE_SIZE);
437 long tmppgsize = sysconf(_SC_PAGESIZE);
442 pgsize = (size_t)tmppgsize;
447 sh.map_size = pgsize + sh.arena_size + pgsize;
450 sh.map_result = mmap(NULL, sh.map_size,
451 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|MAP_CONCEAL, -1, 0);
456 sh.map_result = MAP_FAILED;
457 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
458 sh.map_result = mmap(NULL, sh.map_size,
459 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
463 if (sh.map_result == MAP_FAILED)
465 sh.arena = (char *)(sh.map_result + pgsize);
466 sh_setbit(sh.arena, 0, sh.bittable);
467 sh_add_to_list(&sh.freelist[0], sh.arena);
469 /* Now try to add guard pages and lock into memory. */
472 /* Starting guard is already aligned from mmap. */
473 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
476 /* Ending guard page - need to round up to page boundary */
477 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
478 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
481 #if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
482 if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
483 if (errno == ENOSYS) {
484 if (mlock(sh.arena, sh.arena_size) < 0)
491 if (mlock(sh.arena, sh.arena_size) < 0)
495 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
506 static void sh_done(void)
508 OPENSSL_free(sh.freelist);
509 OPENSSL_free(sh.bittable);
510 OPENSSL_free(sh.bitmalloc);
511 if (sh.map_result != MAP_FAILED && sh.map_size)
512 munmap(sh.map_result, sh.map_size);
513 memset(&sh, 0, sizeof(sh));
516 static int sh_allocated(const char *ptr)
518 return WITHIN_ARENA(ptr) ? 1 : 0;
521 static char *sh_find_my_buddy(char *ptr, int list)
526 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
529 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
530 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
535 static void *sh_malloc(size_t size)
537 ossl_ssize_t list, slist;
541 if (size > sh.arena_size)
544 list = sh.freelist_size - 1;
545 for (i = sh.minsize; i < size; i <<= 1)
550 /* try to find a larger entry to split */
551 for (slist = list; slist >= 0; slist--)
552 if (sh.freelist[slist] != NULL)
557 /* split larger entry */
558 while (slist != list) {
559 char *temp = sh.freelist[slist];
561 /* remove from bigger list */
562 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
563 sh_clearbit(temp, slist, sh.bittable);
564 sh_remove_from_list(temp);
565 OPENSSL_assert(temp != sh.freelist[slist]);
567 /* done with bigger list */
570 /* add to smaller list */
571 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
572 sh_setbit(temp, slist, sh.bittable);
573 sh_add_to_list(&sh.freelist[slist], temp);
574 OPENSSL_assert(sh.freelist[slist] == temp);
577 temp += sh.arena_size >> slist;
578 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
579 sh_setbit(temp, slist, sh.bittable);
580 sh_add_to_list(&sh.freelist[slist], temp);
581 OPENSSL_assert(sh.freelist[slist] == temp);
583 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
586 /* peel off memory to hand back */
587 chunk = sh.freelist[list];
588 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
589 sh_setbit(chunk, list, sh.bitmalloc);
590 sh_remove_from_list(chunk);
592 OPENSSL_assert(WITHIN_ARENA(chunk));
594 /* zero the free list header as a precaution against information leakage */
595 memset(chunk, 0, sizeof(SH_LIST));
600 static void sh_free(void *ptr)
607 OPENSSL_assert(WITHIN_ARENA(ptr));
608 if (!WITHIN_ARENA(ptr))
611 list = sh_getlist(ptr);
612 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
613 sh_clearbit(ptr, list, sh.bitmalloc);
614 sh_add_to_list(&sh.freelist[list], ptr);
616 /* Try to coalesce two adjacent free areas. */
617 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
618 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
619 OPENSSL_assert(ptr != NULL);
620 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
621 sh_clearbit(ptr, list, sh.bittable);
622 sh_remove_from_list(ptr);
623 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
624 sh_clearbit(buddy, list, sh.bittable);
625 sh_remove_from_list(buddy);
629 /* Zero the higher addressed block's free list pointers */
630 memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST));
634 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
635 sh_setbit(ptr, list, sh.bittable);
636 sh_add_to_list(&sh.freelist[list], ptr);
637 OPENSSL_assert(sh.freelist[list] == ptr);
641 static size_t sh_actual_size(char *ptr)
645 OPENSSL_assert(WITHIN_ARENA(ptr));
646 if (!WITHIN_ARENA(ptr))
648 list = sh_getlist(ptr);
649 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
650 return sh.arena_size / (ONE << list);
652 #endif /* OPENSSL_SECURE_MEMORY */