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 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 # include <sys/param.h>
38 # include <sys/stat.h>
42 #define CLEAR(p, s) OPENSSL_cleanse(p, s)
44 # define PAGE_SIZE 4096
46 #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
47 # define MAP_ANON MAP_ANONYMOUS
50 #ifdef OPENSSL_SECURE_MEMORY
51 static size_t secure_mem_used;
53 static int secure_mem_initialized;
55 static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
58 * These are the functions that must be implemented by a secure heap (sh).
60 static int sh_init(size_t size, int minsize);
61 static void *sh_malloc(size_t size);
62 static void sh_free(void *ptr);
63 static void sh_done(void);
64 static size_t sh_actual_size(char *ptr);
65 static int sh_allocated(const char *ptr);
68 int CRYPTO_secure_malloc_init(size_t size, int minsize)
70 #ifdef OPENSSL_SECURE_MEMORY
73 if (!secure_mem_initialized) {
74 sec_malloc_lock = CRYPTO_THREAD_lock_new();
75 if (sec_malloc_lock == NULL)
77 if ((ret = sh_init(size, minsize)) != 0) {
78 secure_mem_initialized = 1;
80 CRYPTO_THREAD_lock_free(sec_malloc_lock);
81 sec_malloc_lock = NULL;
88 #endif /* OPENSSL_SECURE_MEMORY */
91 int CRYPTO_secure_malloc_done(void)
93 #ifdef OPENSSL_SECURE_MEMORY
94 if (secure_mem_used == 0) {
96 secure_mem_initialized = 0;
97 CRYPTO_THREAD_lock_free(sec_malloc_lock);
98 sec_malloc_lock = NULL;
101 #endif /* OPENSSL_SECURE_MEMORY */
105 int CRYPTO_secure_malloc_initialized(void)
107 #ifdef OPENSSL_SECURE_MEMORY
108 return secure_mem_initialized;
111 #endif /* OPENSSL_SECURE_MEMORY */
114 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
116 #ifdef OPENSSL_SECURE_MEMORY
120 if (!secure_mem_initialized) {
121 return CRYPTO_malloc(num, file, line);
123 CRYPTO_THREAD_write_lock(sec_malloc_lock);
124 ret = sh_malloc(num);
125 actual_size = ret ? sh_actual_size(ret) : 0;
126 secure_mem_used += actual_size;
127 CRYPTO_THREAD_unlock(sec_malloc_lock);
130 return CRYPTO_malloc(num, file, line);
131 #endif /* OPENSSL_SECURE_MEMORY */
134 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
136 #ifdef OPENSSL_SECURE_MEMORY
137 if (secure_mem_initialized)
138 /* CRYPTO_secure_malloc() zeroes allocations when it is implemented */
139 return CRYPTO_secure_malloc(num, file, line);
141 return CRYPTO_zalloc(num, file, line);
144 void CRYPTO_secure_free(void *ptr, const char *file, int line)
146 #ifdef OPENSSL_SECURE_MEMORY
151 if (!CRYPTO_secure_allocated(ptr)) {
152 CRYPTO_free(ptr, file, line);
155 CRYPTO_THREAD_write_lock(sec_malloc_lock);
156 actual_size = sh_actual_size(ptr);
157 CLEAR(ptr, actual_size);
158 secure_mem_used -= actual_size;
160 CRYPTO_THREAD_unlock(sec_malloc_lock);
162 CRYPTO_free(ptr, file, line);
163 #endif /* OPENSSL_SECURE_MEMORY */
166 void CRYPTO_secure_clear_free(void *ptr, size_t num,
167 const char *file, int line)
169 #ifdef OPENSSL_SECURE_MEMORY
174 if (!CRYPTO_secure_allocated(ptr)) {
175 OPENSSL_cleanse(ptr, num);
176 CRYPTO_free(ptr, file, line);
179 CRYPTO_THREAD_write_lock(sec_malloc_lock);
180 actual_size = sh_actual_size(ptr);
181 CLEAR(ptr, actual_size);
182 secure_mem_used -= actual_size;
184 CRYPTO_THREAD_unlock(sec_malloc_lock);
188 OPENSSL_cleanse(ptr, num);
189 CRYPTO_free(ptr, file, line);
190 #endif /* OPENSSL_SECURE_MEMORY */
193 int CRYPTO_secure_allocated(const void *ptr)
195 #ifdef OPENSSL_SECURE_MEMORY
198 if (!secure_mem_initialized)
200 CRYPTO_THREAD_write_lock(sec_malloc_lock);
201 ret = sh_allocated(ptr);
202 CRYPTO_THREAD_unlock(sec_malloc_lock);
206 #endif /* OPENSSL_SECURE_MEMORY */
209 size_t CRYPTO_secure_used(void)
211 #ifdef OPENSSL_SECURE_MEMORY
212 return secure_mem_used;
215 #endif /* OPENSSL_SECURE_MEMORY */
218 size_t CRYPTO_secure_actual_size(void *ptr)
220 #ifdef OPENSSL_SECURE_MEMORY
223 CRYPTO_THREAD_write_lock(sec_malloc_lock);
224 actual_size = sh_actual_size(ptr);
225 CRYPTO_THREAD_unlock(sec_malloc_lock);
236 * SECURE HEAP IMPLEMENTATION
238 #ifdef OPENSSL_SECURE_MEMORY
242 * The implementation provided here uses a fixed-sized mmap() heap,
243 * which is locked into memory, not written to core files, and protected
244 * on either side by an unmapped page, which will catch pointer overruns
245 * (or underruns) and an attempt to read data out of the secure heap.
246 * Free'd memory is zero'd or otherwise cleansed.
248 * This is a pretty standard buddy allocator. We keep areas in a multiple
249 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
250 * so all (and only) data is kept in the mmap'd heap.
252 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
256 #define ONE ((size_t)1)
258 # define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
259 # define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
260 # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
262 #define WITHIN_ARENA(p) \
263 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
264 #define WITHIN_FREELIST(p) \
265 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
268 typedef struct sh_list_st
270 struct sh_list_st *next;
271 struct sh_list_st **p_next;
281 ossl_ssize_t freelist_size;
283 unsigned char *bittable;
284 unsigned char *bitmalloc;
285 size_t bittable_size; /* size in bits */
290 static size_t sh_getlist(char *ptr)
292 ossl_ssize_t list = sh.freelist_size - 1;
293 size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
295 for (; bit; bit >>= 1, list--) {
296 if (TESTBIT(sh.bittable, bit))
298 OPENSSL_assert((bit & 1) == 0);
305 static int sh_testbit(char *ptr, int list, unsigned char *table)
309 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
310 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
311 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
312 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
313 return TESTBIT(table, bit);
316 static void sh_clearbit(char *ptr, int list, unsigned char *table)
320 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
321 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
322 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
323 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
324 OPENSSL_assert(TESTBIT(table, bit));
325 CLEARBIT(table, bit);
328 static void sh_setbit(char *ptr, int list, unsigned char *table)
332 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
333 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
334 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
335 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
336 OPENSSL_assert(!TESTBIT(table, bit));
340 static void sh_add_to_list(char **list, char *ptr)
344 OPENSSL_assert(WITHIN_FREELIST(list));
345 OPENSSL_assert(WITHIN_ARENA(ptr));
347 temp = (SH_LIST *)ptr;
348 temp->next = *(SH_LIST **)list;
349 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
350 temp->p_next = (SH_LIST **)list;
352 if (temp->next != NULL) {
353 OPENSSL_assert((char **)temp->next->p_next == list);
354 temp->next->p_next = &(temp->next);
360 static void sh_remove_from_list(char *ptr)
362 SH_LIST *temp, *temp2;
364 temp = (SH_LIST *)ptr;
365 if (temp->next != NULL)
366 temp->next->p_next = temp->p_next;
367 *temp->p_next = temp->next;
368 if (temp->next == NULL)
372 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
376 static int sh_init(size_t size, int minsize)
383 memset(&sh, 0, sizeof(sh));
385 /* make sure size and minsize are powers of 2 */
386 OPENSSL_assert(size > 0);
387 OPENSSL_assert((size & (size - 1)) == 0);
388 OPENSSL_assert(minsize > 0);
389 OPENSSL_assert((minsize & (minsize - 1)) == 0);
390 if (size <= 0 || (size & (size - 1)) != 0)
392 if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
395 while (minsize < (int)sizeof(SH_LIST))
398 sh.arena_size = size;
399 sh.minsize = minsize;
400 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
402 /* Prevent allocations of size 0 later on */
403 if (sh.bittable_size >> 3 == 0)
406 sh.freelist_size = -1;
407 for (i = sh.bittable_size; i; i >>= 1)
410 sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *));
411 OPENSSL_assert(sh.freelist != NULL);
412 if (sh.freelist == NULL)
415 sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
416 OPENSSL_assert(sh.bittable != NULL);
417 if (sh.bittable == NULL)
420 sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
421 OPENSSL_assert(sh.bitmalloc != NULL);
422 if (sh.bitmalloc == NULL)
425 /* Allocate space for heap, and two extra pages as guards */
426 #if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
428 # if defined(_SC_PAGE_SIZE)
429 long tmppgsize = sysconf(_SC_PAGE_SIZE);
431 long tmppgsize = sysconf(_SC_PAGESIZE);
436 pgsize = (size_t)tmppgsize;
441 sh.map_size = pgsize + sh.arena_size + pgsize;
444 sh.map_result = mmap(NULL, sh.map_size,
445 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
450 sh.map_result = MAP_FAILED;
451 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
452 sh.map_result = mmap(NULL, sh.map_size,
453 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
457 if (sh.map_result == MAP_FAILED)
459 sh.arena = (char *)(sh.map_result + pgsize);
460 sh_setbit(sh.arena, 0, sh.bittable);
461 sh_add_to_list(&sh.freelist[0], sh.arena);
463 /* Now try to add guard pages and lock into memory. */
466 /* Starting guard is already aligned from mmap. */
467 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
470 /* Ending guard page - need to round up to page boundary */
471 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
472 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
475 #if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
476 if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
477 if (errno == ENOSYS) {
478 if (mlock(sh.arena, sh.arena_size) < 0)
485 if (mlock(sh.arena, sh.arena_size) < 0)
489 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
500 static void sh_done(void)
502 OPENSSL_free(sh.freelist);
503 OPENSSL_free(sh.bittable);
504 OPENSSL_free(sh.bitmalloc);
505 if (sh.map_result != NULL && sh.map_size)
506 munmap(sh.map_result, sh.map_size);
507 memset(&sh, 0, sizeof(sh));
510 static int sh_allocated(const char *ptr)
512 return WITHIN_ARENA(ptr) ? 1 : 0;
515 static char *sh_find_my_buddy(char *ptr, int list)
520 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
523 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
524 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
529 static void *sh_malloc(size_t size)
531 ossl_ssize_t list, slist;
535 if (size > sh.arena_size)
538 list = sh.freelist_size - 1;
539 for (i = sh.minsize; i < size; i <<= 1)
544 /* try to find a larger entry to split */
545 for (slist = list; slist >= 0; slist--)
546 if (sh.freelist[slist] != NULL)
551 /* split larger entry */
552 while (slist != list) {
553 char *temp = sh.freelist[slist];
555 /* remove from bigger list */
556 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
557 sh_clearbit(temp, slist, sh.bittable);
558 sh_remove_from_list(temp);
559 OPENSSL_assert(temp != sh.freelist[slist]);
561 /* done with bigger list */
564 /* add to smaller list */
565 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
566 sh_setbit(temp, slist, sh.bittable);
567 sh_add_to_list(&sh.freelist[slist], temp);
568 OPENSSL_assert(sh.freelist[slist] == temp);
571 temp += sh.arena_size >> slist;
572 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
573 sh_setbit(temp, slist, sh.bittable);
574 sh_add_to_list(&sh.freelist[slist], temp);
575 OPENSSL_assert(sh.freelist[slist] == temp);
577 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
580 /* peel off memory to hand back */
581 chunk = sh.freelist[list];
582 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
583 sh_setbit(chunk, list, sh.bitmalloc);
584 sh_remove_from_list(chunk);
586 OPENSSL_assert(WITHIN_ARENA(chunk));
588 /* zero the free list header as a precaution against information leakage */
589 memset(chunk, 0, sizeof(SH_LIST));
594 static void sh_free(void *ptr)
601 OPENSSL_assert(WITHIN_ARENA(ptr));
602 if (!WITHIN_ARENA(ptr))
605 list = sh_getlist(ptr);
606 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
607 sh_clearbit(ptr, list, sh.bitmalloc);
608 sh_add_to_list(&sh.freelist[list], ptr);
610 /* Try to coalesce two adjacent free areas. */
611 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
612 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
613 OPENSSL_assert(ptr != NULL);
614 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
615 sh_clearbit(ptr, list, sh.bittable);
616 sh_remove_from_list(ptr);
617 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
618 sh_clearbit(buddy, list, sh.bittable);
619 sh_remove_from_list(buddy);
623 /* Zero the higher addressed block's free list pointers */
624 memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST));
628 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
629 sh_setbit(ptr, list, sh.bittable);
630 sh_add_to_list(&sh.freelist[list], ptr);
631 OPENSSL_assert(sh.freelist[list] == ptr);
635 static size_t sh_actual_size(char *ptr)
639 OPENSSL_assert(WITHIN_ARENA(ptr));
640 if (!WITHIN_ARENA(ptr))
642 list = sh_getlist(ptr);
643 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
644 return sh.arena_size / (ONE << list);
646 #endif /* OPENSSL_SECURE_MEMORY */