Remove some old makefile targets
[openssl.git] / crypto / mem_sec.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/types.h>
23 # include <sys/mman.h>
24 # include <sys/param.h>
25 # include <sys/stat.h>
26 # include <fcntl.h>
27 #endif
28
29 #define LOCK()      CRYPTO_w_lock(CRYPTO_LOCK_MALLOC)
30 #define UNLOCK()    CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC)
31 #define CLEAR(p, s) OPENSSL_cleanse(p, s)
32 #ifndef PAGE_SIZE
33 # define PAGE_SIZE    4096
34 #endif
35
36 #ifdef IMPLEMENTED
37 static size_t secure_mem_used;
38
39 static int secure_mem_initialized;
40 static int too_late;
41
42 /*
43  * These are the functions that must be implemented by a secure heap (sh).
44  */
45 static int sh_init(size_t size, int minsize);
46 static char *sh_malloc(size_t size);
47 static void sh_free(char *ptr);
48 static void sh_done(void);
49 static int sh_actual_size(char *ptr);
50 static int sh_allocated(const char *ptr);
51 #endif
52
53 int CRYPTO_secure_malloc_init(size_t size, int minsize)
54 {
55 #ifdef IMPLEMENTED
56     int ret = 0;
57
58     if (too_late)
59         return ret;
60     LOCK();
61     OPENSSL_assert(!secure_mem_initialized);
62     if (!secure_mem_initialized) {
63         ret = sh_init(size, minsize);
64         secure_mem_initialized = 1;
65     }
66     UNLOCK();
67     return ret;
68 #else
69     return 0;
70 #endif /* IMPLEMENTED */
71 }
72
73 void CRYPTO_secure_malloc_done()
74 {
75 #ifdef IMPLEMENTED
76     LOCK();
77     sh_done();
78     secure_mem_initialized = 0;
79     UNLOCK();
80 #endif /* IMPLEMENTED */
81 }
82
83 int CRYPTO_secure_malloc_initialized()
84 {
85 #ifdef IMPLEMENTED
86     return secure_mem_initialized;
87 #else
88     return 0;
89 #endif /* IMPLEMENTED */
90 }
91
92 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
93 {
94 #ifdef IMPLEMENTED
95     void *ret;
96     size_t actual_size;
97
98     if (!secure_mem_initialized) {
99         too_late = 1;
100         return CRYPTO_malloc(num, file, line);
101     }
102     LOCK();
103     ret = sh_malloc(num);
104     actual_size = ret ? sh_actual_size(ret) : 0;
105     secure_mem_used += actual_size;
106     UNLOCK();
107     return ret;
108 #else
109     return CRYPTO_malloc(num, file, line);
110 #endif /* IMPLEMENTED */
111 }
112
113 void CRYPTO_secure_free(void *ptr)
114 {
115 #ifdef IMPLEMENTED
116     size_t actual_size;
117
118     if (ptr == NULL)
119         return;
120     if (!secure_mem_initialized) {
121         CRYPTO_free(ptr);
122         return;
123     }
124     LOCK();
125     actual_size = sh_actual_size(ptr);
126     CLEAR(ptr, actual_size);
127     secure_mem_used -= actual_size;
128     sh_free(ptr);
129     UNLOCK();
130 #else
131     CRYPTO_free(ptr);
132 #endif /* IMPLEMENTED */
133 }
134
135 int CRYPTO_secure_allocated(const void *ptr)
136 {
137 #ifdef IMPLEMENTED
138     int ret;
139
140     if (!secure_mem_initialized)
141         return 0;
142     LOCK();
143     ret = sh_allocated(ptr);
144     UNLOCK();
145     return ret;
146 #else
147     return 0;
148 #endif /* IMPLEMENTED */
149 }
150
151 size_t CRYPTO_secure_used()
152 {
153 #ifdef IMPLEMENTED
154     return secure_mem_used;
155 #else
156     return 0;
157 #endif /* IMPLEMENTED */
158 }
159
160 size_t CRYPTO_secure_actual_size(void *ptr)
161 {
162 #ifdef IMPLEMENTED
163     size_t actual_size;
164
165     LOCK();
166     actual_size = sh_actual_size(ptr);
167     UNLOCK();
168     return actual_size;
169 #else
170     return 0;
171 #endif
172 }
173 /* END OF PAGE ...
174
175    ... START OF PAGE */
176
177 /*
178  * SECURE HEAP IMPLEMENTATION
179  */
180 #ifdef IMPLEMENTED
181
182
183 /*
184  * The implementation provided here uses a fixed-sized mmap() heap,
185  * which is locked into memory, not written to core files, and protected
186  * on either side by an unmapped page, which will catch pointer overruns
187  * (or underruns) and an attempt to read data out of the secure heap.
188  * Free'd memory is zero'd or otherwise cleansed.
189  *
190  * This is a pretty standard buddy allocator.  We keep areas in a multiple
191  * of "sh.minsize" units.  The freelist and bitmaps are kept separately,
192  * so all (and only) data is kept in the mmap'd heap.
193  *
194  * This code assumes eight-bit bytes.  The numbers 3 and 7 are all over the
195  * place.
196  */
197
198 # define TESTBIT(t, b)  (t[(b) >> 3] &  (1 << ((b) & 7)))
199 # define SETBIT(t, b)   (t[(b) >> 3] |= (1 << ((b) & 7)))
200 # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(1 << ((b) & 7))))
201
202 #define WITHIN_ARENA(p) \
203     ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
204 #define WITHIN_FREELIST(p) \
205     ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
206
207
208 typedef struct sh_list_st
209 {
210     struct sh_list_st *next;
211     struct sh_list_st **p_next;
212 } SH_LIST;
213
214 typedef struct sh_st
215 {
216     char* map_result;
217     size_t map_size;
218     char *arena;
219     int arena_size;
220     char **freelist;
221     int freelist_size;
222     int minsize;
223     unsigned char *bittable;
224     unsigned char *bitmalloc;
225     int bittable_size; /* size in bits */
226 } SH;
227
228 static SH sh;
229
230 static int sh_getlist(char *ptr)
231 {
232     int list = sh.freelist_size - 1;
233     int bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
234
235     for (; bit; bit >>= 1, list--) {
236         if (TESTBIT(sh.bittable, bit))
237             break;
238         OPENSSL_assert((bit & 1) == 0);
239     }
240
241     return list;
242 }
243
244
245 static int sh_testbit(char *ptr, int list, unsigned char *table)
246 {
247     int bit;
248
249     OPENSSL_assert(list >= 0 && list < sh.freelist_size);
250     OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
251     bit = (1 << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
252     OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
253     return TESTBIT(table, bit);
254 }
255
256 static void sh_clearbit(char *ptr, int list, unsigned char *table)
257 {
258     int bit;
259
260     OPENSSL_assert(list >= 0 && list < sh.freelist_size);
261     OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
262     bit = (1 << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
263     OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
264     OPENSSL_assert(TESTBIT(table, bit));
265     CLEARBIT(table, bit);
266 }
267
268 static void sh_setbit(char *ptr, int list, unsigned char *table)
269 {
270     int bit;
271
272     OPENSSL_assert(list >= 0 && list < sh.freelist_size);
273     OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
274     bit = (1 << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
275     OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
276     OPENSSL_assert(!TESTBIT(table, bit));
277     SETBIT(table, bit);
278 }
279
280 static void sh_add_to_list(char **list, char *ptr)
281 {
282     SH_LIST *temp;
283
284     OPENSSL_assert(WITHIN_FREELIST(list));
285     OPENSSL_assert(WITHIN_ARENA(ptr));
286
287     temp = (SH_LIST *)ptr;
288     temp->next = *(SH_LIST **)list;
289     OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
290     temp->p_next = (SH_LIST **)list;
291
292     if (temp->next != NULL) {
293         OPENSSL_assert((char **)temp->next->p_next == list);
294         temp->next->p_next = &(temp->next);
295     }
296
297     *list = ptr;
298 }
299
300 static void sh_remove_from_list(char *ptr, char *list)
301 {
302     SH_LIST *temp, *temp2;
303
304     temp = (SH_LIST *)ptr;
305     if (temp->next != NULL)
306         temp->next->p_next = temp->p_next;
307     *temp->p_next = temp->next;
308     if (temp->next == NULL)
309         return;
310
311     temp2 = temp->next;
312     OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
313 }
314
315
316 static int sh_init(size_t size, int minsize)
317 {
318     int i, ret;
319     size_t pgsize;
320     size_t aligned;
321
322     memset(&sh, 0, sizeof sh);
323
324     /* make sure size and minsize are powers of 2 */
325     OPENSSL_assert(size > 0);
326     OPENSSL_assert((size & (size - 1)) == 0);
327     OPENSSL_assert(minsize > 0);
328     OPENSSL_assert((minsize & (minsize - 1)) == 0);
329     if (size <= 0 || (size & (size - 1)) != 0)
330         goto err;
331     if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
332         goto err;
333
334     sh.arena_size = size;
335     sh.minsize = minsize;
336     sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
337
338     sh.freelist_size = -1;
339     for (i = sh.bittable_size; i; i >>= 1)
340         sh.freelist_size++;
341
342     sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof (char *));
343     OPENSSL_assert(sh.freelist != NULL);
344     if (sh.freelist == NULL)
345         goto err;
346
347     sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
348     OPENSSL_assert(sh.bittable != NULL);
349     if (sh.bittable == NULL)
350         goto err;
351
352     sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
353     OPENSSL_assert(sh.bitmalloc != NULL);
354     if (sh.bitmalloc == NULL)
355         goto err;
356
357     /* Allocate space for heap, and two extra pages as guards */
358 #if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
359     {
360 # if defined(_SC_PAGE_SIZE)
361         long tmppgsize = sysconf(_SC_PAGE_SIZE);
362 # else
363         long tmppgsize = sysconf(_SC_PAGESIZE);
364 # endif
365         if (tmppgsize < 1)
366             pgsize = PAGE_SIZE;
367         else
368             pgsize = (size_t)tmppgsize;
369     }
370 #else
371     pgsize = PAGE_SIZE;
372 #endif
373     sh.map_size = pgsize + sh.arena_size + pgsize;
374     if (1) {
375 #ifdef MAP_ANON
376         sh.map_result = mmap(NULL, sh.map_size,
377                              PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
378     } else {
379 #endif
380         int fd;
381
382         sh.map_result = MAP_FAILED;
383         if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
384             sh.map_result = mmap(NULL, sh.map_size,
385                                  PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
386             close(fd);
387         }
388     }
389     OPENSSL_assert(sh.map_result != MAP_FAILED);
390     if (sh.map_result == MAP_FAILED)
391         goto err;
392     sh.arena = (char *)(sh.map_result + pgsize);
393     sh_setbit(sh.arena, 0, sh.bittable);
394     sh_add_to_list(&sh.freelist[0], sh.arena);
395
396     /* Now try to add guard pages and lock into memory. */
397     ret = 1;
398
399     /* Starting guard is already aligned from mmap. */
400     if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
401         ret = 2;
402
403     /* Ending guard page - need to round up to page boundary */
404     aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
405     if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
406         ret = 2;
407
408     if (mlock(sh.arena, sh.arena_size) < 0)
409         ret = 2;
410 #ifdef MADV_DONTDUMP
411     if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
412         ret = 2;
413 #endif
414
415     return ret;
416
417  err:
418     sh_done();
419     return 0;
420 }
421
422 static void sh_done()
423 {
424     OPENSSL_free(sh.freelist);
425     OPENSSL_free(sh.bittable);
426     OPENSSL_free(sh.bitmalloc);
427     if (sh.map_result != NULL && sh.map_size)
428         munmap(sh.map_result, sh.map_size);
429     memset(&sh, 0, sizeof sh);
430 }
431
432 static int sh_allocated(const char *ptr)
433 {
434     return WITHIN_ARENA(ptr) ? 1 : 0;
435 }
436
437 static char *sh_find_my_buddy(char *ptr, int list)
438 {
439     int bit;
440     char *chunk = NULL;
441
442     bit = (1 << list) + (ptr - sh.arena) / (sh.arena_size >> list);
443     bit ^= 1;
444
445     if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
446         chunk = sh.arena + ((bit & ((1 << list) - 1)) * (sh.arena_size >> list));
447
448     return chunk;
449 }
450
451 static char *sh_malloc(size_t size)
452 {
453     int list, slist;
454     size_t i;
455     char *chunk;
456
457     list = sh.freelist_size - 1;
458     for (i = sh.minsize; i < size; i <<= 1)
459         list--;
460     if (list < 0)
461         return NULL;
462
463     /* try to find a larger entry to split */
464     for (slist = list; slist >= 0; slist--)
465         if (sh.freelist[slist] != NULL)
466             break;
467     if (slist < 0)
468         return NULL;
469
470     /* split larger entry */
471     while (slist != list) {
472         char *temp = sh.freelist[slist];
473
474         /* remove from bigger list */
475         OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
476         sh_clearbit(temp, slist, sh.bittable);
477         sh_remove_from_list(temp, sh.freelist[slist]);
478         OPENSSL_assert(temp != sh.freelist[slist]);
479
480         /* done with bigger list */
481         slist++;
482
483         /* add to smaller list */
484         OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
485         sh_setbit(temp, slist, sh.bittable);
486         sh_add_to_list(&sh.freelist[slist], temp);
487         OPENSSL_assert(sh.freelist[slist] == temp);
488
489         /* split in 2 */
490         temp += sh.arena_size >> slist;
491         OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
492         sh_setbit(temp, slist, sh.bittable);
493         sh_add_to_list(&sh.freelist[slist], temp);
494         OPENSSL_assert(sh.freelist[slist] == temp);
495
496         OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
497     }
498
499     /* peel off memory to hand back */
500     chunk = sh.freelist[list];
501     OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
502     sh_setbit(chunk, list, sh.bitmalloc);
503     sh_remove_from_list(chunk, sh.freelist[list]);
504
505     OPENSSL_assert(WITHIN_ARENA(chunk));
506
507     return chunk;
508 }
509
510 static void sh_free(char *ptr)
511 {
512     int list;
513     char *buddy;
514
515     if (ptr == NULL)
516         return;
517     OPENSSL_assert(WITHIN_ARENA(ptr));
518     if (!WITHIN_ARENA(ptr))
519         return;
520
521     list = sh_getlist(ptr);
522     OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
523     sh_clearbit(ptr, list, sh.bitmalloc);
524     sh_add_to_list(&sh.freelist[list], ptr);
525
526     /* Try to coalesce two adjacent free areas. */
527     while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
528         OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
529         OPENSSL_assert(ptr != NULL);
530         OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
531         sh_clearbit(ptr, list, sh.bittable);
532         sh_remove_from_list(ptr, sh.freelist[list]);
533         OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
534         sh_clearbit(buddy, list, sh.bittable);
535         sh_remove_from_list(buddy, sh.freelist[list]);
536
537         list--;
538
539         if (ptr > buddy)
540             ptr = buddy;
541
542         OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
543         sh_setbit(ptr, list, sh.bittable);
544         sh_add_to_list(&sh.freelist[list], ptr);
545         OPENSSL_assert(sh.freelist[list] == ptr);
546     }
547 }
548
549 static int sh_actual_size(char *ptr)
550 {
551     int list;
552
553     OPENSSL_assert(WITHIN_ARENA(ptr));
554     if (!WITHIN_ARENA(ptr))
555         return 0;
556     list = sh_getlist(ptr);
557     OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
558     return sh.arena_size / (1 << list);
559 }
560 #endif /* IMPLEMENTED */