2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
5 * Licensed under the Apache License 2.0 (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 #ifndef OPENSSL_NO_SECURE_MEMORY
27 # include <sys/types.h>
28 # include <sys/mman.h>
29 # if defined(OPENSSL_SYS_LINUX)
30 # include <sys/syscall.h>
31 # if defined(SYS_mlock2)
32 # include <linux/mman.h>
35 # include <sys/param.h>
37 # include <sys/stat.h>
41 #define CLEAR(p, s) OPENSSL_cleanse(p, s)
43 # define PAGE_SIZE 4096
45 #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
46 # define MAP_ANON MAP_ANONYMOUS
49 #ifndef OPENSSL_NO_SECURE_MEMORY
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, size_t 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, size_t minsize)
69 #ifndef OPENSSL_NO_SECURE_MEMORY
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 /* OPENSSL_NO_SECURE_MEMORY */
90 int CRYPTO_secure_malloc_done(void)
92 #ifndef OPENSSL_NO_SECURE_MEMORY
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 /* OPENSSL_NO_SECURE_MEMORY */
104 int CRYPTO_secure_malloc_initialized(void)
106 #ifndef OPENSSL_NO_SECURE_MEMORY
107 return secure_mem_initialized;
110 #endif /* OPENSSL_NO_SECURE_MEMORY */
113 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
115 #ifndef OPENSSL_NO_SECURE_MEMORY
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 /* OPENSSL_NO_SECURE_MEMORY */
133 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
135 #ifndef OPENSSL_NO_SECURE_MEMORY
136 if (secure_mem_initialized)
137 /* CRYPTO_secure_malloc() zeroes allocations when it is implemented */
138 return CRYPTO_secure_malloc(num, file, line);
140 return CRYPTO_zalloc(num, file, line);
143 void CRYPTO_secure_free(void *ptr, const char *file, int line)
145 #ifndef OPENSSL_NO_SECURE_MEMORY
150 if (!CRYPTO_secure_allocated(ptr)) {
151 CRYPTO_free(ptr, file, line);
154 CRYPTO_THREAD_write_lock(sec_malloc_lock);
155 actual_size = sh_actual_size(ptr);
156 CLEAR(ptr, actual_size);
157 secure_mem_used -= actual_size;
159 CRYPTO_THREAD_unlock(sec_malloc_lock);
161 CRYPTO_free(ptr, file, line);
162 #endif /* OPENSSL_NO_SECURE_MEMORY */
165 void CRYPTO_secure_clear_free(void *ptr, size_t num,
166 const char *file, int line)
168 #ifndef OPENSSL_NO_SECURE_MEMORY
173 if (!CRYPTO_secure_allocated(ptr)) {
174 OPENSSL_cleanse(ptr, num);
175 CRYPTO_free(ptr, file, line);
178 CRYPTO_THREAD_write_lock(sec_malloc_lock);
179 actual_size = sh_actual_size(ptr);
180 CLEAR(ptr, actual_size);
181 secure_mem_used -= actual_size;
183 CRYPTO_THREAD_unlock(sec_malloc_lock);
187 OPENSSL_cleanse(ptr, num);
188 CRYPTO_free(ptr, file, line);
189 #endif /* OPENSSL_NO_SECURE_MEMORY */
192 int CRYPTO_secure_allocated(const void *ptr)
194 #ifndef OPENSSL_NO_SECURE_MEMORY
197 if (!secure_mem_initialized)
199 CRYPTO_THREAD_write_lock(sec_malloc_lock);
200 ret = sh_allocated(ptr);
201 CRYPTO_THREAD_unlock(sec_malloc_lock);
205 #endif /* OPENSSL_NO_SECURE_MEMORY */
208 size_t CRYPTO_secure_used(void)
210 #ifndef OPENSSL_NO_SECURE_MEMORY
211 return secure_mem_used;
214 #endif /* OPENSSL_NO_SECURE_MEMORY */
217 size_t CRYPTO_secure_actual_size(void *ptr)
219 #ifndef OPENSSL_NO_SECURE_MEMORY
222 CRYPTO_THREAD_write_lock(sec_malloc_lock);
223 actual_size = sh_actual_size(ptr);
224 CRYPTO_THREAD_unlock(sec_malloc_lock);
232 * SECURE HEAP IMPLEMENTATION
234 #ifndef OPENSSL_NO_SECURE_MEMORY
238 * The implementation provided here uses a fixed-sized mmap() heap,
239 * which is locked into memory, not written to core files, and protected
240 * on either side by an unmapped page, which will catch pointer overruns
241 * (or underruns) and an attempt to read data out of the secure heap.
242 * Free'd memory is zero'd or otherwise cleansed.
244 * This is a pretty standard buddy allocator. We keep areas in a multiple
245 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
246 * so all (and only) data is kept in the mmap'd heap.
248 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
252 #define ONE ((size_t)1)
254 # define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
255 # define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
256 # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
258 #define WITHIN_ARENA(p) \
259 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
260 #define WITHIN_FREELIST(p) \
261 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
264 typedef struct sh_list_st
266 struct sh_list_st *next;
267 struct sh_list_st **p_next;
277 ossl_ssize_t freelist_size;
279 unsigned char *bittable;
280 unsigned char *bitmalloc;
281 size_t bittable_size; /* size in bits */
286 static size_t sh_getlist(char *ptr)
288 ossl_ssize_t list = sh.freelist_size - 1;
289 size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
291 for (; bit; bit >>= 1, list--) {
292 if (TESTBIT(sh.bittable, bit))
294 OPENSSL_assert((bit & 1) == 0);
301 static int sh_testbit(char *ptr, int list, unsigned char *table)
305 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
306 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
307 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
308 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
309 return TESTBIT(table, bit);
312 static void sh_clearbit(char *ptr, int list, unsigned char *table)
316 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
317 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
318 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
319 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
320 OPENSSL_assert(TESTBIT(table, bit));
321 CLEARBIT(table, bit);
324 static void sh_setbit(char *ptr, int list, unsigned char *table)
328 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
329 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
330 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
331 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
332 OPENSSL_assert(!TESTBIT(table, bit));
336 static void sh_add_to_list(char **list, char *ptr)
340 OPENSSL_assert(WITHIN_FREELIST(list));
341 OPENSSL_assert(WITHIN_ARENA(ptr));
343 temp = (SH_LIST *)ptr;
344 temp->next = *(SH_LIST **)list;
345 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
346 temp->p_next = (SH_LIST **)list;
348 if (temp->next != NULL) {
349 OPENSSL_assert((char **)temp->next->p_next == list);
350 temp->next->p_next = &(temp->next);
356 static void sh_remove_from_list(char *ptr)
358 SH_LIST *temp, *temp2;
360 temp = (SH_LIST *)ptr;
361 if (temp->next != NULL)
362 temp->next->p_next = temp->p_next;
363 *temp->p_next = temp->next;
364 if (temp->next == NULL)
368 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
372 static int sh_init(size_t size, size_t minsize)
379 memset(&sh, 0, sizeof(sh));
381 /* make sure size and minsize are powers of 2 */
382 OPENSSL_assert(size > 0);
383 OPENSSL_assert((size & (size - 1)) == 0);
384 OPENSSL_assert((minsize & (minsize - 1)) == 0);
385 if (size == 0 || (size & (size - 1)) != 0)
387 if (minsize == 0 || (minsize & (minsize - 1)) != 0)
390 while (minsize < sizeof(SH_LIST))
393 sh.arena_size = size;
394 sh.minsize = minsize;
395 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
397 /* Prevent allocations of size 0 later on */
398 if (sh.bittable_size >> 3 == 0)
401 sh.freelist_size = -1;
402 for (i = sh.bittable_size; i; i >>= 1)
405 sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *));
406 OPENSSL_assert(sh.freelist != NULL);
407 if (sh.freelist == NULL)
410 sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
411 OPENSSL_assert(sh.bittable != NULL);
412 if (sh.bittable == NULL)
415 sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
416 OPENSSL_assert(sh.bitmalloc != NULL);
417 if (sh.bitmalloc == NULL)
420 /* Allocate space for heap, and two extra pages as guards */
421 #if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
423 # if defined(_SC_PAGE_SIZE)
424 long tmppgsize = sysconf(_SC_PAGE_SIZE);
426 long tmppgsize = sysconf(_SC_PAGESIZE);
431 pgsize = (size_t)tmppgsize;
436 sh.map_size = pgsize + sh.arena_size + pgsize;
439 sh.map_result = mmap(NULL, sh.map_size,
440 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
445 sh.map_result = MAP_FAILED;
446 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
447 sh.map_result = mmap(NULL, sh.map_size,
448 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
453 if (sh.map_result == MAP_FAILED)
455 sh.arena = (char *)(sh.map_result + pgsize);
456 sh_setbit(sh.arena, 0, sh.bittable);
457 sh_add_to_list(&sh.freelist[0], sh.arena);
459 /* Now try to add guard pages and lock into memory. */
462 /* Starting guard is already aligned from mmap. */
463 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
466 /* Ending guard page - need to round up to page boundary */
467 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
468 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
471 #if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
472 if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
473 if (errno == ENOSYS) {
474 if (mlock(sh.arena, sh.arena_size) < 0)
481 if (mlock(sh.arena, sh.arena_size) < 0)
485 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
496 static void sh_done(void)
498 OPENSSL_free(sh.freelist);
499 OPENSSL_free(sh.bittable);
500 OPENSSL_free(sh.bitmalloc);
501 if (sh.map_result != NULL && sh.map_size)
502 munmap(sh.map_result, sh.map_size);
503 memset(&sh, 0, sizeof(sh));
506 static int sh_allocated(const char *ptr)
508 return WITHIN_ARENA(ptr) ? 1 : 0;
511 static char *sh_find_my_buddy(char *ptr, int list)
516 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
519 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
520 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
525 static void *sh_malloc(size_t size)
527 ossl_ssize_t list, slist;
531 if (size > sh.arena_size)
534 list = sh.freelist_size - 1;
535 for (i = sh.minsize; i < size; i <<= 1)
540 /* try to find a larger entry to split */
541 for (slist = list; slist >= 0; slist--)
542 if (sh.freelist[slist] != NULL)
547 /* split larger entry */
548 while (slist != list) {
549 char *temp = sh.freelist[slist];
551 /* remove from bigger list */
552 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
553 sh_clearbit(temp, slist, sh.bittable);
554 sh_remove_from_list(temp);
555 OPENSSL_assert(temp != sh.freelist[slist]);
557 /* done with bigger list */
560 /* add to smaller list */
561 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
562 sh_setbit(temp, slist, sh.bittable);
563 sh_add_to_list(&sh.freelist[slist], temp);
564 OPENSSL_assert(sh.freelist[slist] == temp);
567 temp += sh.arena_size >> slist;
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);
573 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
576 /* peel off memory to hand back */
577 chunk = sh.freelist[list];
578 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
579 sh_setbit(chunk, list, sh.bitmalloc);
580 sh_remove_from_list(chunk);
582 OPENSSL_assert(WITHIN_ARENA(chunk));
584 /* zero the free list header as a precaution against information leakage */
585 memset(chunk, 0, sizeof(SH_LIST));
590 static void sh_free(void *ptr)
597 OPENSSL_assert(WITHIN_ARENA(ptr));
598 if (!WITHIN_ARENA(ptr))
601 list = sh_getlist(ptr);
602 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
603 sh_clearbit(ptr, list, sh.bitmalloc);
604 sh_add_to_list(&sh.freelist[list], ptr);
606 /* Try to coalesce two adjacent free areas. */
607 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
608 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
609 OPENSSL_assert(ptr != NULL);
610 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
611 sh_clearbit(ptr, list, sh.bittable);
612 sh_remove_from_list(ptr);
613 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
614 sh_clearbit(buddy, list, sh.bittable);
615 sh_remove_from_list(buddy);
619 /* Zero the higher addressed block's free list pointers */
620 memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST));
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 /* OPENSSL_NO_SECURE_MEMORY */