2 * Copyright 2015-2017 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").
18 #include <openssl/crypto.h>
23 #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
28 # include <sys/types.h>
29 # include <sys/mman.h>
30 # if defined(OPENSSL_SYS_LINUX)
31 # include <sys/syscall.h>
32 # include <linux/mman.h>
35 # include <sys/param.h>
36 # include <sys/stat.h>
40 #define CLEAR(p, s) OPENSSL_cleanse(p, s)
42 # define PAGE_SIZE 4096
46 static size_t secure_mem_used;
48 static int secure_mem_initialized;
50 static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
53 * These are the functions that must be implemented by a secure heap (sh).
55 static int sh_init(size_t size, int minsize);
56 static void *sh_malloc(size_t size);
57 static void sh_free(void *ptr);
58 static void sh_done(void);
59 static size_t sh_actual_size(char *ptr);
60 static int sh_allocated(const char *ptr);
63 int CRYPTO_secure_malloc_init(size_t size, int minsize)
68 if (!secure_mem_initialized) {
69 sec_malloc_lock = CRYPTO_THREAD_lock_new();
70 if (sec_malloc_lock == NULL)
72 if ((ret = sh_init(size, minsize)) != 0) {
73 secure_mem_initialized = 1;
75 CRYPTO_THREAD_lock_free(sec_malloc_lock);
76 sec_malloc_lock = NULL;
83 #endif /* IMPLEMENTED */
86 int CRYPTO_secure_malloc_done()
89 if (secure_mem_used == 0) {
91 secure_mem_initialized = 0;
92 CRYPTO_THREAD_lock_free(sec_malloc_lock);
93 sec_malloc_lock = NULL;
96 #endif /* IMPLEMENTED */
100 int CRYPTO_secure_malloc_initialized()
103 return secure_mem_initialized;
106 #endif /* IMPLEMENTED */
109 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
115 if (!secure_mem_initialized) {
116 return CRYPTO_malloc(num, file, line);
118 CRYPTO_THREAD_write_lock(sec_malloc_lock);
119 ret = sh_malloc(num);
120 actual_size = ret ? sh_actual_size(ret) : 0;
121 secure_mem_used += actual_size;
122 CRYPTO_THREAD_unlock(sec_malloc_lock);
125 return CRYPTO_malloc(num, file, line);
126 #endif /* IMPLEMENTED */
129 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
131 void *ret = CRYPTO_secure_malloc(num, file, line);
138 void CRYPTO_secure_free(void *ptr, const char *file, int line)
145 if (!CRYPTO_secure_allocated(ptr)) {
146 CRYPTO_free(ptr, file, line);
149 CRYPTO_THREAD_write_lock(sec_malloc_lock);
150 actual_size = sh_actual_size(ptr);
151 CLEAR(ptr, actual_size);
152 secure_mem_used -= actual_size;
154 CRYPTO_THREAD_unlock(sec_malloc_lock);
156 CRYPTO_free(ptr, file, line);
157 #endif /* IMPLEMENTED */
160 int CRYPTO_secure_allocated(const void *ptr)
165 if (!secure_mem_initialized)
167 CRYPTO_THREAD_write_lock(sec_malloc_lock);
168 ret = sh_allocated(ptr);
169 CRYPTO_THREAD_unlock(sec_malloc_lock);
173 #endif /* IMPLEMENTED */
176 size_t CRYPTO_secure_used()
179 return secure_mem_used;
182 #endif /* IMPLEMENTED */
185 size_t CRYPTO_secure_actual_size(void *ptr)
190 CRYPTO_THREAD_write_lock(sec_malloc_lock);
191 actual_size = sh_actual_size(ptr);
192 CRYPTO_THREAD_unlock(sec_malloc_lock);
203 * SECURE HEAP IMPLEMENTATION
209 * The implementation provided here uses a fixed-sized mmap() heap,
210 * which is locked into memory, not written to core files, and protected
211 * on either side by an unmapped page, which will catch pointer overruns
212 * (or underruns) and an attempt to read data out of the secure heap.
213 * Free'd memory is zero'd or otherwise cleansed.
215 * This is a pretty standard buddy allocator. We keep areas in a multiple
216 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
217 * so all (and only) data is kept in the mmap'd heap.
219 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
223 #define ONE ((size_t)1)
225 # define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
226 # define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
227 # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
229 #define WITHIN_ARENA(p) \
230 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
231 #define WITHIN_FREELIST(p) \
232 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
235 typedef struct sh_list_st
237 struct sh_list_st *next;
238 struct sh_list_st **p_next;
248 ossl_ssize_t freelist_size;
250 unsigned char *bittable;
251 unsigned char *bitmalloc;
252 size_t bittable_size; /* size in bits */
257 static size_t sh_getlist(char *ptr)
259 ossl_ssize_t list = sh.freelist_size - 1;
260 size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
262 for (; bit; bit >>= 1, list--) {
263 if (TESTBIT(sh.bittable, bit))
265 OPENSSL_assert((bit & 1) == 0);
272 static int sh_testbit(char *ptr, int list, unsigned char *table)
276 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
277 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
278 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
279 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
280 return TESTBIT(table, bit);
283 static void sh_clearbit(char *ptr, int list, unsigned char *table)
287 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
288 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
289 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
290 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
291 OPENSSL_assert(TESTBIT(table, bit));
292 CLEARBIT(table, bit);
295 static void sh_setbit(char *ptr, int list, unsigned char *table)
299 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
300 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
301 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
302 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
303 OPENSSL_assert(!TESTBIT(table, bit));
307 static void sh_add_to_list(char **list, char *ptr)
311 OPENSSL_assert(WITHIN_FREELIST(list));
312 OPENSSL_assert(WITHIN_ARENA(ptr));
314 temp = (SH_LIST *)ptr;
315 temp->next = *(SH_LIST **)list;
316 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
317 temp->p_next = (SH_LIST **)list;
319 if (temp->next != NULL) {
320 OPENSSL_assert((char **)temp->next->p_next == list);
321 temp->next->p_next = &(temp->next);
327 static void sh_remove_from_list(char *ptr)
329 SH_LIST *temp, *temp2;
331 temp = (SH_LIST *)ptr;
332 if (temp->next != NULL)
333 temp->next->p_next = temp->p_next;
334 *temp->p_next = temp->next;
335 if (temp->next == NULL)
339 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
343 static int sh_init(size_t size, int minsize)
350 memset(&sh, 0, sizeof sh);
352 /* make sure size and minsize are powers of 2 */
353 OPENSSL_assert(size > 0);
354 OPENSSL_assert((size & (size - 1)) == 0);
355 OPENSSL_assert(minsize > 0);
356 OPENSSL_assert((minsize & (minsize - 1)) == 0);
357 if (size <= 0 || (size & (size - 1)) != 0)
359 if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
362 while (minsize < (int)sizeof(SH_LIST))
365 sh.arena_size = size;
366 sh.minsize = minsize;
367 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
369 /* Prevent allocations of size 0 later on */
370 if (sh.bittable_size >> 3 == 0)
373 sh.freelist_size = -1;
374 for (i = sh.bittable_size; i; i >>= 1)
377 sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof (char *));
378 OPENSSL_assert(sh.freelist != NULL);
379 if (sh.freelist == NULL)
382 sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
383 OPENSSL_assert(sh.bittable != NULL);
384 if (sh.bittable == NULL)
387 sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
388 OPENSSL_assert(sh.bitmalloc != NULL);
389 if (sh.bitmalloc == NULL)
392 /* Allocate space for heap, and two extra pages as guards */
393 #if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
395 # if defined(_SC_PAGE_SIZE)
396 long tmppgsize = sysconf(_SC_PAGE_SIZE);
398 long tmppgsize = sysconf(_SC_PAGESIZE);
403 pgsize = (size_t)tmppgsize;
408 sh.map_size = pgsize + sh.arena_size + pgsize;
411 sh.map_result = mmap(NULL, sh.map_size,
412 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
417 sh.map_result = MAP_FAILED;
418 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
419 sh.map_result = mmap(NULL, sh.map_size,
420 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
424 if (sh.map_result == MAP_FAILED)
426 sh.arena = (char *)(sh.map_result + pgsize);
427 sh_setbit(sh.arena, 0, sh.bittable);
428 sh_add_to_list(&sh.freelist[0], sh.arena);
430 /* Now try to add guard pages and lock into memory. */
433 /* Starting guard is already aligned from mmap. */
434 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
437 /* Ending guard page - need to round up to page boundary */
438 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
439 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
442 #if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
443 if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
444 if (errno == ENOSYS) {
445 if (mlock(sh.arena, sh.arena_size) < 0)
452 if (mlock(sh.arena, sh.arena_size) < 0)
456 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
467 static void sh_done()
469 OPENSSL_free(sh.freelist);
470 OPENSSL_free(sh.bittable);
471 OPENSSL_free(sh.bitmalloc);
472 if (sh.map_result != NULL && sh.map_size)
473 munmap(sh.map_result, sh.map_size);
474 memset(&sh, 0, sizeof sh);
477 static int sh_allocated(const char *ptr)
479 return WITHIN_ARENA(ptr) ? 1 : 0;
482 static char *sh_find_my_buddy(char *ptr, int list)
487 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
490 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
491 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
496 static void *sh_malloc(size_t size)
498 ossl_ssize_t list, slist;
502 if (size > sh.arena_size)
505 list = sh.freelist_size - 1;
506 for (i = sh.minsize; i < size; i <<= 1)
511 /* try to find a larger entry to split */
512 for (slist = list; slist >= 0; slist--)
513 if (sh.freelist[slist] != NULL)
518 /* split larger entry */
519 while (slist != list) {
520 char *temp = sh.freelist[slist];
522 /* remove from bigger list */
523 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
524 sh_clearbit(temp, slist, sh.bittable);
525 sh_remove_from_list(temp);
526 OPENSSL_assert(temp != sh.freelist[slist]);
528 /* done with bigger list */
531 /* add to smaller list */
532 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
533 sh_setbit(temp, slist, sh.bittable);
534 sh_add_to_list(&sh.freelist[slist], temp);
535 OPENSSL_assert(sh.freelist[slist] == temp);
538 temp += sh.arena_size >> slist;
539 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
540 sh_setbit(temp, slist, sh.bittable);
541 sh_add_to_list(&sh.freelist[slist], temp);
542 OPENSSL_assert(sh.freelist[slist] == temp);
544 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
547 /* peel off memory to hand back */
548 chunk = sh.freelist[list];
549 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
550 sh_setbit(chunk, list, sh.bitmalloc);
551 sh_remove_from_list(chunk);
553 OPENSSL_assert(WITHIN_ARENA(chunk));
558 static void sh_free(void *ptr)
565 OPENSSL_assert(WITHIN_ARENA(ptr));
566 if (!WITHIN_ARENA(ptr))
569 list = sh_getlist(ptr);
570 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
571 sh_clearbit(ptr, list, sh.bitmalloc);
572 sh_add_to_list(&sh.freelist[list], ptr);
574 /* Try to coalesce two adjacent free areas. */
575 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
576 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
577 OPENSSL_assert(ptr != NULL);
578 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
579 sh_clearbit(ptr, list, sh.bittable);
580 sh_remove_from_list(ptr);
581 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
582 sh_clearbit(buddy, list, sh.bittable);
583 sh_remove_from_list(buddy);
590 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
591 sh_setbit(ptr, list, sh.bittable);
592 sh_add_to_list(&sh.freelist[list], ptr);
593 OPENSSL_assert(sh.freelist[list] == ptr);
597 static size_t sh_actual_size(char *ptr)
601 OPENSSL_assert(WITHIN_ARENA(ptr));
602 if (!WITHIN_ARENA(ptr))
604 list = sh_getlist(ptr);
605 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
606 return sh.arena_size / (ONE << list);
608 #endif /* IMPLEMENTED */