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>
20 #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)
29 #define PAGE_SIZE 4096
32 size_t secure_mem_used;
34 static int secure_mem_initialized;
38 * These are the functions that must be implemented by a secure heap (sh).
40 static int sh_init(size_t size, int minsize);
41 static char *sh_malloc(size_t size);
42 static void sh_free(char *ptr);
43 static void sh_done(void);
44 static int sh_actual_size(char *ptr);
45 static int sh_allocated(const char *ptr);
48 int CRYPTO_secure_malloc_init(size_t size, int minsize)
56 OPENSSL_assert(!secure_mem_initialized);
57 if (!secure_mem_initialized) {
58 ret = sh_init(size, minsize);
59 secure_mem_initialized = 1;
65 #endif /* IMPLEMENTED */
68 void CRYPTO_secure_malloc_done()
73 secure_mem_initialized = 0;
75 #endif /* IMPLEMENTED */
78 int CRYPTO_secure_malloc_initialized()
81 return secure_mem_initialized;
84 #endif /* IMPLEMENTED */
87 void *CRYPTO_secure_malloc(int num, const char *file, int line)
93 if (!secure_mem_initialized) {
95 return CRYPTO_malloc(num, file, line);
99 actual_size = ret ? sh_actual_size(ret) : 0;
100 secure_mem_used += actual_size;
104 return CRYPTO_malloc(num, file, line);
105 #endif /* IMPLEMENTED */
108 void CRYPTO_secure_free(void *ptr)
115 if (!secure_mem_initialized) {
120 actual_size = sh_actual_size(ptr);
121 CLEAR(ptr, actual_size);
122 secure_mem_used -= actual_size;
127 #endif /* IMPLEMENTED */
130 int CRYPTO_secure_allocated(const void *ptr)
135 if (!secure_mem_initialized)
138 ret = sh_allocated(ptr);
143 #endif /* IMPLEMENTED */
151 * SECURE HEAP IMPLEMENTATION
157 * The implementation provided here uses a fixed-sized mmap() heap,
158 * which is locked into memory, not written to core files, and protected
159 * on either side by an unmapped page, which will catch pointer overruns
160 * (or underruns) and an attempt to read data out of the secure heap.
161 * Free'd memory is zero'd or otherwise cleansed.
163 * This is a pretty standard buddy allocator. We keep areas in a multiple
164 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
165 * so all (and only) data is kept in the mmap'd heap.
167 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
171 # define TESTBIT(t, b) (t[(b) >> 3] & (1 << ((b) & 7)))
172 # define SETBIT(t, b) (t[(b) >> 3] |= (1 << ((b) & 7)))
173 # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(1 << ((b) & 7))))
175 #define WITHIN_ARENA(p) \
176 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
177 #define WITHIN_FREELIST(p) \
178 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
181 typedef struct sh_list_st
183 struct sh_list_st *next;
184 struct sh_list_st **p_next;
196 unsigned char *bittable;
197 unsigned char *bitmalloc;
198 int bittable_size; /* size in bits */
203 static int sh_getlist(char *ptr)
205 int list = sh.freelist_size - 1;
206 int bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
208 for (; bit; bit >>= 1, list--) {
209 if (TESTBIT(sh.bittable, bit))
211 OPENSSL_assert((bit & 1) == 0);
218 static int sh_testbit(char *ptr, int list, unsigned char *table)
222 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
223 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
224 bit = (1 << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
225 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
226 return TESTBIT(table, bit);
229 static void sh_clearbit(char *ptr, int list, unsigned char *table)
233 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
234 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
235 bit = (1 << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
236 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
237 OPENSSL_assert(TESTBIT(table, bit));
238 CLEARBIT(table, bit);
241 static void sh_setbit(char *ptr, int list, unsigned char *table)
245 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
246 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
247 bit = (1 << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
248 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
249 OPENSSL_assert(!TESTBIT(table, bit));
253 static void sh_add_to_list(char **list, char *ptr)
257 OPENSSL_assert(WITHIN_FREELIST(list));
258 OPENSSL_assert(WITHIN_ARENA(ptr));
260 temp = (SH_LIST *)ptr;
261 temp->next = *(SH_LIST **)list;
262 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
263 temp->p_next = (SH_LIST **)list;
265 if (temp->next != NULL) {
266 OPENSSL_assert((char **)temp->next->p_next == list);
267 temp->next->p_next = &(temp->next);
273 static void sh_remove_from_list(char *ptr, char *list)
275 SH_LIST *temp, *temp2;
277 temp = (SH_LIST *)ptr;
278 if (temp->next != NULL)
279 temp->next->p_next = temp->p_next;
280 *temp->p_next = temp->next;
281 if (temp->next == NULL)
285 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
289 static int sh_init(size_t size, int minsize)
295 memset(&sh, 0, sizeof sh);
297 /* make sure size and minsize are powers of 2 */
298 OPENSSL_assert(size > 0);
299 OPENSSL_assert((size & (size - 1)) == 0);
300 OPENSSL_assert(minsize > 0);
301 OPENSSL_assert((minsize & (minsize - 1)) == 0);
302 if (size <= 0 || (size & (size - 1)) != 0)
304 if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
307 sh.arena_size = size;
308 sh.minsize = minsize;
309 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
311 sh.freelist_size = -1;
312 for (i = sh.bittable_size; i; i >>= 1)
315 sh.freelist = OPENSSL_malloc(sh.freelist_size * sizeof (char *));
316 OPENSSL_assert(sh.freelist != NULL);
317 if (sh.freelist == NULL)
319 memset(sh.freelist, 0, sh.freelist_size * sizeof (char *));
321 sh.bittable = OPENSSL_malloc(sh.bittable_size >> 3);
322 OPENSSL_assert(sh.bittable != NULL);
323 if (sh.bittable == NULL)
325 memset(sh.bittable, 0, sh.bittable_size >> 3);
327 sh.bitmalloc = OPENSSL_malloc(sh.bittable_size >> 3);
328 OPENSSL_assert(sh.bitmalloc != NULL);
329 if (sh.bitmalloc == NULL)
331 memset(sh.bitmalloc, 0, sh.bittable_size >> 3);
333 /* Allocate space for heap, and two extra pages as guards */
335 pgsize = (size_t)sysconf(_SC_PAGE_SIZE);
339 sh.map_size = pgsize + sh.arena_size + pgsize;
340 sh.map_result = mmap(NULL, sh.map_size,
341 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
342 OPENSSL_assert(sh.map_result != MAP_FAILED);
343 if (sh.map_result == MAP_FAILED)
345 sh.arena = (char *)(sh.map_result + pgsize);
346 sh_setbit(sh.arena, 0, sh.bittable);
347 sh_add_to_list(&sh.freelist[0], sh.arena);
349 /* Now try to add guard pages and lock into memory. */
352 /* Starting guard is already aligned from mmap. */
353 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
356 /* Ending guard page - need to round up to page boundary */
357 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
358 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
361 if (mlock(sh.arena, sh.arena_size) < 0)
364 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
375 static void sh_done()
377 OPENSSL_free(sh.freelist);
378 OPENSSL_free(sh.bittable);
379 OPENSSL_free(sh.bitmalloc);
380 if (sh.map_result != NULL && sh.map_size)
381 munmap(sh.map_result, sh.map_size);
382 memset(&sh, 0, sizeof sh);
385 static int sh_allocated(const char *ptr)
387 return WITHIN_ARENA(ptr) ? 1 : 0;
390 static char *sh_find_my_buddy(char *ptr, int list)
395 bit = (1 << list) + (ptr - sh.arena) / (sh.arena_size >> list);
398 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
399 chunk = sh.arena + ((bit & ((1 << list) - 1)) * (sh.arena_size >> list));
404 static char *sh_malloc(size_t size)
410 list = sh.freelist_size - 1;
411 for (i = sh.minsize; i < size; i <<= 1)
416 /* try to find a larger entry to split */
417 for (slist = list; slist >= 0; slist--)
418 if (sh.freelist[slist] != NULL)
423 /* split larger entry */
424 while (slist != list) {
425 char *temp = sh.freelist[slist];
427 /* remove from bigger list */
428 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
429 sh_clearbit(temp, slist, sh.bittable);
430 sh_remove_from_list(temp, sh.freelist[slist]);
431 OPENSSL_assert(temp != sh.freelist[slist]);
433 /* done with bigger list */
436 /* add to smaller list */
437 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
438 sh_setbit(temp, slist, sh.bittable);
439 sh_add_to_list(&sh.freelist[slist], temp);
440 OPENSSL_assert(sh.freelist[slist] == temp);
443 temp += sh.arena_size >> slist;
444 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
445 sh_setbit(temp, slist, sh.bittable);
446 sh_add_to_list(&sh.freelist[slist], temp);
447 OPENSSL_assert(sh.freelist[slist] == temp);
449 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
452 /* peel off memory to hand back */
453 chunk = sh.freelist[list];
454 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
455 sh_setbit(chunk, list, sh.bitmalloc);
456 sh_remove_from_list(chunk, sh.freelist[list]);
458 OPENSSL_assert(WITHIN_ARENA(chunk));
463 static void sh_free(char *ptr)
470 OPENSSL_assert(WITHIN_ARENA(ptr));
471 if (!WITHIN_ARENA(ptr))
474 list = sh_getlist(ptr);
475 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
476 sh_clearbit(ptr, list, sh.bitmalloc);
477 sh_add_to_list(&sh.freelist[list], ptr);
479 /* Try to coalesce two adjacent free areas. */
480 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
481 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
482 OPENSSL_assert(ptr != NULL);
483 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
484 sh_clearbit(ptr, list, sh.bittable);
485 sh_remove_from_list(ptr, sh.freelist[list]);
486 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
487 sh_clearbit(buddy, list, sh.bittable);
488 sh_remove_from_list(buddy, sh.freelist[list]);
495 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
496 sh_setbit(ptr, list, sh.bittable);
497 sh_add_to_list(&sh.freelist[list], ptr);
498 OPENSSL_assert(sh.freelist[list] == ptr);
502 static int sh_actual_size(char *ptr)
506 OPENSSL_assert(WITHIN_ARENA(ptr));
507 if (!WITHIN_ARENA(ptr))
509 list = sh_getlist(ptr);
510 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
511 return sh.arena_size / (1 << list);
513 #endif /* IMPLEMENTED */