2 * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
3 * This file is distributed under the terms of the OpenSSL license.
7 * This file is in two halves. The first half implements the public API
8 * to be used by external consumers, and to be used by OpenSSL to store
9 * data in a "secure arena." The second half implements the secure arena.
10 * For details on that implementation, see below (look for uppercase
11 * "SECURE HEAP IMPLEMENTATION").
13 #include <openssl/crypto.h>
16 #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
22 # include <sys/mman.h>
23 # include <sys/param.h>
26 #define LOCK() CRYPTO_w_lock(CRYPTO_LOCK_MALLOC)
27 #define UNLOCK() CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC)
28 #define CLEAR(p, s) OPENSSL_cleanse(p, s)
30 # define PAGE_SIZE 4096
34 size_t secure_mem_used;
36 static int secure_mem_initialized;
40 * These are the functions that must be implemented by a secure heap (sh).
42 static int sh_init(size_t size, int minsize);
43 static char *sh_malloc(size_t size);
44 static void sh_free(char *ptr);
45 static void sh_done(void);
46 static int sh_actual_size(char *ptr);
47 static int sh_allocated(const char *ptr);
50 int CRYPTO_secure_malloc_init(size_t size, int minsize)
58 OPENSSL_assert(!secure_mem_initialized);
59 if (!secure_mem_initialized) {
60 ret = sh_init(size, minsize);
61 secure_mem_initialized = 1;
67 #endif /* IMPLEMENTED */
70 void CRYPTO_secure_malloc_done()
75 secure_mem_initialized = 0;
77 #endif /* IMPLEMENTED */
80 int CRYPTO_secure_malloc_initialized()
83 return secure_mem_initialized;
86 #endif /* IMPLEMENTED */
89 void *CRYPTO_secure_malloc(int num, const char *file, int line)
95 if (!secure_mem_initialized) {
97 return CRYPTO_malloc(num, file, line);
100 ret = sh_malloc(num);
101 actual_size = ret ? sh_actual_size(ret) : 0;
102 secure_mem_used += actual_size;
106 return CRYPTO_malloc(num, file, line);
107 #endif /* IMPLEMENTED */
110 void CRYPTO_secure_free(void *ptr)
117 if (!secure_mem_initialized) {
122 actual_size = sh_actual_size(ptr);
123 CLEAR(ptr, actual_size);
124 secure_mem_used -= actual_size;
129 #endif /* IMPLEMENTED */
132 int CRYPTO_secure_allocated(const void *ptr)
137 if (!secure_mem_initialized)
140 ret = sh_allocated(ptr);
145 #endif /* IMPLEMENTED */
153 * SECURE HEAP IMPLEMENTATION
159 * The implementation provided here uses a fixed-sized mmap() heap,
160 * which is locked into memory, not written to core files, and protected
161 * on either side by an unmapped page, which will catch pointer overruns
162 * (or underruns) and an attempt to read data out of the secure heap.
163 * Free'd memory is zero'd or otherwise cleansed.
165 * This is a pretty standard buddy allocator. We keep areas in a multiple
166 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
167 * so all (and only) data is kept in the mmap'd heap.
169 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
173 # define TESTBIT(t, b) (t[(b) >> 3] & (1 << ((b) & 7)))
174 # define SETBIT(t, b) (t[(b) >> 3] |= (1 << ((b) & 7)))
175 # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(1 << ((b) & 7))))
177 #define WITHIN_ARENA(p) \
178 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
179 #define WITHIN_FREELIST(p) \
180 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
183 typedef struct sh_list_st
185 struct sh_list_st *next;
186 struct sh_list_st **p_next;
198 unsigned char *bittable;
199 unsigned char *bitmalloc;
200 int bittable_size; /* size in bits */
205 static int sh_getlist(char *ptr)
207 int list = sh.freelist_size - 1;
208 int bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
210 for (; bit; bit >>= 1, list--) {
211 if (TESTBIT(sh.bittable, bit))
213 OPENSSL_assert((bit & 1) == 0);
220 static int sh_testbit(char *ptr, int list, unsigned char *table)
224 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
225 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
226 bit = (1 << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
227 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
228 return TESTBIT(table, bit);
231 static void sh_clearbit(char *ptr, int list, unsigned char *table)
235 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
236 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
237 bit = (1 << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
238 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
239 OPENSSL_assert(TESTBIT(table, bit));
240 CLEARBIT(table, bit);
243 static void sh_setbit(char *ptr, int list, unsigned char *table)
247 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
248 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
249 bit = (1 << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
250 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
251 OPENSSL_assert(!TESTBIT(table, bit));
255 static void sh_add_to_list(char **list, char *ptr)
259 OPENSSL_assert(WITHIN_FREELIST(list));
260 OPENSSL_assert(WITHIN_ARENA(ptr));
262 temp = (SH_LIST *)ptr;
263 temp->next = *(SH_LIST **)list;
264 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
265 temp->p_next = (SH_LIST **)list;
267 if (temp->next != NULL) {
268 OPENSSL_assert((char **)temp->next->p_next == list);
269 temp->next->p_next = &(temp->next);
275 static void sh_remove_from_list(char *ptr, char *list)
277 SH_LIST *temp, *temp2;
279 temp = (SH_LIST *)ptr;
280 if (temp->next != NULL)
281 temp->next->p_next = temp->p_next;
282 *temp->p_next = temp->next;
283 if (temp->next == NULL)
287 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
291 static int sh_init(size_t size, int minsize)
297 memset(&sh, 0, sizeof sh);
299 /* make sure size and minsize are powers of 2 */
300 OPENSSL_assert(size > 0);
301 OPENSSL_assert((size & (size - 1)) == 0);
302 OPENSSL_assert(minsize > 0);
303 OPENSSL_assert((minsize & (minsize - 1)) == 0);
304 if (size <= 0 || (size & (size - 1)) != 0)
306 if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
309 sh.arena_size = size;
310 sh.minsize = minsize;
311 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
313 sh.freelist_size = -1;
314 for (i = sh.bittable_size; i; i >>= 1)
317 sh.freelist = OPENSSL_malloc(sh.freelist_size * sizeof (char *));
318 OPENSSL_assert(sh.freelist != NULL);
319 if (sh.freelist == NULL)
321 memset(sh.freelist, 0, sh.freelist_size * sizeof (char *));
323 sh.bittable = OPENSSL_malloc(sh.bittable_size >> 3);
324 OPENSSL_assert(sh.bittable != NULL);
325 if (sh.bittable == NULL)
327 memset(sh.bittable, 0, sh.bittable_size >> 3);
329 sh.bitmalloc = OPENSSL_malloc(sh.bittable_size >> 3);
330 OPENSSL_assert(sh.bitmalloc != NULL);
331 if (sh.bitmalloc == NULL)
333 memset(sh.bitmalloc, 0, sh.bittable_size >> 3);
335 /* Allocate space for heap, and two extra pages as guards */
337 pgsize = (size_t)sysconf(_SC_PAGE_SIZE);
341 sh.map_size = pgsize + sh.arena_size + pgsize;
342 sh.map_result = mmap(NULL, sh.map_size,
343 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
344 OPENSSL_assert(sh.map_result != MAP_FAILED);
345 if (sh.map_result == MAP_FAILED)
347 sh.arena = (char *)(sh.map_result + pgsize);
348 sh_setbit(sh.arena, 0, sh.bittable);
349 sh_add_to_list(&sh.freelist[0], sh.arena);
351 /* Now try to add guard pages and lock into memory. */
354 /* Starting guard is already aligned from mmap. */
355 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
358 /* Ending guard page - need to round up to page boundary */
359 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
360 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
363 if (mlock(sh.arena, sh.arena_size) < 0)
366 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
377 static void sh_done()
379 OPENSSL_free(sh.freelist);
380 OPENSSL_free(sh.bittable);
381 OPENSSL_free(sh.bitmalloc);
382 if (sh.map_result != NULL && sh.map_size)
383 munmap(sh.map_result, sh.map_size);
384 memset(&sh, 0, sizeof sh);
387 static int sh_allocated(const char *ptr)
389 return WITHIN_ARENA(ptr) ? 1 : 0;
392 static char *sh_find_my_buddy(char *ptr, int list)
397 bit = (1 << list) + (ptr - sh.arena) / (sh.arena_size >> list);
400 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
401 chunk = sh.arena + ((bit & ((1 << list) - 1)) * (sh.arena_size >> list));
406 static char *sh_malloc(size_t size)
412 list = sh.freelist_size - 1;
413 for (i = sh.minsize; i < size; i <<= 1)
418 /* try to find a larger entry to split */
419 for (slist = list; slist >= 0; slist--)
420 if (sh.freelist[slist] != NULL)
425 /* split larger entry */
426 while (slist != list) {
427 char *temp = sh.freelist[slist];
429 /* remove from bigger list */
430 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
431 sh_clearbit(temp, slist, sh.bittable);
432 sh_remove_from_list(temp, sh.freelist[slist]);
433 OPENSSL_assert(temp != sh.freelist[slist]);
435 /* done with bigger list */
438 /* add to smaller list */
439 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
440 sh_setbit(temp, slist, sh.bittable);
441 sh_add_to_list(&sh.freelist[slist], temp);
442 OPENSSL_assert(sh.freelist[slist] == temp);
445 temp += sh.arena_size >> slist;
446 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
447 sh_setbit(temp, slist, sh.bittable);
448 sh_add_to_list(&sh.freelist[slist], temp);
449 OPENSSL_assert(sh.freelist[slist] == temp);
451 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
454 /* peel off memory to hand back */
455 chunk = sh.freelist[list];
456 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
457 sh_setbit(chunk, list, sh.bitmalloc);
458 sh_remove_from_list(chunk, sh.freelist[list]);
460 OPENSSL_assert(WITHIN_ARENA(chunk));
465 static void sh_free(char *ptr)
472 OPENSSL_assert(WITHIN_ARENA(ptr));
473 if (!WITHIN_ARENA(ptr))
476 list = sh_getlist(ptr);
477 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
478 sh_clearbit(ptr, list, sh.bitmalloc);
479 sh_add_to_list(&sh.freelist[list], ptr);
481 /* Try to coalesce two adjacent free areas. */
482 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
483 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
484 OPENSSL_assert(ptr != NULL);
485 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
486 sh_clearbit(ptr, list, sh.bittable);
487 sh_remove_from_list(ptr, sh.freelist[list]);
488 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
489 sh_clearbit(buddy, list, sh.bittable);
490 sh_remove_from_list(buddy, sh.freelist[list]);
497 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
498 sh_setbit(ptr, list, sh.bittable);
499 sh_add_to_list(&sh.freelist[list], ptr);
500 OPENSSL_assert(sh.freelist[list] == ptr);
504 static int sh_actual_size(char *ptr)
508 OPENSSL_assert(WITHIN_ARENA(ptr));
509 if (!WITHIN_ARENA(ptr))
511 list = sh_getlist(ptr);
512 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
513 return sh.arena_size / (1 << list);
515 #endif /* IMPLEMENTED */