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