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 # if defined(SYS_mlock2)
37 # include <linux/mman.h>
41 # include <sys/param.h>
42 # include <sys/stat.h>
46 #define CLEAR(p, s) OPENSSL_cleanse(p, s)
48 # define PAGE_SIZE 4096
50 #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
51 # define MAP_ANON MAP_ANONYMOUS
55 static size_t secure_mem_used;
57 static int secure_mem_initialized;
59 static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
62 * These are the functions that must be implemented by a secure heap (sh).
64 static int sh_init(size_t size, int minsize);
65 static void *sh_malloc(size_t size);
66 static void sh_free(void *ptr);
67 static void sh_done(void);
68 static size_t sh_actual_size(char *ptr);
69 static int sh_allocated(const char *ptr);
72 int CRYPTO_secure_malloc_init(size_t size, int minsize)
77 if (!secure_mem_initialized) {
78 sec_malloc_lock = CRYPTO_THREAD_lock_new();
79 if (sec_malloc_lock == NULL)
81 if ((ret = sh_init(size, minsize)) != 0) {
82 secure_mem_initialized = 1;
84 CRYPTO_THREAD_lock_free(sec_malloc_lock);
85 sec_malloc_lock = NULL;
92 #endif /* IMPLEMENTED */
95 int CRYPTO_secure_malloc_done(void)
98 if (secure_mem_used == 0) {
100 secure_mem_initialized = 0;
101 CRYPTO_THREAD_lock_free(sec_malloc_lock);
102 sec_malloc_lock = NULL;
105 #endif /* IMPLEMENTED */
109 int CRYPTO_secure_malloc_initialized(void)
112 return secure_mem_initialized;
115 #endif /* IMPLEMENTED */
118 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
124 if (!secure_mem_initialized) {
125 return CRYPTO_malloc(num, file, line);
127 CRYPTO_THREAD_write_lock(sec_malloc_lock);
128 ret = sh_malloc(num);
129 actual_size = ret ? sh_actual_size(ret) : 0;
130 secure_mem_used += actual_size;
131 CRYPTO_THREAD_unlock(sec_malloc_lock);
134 return CRYPTO_malloc(num, file, line);
135 #endif /* IMPLEMENTED */
138 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
140 void *ret = CRYPTO_secure_malloc(num, file, line);
147 void CRYPTO_secure_free(void *ptr, const char *file, int line)
154 if (!CRYPTO_secure_allocated(ptr)) {
155 CRYPTO_free(ptr, file, line);
158 CRYPTO_THREAD_write_lock(sec_malloc_lock);
159 actual_size = sh_actual_size(ptr);
160 CLEAR(ptr, actual_size);
161 secure_mem_used -= actual_size;
163 CRYPTO_THREAD_unlock(sec_malloc_lock);
165 CRYPTO_free(ptr, file, line);
166 #endif /* IMPLEMENTED */
169 void CRYPTO_secure_clear_free(void *ptr, size_t num,
170 const char *file, int line)
177 if (!CRYPTO_secure_allocated(ptr)) {
178 OPENSSL_cleanse(ptr, num);
179 CRYPTO_free(ptr, file, line);
182 CRYPTO_THREAD_write_lock(sec_malloc_lock);
183 actual_size = sh_actual_size(ptr);
184 CLEAR(ptr, actual_size);
185 secure_mem_used -= actual_size;
187 CRYPTO_THREAD_unlock(sec_malloc_lock);
191 OPENSSL_cleanse(ptr, num);
192 CRYPTO_free(ptr, file, line);
193 #endif /* IMPLEMENTED */
196 int CRYPTO_secure_allocated(const void *ptr)
201 if (!secure_mem_initialized)
203 CRYPTO_THREAD_write_lock(sec_malloc_lock);
204 ret = sh_allocated(ptr);
205 CRYPTO_THREAD_unlock(sec_malloc_lock);
209 #endif /* IMPLEMENTED */
212 size_t CRYPTO_secure_used(void)
215 return secure_mem_used;
218 #endif /* IMPLEMENTED */
221 size_t CRYPTO_secure_actual_size(void *ptr)
226 CRYPTO_THREAD_write_lock(sec_malloc_lock);
227 actual_size = sh_actual_size(ptr);
228 CRYPTO_THREAD_unlock(sec_malloc_lock);
239 * SECURE HEAP IMPLEMENTATION
245 * The implementation provided here uses a fixed-sized mmap() heap,
246 * which is locked into memory, not written to core files, and protected
247 * on either side by an unmapped page, which will catch pointer overruns
248 * (or underruns) and an attempt to read data out of the secure heap.
249 * Free'd memory is zero'd or otherwise cleansed.
251 * This is a pretty standard buddy allocator. We keep areas in a multiple
252 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
253 * so all (and only) data is kept in the mmap'd heap.
255 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
259 #define ONE ((size_t)1)
261 # define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
262 # define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
263 # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
265 #define WITHIN_ARENA(p) \
266 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
267 #define WITHIN_FREELIST(p) \
268 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
271 typedef struct sh_list_st
273 struct sh_list_st *next;
274 struct sh_list_st **p_next;
284 ossl_ssize_t freelist_size;
286 unsigned char *bittable;
287 unsigned char *bitmalloc;
288 size_t bittable_size; /* size in bits */
293 static size_t sh_getlist(char *ptr)
295 ossl_ssize_t list = sh.freelist_size - 1;
296 size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
298 for (; bit; bit >>= 1, list--) {
299 if (TESTBIT(sh.bittable, bit))
301 OPENSSL_assert((bit & 1) == 0);
308 static int sh_testbit(char *ptr, int list, unsigned char *table)
312 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
313 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
314 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
315 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
316 return TESTBIT(table, bit);
319 static void sh_clearbit(char *ptr, int list, unsigned char *table)
323 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
324 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
325 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
326 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
327 OPENSSL_assert(TESTBIT(table, bit));
328 CLEARBIT(table, bit);
331 static void sh_setbit(char *ptr, int list, unsigned char *table)
335 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
336 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
337 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
338 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
339 OPENSSL_assert(!TESTBIT(table, bit));
343 static void sh_add_to_list(char **list, char *ptr)
347 OPENSSL_assert(WITHIN_FREELIST(list));
348 OPENSSL_assert(WITHIN_ARENA(ptr));
350 temp = (SH_LIST *)ptr;
351 temp->next = *(SH_LIST **)list;
352 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
353 temp->p_next = (SH_LIST **)list;
355 if (temp->next != NULL) {
356 OPENSSL_assert((char **)temp->next->p_next == list);
357 temp->next->p_next = &(temp->next);
363 static void sh_remove_from_list(char *ptr)
365 SH_LIST *temp, *temp2;
367 temp = (SH_LIST *)ptr;
368 if (temp->next != NULL)
369 temp->next->p_next = temp->p_next;
370 *temp->p_next = temp->next;
371 if (temp->next == NULL)
375 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
379 static int sh_init(size_t size, int minsize)
386 memset(&sh, 0, sizeof(sh));
388 /* make sure size and minsize are powers of 2 */
389 OPENSSL_assert(size > 0);
390 OPENSSL_assert((size & (size - 1)) == 0);
391 OPENSSL_assert(minsize > 0);
392 OPENSSL_assert((minsize & (minsize - 1)) == 0);
393 if (size <= 0 || (size & (size - 1)) != 0)
395 if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
398 while (minsize < (int)sizeof(SH_LIST))
401 sh.arena_size = size;
402 sh.minsize = minsize;
403 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
405 /* Prevent allocations of size 0 later on */
406 if (sh.bittable_size >> 3 == 0)
409 sh.freelist_size = -1;
410 for (i = sh.bittable_size; i; i >>= 1)
413 sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *));
414 OPENSSL_assert(sh.freelist != NULL);
415 if (sh.freelist == NULL)
418 sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
419 OPENSSL_assert(sh.bittable != NULL);
420 if (sh.bittable == NULL)
423 sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
424 OPENSSL_assert(sh.bitmalloc != NULL);
425 if (sh.bitmalloc == NULL)
428 /* Allocate space for heap, and two extra pages as guards */
429 #if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
431 # if defined(_SC_PAGE_SIZE)
432 long tmppgsize = sysconf(_SC_PAGE_SIZE);
434 long tmppgsize = sysconf(_SC_PAGESIZE);
439 pgsize = (size_t)tmppgsize;
444 sh.map_size = pgsize + sh.arena_size + pgsize;
447 sh.map_result = mmap(NULL, sh.map_size,
448 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
453 sh.map_result = MAP_FAILED;
454 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
455 sh.map_result = mmap(NULL, sh.map_size,
456 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
460 if (sh.map_result == MAP_FAILED)
462 sh.arena = (char *)(sh.map_result + pgsize);
463 sh_setbit(sh.arena, 0, sh.bittable);
464 sh_add_to_list(&sh.freelist[0], sh.arena);
466 /* Now try to add guard pages and lock into memory. */
469 /* Starting guard is already aligned from mmap. */
470 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
473 /* Ending guard page - need to round up to page boundary */
474 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
475 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
478 #if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
479 if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
480 if (errno == ENOSYS) {
481 if (mlock(sh.arena, sh.arena_size) < 0)
488 if (mlock(sh.arena, sh.arena_size) < 0)
492 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
503 static void sh_done(void)
505 OPENSSL_free(sh.freelist);
506 OPENSSL_free(sh.bittable);
507 OPENSSL_free(sh.bitmalloc);
508 if (sh.map_result != NULL && sh.map_size)
509 munmap(sh.map_result, sh.map_size);
510 memset(&sh, 0, sizeof(sh));
513 static int sh_allocated(const char *ptr)
515 return WITHIN_ARENA(ptr) ? 1 : 0;
518 static char *sh_find_my_buddy(char *ptr, int list)
523 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
526 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
527 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
532 static void *sh_malloc(size_t size)
534 ossl_ssize_t list, slist;
538 if (size > sh.arena_size)
541 list = sh.freelist_size - 1;
542 for (i = sh.minsize; i < size; i <<= 1)
547 /* try to find a larger entry to split */
548 for (slist = list; slist >= 0; slist--)
549 if (sh.freelist[slist] != NULL)
554 /* split larger entry */
555 while (slist != list) {
556 char *temp = sh.freelist[slist];
558 /* remove from bigger list */
559 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
560 sh_clearbit(temp, slist, sh.bittable);
561 sh_remove_from_list(temp);
562 OPENSSL_assert(temp != sh.freelist[slist]);
564 /* done with bigger list */
567 /* add to smaller list */
568 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
569 sh_setbit(temp, slist, sh.bittable);
570 sh_add_to_list(&sh.freelist[slist], temp);
571 OPENSSL_assert(sh.freelist[slist] == temp);
574 temp += sh.arena_size >> slist;
575 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
576 sh_setbit(temp, slist, sh.bittable);
577 sh_add_to_list(&sh.freelist[slist], temp);
578 OPENSSL_assert(sh.freelist[slist] == temp);
580 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
583 /* peel off memory to hand back */
584 chunk = sh.freelist[list];
585 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
586 sh_setbit(chunk, list, sh.bitmalloc);
587 sh_remove_from_list(chunk);
589 OPENSSL_assert(WITHIN_ARENA(chunk));
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);
626 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
627 sh_setbit(ptr, list, sh.bittable);
628 sh_add_to_list(&sh.freelist[list], ptr);
629 OPENSSL_assert(sh.freelist[list] == ptr);
633 static size_t sh_actual_size(char *ptr)
637 OPENSSL_assert(WITHIN_ARENA(ptr));
638 if (!WITHIN_ARENA(ptr))
640 list = sh_getlist(ptr);
641 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
642 return sh.arena_size / (ONE << list);
644 #endif /* IMPLEMENTED */