Extend -show_chain option to verify to show more info
[openssl.git] / crypto / sec_mem.c
1 /*
2  * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
3  * This file is distributed under the terms of the OpenSSL license.
4  */
5
6 /*
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").
12  */
13 #include <openssl/crypto.h>
14 #include <e_os.h>
15
16 #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
17 # define IMPLEMENTED
18 # include <stdlib.h>
19 # include <string.h>
20 # include <assert.h>
21 # include <unistd.h>
22 # include <sys/mman.h>
23 # include <sys/param.h>
24 #endif
25
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
30
31 #ifdef IMPLEMENTED
32 size_t secure_mem_used;
33
34 static int secure_mem_initialized;
35 static int too_late;
36
37 /*
38  * These are the functions that must be implemented by a secure heap (sh).
39  */
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);
46 #endif
47
48 int CRYPTO_secure_malloc_init(size_t size, int minsize)
49 {
50 #ifdef IMPLEMENTED
51     int ret = 0;
52
53     if (too_late)
54         return ret;
55     LOCK();
56     OPENSSL_assert(!secure_mem_initialized);
57     if (!secure_mem_initialized) {
58         ret = sh_init(size, minsize);
59         secure_mem_initialized = 1;
60     }
61     UNLOCK();
62     return ret;
63 #else
64     return 0;
65 #endif /* IMPLEMENTED */
66 }
67
68 void CRYPTO_secure_malloc_done()
69 {
70 #ifdef IMPLEMENTED
71     LOCK();
72     sh_done();
73     secure_mem_initialized = 0;
74     UNLOCK();
75 #endif /* IMPLEMENTED */
76 }
77
78 int CRYPTO_secure_malloc_initialized()
79 {
80 #ifdef IMPLEMENTED
81     return secure_mem_initialized;
82 #else
83     return 0;
84 #endif /* IMPLEMENTED */
85 }
86
87 void *CRYPTO_secure_malloc(int num, const char *file, int line)
88 {
89 #ifdef IMPLEMENTED
90     void *ret;
91     size_t actual_size;
92
93     if (!secure_mem_initialized) {
94         too_late = 1;
95         return CRYPTO_malloc(num, file, line);
96     }
97     LOCK();
98     ret = sh_malloc(num);
99     actual_size = ret ? sh_actual_size(ret) : 0;
100     secure_mem_used += actual_size;
101     UNLOCK();
102     return ret;
103 #else
104     return CRYPTO_malloc(num, file, line);
105 #endif /* IMPLEMENTED */
106 }
107
108 void CRYPTO_secure_free(void *ptr)
109 {
110 #ifdef IMPLEMENTED
111     size_t actual_size;
112
113     if (ptr == NULL)
114         return;
115     if (!secure_mem_initialized) {
116         CRYPTO_free(ptr);
117         return;
118     }
119     LOCK();
120     actual_size = sh_actual_size(ptr);
121     CLEAR(ptr, actual_size);
122     secure_mem_used -= actual_size;
123     sh_free(ptr);
124     UNLOCK();
125 #else
126     CRYPTO_free(ptr);
127 #endif /* IMPLEMENTED */
128 }
129
130 int CRYPTO_secure_allocated(const void *ptr)
131 {
132 #ifdef IMPLEMENTED
133     int ret;
134
135     if (!secure_mem_initialized)
136         return 0;
137     LOCK();
138     ret = sh_allocated(ptr);
139     UNLOCK();
140     return ret;
141 #else
142     return 0;
143 #endif /* IMPLEMENTED */
144 }
145
146 /* END OF PAGE ...
147
148    ... START OF PAGE */
149
150 /*
151  * SECURE HEAP IMPLEMENTATION
152  */
153 #ifdef IMPLEMENTED
154
155
156 /*
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.
162  *
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.
166  *
167  * This code assumes eight-bit bytes.  The numbers 3 and 7 are all over the
168  * place.
169  */
170
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))))
174
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])
179
180
181 typedef struct sh_list_st
182 {
183     struct sh_list_st *next;
184     struct sh_list_st **p_next;
185 } SH_LIST;
186
187 typedef struct sh_st
188 {
189     char* map_result;
190     size_t map_size;
191     char *arena;
192     int arena_size;
193     char **freelist;
194     int freelist_size;
195     int minsize;
196     unsigned char *bittable;
197     unsigned char *bitmalloc;
198     int bittable_size; /* size in bits */
199 } SH;
200
201 static SH sh;
202
203 static int sh_getlist(char *ptr)
204 {
205     int list = sh.freelist_size - 1;
206     int bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
207
208     for (; bit; bit >>= 1, list--) {
209         if (TESTBIT(sh.bittable, bit))
210             break;
211         OPENSSL_assert((bit & 1) == 0);
212     }
213
214     return list;
215 }
216
217
218 static int sh_testbit(char *ptr, int list, unsigned char *table)
219 {
220     int bit;
221
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);
227 }
228
229 static void sh_clearbit(char *ptr, int list, unsigned char *table)
230 {
231     int bit;
232
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);
239 }
240
241 static void sh_setbit(char *ptr, int list, unsigned char *table)
242 {
243     int bit;
244
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));
250     SETBIT(table, bit);
251 }
252
253 static void sh_add_to_list(char **list, char *ptr)
254 {
255     SH_LIST *temp;
256
257     OPENSSL_assert(WITHIN_FREELIST(list));
258     OPENSSL_assert(WITHIN_ARENA(ptr));
259
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;
264
265     if (temp->next != NULL) {
266         OPENSSL_assert((char **)temp->next->p_next == list);
267         temp->next->p_next = &(temp->next);
268     }
269
270     *list = ptr;
271 }
272
273 static void sh_remove_from_list(char *ptr, char *list)
274 {
275     SH_LIST *temp, *temp2;
276
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)
282         return;
283
284     temp2 = temp->next;
285     OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
286 }
287
288
289 static int sh_init(size_t size, int minsize)
290 {
291     int i, ret;
292     size_t pgsize;
293     size_t aligned;
294
295     memset(&sh, 0, sizeof sh);
296
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)
303         goto err;
304     if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
305         goto err;
306
307     sh.arena_size = size;
308     sh.minsize = minsize;
309     sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
310
311     sh.freelist_size = -1;
312     for (i = sh.bittable_size; i; i >>= 1)
313         sh.freelist_size++;
314
315     sh.freelist = OPENSSL_malloc(sh.freelist_size * sizeof (char *));
316     OPENSSL_assert(sh.freelist != NULL);
317     if (sh.freelist == NULL)
318         goto err;
319     memset(sh.freelist, 0, sh.freelist_size * sizeof (char *));
320
321     sh.bittable = OPENSSL_malloc(sh.bittable_size >> 3);
322     OPENSSL_assert(sh.bittable != NULL);
323     if (sh.bittable == NULL)
324         goto err;
325     memset(sh.bittable, 0, sh.bittable_size >> 3);
326
327     sh.bitmalloc = OPENSSL_malloc(sh.bittable_size >> 3);
328     OPENSSL_assert(sh.bitmalloc != NULL);
329     if (sh.bitmalloc == NULL)
330         goto err;
331     memset(sh.bitmalloc, 0, sh.bittable_size >> 3);
332
333     /* Allocate space for heap, and two extra pages as guards */
334 #ifdef _SC_PAGE_SIZE
335     pgsize = (size_t)sysconf(_SC_PAGE_SIZE);
336 #else
337     pgsize = PAGE_SIZE;
338 #endif
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)
344         goto err;
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);
348
349     /* Now try to add guard pages and lock into memory. */
350     ret = 1;
351
352     /* Starting guard is already aligned from mmap. */
353     if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
354         ret = 2;
355
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)
359         ret = 2;
360
361     if (mlock(sh.arena, sh.arena_size) < 0)
362         ret = 2;
363 #ifdef MADV_DONTDUMP
364     if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
365         ret = 2;
366 #endif
367
368     return ret;
369
370  err:
371     sh_done();
372     return 0;
373 }
374
375 static void sh_done()
376 {
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);
383 }
384
385 static int sh_allocated(const char *ptr)
386 {
387     return WITHIN_ARENA(ptr) ? 1 : 0;
388 }
389
390 static char *sh_find_my_buddy(char *ptr, int list)
391 {
392     int bit;
393     char *chunk = NULL;
394
395     bit = (1 << list) + (ptr - sh.arena) / (sh.arena_size >> list);
396     bit ^= 1;
397
398     if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
399         chunk = sh.arena + ((bit & ((1 << list) - 1)) * (sh.arena_size >> list));
400
401     return chunk;
402 }
403
404 static char *sh_malloc(size_t size)
405 {
406     int list, slist;
407     size_t i;
408     char *chunk;
409
410     list = sh.freelist_size - 1;
411     for (i = sh.minsize; i < size; i <<= 1)
412         list--;
413     if (list < 0)
414         return NULL;
415
416     /* try to find a larger entry to split */
417     for (slist = list; slist >= 0; slist--)
418         if (sh.freelist[slist] != NULL)
419             break;
420     if (slist < 0)
421         return NULL;
422
423     /* split larger entry */
424     while (slist != list) {
425         char *temp = sh.freelist[slist];
426
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]);
432
433         /* done with bigger list */
434         slist++;
435
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);
441
442         /* split in 2 */
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);
448
449         OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
450     }
451
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]);
457
458     OPENSSL_assert(WITHIN_ARENA(chunk));
459
460     return chunk;
461 }
462
463 static void sh_free(char *ptr)
464 {
465     int list;
466     char *buddy;
467
468     if (ptr == NULL)
469         return;
470     OPENSSL_assert(WITHIN_ARENA(ptr));
471     if (!WITHIN_ARENA(ptr))
472         return;
473
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);
478
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]);
489
490         list--;
491
492         if (ptr > buddy)
493             ptr = buddy;
494
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);
499     }
500 }
501
502 static int sh_actual_size(char *ptr)
503 {
504     int list;
505
506     OPENSSL_assert(WITHIN_ARENA(ptr));
507     if (!WITHIN_ARENA(ptr))
508         return 0;
509     list = sh_getlist(ptr);
510     OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
511     return sh.arena_size / (1 << list);
512 }
513 #endif /* IMPLEMENTED */