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 /* e_os.h includes unistd.h, which defines _POSIX_VERSION */
28 #if !defined(OPENSSL_NO_SECURE_MEMORY) && defined(OPENSSL_SYS_UNIX) \
29 && defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
34 # include <sys/types.h>
35 # include <sys/mman.h>
36 # include <sys/param.h>
37 # include <sys/stat.h>
41 #define CLEAR(p, s) OPENSSL_cleanse(p, s)
43 # define PAGE_SIZE 4096
47 static size_t secure_mem_used;
49 static int secure_mem_initialized;
51 static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
54 * These are the functions that must be implemented by a secure heap (sh).
56 static int sh_init(size_t size, int minsize);
57 static char *sh_malloc(size_t size);
58 static void sh_free(char *ptr);
59 static void sh_done(void);
60 static size_t sh_actual_size(char *ptr);
61 static int sh_allocated(const char *ptr);
64 int CRYPTO_secure_malloc_init(size_t size, int minsize)
69 if (!secure_mem_initialized) {
70 sec_malloc_lock = CRYPTO_THREAD_lock_new();
71 if (sec_malloc_lock == NULL)
73 if ((ret = sh_init(size, minsize)) != 0) {
74 secure_mem_initialized = 1;
76 CRYPTO_THREAD_lock_free(sec_malloc_lock);
77 sec_malloc_lock = NULL;
84 #endif /* IMPLEMENTED */
87 int CRYPTO_secure_malloc_done()
90 if (secure_mem_used == 0) {
92 secure_mem_initialized = 0;
93 CRYPTO_THREAD_lock_free(sec_malloc_lock);
94 sec_malloc_lock = NULL;
97 #endif /* IMPLEMENTED */
101 int CRYPTO_secure_malloc_initialized()
104 return secure_mem_initialized;
107 #endif /* IMPLEMENTED */
110 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
116 if (!secure_mem_initialized) {
117 return CRYPTO_malloc(num, file, line);
119 CRYPTO_THREAD_write_lock(sec_malloc_lock);
120 ret = sh_malloc(num);
121 actual_size = ret ? sh_actual_size(ret) : 0;
122 secure_mem_used += actual_size;
123 CRYPTO_THREAD_unlock(sec_malloc_lock);
126 return CRYPTO_malloc(num, file, line);
127 #endif /* IMPLEMENTED */
130 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
132 void *ret = CRYPTO_secure_malloc(num, file, line);
139 void CRYPTO_secure_free(void *ptr, const char *file, int line)
146 if (!CRYPTO_secure_allocated(ptr)) {
147 CRYPTO_free(ptr, file, line);
150 CRYPTO_THREAD_write_lock(sec_malloc_lock);
151 actual_size = sh_actual_size(ptr);
152 CLEAR(ptr, actual_size);
153 secure_mem_used -= actual_size;
155 CRYPTO_THREAD_unlock(sec_malloc_lock);
157 CRYPTO_free(ptr, file, line);
158 #endif /* IMPLEMENTED */
161 void CRYPTO_secure_clear_free(void *ptr, size_t num,
162 const char *file, int line)
169 if (!CRYPTO_secure_allocated(ptr)) {
170 OPENSSL_cleanse(ptr, num);
171 CRYPTO_free(ptr, file, line);
174 CRYPTO_THREAD_write_lock(sec_malloc_lock);
175 actual_size = sh_actual_size(ptr);
176 CLEAR(ptr, actual_size);
177 secure_mem_used -= actual_size;
179 CRYPTO_THREAD_unlock(sec_malloc_lock);
183 OPENSSL_cleanse(ptr, num);
184 CRYPTO_free(ptr, file, line);
185 #endif /* IMPLEMENTED */
188 int CRYPTO_secure_allocated(const void *ptr)
193 if (!secure_mem_initialized)
195 CRYPTO_THREAD_write_lock(sec_malloc_lock);
196 ret = sh_allocated(ptr);
197 CRYPTO_THREAD_unlock(sec_malloc_lock);
201 #endif /* IMPLEMENTED */
204 size_t CRYPTO_secure_used()
207 return secure_mem_used;
210 #endif /* IMPLEMENTED */
213 size_t CRYPTO_secure_actual_size(void *ptr)
218 CRYPTO_THREAD_write_lock(sec_malloc_lock);
219 actual_size = sh_actual_size(ptr);
220 CRYPTO_THREAD_unlock(sec_malloc_lock);
231 * SECURE HEAP IMPLEMENTATION
237 * The implementation provided here uses a fixed-sized mmap() heap,
238 * which is locked into memory, not written to core files, and protected
239 * on either side by an unmapped page, which will catch pointer overruns
240 * (or underruns) and an attempt to read data out of the secure heap.
241 * Free'd memory is zero'd or otherwise cleansed.
243 * This is a pretty standard buddy allocator. We keep areas in a multiple
244 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
245 * so all (and only) data is kept in the mmap'd heap.
247 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
251 #define ONE ((size_t)1)
253 # define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
254 # define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
255 # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
257 #define WITHIN_ARENA(p) \
258 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
259 #define WITHIN_FREELIST(p) \
260 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
263 typedef struct sh_list_st
265 struct sh_list_st *next;
266 struct sh_list_st **p_next;
276 ossl_ssize_t freelist_size;
278 unsigned char *bittable;
279 unsigned char *bitmalloc;
280 size_t bittable_size; /* size in bits */
285 static size_t sh_getlist(char *ptr)
287 ossl_ssize_t list = sh.freelist_size - 1;
288 size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
290 for (; bit; bit >>= 1, list--) {
291 if (TESTBIT(sh.bittable, bit))
293 OPENSSL_assert((bit & 1) == 0);
300 static int sh_testbit(char *ptr, int list, unsigned char *table)
304 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
305 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
306 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
307 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
308 return TESTBIT(table, bit);
311 static void sh_clearbit(char *ptr, int list, unsigned char *table)
315 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
316 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
317 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
318 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
319 OPENSSL_assert(TESTBIT(table, bit));
320 CLEARBIT(table, bit);
323 static void sh_setbit(char *ptr, int list, unsigned char *table)
327 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
328 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
329 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
330 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
331 OPENSSL_assert(!TESTBIT(table, bit));
335 static void sh_add_to_list(char **list, char *ptr)
339 OPENSSL_assert(WITHIN_FREELIST(list));
340 OPENSSL_assert(WITHIN_ARENA(ptr));
342 temp = (SH_LIST *)ptr;
343 temp->next = *(SH_LIST **)list;
344 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
345 temp->p_next = (SH_LIST **)list;
347 if (temp->next != NULL) {
348 OPENSSL_assert((char **)temp->next->p_next == list);
349 temp->next->p_next = &(temp->next);
355 static void sh_remove_from_list(char *ptr)
357 SH_LIST *temp, *temp2;
359 temp = (SH_LIST *)ptr;
360 if (temp->next != NULL)
361 temp->next->p_next = temp->p_next;
362 *temp->p_next = temp->next;
363 if (temp->next == NULL)
367 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
371 static int sh_init(size_t size, int minsize)
378 memset(&sh, 0, sizeof(sh));
380 /* make sure size and minsize are powers of 2 */
381 OPENSSL_assert(size > 0);
382 OPENSSL_assert((size & (size - 1)) == 0);
383 OPENSSL_assert(minsize > 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 < (int)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);
452 if (sh.map_result == MAP_FAILED)
454 sh.arena = (char *)(sh.map_result + pgsize);
455 sh_setbit(sh.arena, 0, sh.bittable);
456 sh_add_to_list(&sh.freelist[0], sh.arena);
458 /* Now try to add guard pages and lock into memory. */
461 /* Starting guard is already aligned from mmap. */
462 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
465 /* Ending guard page - need to round up to page boundary */
466 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
467 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
470 if (mlock(sh.arena, sh.arena_size) < 0)
473 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
484 static void sh_done()
486 OPENSSL_free(sh.freelist);
487 OPENSSL_free(sh.bittable);
488 OPENSSL_free(sh.bitmalloc);
489 if (sh.map_result != NULL && sh.map_size)
490 munmap(sh.map_result, sh.map_size);
491 memset(&sh, 0, sizeof(sh));
494 static int sh_allocated(const char *ptr)
496 return WITHIN_ARENA(ptr) ? 1 : 0;
499 static char *sh_find_my_buddy(char *ptr, int list)
504 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
507 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
508 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
513 static char *sh_malloc(size_t size)
515 ossl_ssize_t list, slist;
519 if (size > sh.arena_size)
522 list = sh.freelist_size - 1;
523 for (i = sh.minsize; i < size; i <<= 1)
528 /* try to find a larger entry to split */
529 for (slist = list; slist >= 0; slist--)
530 if (sh.freelist[slist] != NULL)
535 /* split larger entry */
536 while (slist != list) {
537 char *temp = sh.freelist[slist];
539 /* remove from bigger list */
540 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
541 sh_clearbit(temp, slist, sh.bittable);
542 sh_remove_from_list(temp);
543 OPENSSL_assert(temp != sh.freelist[slist]);
545 /* done with bigger list */
548 /* add to smaller list */
549 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
550 sh_setbit(temp, slist, sh.bittable);
551 sh_add_to_list(&sh.freelist[slist], temp);
552 OPENSSL_assert(sh.freelist[slist] == temp);
555 temp += sh.arena_size >> slist;
556 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
557 sh_setbit(temp, slist, sh.bittable);
558 sh_add_to_list(&sh.freelist[slist], temp);
559 OPENSSL_assert(sh.freelist[slist] == temp);
561 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
564 /* peel off memory to hand back */
565 chunk = sh.freelist[list];
566 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
567 sh_setbit(chunk, list, sh.bitmalloc);
568 sh_remove_from_list(chunk);
570 OPENSSL_assert(WITHIN_ARENA(chunk));
575 static void sh_free(char *ptr)
582 OPENSSL_assert(WITHIN_ARENA(ptr));
583 if (!WITHIN_ARENA(ptr))
586 list = sh_getlist(ptr);
587 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
588 sh_clearbit(ptr, list, sh.bitmalloc);
589 sh_add_to_list(&sh.freelist[list], ptr);
591 /* Try to coalesce two adjacent free areas. */
592 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
593 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
594 OPENSSL_assert(ptr != NULL);
595 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
596 sh_clearbit(ptr, list, sh.bittable);
597 sh_remove_from_list(ptr);
598 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
599 sh_clearbit(buddy, list, sh.bittable);
600 sh_remove_from_list(buddy);
607 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
608 sh_setbit(ptr, list, sh.bittable);
609 sh_add_to_list(&sh.freelist[list], ptr);
610 OPENSSL_assert(sh.freelist[list] == ptr);
614 static size_t sh_actual_size(char *ptr)
618 OPENSSL_assert(WITHIN_ARENA(ptr));
619 if (!WITHIN_ARENA(ptr))
621 list = sh_getlist(ptr);
622 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
623 return sh.arena_size / (ONE << list);
625 #endif /* IMPLEMENTED */