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