2 * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
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
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 #ifndef OPENSSL_NO_SECURE_MEMORY
29 # if defined(OPENSSL_SYS_UNIX)
32 # include <sys/types.h>
33 # if defined(OPENSSL_SYS_UNIX)
34 # include <sys/mman.h>
35 # if defined(__FreeBSD__)
36 # define MADV_DONTDUMP MADV_NOCORE
39 # if defined(OPENSSL_SYS_LINUX)
40 # include <sys/syscall.h>
41 # if defined(SYS_mlock2)
42 # include <linux/mman.h>
45 # include <sys/param.h>
47 # include <sys/stat.h>
51 #define CLEAR(p, s) OPENSSL_cleanse(p, s)
53 # define PAGE_SIZE 4096
55 #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
56 # define MAP_ANON MAP_ANONYMOUS
59 #ifndef OPENSSL_NO_SECURE_MEMORY
60 static size_t secure_mem_used;
62 static int secure_mem_initialized;
64 static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
67 * These are the functions that must be implemented by a secure heap (sh).
69 static int sh_init(size_t size, size_t minsize);
70 static void *sh_malloc(size_t size);
71 static void sh_free(void *ptr);
72 static void sh_done(void);
73 static size_t sh_actual_size(char *ptr);
74 static int sh_allocated(const char *ptr);
77 int CRYPTO_secure_malloc_init(size_t size, size_t minsize)
79 #ifndef OPENSSL_NO_SECURE_MEMORY
82 if (!secure_mem_initialized) {
83 sec_malloc_lock = CRYPTO_THREAD_lock_new();
84 if (sec_malloc_lock == NULL)
86 if ((ret = sh_init(size, minsize)) != 0) {
87 secure_mem_initialized = 1;
89 CRYPTO_THREAD_lock_free(sec_malloc_lock);
90 sec_malloc_lock = NULL;
97 #endif /* OPENSSL_NO_SECURE_MEMORY */
100 int CRYPTO_secure_malloc_done(void)
102 #ifndef OPENSSL_NO_SECURE_MEMORY
103 if (secure_mem_used == 0) {
105 secure_mem_initialized = 0;
106 CRYPTO_THREAD_lock_free(sec_malloc_lock);
107 sec_malloc_lock = NULL;
110 #endif /* OPENSSL_NO_SECURE_MEMORY */
114 int CRYPTO_secure_malloc_initialized(void)
116 #ifndef OPENSSL_NO_SECURE_MEMORY
117 return secure_mem_initialized;
120 #endif /* OPENSSL_NO_SECURE_MEMORY */
123 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
125 #ifndef OPENSSL_NO_SECURE_MEMORY
129 if (!secure_mem_initialized) {
130 return CRYPTO_malloc(num, file, line);
132 CRYPTO_THREAD_write_lock(sec_malloc_lock);
133 ret = sh_malloc(num);
134 actual_size = ret ? sh_actual_size(ret) : 0;
135 secure_mem_used += actual_size;
136 CRYPTO_THREAD_unlock(sec_malloc_lock);
139 return CRYPTO_malloc(num, file, line);
140 #endif /* OPENSSL_NO_SECURE_MEMORY */
143 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
145 #ifndef OPENSSL_NO_SECURE_MEMORY
146 if (secure_mem_initialized)
147 /* CRYPTO_secure_malloc() zeroes allocations when it is implemented */
148 return CRYPTO_secure_malloc(num, file, line);
150 return CRYPTO_zalloc(num, file, line);
153 void CRYPTO_secure_free(void *ptr, const char *file, int line)
155 #ifndef OPENSSL_NO_SECURE_MEMORY
160 if (!CRYPTO_secure_allocated(ptr)) {
161 CRYPTO_free(ptr, file, line);
164 CRYPTO_THREAD_write_lock(sec_malloc_lock);
165 actual_size = sh_actual_size(ptr);
166 CLEAR(ptr, actual_size);
167 secure_mem_used -= actual_size;
169 CRYPTO_THREAD_unlock(sec_malloc_lock);
171 CRYPTO_free(ptr, file, line);
172 #endif /* OPENSSL_NO_SECURE_MEMORY */
175 void CRYPTO_secure_clear_free(void *ptr, size_t num,
176 const char *file, int line)
178 #ifndef OPENSSL_NO_SECURE_MEMORY
183 if (!CRYPTO_secure_allocated(ptr)) {
184 OPENSSL_cleanse(ptr, num);
185 CRYPTO_free(ptr, file, line);
188 CRYPTO_THREAD_write_lock(sec_malloc_lock);
189 actual_size = sh_actual_size(ptr);
190 CLEAR(ptr, actual_size);
191 secure_mem_used -= actual_size;
193 CRYPTO_THREAD_unlock(sec_malloc_lock);
197 OPENSSL_cleanse(ptr, num);
198 CRYPTO_free(ptr, file, line);
199 #endif /* OPENSSL_NO_SECURE_MEMORY */
202 int CRYPTO_secure_allocated(const void *ptr)
204 #ifndef OPENSSL_NO_SECURE_MEMORY
207 if (!secure_mem_initialized)
209 CRYPTO_THREAD_write_lock(sec_malloc_lock);
210 ret = sh_allocated(ptr);
211 CRYPTO_THREAD_unlock(sec_malloc_lock);
215 #endif /* OPENSSL_NO_SECURE_MEMORY */
218 size_t CRYPTO_secure_used(void)
220 #ifndef OPENSSL_NO_SECURE_MEMORY
221 return secure_mem_used;
224 #endif /* OPENSSL_NO_SECURE_MEMORY */
227 size_t CRYPTO_secure_actual_size(void *ptr)
229 #ifndef OPENSSL_NO_SECURE_MEMORY
232 CRYPTO_THREAD_write_lock(sec_malloc_lock);
233 actual_size = sh_actual_size(ptr);
234 CRYPTO_THREAD_unlock(sec_malloc_lock);
242 * SECURE HEAP IMPLEMENTATION
244 #ifndef OPENSSL_NO_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, size_t minsize)
390 SYSTEM_INFO systemInfo;
393 memset(&sh, 0, sizeof(sh));
395 /* make sure size is a powers of 2 */
396 OPENSSL_assert(size > 0);
397 OPENSSL_assert((size & (size - 1)) == 0);
398 if (size == 0 || (size & (size - 1)) != 0)
401 if (minsize <= sizeof(SH_LIST)) {
402 OPENSSL_assert(sizeof(SH_LIST) <= 65536);
404 * Compute the minimum possible allocation size.
405 * This must be a power of 2 and at least as large as the SH_LIST
408 minsize = sizeof(SH_LIST) - 1;
409 minsize |= minsize >> 1;
410 minsize |= minsize >> 2;
411 if (sizeof(SH_LIST) > 16)
412 minsize |= minsize >> 4;
413 if (sizeof(SH_LIST) > 256)
414 minsize |= minsize >> 8;
417 /* make sure minsize is a powers of 2 */
418 OPENSSL_assert((minsize & (minsize - 1)) == 0);
419 if ((minsize & (minsize - 1)) != 0)
423 sh.arena_size = size;
424 sh.minsize = minsize;
425 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
427 /* Prevent allocations of size 0 later on */
428 if (sh.bittable_size >> 3 == 0)
431 sh.freelist_size = -1;
432 for (i = sh.bittable_size; i; i >>= 1)
435 sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *));
436 OPENSSL_assert(sh.freelist != NULL);
437 if (sh.freelist == NULL)
440 sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
441 OPENSSL_assert(sh.bittable != NULL);
442 if (sh.bittable == NULL)
445 sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
446 OPENSSL_assert(sh.bitmalloc != NULL);
447 if (sh.bitmalloc == NULL)
450 /* Allocate space for heap, and two extra pages as guards */
451 #if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
453 # if defined(_SC_PAGE_SIZE)
454 long tmppgsize = sysconf(_SC_PAGE_SIZE);
456 long tmppgsize = sysconf(_SC_PAGESIZE);
461 pgsize = (size_t)tmppgsize;
463 #elif defined(_WIN32)
464 GetSystemInfo(&systemInfo);
465 pgsize = (size_t)systemInfo.dwPageSize;
469 sh.map_size = pgsize + sh.arena_size + pgsize;
473 sh.map_result = mmap(NULL, sh.map_size,
474 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
479 sh.map_result = MAP_FAILED;
480 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
481 sh.map_result = mmap(NULL, sh.map_size,
482 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
487 if (sh.map_result == MAP_FAILED)
490 sh.map_result = VirtualAlloc(NULL, sh.map_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
492 if (sh.map_result == NULL)
496 sh.arena = (char *)(sh.map_result + pgsize);
497 sh_setbit(sh.arena, 0, sh.bittable);
498 sh_add_to_list(&sh.freelist[0], sh.arena);
500 /* Now try to add guard pages and lock into memory. */
504 /* Starting guard is already aligned from mmap. */
505 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
508 if (VirtualProtect(sh.map_result, pgsize, PAGE_NOACCESS, &flOldProtect) == FALSE)
512 /* Ending guard page - need to round up to page boundary */
513 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
515 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
518 if (VirtualProtect(sh.map_result + aligned, pgsize, PAGE_NOACCESS, &flOldProtect) == FALSE)
522 #if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
523 if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
524 if (errno == ENOSYS) {
525 if (mlock(sh.arena, sh.arena_size) < 0)
531 #elif defined(_WIN32)
532 if (VirtualLock(sh.arena, sh.arena_size) == FALSE)
535 if (mlock(sh.arena, sh.arena_size) < 0)
539 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
550 static void sh_done(void)
552 OPENSSL_free(sh.freelist);
553 OPENSSL_free(sh.bittable);
554 OPENSSL_free(sh.bitmalloc);
556 if (sh.map_result != MAP_FAILED && sh.map_size)
557 munmap(sh.map_result, sh.map_size);
559 if (sh.map_result != NULL && sh.map_size)
560 VirtualFree(sh.map_result, 0, MEM_RELEASE);
562 memset(&sh, 0, sizeof(sh));
565 static int sh_allocated(const char *ptr)
567 return WITHIN_ARENA(ptr) ? 1 : 0;
570 static char *sh_find_my_buddy(char *ptr, int list)
575 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
578 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
579 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
584 static void *sh_malloc(size_t size)
586 ossl_ssize_t list, slist;
590 if (size > sh.arena_size)
593 list = sh.freelist_size - 1;
594 for (i = sh.minsize; i < size; i <<= 1)
599 /* try to find a larger entry to split */
600 for (slist = list; slist >= 0; slist--)
601 if (sh.freelist[slist] != NULL)
606 /* split larger entry */
607 while (slist != list) {
608 char *temp = sh.freelist[slist];
610 /* remove from bigger list */
611 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
612 sh_clearbit(temp, slist, sh.bittable);
613 sh_remove_from_list(temp);
614 OPENSSL_assert(temp != sh.freelist[slist]);
616 /* done with bigger list */
619 /* add to smaller list */
620 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
621 sh_setbit(temp, slist, sh.bittable);
622 sh_add_to_list(&sh.freelist[slist], temp);
623 OPENSSL_assert(sh.freelist[slist] == temp);
626 temp += sh.arena_size >> slist;
627 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
628 sh_setbit(temp, slist, sh.bittable);
629 sh_add_to_list(&sh.freelist[slist], temp);
630 OPENSSL_assert(sh.freelist[slist] == temp);
632 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
635 /* peel off memory to hand back */
636 chunk = sh.freelist[list];
637 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
638 sh_setbit(chunk, list, sh.bitmalloc);
639 sh_remove_from_list(chunk);
641 OPENSSL_assert(WITHIN_ARENA(chunk));
643 /* zero the free list header as a precaution against information leakage */
644 memset(chunk, 0, sizeof(SH_LIST));
649 static void sh_free(void *ptr)
656 OPENSSL_assert(WITHIN_ARENA(ptr));
657 if (!WITHIN_ARENA(ptr))
660 list = sh_getlist(ptr);
661 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
662 sh_clearbit(ptr, list, sh.bitmalloc);
663 sh_add_to_list(&sh.freelist[list], ptr);
665 /* Try to coalesce two adjacent free areas. */
666 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
667 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
668 OPENSSL_assert(ptr != NULL);
669 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
670 sh_clearbit(ptr, list, sh.bittable);
671 sh_remove_from_list(ptr);
672 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
673 sh_clearbit(buddy, list, sh.bittable);
674 sh_remove_from_list(buddy);
678 /* Zero the higher addressed block's free list pointers */
679 memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST));
683 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
684 sh_setbit(ptr, list, sh.bittable);
685 sh_add_to_list(&sh.freelist[list], ptr);
686 OPENSSL_assert(sh.freelist[list] == ptr);
690 static size_t sh_actual_size(char *ptr)
694 OPENSSL_assert(WITHIN_ARENA(ptr));
695 if (!WITHIN_ARENA(ptr))
697 list = sh_getlist(ptr);
698 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
699 return sh.arena_size / (ONE << list);
701 #endif /* OPENSSL_NO_SECURE_MEMORY */