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 # 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
50 static size_t secure_mem_used;
52 static int secure_mem_initialized;
54 static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
57 * These are the functions that must be implemented by a secure heap (sh).
59 static int sh_init(size_t size, int minsize);
60 static void *sh_malloc(size_t size);
61 static void sh_free(void *ptr);
62 static void sh_done(void);
63 static size_t sh_actual_size(char *ptr);
64 static int sh_allocated(const char *ptr);
67 int CRYPTO_secure_malloc_init(size_t size, int minsize)
72 if (!secure_mem_initialized) {
73 sec_malloc_lock = CRYPTO_THREAD_lock_new();
74 if (sec_malloc_lock == NULL)
76 if ((ret = sh_init(size, minsize)) != 0) {
77 secure_mem_initialized = 1;
79 CRYPTO_THREAD_lock_free(sec_malloc_lock);
80 sec_malloc_lock = NULL;
87 #endif /* IMPLEMENTED */
90 int CRYPTO_secure_malloc_done()
93 if (secure_mem_used == 0) {
95 secure_mem_initialized = 0;
96 CRYPTO_THREAD_lock_free(sec_malloc_lock);
97 sec_malloc_lock = NULL;
100 #endif /* IMPLEMENTED */
104 int CRYPTO_secure_malloc_initialized()
107 return secure_mem_initialized;
110 #endif /* IMPLEMENTED */
113 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
119 if (!secure_mem_initialized) {
120 return CRYPTO_malloc(num, file, line);
122 CRYPTO_THREAD_write_lock(sec_malloc_lock);
123 ret = sh_malloc(num);
124 actual_size = ret ? sh_actual_size(ret) : 0;
125 secure_mem_used += actual_size;
126 CRYPTO_THREAD_unlock(sec_malloc_lock);
129 return CRYPTO_malloc(num, file, line);
130 #endif /* IMPLEMENTED */
133 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
135 void *ret = CRYPTO_secure_malloc(num, file, line);
142 void CRYPTO_secure_free(void *ptr, const char *file, int line)
149 if (!CRYPTO_secure_allocated(ptr)) {
150 CRYPTO_free(ptr, file, line);
153 CRYPTO_THREAD_write_lock(sec_malloc_lock);
154 actual_size = sh_actual_size(ptr);
155 CLEAR(ptr, actual_size);
156 secure_mem_used -= actual_size;
158 CRYPTO_THREAD_unlock(sec_malloc_lock);
160 CRYPTO_free(ptr, file, line);
161 #endif /* IMPLEMENTED */
164 int CRYPTO_secure_allocated(const void *ptr)
169 if (!secure_mem_initialized)
171 CRYPTO_THREAD_write_lock(sec_malloc_lock);
172 ret = sh_allocated(ptr);
173 CRYPTO_THREAD_unlock(sec_malloc_lock);
177 #endif /* IMPLEMENTED */
180 size_t CRYPTO_secure_used()
183 return secure_mem_used;
186 #endif /* IMPLEMENTED */
189 size_t CRYPTO_secure_actual_size(void *ptr)
194 CRYPTO_THREAD_write_lock(sec_malloc_lock);
195 actual_size = sh_actual_size(ptr);
196 CRYPTO_THREAD_unlock(sec_malloc_lock);
207 * SECURE HEAP IMPLEMENTATION
213 * The implementation provided here uses a fixed-sized mmap() heap,
214 * which is locked into memory, not written to core files, and protected
215 * on either side by an unmapped page, which will catch pointer overruns
216 * (or underruns) and an attempt to read data out of the secure heap.
217 * Free'd memory is zero'd or otherwise cleansed.
219 * This is a pretty standard buddy allocator. We keep areas in a multiple
220 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
221 * so all (and only) data is kept in the mmap'd heap.
223 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
227 #define ONE ((size_t)1)
229 # define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
230 # define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
231 # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
233 #define WITHIN_ARENA(p) \
234 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
235 #define WITHIN_FREELIST(p) \
236 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
239 typedef struct sh_list_st
241 struct sh_list_st *next;
242 struct sh_list_st **p_next;
252 ossl_ssize_t freelist_size;
254 unsigned char *bittable;
255 unsigned char *bitmalloc;
256 size_t bittable_size; /* size in bits */
261 static size_t sh_getlist(char *ptr)
263 ossl_ssize_t list = sh.freelist_size - 1;
264 size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
266 for (; bit; bit >>= 1, list--) {
267 if (TESTBIT(sh.bittable, bit))
269 OPENSSL_assert((bit & 1) == 0);
276 static int sh_testbit(char *ptr, int list, unsigned char *table)
280 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
281 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
282 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
283 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
284 return TESTBIT(table, bit);
287 static void sh_clearbit(char *ptr, int list, unsigned char *table)
291 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
292 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
293 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
294 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
295 OPENSSL_assert(TESTBIT(table, bit));
296 CLEARBIT(table, bit);
299 static void sh_setbit(char *ptr, int list, unsigned char *table)
303 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
304 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
305 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
306 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
307 OPENSSL_assert(!TESTBIT(table, bit));
311 static void sh_add_to_list(char **list, char *ptr)
315 OPENSSL_assert(WITHIN_FREELIST(list));
316 OPENSSL_assert(WITHIN_ARENA(ptr));
318 temp = (SH_LIST *)ptr;
319 temp->next = *(SH_LIST **)list;
320 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
321 temp->p_next = (SH_LIST **)list;
323 if (temp->next != NULL) {
324 OPENSSL_assert((char **)temp->next->p_next == list);
325 temp->next->p_next = &(temp->next);
331 static void sh_remove_from_list(char *ptr)
333 SH_LIST *temp, *temp2;
335 temp = (SH_LIST *)ptr;
336 if (temp->next != NULL)
337 temp->next->p_next = temp->p_next;
338 *temp->p_next = temp->next;
339 if (temp->next == NULL)
343 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
347 static int sh_init(size_t size, int minsize)
354 memset(&sh, 0, sizeof sh);
356 /* make sure size and minsize are powers of 2 */
357 OPENSSL_assert(size > 0);
358 OPENSSL_assert((size & (size - 1)) == 0);
359 OPENSSL_assert(minsize > 0);
360 OPENSSL_assert((minsize & (minsize - 1)) == 0);
361 if (size <= 0 || (size & (size - 1)) != 0)
363 if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
366 while (minsize < (int)sizeof(SH_LIST))
369 sh.arena_size = size;
370 sh.minsize = minsize;
371 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
373 /* Prevent allocations of size 0 later on */
374 if (sh.bittable_size >> 3 == 0)
377 sh.freelist_size = -1;
378 for (i = sh.bittable_size; i; i >>= 1)
381 sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof (char *));
382 OPENSSL_assert(sh.freelist != NULL);
383 if (sh.freelist == NULL)
386 sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
387 OPENSSL_assert(sh.bittable != NULL);
388 if (sh.bittable == NULL)
391 sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
392 OPENSSL_assert(sh.bitmalloc != NULL);
393 if (sh.bitmalloc == NULL)
396 /* Allocate space for heap, and two extra pages as guards */
397 #if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
399 # if defined(_SC_PAGE_SIZE)
400 long tmppgsize = sysconf(_SC_PAGE_SIZE);
402 long tmppgsize = sysconf(_SC_PAGESIZE);
407 pgsize = (size_t)tmppgsize;
412 sh.map_size = pgsize + sh.arena_size + pgsize;
415 sh.map_result = mmap(NULL, sh.map_size,
416 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
421 sh.map_result = MAP_FAILED;
422 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
423 sh.map_result = mmap(NULL, sh.map_size,
424 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
428 if (sh.map_result == MAP_FAILED)
430 sh.arena = (char *)(sh.map_result + pgsize);
431 sh_setbit(sh.arena, 0, sh.bittable);
432 sh_add_to_list(&sh.freelist[0], sh.arena);
434 /* Now try to add guard pages and lock into memory. */
437 /* Starting guard is already aligned from mmap. */
438 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
441 /* Ending guard page - need to round up to page boundary */
442 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
443 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
446 #if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
447 if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
448 if (errno == ENOSYS) {
449 if (mlock(sh.arena, sh.arena_size) < 0)
456 if (mlock(sh.arena, sh.arena_size) < 0)
460 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
471 static void sh_done()
473 OPENSSL_free(sh.freelist);
474 OPENSSL_free(sh.bittable);
475 OPENSSL_free(sh.bitmalloc);
476 if (sh.map_result != NULL && sh.map_size)
477 munmap(sh.map_result, sh.map_size);
478 memset(&sh, 0, sizeof sh);
481 static int sh_allocated(const char *ptr)
483 return WITHIN_ARENA(ptr) ? 1 : 0;
486 static char *sh_find_my_buddy(char *ptr, int list)
491 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
494 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
495 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
500 static void *sh_malloc(size_t size)
502 ossl_ssize_t list, slist;
506 if (size > sh.arena_size)
509 list = sh.freelist_size - 1;
510 for (i = sh.minsize; i < size; i <<= 1)
515 /* try to find a larger entry to split */
516 for (slist = list; slist >= 0; slist--)
517 if (sh.freelist[slist] != NULL)
522 /* split larger entry */
523 while (slist != list) {
524 char *temp = sh.freelist[slist];
526 /* remove from bigger list */
527 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
528 sh_clearbit(temp, slist, sh.bittable);
529 sh_remove_from_list(temp);
530 OPENSSL_assert(temp != sh.freelist[slist]);
532 /* done with bigger list */
535 /* add to smaller list */
536 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
537 sh_setbit(temp, slist, sh.bittable);
538 sh_add_to_list(&sh.freelist[slist], temp);
539 OPENSSL_assert(sh.freelist[slist] == temp);
542 temp += sh.arena_size >> slist;
543 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
544 sh_setbit(temp, slist, sh.bittable);
545 sh_add_to_list(&sh.freelist[slist], temp);
546 OPENSSL_assert(sh.freelist[slist] == temp);
548 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
551 /* peel off memory to hand back */
552 chunk = sh.freelist[list];
553 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
554 sh_setbit(chunk, list, sh.bitmalloc);
555 sh_remove_from_list(chunk);
557 OPENSSL_assert(WITHIN_ARENA(chunk));
562 static void sh_free(void *ptr)
569 OPENSSL_assert(WITHIN_ARENA(ptr));
570 if (!WITHIN_ARENA(ptr))
573 list = sh_getlist(ptr);
574 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
575 sh_clearbit(ptr, list, sh.bitmalloc);
576 sh_add_to_list(&sh.freelist[list], ptr);
578 /* Try to coalesce two adjacent free areas. */
579 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
580 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
581 OPENSSL_assert(ptr != NULL);
582 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
583 sh_clearbit(ptr, list, sh.bittable);
584 sh_remove_from_list(ptr);
585 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
586 sh_clearbit(buddy, list, sh.bittable);
587 sh_remove_from_list(buddy);
594 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
595 sh_setbit(ptr, list, sh.bittable);
596 sh_add_to_list(&sh.freelist[list], ptr);
597 OPENSSL_assert(sh.freelist[list] == ptr);
601 static size_t sh_actual_size(char *ptr)
605 OPENSSL_assert(WITHIN_ARENA(ptr));
606 if (!WITHIN_ARENA(ptr))
608 list = sh_getlist(ptr);
609 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
610 return sh.arena_size / (ONE << list);
612 #endif /* IMPLEMENTED */