2 * Copyright 2015-2018 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 includes unistd.h, which defines _POSIX_VERSION */
24 #if !defined(OPENSSL_NO_SECURE_MEMORY) && defined(OPENSSL_SYS_UNIX) \
25 && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) \
26 || defined(__sun) || defined(__hpux) || defined(__sgi) \
32 # include <sys/types.h>
33 # include <sys/mman.h>
34 # if defined(OPENSSL_SYS_LINUX)
35 # include <sys/syscall.h>
36 # include <linux/mman.h>
39 # include <sys/param.h>
40 # include <sys/stat.h>
44 #define CLEAR(p, s) OPENSSL_cleanse(p, s)
46 # define PAGE_SIZE 4096
48 #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
49 # define MAP_ANON MAP_ANONYMOUS
53 static size_t secure_mem_used;
55 static int secure_mem_initialized;
57 static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
60 * These are the functions that must be implemented by a secure heap (sh).
62 static int sh_init(size_t size, int minsize);
63 static void *sh_malloc(size_t size);
64 static void sh_free(void *ptr);
65 static void sh_done(void);
66 static size_t sh_actual_size(char *ptr);
67 static int sh_allocated(const char *ptr);
70 int CRYPTO_secure_malloc_init(size_t size, int minsize)
75 if (!secure_mem_initialized) {
76 sec_malloc_lock = CRYPTO_THREAD_lock_new();
77 if (sec_malloc_lock == NULL)
79 if ((ret = sh_init(size, minsize)) != 0) {
80 secure_mem_initialized = 1;
82 CRYPTO_THREAD_lock_free(sec_malloc_lock);
83 sec_malloc_lock = NULL;
90 #endif /* IMPLEMENTED */
93 int CRYPTO_secure_malloc_done()
96 if (secure_mem_used == 0) {
98 secure_mem_initialized = 0;
99 CRYPTO_THREAD_lock_free(sec_malloc_lock);
100 sec_malloc_lock = NULL;
103 #endif /* IMPLEMENTED */
107 int CRYPTO_secure_malloc_initialized()
110 return secure_mem_initialized;
113 #endif /* IMPLEMENTED */
116 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
122 if (!secure_mem_initialized) {
123 return CRYPTO_malloc(num, file, line);
125 CRYPTO_THREAD_write_lock(sec_malloc_lock);
126 ret = sh_malloc(num);
127 actual_size = ret ? sh_actual_size(ret) : 0;
128 secure_mem_used += actual_size;
129 CRYPTO_THREAD_unlock(sec_malloc_lock);
132 return CRYPTO_malloc(num, file, line);
133 #endif /* IMPLEMENTED */
136 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
138 void *ret = CRYPTO_secure_malloc(num, file, line);
145 void CRYPTO_secure_free(void *ptr, const char *file, int line)
152 if (!CRYPTO_secure_allocated(ptr)) {
153 CRYPTO_free(ptr, file, line);
156 CRYPTO_THREAD_write_lock(sec_malloc_lock);
157 actual_size = sh_actual_size(ptr);
158 CLEAR(ptr, actual_size);
159 secure_mem_used -= actual_size;
161 CRYPTO_THREAD_unlock(sec_malloc_lock);
163 CRYPTO_free(ptr, file, line);
164 #endif /* IMPLEMENTED */
167 void CRYPTO_secure_clear_free(void *ptr, size_t num,
168 const char *file, int line)
175 if (!CRYPTO_secure_allocated(ptr)) {
176 OPENSSL_cleanse(ptr, num);
177 CRYPTO_free(ptr, file, line);
180 CRYPTO_THREAD_write_lock(sec_malloc_lock);
181 actual_size = sh_actual_size(ptr);
182 CLEAR(ptr, actual_size);
183 secure_mem_used -= actual_size;
185 CRYPTO_THREAD_unlock(sec_malloc_lock);
189 OPENSSL_cleanse(ptr, num);
190 CRYPTO_free(ptr, file, line);
191 #endif /* IMPLEMENTED */
194 int CRYPTO_secure_allocated(const void *ptr)
199 if (!secure_mem_initialized)
201 CRYPTO_THREAD_write_lock(sec_malloc_lock);
202 ret = sh_allocated(ptr);
203 CRYPTO_THREAD_unlock(sec_malloc_lock);
207 #endif /* IMPLEMENTED */
210 size_t CRYPTO_secure_used()
213 return secure_mem_used;
216 #endif /* IMPLEMENTED */
219 size_t CRYPTO_secure_actual_size(void *ptr)
224 CRYPTO_THREAD_write_lock(sec_malloc_lock);
225 actual_size = sh_actual_size(ptr);
226 CRYPTO_THREAD_unlock(sec_malloc_lock);
237 * SECURE HEAP IMPLEMENTATION
243 * The implementation provided here uses a fixed-sized mmap() heap,
244 * which is locked into memory, not written to core files, and protected
245 * on either side by an unmapped page, which will catch pointer overruns
246 * (or underruns) and an attempt to read data out of the secure heap.
247 * Free'd memory is zero'd or otherwise cleansed.
249 * This is a pretty standard buddy allocator. We keep areas in a multiple
250 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
251 * so all (and only) data is kept in the mmap'd heap.
253 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
257 #define ONE ((size_t)1)
259 # define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
260 # define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
261 # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
263 #define WITHIN_ARENA(p) \
264 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
265 #define WITHIN_FREELIST(p) \
266 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
269 typedef struct sh_list_st
271 struct sh_list_st *next;
272 struct sh_list_st **p_next;
282 ossl_ssize_t freelist_size;
284 unsigned char *bittable;
285 unsigned char *bitmalloc;
286 size_t bittable_size; /* size in bits */
291 static size_t sh_getlist(char *ptr)
293 ossl_ssize_t list = sh.freelist_size - 1;
294 size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
296 for (; bit; bit >>= 1, list--) {
297 if (TESTBIT(sh.bittable, bit))
299 OPENSSL_assert((bit & 1) == 0);
306 static int sh_testbit(char *ptr, int list, unsigned char *table)
310 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
311 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
312 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
313 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
314 return TESTBIT(table, bit);
317 static void sh_clearbit(char *ptr, int list, unsigned char *table)
321 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
322 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
323 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
324 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
325 OPENSSL_assert(TESTBIT(table, bit));
326 CLEARBIT(table, bit);
329 static void sh_setbit(char *ptr, int list, unsigned char *table)
333 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
334 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
335 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
336 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
337 OPENSSL_assert(!TESTBIT(table, bit));
341 static void sh_add_to_list(char **list, char *ptr)
345 OPENSSL_assert(WITHIN_FREELIST(list));
346 OPENSSL_assert(WITHIN_ARENA(ptr));
348 temp = (SH_LIST *)ptr;
349 temp->next = *(SH_LIST **)list;
350 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
351 temp->p_next = (SH_LIST **)list;
353 if (temp->next != NULL) {
354 OPENSSL_assert((char **)temp->next->p_next == list);
355 temp->next->p_next = &(temp->next);
361 static void sh_remove_from_list(char *ptr)
363 SH_LIST *temp, *temp2;
365 temp = (SH_LIST *)ptr;
366 if (temp->next != NULL)
367 temp->next->p_next = temp->p_next;
368 *temp->p_next = temp->next;
369 if (temp->next == NULL)
373 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
377 static int sh_init(size_t size, int minsize)
384 memset(&sh, 0, sizeof(sh));
386 /* make sure size and minsize are powers of 2 */
387 OPENSSL_assert(size > 0);
388 OPENSSL_assert((size & (size - 1)) == 0);
389 OPENSSL_assert(minsize > 0);
390 OPENSSL_assert((minsize & (minsize - 1)) == 0);
391 if (size <= 0 || (size & (size - 1)) != 0)
393 if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
396 while (minsize < (int)sizeof(SH_LIST))
399 sh.arena_size = size;
400 sh.minsize = minsize;
401 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
403 /* Prevent allocations of size 0 later on */
404 if (sh.bittable_size >> 3 == 0)
407 sh.freelist_size = -1;
408 for (i = sh.bittable_size; i; i >>= 1)
411 sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *));
412 OPENSSL_assert(sh.freelist != NULL);
413 if (sh.freelist == NULL)
416 sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
417 OPENSSL_assert(sh.bittable != NULL);
418 if (sh.bittable == NULL)
421 sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
422 OPENSSL_assert(sh.bitmalloc != NULL);
423 if (sh.bitmalloc == NULL)
426 /* Allocate space for heap, and two extra pages as guards */
427 #if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
429 # if defined(_SC_PAGE_SIZE)
430 long tmppgsize = sysconf(_SC_PAGE_SIZE);
432 long tmppgsize = sysconf(_SC_PAGESIZE);
437 pgsize = (size_t)tmppgsize;
442 sh.map_size = pgsize + sh.arena_size + pgsize;
445 sh.map_result = mmap(NULL, sh.map_size,
446 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
451 sh.map_result = MAP_FAILED;
452 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
453 sh.map_result = mmap(NULL, sh.map_size,
454 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
458 if (sh.map_result == MAP_FAILED)
460 sh.arena = (char *)(sh.map_result + pgsize);
461 sh_setbit(sh.arena, 0, sh.bittable);
462 sh_add_to_list(&sh.freelist[0], sh.arena);
464 /* Now try to add guard pages and lock into memory. */
467 /* Starting guard is already aligned from mmap. */
468 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
471 /* Ending guard page - need to round up to page boundary */
472 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
473 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
476 #if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
477 if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
478 if (errno == ENOSYS) {
479 if (mlock(sh.arena, sh.arena_size) < 0)
486 if (mlock(sh.arena, sh.arena_size) < 0)
490 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
501 static void sh_done()
503 OPENSSL_free(sh.freelist);
504 OPENSSL_free(sh.bittable);
505 OPENSSL_free(sh.bitmalloc);
506 if (sh.map_result != NULL && sh.map_size)
507 munmap(sh.map_result, sh.map_size);
508 memset(&sh, 0, sizeof(sh));
511 static int sh_allocated(const char *ptr)
513 return WITHIN_ARENA(ptr) ? 1 : 0;
516 static char *sh_find_my_buddy(char *ptr, int list)
521 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
524 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
525 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
530 static void *sh_malloc(size_t size)
532 ossl_ssize_t list, slist;
536 if (size > sh.arena_size)
539 list = sh.freelist_size - 1;
540 for (i = sh.minsize; i < size; i <<= 1)
545 /* try to find a larger entry to split */
546 for (slist = list; slist >= 0; slist--)
547 if (sh.freelist[slist] != NULL)
552 /* split larger entry */
553 while (slist != list) {
554 char *temp = sh.freelist[slist];
556 /* remove from bigger list */
557 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
558 sh_clearbit(temp, slist, sh.bittable);
559 sh_remove_from_list(temp);
560 OPENSSL_assert(temp != sh.freelist[slist]);
562 /* done with bigger list */
565 /* add to smaller list */
566 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
567 sh_setbit(temp, slist, sh.bittable);
568 sh_add_to_list(&sh.freelist[slist], temp);
569 OPENSSL_assert(sh.freelist[slist] == temp);
572 temp += sh.arena_size >> slist;
573 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
574 sh_setbit(temp, slist, sh.bittable);
575 sh_add_to_list(&sh.freelist[slist], temp);
576 OPENSSL_assert(sh.freelist[slist] == temp);
578 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
581 /* peel off memory to hand back */
582 chunk = sh.freelist[list];
583 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
584 sh_setbit(chunk, list, sh.bitmalloc);
585 sh_remove_from_list(chunk);
587 OPENSSL_assert(WITHIN_ARENA(chunk));
592 static void sh_free(void *ptr)
599 OPENSSL_assert(WITHIN_ARENA(ptr));
600 if (!WITHIN_ARENA(ptr))
603 list = sh_getlist(ptr);
604 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
605 sh_clearbit(ptr, list, sh.bitmalloc);
606 sh_add_to_list(&sh.freelist[list], ptr);
608 /* Try to coalesce two adjacent free areas. */
609 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
610 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
611 OPENSSL_assert(ptr != NULL);
612 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
613 sh_clearbit(ptr, list, sh.bittable);
614 sh_remove_from_list(ptr);
615 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
616 sh_clearbit(buddy, list, sh.bittable);
617 sh_remove_from_list(buddy);
624 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
625 sh_setbit(ptr, list, sh.bittable);
626 sh_add_to_list(&sh.freelist[list], ptr);
627 OPENSSL_assert(sh.freelist[list] == ptr);
631 static size_t sh_actual_size(char *ptr)
635 OPENSSL_assert(WITHIN_ARENA(ptr));
636 if (!WITHIN_ARENA(ptr))
638 list = sh_getlist(ptr);
639 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
640 return sh.arena_size / (ONE << list);
642 #endif /* IMPLEMENTED */