VirtualBox

source: vbox/trunk/src/libs/openssl-1.1.0g/crypto/mem_sec.c@ 69881

Last change on this file since 69881 was 69881, checked in by vboxsync, 7 years ago

Update OpenSSL to 1.1.0g.
bugref:8070: src/libs maintenance

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette