2 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
12 * This file is distributed under the terms of the OpenSSL license.
16 * This file is in two halves. The first half implements the public API
17 * to be used by external consumers, and to be used by OpenSSL to store
18 * data in a "secure arena." The second half implements the secure arena.
19 * For details on that implementation, see below (look for uppercase
20 * "SECURE HEAP IMPLEMENTATION").
22 #include <openssl/crypto.h>
27 #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
32 # include <sys/types.h>
33 # include <sys/mman.h>
34 # include <sys/param.h>
35 # include <sys/stat.h>
39 #define CLEAR(p, s) OPENSSL_cleanse(p, s)
41 # define PAGE_SIZE 4096
45 static size_t secure_mem_used;
47 static int secure_mem_initialized;
49 static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
52 * These are the functions that must be implemented by a secure heap (sh).
54 static int sh_init(size_t size, int minsize);
55 static char *sh_malloc(size_t size);
56 static void sh_free(char *ptr);
57 static void sh_done(void);
58 static size_t sh_actual_size(char *ptr);
59 static int sh_allocated(const char *ptr);
62 int CRYPTO_secure_malloc_init(size_t size, int minsize)
67 if (!secure_mem_initialized) {
68 sec_malloc_lock = CRYPTO_THREAD_lock_new();
69 if (sec_malloc_lock == NULL)
71 if ((ret = sh_init(size, minsize)) != 0) {
72 secure_mem_initialized = 1;
74 CRYPTO_THREAD_lock_free(sec_malloc_lock);
75 sec_malloc_lock = NULL;
82 #endif /* IMPLEMENTED */
85 int CRYPTO_secure_malloc_done()
88 if (secure_mem_used == 0) {
90 secure_mem_initialized = 0;
91 CRYPTO_THREAD_lock_free(sec_malloc_lock);
92 sec_malloc_lock = NULL;
95 #endif /* IMPLEMENTED */
99 int CRYPTO_secure_malloc_initialized()
102 return secure_mem_initialized;
105 #endif /* IMPLEMENTED */
108 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
114 if (!secure_mem_initialized) {
115 return CRYPTO_malloc(num, file, line);
117 CRYPTO_THREAD_write_lock(sec_malloc_lock);
118 ret = sh_malloc(num);
119 actual_size = ret ? sh_actual_size(ret) : 0;
120 secure_mem_used += actual_size;
121 CRYPTO_THREAD_unlock(sec_malloc_lock);
124 return CRYPTO_malloc(num, file, line);
125 #endif /* IMPLEMENTED */
128 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
130 void *ret = CRYPTO_secure_malloc(num, file, line);
137 void CRYPTO_secure_free(void *ptr, const char *file, int line)
144 if (!CRYPTO_secure_allocated(ptr)) {
145 CRYPTO_free(ptr, file, line);
148 CRYPTO_THREAD_write_lock(sec_malloc_lock);
149 actual_size = sh_actual_size(ptr);
150 CLEAR(ptr, actual_size);
151 secure_mem_used -= actual_size;
153 CRYPTO_THREAD_unlock(sec_malloc_lock);
155 CRYPTO_free(ptr, file, line);
156 #endif /* IMPLEMENTED */
159 void CRYPTO_secure_clear_free(void *ptr, size_t num,
160 const char *file, int line)
167 if (!CRYPTO_secure_allocated(ptr)) {
168 OPENSSL_cleanse(ptr, num);
169 CRYPTO_free(ptr, file, line);
172 CRYPTO_THREAD_write_lock(sec_malloc_lock);
173 actual_size = sh_actual_size(ptr);
174 CLEAR(ptr, actual_size);
175 secure_mem_used -= actual_size;
177 CRYPTO_THREAD_unlock(sec_malloc_lock);
181 OPENSSL_cleanse(ptr, num);
182 CRYPTO_free(ptr, file, line);
183 #endif /* IMPLEMENTED */
186 int CRYPTO_secure_allocated(const void *ptr)
191 if (!secure_mem_initialized)
193 CRYPTO_THREAD_write_lock(sec_malloc_lock);
194 ret = sh_allocated(ptr);
195 CRYPTO_THREAD_unlock(sec_malloc_lock);
199 #endif /* IMPLEMENTED */
202 size_t CRYPTO_secure_used()
205 return secure_mem_used;
208 #endif /* IMPLEMENTED */
211 size_t CRYPTO_secure_actual_size(void *ptr)
216 CRYPTO_THREAD_write_lock(sec_malloc_lock);
217 actual_size = sh_actual_size(ptr);
218 CRYPTO_THREAD_unlock(sec_malloc_lock);
229 * SECURE HEAP IMPLEMENTATION
235 * The implementation provided here uses a fixed-sized mmap() heap,
236 * which is locked into memory, not written to core files, and protected
237 * on either side by an unmapped page, which will catch pointer overruns
238 * (or underruns) and an attempt to read data out of the secure heap.
239 * Free'd memory is zero'd or otherwise cleansed.
241 * This is a pretty standard buddy allocator. We keep areas in a multiple
242 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
243 * so all (and only) data is kept in the mmap'd heap.
245 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
249 #define ONE ((size_t)1)
251 # define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
252 # define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
253 # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
255 #define WITHIN_ARENA(p) \
256 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
257 #define WITHIN_FREELIST(p) \
258 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
261 typedef struct sh_list_st
263 struct sh_list_st *next;
264 struct sh_list_st **p_next;
274 ossl_ssize_t freelist_size;
276 unsigned char *bittable;
277 unsigned char *bitmalloc;
278 size_t bittable_size; /* size in bits */
283 static size_t sh_getlist(char *ptr)
285 ossl_ssize_t list = sh.freelist_size - 1;
286 size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
288 for (; bit; bit >>= 1, list--) {
289 if (TESTBIT(sh.bittable, bit))
291 OPENSSL_assert((bit & 1) == 0);
298 static int sh_testbit(char *ptr, int list, unsigned char *table)
302 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
303 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
304 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
305 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
306 return TESTBIT(table, bit);
309 static void sh_clearbit(char *ptr, int list, unsigned char *table)
313 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
314 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
315 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
316 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
317 OPENSSL_assert(TESTBIT(table, bit));
318 CLEARBIT(table, bit);
321 static void sh_setbit(char *ptr, int list, unsigned char *table)
325 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
326 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
327 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
328 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
329 OPENSSL_assert(!TESTBIT(table, bit));
333 static void sh_add_to_list(char **list, char *ptr)
337 OPENSSL_assert(WITHIN_FREELIST(list));
338 OPENSSL_assert(WITHIN_ARENA(ptr));
340 temp = (SH_LIST *)ptr;
341 temp->next = *(SH_LIST **)list;
342 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
343 temp->p_next = (SH_LIST **)list;
345 if (temp->next != NULL) {
346 OPENSSL_assert((char **)temp->next->p_next == list);
347 temp->next->p_next = &(temp->next);
353 static void sh_remove_from_list(char *ptr)
355 SH_LIST *temp, *temp2;
357 temp = (SH_LIST *)ptr;
358 if (temp->next != NULL)
359 temp->next->p_next = temp->p_next;
360 *temp->p_next = temp->next;
361 if (temp->next == NULL)
365 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
369 static int sh_init(size_t size, int minsize)
376 memset(&sh, 0, sizeof sh);
378 /* make sure size and minsize are powers of 2 */
379 OPENSSL_assert(size > 0);
380 OPENSSL_assert((size & (size - 1)) == 0);
381 OPENSSL_assert(minsize > 0);
382 OPENSSL_assert((minsize & (minsize - 1)) == 0);
383 if (size <= 0 || (size & (size - 1)) != 0)
385 if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
388 while (minsize < (int)sizeof(SH_LIST))
391 sh.arena_size = size;
392 sh.minsize = minsize;
393 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
395 /* Prevent allocations of size 0 later on */
396 if (sh.bittable_size >> 3 == 0)
399 sh.freelist_size = -1;
400 for (i = sh.bittable_size; i; i >>= 1)
403 sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof (char *));
404 OPENSSL_assert(sh.freelist != NULL);
405 if (sh.freelist == NULL)
408 sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
409 OPENSSL_assert(sh.bittable != NULL);
410 if (sh.bittable == NULL)
413 sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
414 OPENSSL_assert(sh.bitmalloc != NULL);
415 if (sh.bitmalloc == NULL)
418 /* Allocate space for heap, and two extra pages as guards */
419 #if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
421 # if defined(_SC_PAGE_SIZE)
422 long tmppgsize = sysconf(_SC_PAGE_SIZE);
424 long tmppgsize = sysconf(_SC_PAGESIZE);
429 pgsize = (size_t)tmppgsize;
434 sh.map_size = pgsize + sh.arena_size + pgsize;
437 sh.map_result = mmap(NULL, sh.map_size,
438 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
443 sh.map_result = MAP_FAILED;
444 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
445 sh.map_result = mmap(NULL, sh.map_size,
446 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
450 if (sh.map_result == MAP_FAILED)
452 sh.arena = (char *)(sh.map_result + pgsize);
453 sh_setbit(sh.arena, 0, sh.bittable);
454 sh_add_to_list(&sh.freelist[0], sh.arena);
456 /* Now try to add guard pages and lock into memory. */
459 /* Starting guard is already aligned from mmap. */
460 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
463 /* Ending guard page - need to round up to page boundary */
464 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
465 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
468 if (mlock(sh.arena, sh.arena_size) < 0)
471 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
482 static void sh_done()
484 OPENSSL_free(sh.freelist);
485 OPENSSL_free(sh.bittable);
486 OPENSSL_free(sh.bitmalloc);
487 if (sh.map_result != NULL && sh.map_size)
488 munmap(sh.map_result, sh.map_size);
489 memset(&sh, 0, sizeof sh);
492 static int sh_allocated(const char *ptr)
494 return WITHIN_ARENA(ptr) ? 1 : 0;
497 static char *sh_find_my_buddy(char *ptr, int list)
502 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
505 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
506 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
511 static char *sh_malloc(size_t size)
513 ossl_ssize_t list, slist;
517 if (size > sh.arena_size)
520 list = sh.freelist_size - 1;
521 for (i = sh.minsize; i < size; i <<= 1)
526 /* try to find a larger entry to split */
527 for (slist = list; slist >= 0; slist--)
528 if (sh.freelist[slist] != NULL)
533 /* split larger entry */
534 while (slist != list) {
535 char *temp = sh.freelist[slist];
537 /* remove from bigger list */
538 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
539 sh_clearbit(temp, slist, sh.bittable);
540 sh_remove_from_list(temp);
541 OPENSSL_assert(temp != sh.freelist[slist]);
543 /* done with bigger list */
546 /* add to smaller list */
547 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
548 sh_setbit(temp, slist, sh.bittable);
549 sh_add_to_list(&sh.freelist[slist], temp);
550 OPENSSL_assert(sh.freelist[slist] == temp);
553 temp += sh.arena_size >> slist;
554 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
555 sh_setbit(temp, slist, sh.bittable);
556 sh_add_to_list(&sh.freelist[slist], temp);
557 OPENSSL_assert(sh.freelist[slist] == temp);
559 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
562 /* peel off memory to hand back */
563 chunk = sh.freelist[list];
564 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
565 sh_setbit(chunk, list, sh.bitmalloc);
566 sh_remove_from_list(chunk);
568 OPENSSL_assert(WITHIN_ARENA(chunk));
573 static void sh_free(char *ptr)
580 OPENSSL_assert(WITHIN_ARENA(ptr));
581 if (!WITHIN_ARENA(ptr))
584 list = sh_getlist(ptr);
585 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
586 sh_clearbit(ptr, list, sh.bitmalloc);
587 sh_add_to_list(&sh.freelist[list], ptr);
589 /* Try to coalesce two adjacent free areas. */
590 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
591 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
592 OPENSSL_assert(ptr != NULL);
593 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
594 sh_clearbit(ptr, list, sh.bittable);
595 sh_remove_from_list(ptr);
596 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
597 sh_clearbit(buddy, list, sh.bittable);
598 sh_remove_from_list(buddy);
605 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
606 sh_setbit(ptr, list, sh.bittable);
607 sh_add_to_list(&sh.freelist[list], ptr);
608 OPENSSL_assert(sh.freelist[list] == ptr);
612 static size_t sh_actual_size(char *ptr)
616 OPENSSL_assert(WITHIN_ARENA(ptr));
617 if (!WITHIN_ARENA(ptr))
619 list = sh_getlist(ptr);
620 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
621 return sh.arena_size / (ONE << list);
623 #endif /* IMPLEMENTED */