VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.7/crypto/mem_sec.c@ 100947

Last change on this file since 100947 was 97372, checked in by vboxsync, 2 years ago

libs: Switch to openssl-3.0.7, bugref:10317

File size: 19.8 KB
Line 
1/*
2 * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11/*
12 * This file is in two halves. The first half implements the public API
13 * to be used by external consumers, and to be used by OpenSSL to store
14 * data in a "secure arena." The second half implements the secure arena.
15 * For details on that implementation, see below (look for uppercase
16 * "SECURE HEAP IMPLEMENTATION").
17 */
18#include "e_os.h"
19#include <openssl/crypto.h>
20
21#include <string.h>
22
23#if defined(VBOX)
24# include <iprt/memsafer.h>
25#else
26#ifndef OPENSSL_NO_SECURE_MEMORY
27# if defined(_WIN32)
28# include <windows.h>
29# if defined(WINAPI_FAMILY_PARTITION) \
30 && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
31/*
32 * While VirtualLock is available under the app partition (e.g. UWP),
33 * the headers do not define the API. Define it ourselves instead.
34 */
35WINBASEAPI
36BOOL
37WINAPI
38VirtualLock(
39 _In_ LPVOID lpAddress,
40 _In_ SIZE_T dwSize
41 );
42# endif
43# endif
44# include <stdlib.h>
45# include <assert.h>
46# if defined(OPENSSL_SYS_UNIX)
47# include <unistd.h>
48# endif
49# include <sys/types.h>
50# if defined(OPENSSL_SYS_UNIX)
51# include <sys/mman.h>
52# if defined(__FreeBSD__)
53# define MADV_DONTDUMP MADV_NOCORE
54# endif
55# if !defined(MAP_CONCEAL)
56# define MAP_CONCEAL 0
57# endif
58# endif
59# if defined(OPENSSL_SYS_LINUX)
60# include <sys/syscall.h>
61# if defined(SYS_mlock2)
62# include <linux/mman.h>
63# include <errno.h>
64# endif
65# include <sys/param.h>
66# endif
67# include <sys/stat.h>
68# include <fcntl.h>
69#endif
70#endif /* !VBOX */
71
72#define CLEAR(p, s) OPENSSL_cleanse(p, s)
73#ifndef PAGE_SIZE
74# define PAGE_SIZE 4096
75#endif
76#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
77# define MAP_ANON MAP_ANONYMOUS
78#endif
79
80#ifndef OPENSSL_NO_SECURE_MEMORY
81static size_t secure_mem_used;
82
83static int secure_mem_initialized;
84
85static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
86
87/*
88 * These are the functions that must be implemented by a secure heap (sh).
89 */
90static int sh_init(size_t size, size_t minsize);
91static void *sh_malloc(size_t size);
92static void sh_free(void *ptr);
93static void sh_done(void);
94static size_t sh_actual_size(char *ptr);
95static int sh_allocated(const char *ptr);
96#endif
97
98int CRYPTO_secure_malloc_init(size_t size, size_t minsize)
99{
100#ifndef VBOX
101#ifndef OPENSSL_NO_SECURE_MEMORY
102 int ret = 0;
103
104 if (!secure_mem_initialized) {
105 sec_malloc_lock = CRYPTO_THREAD_lock_new();
106 if (sec_malloc_lock == NULL)
107 return 0;
108 if ((ret = sh_init(size, minsize)) != 0) {
109 secure_mem_initialized = 1;
110 } else {
111 CRYPTO_THREAD_lock_free(sec_malloc_lock);
112 sec_malloc_lock = NULL;
113 }
114 }
115
116 return ret;
117#else
118 return 0;
119#endif /* OPENSSL_NO_SECURE_MEMORY */
120#else
121 return 0;
122#endif /* VBOX */
123}
124
125int CRYPTO_secure_malloc_done(void)
126{
127#ifndef VBOX
128#ifndef OPENSSL_NO_SECURE_MEMORY
129 if (secure_mem_used == 0) {
130 sh_done();
131 secure_mem_initialized = 0;
132 CRYPTO_THREAD_lock_free(sec_malloc_lock);
133 sec_malloc_lock = NULL;
134 return 1;
135 }
136#endif /* OPENSSL_NO_SECURE_MEMORY */
137#endif /* VBOX */
138 return 0;
139}
140
141int CRYPTO_secure_malloc_initialized(void)
142{
143#ifndef VBOX
144#ifndef OPENSSL_NO_SECURE_MEMORY
145 return secure_mem_initialized;
146#else
147 return 0;
148#endif /* OPENSSL_NO_SECURE_MEMORY */
149#else
150 return 0;
151#endif /* VBOX */
152}
153
154void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
155{
156#ifndef VBOX
157#ifndef OPENSSL_NO_SECURE_MEMORY
158 void *ret;
159 size_t actual_size;
160
161 if (!secure_mem_initialized) {
162 return CRYPTO_malloc(num, file, line);
163 }
164 if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
165 return NULL;
166 ret = sh_malloc(num);
167 actual_size = ret ? sh_actual_size(ret) : 0;
168 secure_mem_used += actual_size;
169 CRYPTO_THREAD_unlock(sec_malloc_lock);
170 return ret;
171#else
172 return CRYPTO_malloc(num, file, line);
173#endif /* OPENSSL_NO_SECURE_MEMORY */
174#else
175 RT_NOREF(line);
176 return RTMemSaferAllocZTag(num, file);
177#endif /* VBOX */
178}
179
180void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
181{
182#ifndef VBOX
183#ifndef OPENSSL_NO_SECURE_MEMORY
184 if (secure_mem_initialized)
185 /* CRYPTO_secure_malloc() zeroes allocations when it is implemented */
186 return CRYPTO_secure_malloc(num, file, line);
187#endif
188 return CRYPTO_zalloc(num, file, line);
189#else
190 RT_NOREF(line);
191 return RTMemSaferAllocZTag(num, file);
192#endif
193}
194
195void CRYPTO_secure_free(void *ptr, const char *file, int line)
196{
197#ifndef VBOX
198#ifndef OPENSSL_NO_SECURE_MEMORY
199 size_t actual_size;
200
201 if (ptr == NULL)
202 return;
203 if (!CRYPTO_secure_allocated(ptr)) {
204 CRYPTO_free(ptr, file, line);
205 return;
206 }
207 if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
208 return;
209 actual_size = sh_actual_size(ptr);
210 CLEAR(ptr, actual_size);
211 secure_mem_used -= actual_size;
212 sh_free(ptr);
213 CRYPTO_THREAD_unlock(sec_malloc_lock);
214#else
215 CRYPTO_free(ptr, file, line);
216#endif /* OPENSSL_NO_SECURE_MEMORY */
217#else
218 RT_NOREF(line);
219 RTMemSaferFree(ptr, 0);
220#endif /* VBOX */
221}
222
223void CRYPTO_secure_clear_free(void *ptr, size_t num,
224 const char *file, int line)
225{
226#ifndef VBOX
227#ifndef OPENSSL_NO_SECURE_MEMORY
228 size_t actual_size;
229
230 if (ptr == NULL)
231 return;
232 if (!CRYPTO_secure_allocated(ptr)) {
233 OPENSSL_cleanse(ptr, num);
234 CRYPTO_free(ptr, file, line);
235 return;
236 }
237 if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
238 return;
239 actual_size = sh_actual_size(ptr);
240 CLEAR(ptr, actual_size);
241 secure_mem_used -= actual_size;
242 sh_free(ptr);
243 CRYPTO_THREAD_unlock(sec_malloc_lock);
244#else
245 if (ptr == NULL)
246 return;
247 OPENSSL_cleanse(ptr, num);
248 CRYPTO_free(ptr, file, line);
249#endif /* OPENSSL_NO_SECURE_MEMORY */
250#else
251 RT_NOREF(line);
252 RTMemSaferFree(ptr, 0);
253#endif /* VBOX */
254}
255
256int CRYPTO_secure_allocated(const void *ptr)
257{
258#ifndef VBOX
259#ifndef OPENSSL_NO_SECURE_MEMORY
260 if (!secure_mem_initialized)
261 return 0;
262 /*
263 * Only read accesses to the arena take place in sh_allocated() and this
264 * is only changed by the sh_init() and sh_done() calls which are not
265 * locked. Hence, it is safe to make this check without a lock too.
266 */
267 return sh_allocated(ptr);
268#else
269 return 0;
270#endif /* OPENSSL_NO_SECURE_MEMORY */
271#else
272 return RTMemSaferGetSize((void *)ptr) > 0;
273#endif /* VBOX */
274}
275
276size_t CRYPTO_secure_used(void)
277{
278#ifndef VBOX
279#ifndef OPENSSL_NO_SECURE_MEMORY
280 return secure_mem_used;
281#else
282 return 0;
283#endif /* OPENSSL_NO_SECURE_MEMORY */
284#else
285 return 0;
286#endif /* VBOX */
287}
288
289size_t CRYPTO_secure_actual_size(void *ptr)
290{
291#ifndef VBOX
292#ifndef OPENSSL_NO_SECURE_MEMORY
293 size_t actual_size;
294
295 if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
296 return 0;
297 actual_size = sh_actual_size(ptr);
298 CRYPTO_THREAD_unlock(sec_malloc_lock);
299 return actual_size;
300#else
301 return 0;
302#endif
303#else
304 return RTMemSaferGetSize(ptr);
305#endif /* VBOX */
306}
307
308#ifndef VBOX
309/*
310 * SECURE HEAP IMPLEMENTATION
311 */
312#ifndef OPENSSL_NO_SECURE_MEMORY
313
314
315/*
316 * The implementation provided here uses a fixed-sized mmap() heap,
317 * which is locked into memory, not written to core files, and protected
318 * on either side by an unmapped page, which will catch pointer overruns
319 * (or underruns) and an attempt to read data out of the secure heap.
320 * Free'd memory is zero'd or otherwise cleansed.
321 *
322 * This is a pretty standard buddy allocator. We keep areas in a multiple
323 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
324 * so all (and only) data is kept in the mmap'd heap.
325 *
326 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
327 * place.
328 */
329
330#define ONE ((size_t)1)
331
332# define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
333# define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
334# define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
335
336#define WITHIN_ARENA(p) \
337 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
338#define WITHIN_FREELIST(p) \
339 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
340
341
342typedef struct sh_list_st
343{
344 struct sh_list_st *next;
345 struct sh_list_st **p_next;
346} SH_LIST;
347
348typedef struct sh_st
349{
350 char* map_result;
351 size_t map_size;
352 char *arena;
353 size_t arena_size;
354 char **freelist;
355 ossl_ssize_t freelist_size;
356 size_t minsize;
357 unsigned char *bittable;
358 unsigned char *bitmalloc;
359 size_t bittable_size; /* size in bits */
360} SH;
361
362static SH sh;
363
364static size_t sh_getlist(char *ptr)
365{
366 ossl_ssize_t list = sh.freelist_size - 1;
367 size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
368
369 for (; bit; bit >>= 1, list--) {
370 if (TESTBIT(sh.bittable, bit))
371 break;
372 OPENSSL_assert((bit & 1) == 0);
373 }
374
375 return list;
376}
377
378
379static int sh_testbit(char *ptr, int list, unsigned char *table)
380{
381 size_t bit;
382
383 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
384 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
385 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
386 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
387 return TESTBIT(table, bit);
388}
389
390static void sh_clearbit(char *ptr, int list, unsigned char *table)
391{
392 size_t bit;
393
394 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
395 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
396 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
397 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
398 OPENSSL_assert(TESTBIT(table, bit));
399 CLEARBIT(table, bit);
400}
401
402static void sh_setbit(char *ptr, int list, unsigned char *table)
403{
404 size_t bit;
405
406 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
407 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
408 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
409 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
410 OPENSSL_assert(!TESTBIT(table, bit));
411 SETBIT(table, bit);
412}
413
414static void sh_add_to_list(char **list, char *ptr)
415{
416 SH_LIST *temp;
417
418 OPENSSL_assert(WITHIN_FREELIST(list));
419 OPENSSL_assert(WITHIN_ARENA(ptr));
420
421 temp = (SH_LIST *)ptr;
422 temp->next = *(SH_LIST **)list;
423 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
424 temp->p_next = (SH_LIST **)list;
425
426 if (temp->next != NULL) {
427 OPENSSL_assert((char **)temp->next->p_next == list);
428 temp->next->p_next = &(temp->next);
429 }
430
431 *list = ptr;
432}
433
434static void sh_remove_from_list(char *ptr)
435{
436 SH_LIST *temp, *temp2;
437
438 temp = (SH_LIST *)ptr;
439 if (temp->next != NULL)
440 temp->next->p_next = temp->p_next;
441 *temp->p_next = temp->next;
442 if (temp->next == NULL)
443 return;
444
445 temp2 = temp->next;
446 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
447}
448
449
450static int sh_init(size_t size, size_t minsize)
451{
452 int ret;
453 size_t i;
454 size_t pgsize;
455 size_t aligned;
456#if defined(_WIN32)
457 DWORD flOldProtect;
458 SYSTEM_INFO systemInfo;
459#endif
460
461 memset(&sh, 0, sizeof(sh));
462
463 /* make sure size is a powers of 2 */
464 OPENSSL_assert(size > 0);
465 OPENSSL_assert((size & (size - 1)) == 0);
466 if (size == 0 || (size & (size - 1)) != 0)
467 goto err;
468
469 if (minsize <= sizeof(SH_LIST)) {
470 OPENSSL_assert(sizeof(SH_LIST) <= 65536);
471 /*
472 * Compute the minimum possible allocation size.
473 * This must be a power of 2 and at least as large as the SH_LIST
474 * structure.
475 */
476 minsize = sizeof(SH_LIST) - 1;
477 minsize |= minsize >> 1;
478 minsize |= minsize >> 2;
479 if (sizeof(SH_LIST) > 16)
480 minsize |= minsize >> 4;
481 if (sizeof(SH_LIST) > 256)
482 minsize |= minsize >> 8;
483 minsize++;
484 } else {
485 /* make sure minsize is a powers of 2 */
486 OPENSSL_assert((minsize & (minsize - 1)) == 0);
487 if ((minsize & (minsize - 1)) != 0)
488 goto err;
489 }
490
491 sh.arena_size = size;
492 sh.minsize = minsize;
493 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
494
495 /* Prevent allocations of size 0 later on */
496 if (sh.bittable_size >> 3 == 0)
497 goto err;
498
499 sh.freelist_size = -1;
500 for (i = sh.bittable_size; i; i >>= 1)
501 sh.freelist_size++;
502
503 sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *));
504 OPENSSL_assert(sh.freelist != NULL);
505 if (sh.freelist == NULL)
506 goto err;
507
508 sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
509 OPENSSL_assert(sh.bittable != NULL);
510 if (sh.bittable == NULL)
511 goto err;
512
513 sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
514 OPENSSL_assert(sh.bitmalloc != NULL);
515 if (sh.bitmalloc == NULL)
516 goto err;
517
518 /* Allocate space for heap, and two extra pages as guards */
519#if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
520 {
521# if defined(_SC_PAGE_SIZE)
522 long tmppgsize = sysconf(_SC_PAGE_SIZE);
523# else
524 long tmppgsize = sysconf(_SC_PAGESIZE);
525# endif
526 if (tmppgsize < 1)
527 pgsize = PAGE_SIZE;
528 else
529 pgsize = (size_t)tmppgsize;
530 }
531#elif defined(_WIN32)
532 GetSystemInfo(&systemInfo);
533 pgsize = (size_t)systemInfo.dwPageSize;
534#else
535 pgsize = PAGE_SIZE;
536#endif
537 sh.map_size = pgsize + sh.arena_size + pgsize;
538
539#if !defined(_WIN32)
540# ifdef MAP_ANON
541 sh.map_result = mmap(NULL, sh.map_size,
542 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|MAP_CONCEAL, -1, 0);
543# else
544 {
545 int fd;
546
547 sh.map_result = MAP_FAILED;
548 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
549 sh.map_result = mmap(NULL, sh.map_size,
550 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
551 close(fd);
552 }
553 }
554# endif
555 if (sh.map_result == MAP_FAILED)
556 goto err;
557#else
558 sh.map_result = VirtualAlloc(NULL, sh.map_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
559
560 if (sh.map_result == NULL)
561 goto err;
562#endif
563
564 sh.arena = (char *)(sh.map_result + pgsize);
565 sh_setbit(sh.arena, 0, sh.bittable);
566 sh_add_to_list(&sh.freelist[0], sh.arena);
567
568 /* Now try to add guard pages and lock into memory. */
569 ret = 1;
570
571#if !defined(_WIN32)
572 /* Starting guard is already aligned from mmap. */
573 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
574 ret = 2;
575#else
576 if (VirtualProtect(sh.map_result, pgsize, PAGE_NOACCESS, &flOldProtect) == FALSE)
577 ret = 2;
578#endif
579
580 /* Ending guard page - need to round up to page boundary */
581 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
582#if !defined(_WIN32)
583 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
584 ret = 2;
585#else
586 if (VirtualProtect(sh.map_result + aligned, pgsize, PAGE_NOACCESS, &flOldProtect) == FALSE)
587 ret = 2;
588#endif
589
590#if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
591 if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
592 if (errno == ENOSYS) {
593 if (mlock(sh.arena, sh.arena_size) < 0)
594 ret = 2;
595 } else {
596 ret = 2;
597 }
598 }
599#elif defined(_WIN32)
600 if (VirtualLock(sh.arena, sh.arena_size) == FALSE)
601 ret = 2;
602#else
603 if (mlock(sh.arena, sh.arena_size) < 0)
604 ret = 2;
605#endif
606#ifdef MADV_DONTDUMP
607 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
608 ret = 2;
609#endif
610
611 return ret;
612
613 err:
614 sh_done();
615 return 0;
616}
617
618static void sh_done(void)
619{
620 OPENSSL_free(sh.freelist);
621 OPENSSL_free(sh.bittable);
622 OPENSSL_free(sh.bitmalloc);
623#if !defined(_WIN32)
624 if (sh.map_result != MAP_FAILED && sh.map_size)
625 munmap(sh.map_result, sh.map_size);
626#else
627 if (sh.map_result != NULL && sh.map_size)
628 VirtualFree(sh.map_result, 0, MEM_RELEASE);
629#endif
630 memset(&sh, 0, sizeof(sh));
631}
632
633static int sh_allocated(const char *ptr)
634{
635 return WITHIN_ARENA(ptr) ? 1 : 0;
636}
637
638static char *sh_find_my_buddy(char *ptr, int list)
639{
640 size_t bit;
641 char *chunk = NULL;
642
643 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
644 bit ^= 1;
645
646 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
647 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
648
649 return chunk;
650}
651
652static void *sh_malloc(size_t size)
653{
654 ossl_ssize_t list, slist;
655 size_t i;
656 char *chunk;
657
658 if (size > sh.arena_size)
659 return NULL;
660
661 list = sh.freelist_size - 1;
662 for (i = sh.minsize; i < size; i <<= 1)
663 list--;
664 if (list < 0)
665 return NULL;
666
667 /* try to find a larger entry to split */
668 for (slist = list; slist >= 0; slist--)
669 if (sh.freelist[slist] != NULL)
670 break;
671 if (slist < 0)
672 return NULL;
673
674 /* split larger entry */
675 while (slist != list) {
676 char *temp = sh.freelist[slist];
677
678 /* remove from bigger list */
679 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
680 sh_clearbit(temp, slist, sh.bittable);
681 sh_remove_from_list(temp);
682 OPENSSL_assert(temp != sh.freelist[slist]);
683
684 /* done with bigger list */
685 slist++;
686
687 /* add to smaller list */
688 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
689 sh_setbit(temp, slist, sh.bittable);
690 sh_add_to_list(&sh.freelist[slist], temp);
691 OPENSSL_assert(sh.freelist[slist] == temp);
692
693 /* split in 2 */
694 temp += sh.arena_size >> slist;
695 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
696 sh_setbit(temp, slist, sh.bittable);
697 sh_add_to_list(&sh.freelist[slist], temp);
698 OPENSSL_assert(sh.freelist[slist] == temp);
699
700 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
701 }
702
703 /* peel off memory to hand back */
704 chunk = sh.freelist[list];
705 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
706 sh_setbit(chunk, list, sh.bitmalloc);
707 sh_remove_from_list(chunk);
708
709 OPENSSL_assert(WITHIN_ARENA(chunk));
710
711 /* zero the free list header as a precaution against information leakage */
712 memset(chunk, 0, sizeof(SH_LIST));
713
714 return chunk;
715}
716
717static void sh_free(void *ptr)
718{
719 size_t list;
720 void *buddy;
721
722 if (ptr == NULL)
723 return;
724 OPENSSL_assert(WITHIN_ARENA(ptr));
725 if (!WITHIN_ARENA(ptr))
726 return;
727
728 list = sh_getlist(ptr);
729 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
730 sh_clearbit(ptr, list, sh.bitmalloc);
731 sh_add_to_list(&sh.freelist[list], ptr);
732
733 /* Try to coalesce two adjacent free areas. */
734 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
735 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
736 OPENSSL_assert(ptr != NULL);
737 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
738 sh_clearbit(ptr, list, sh.bittable);
739 sh_remove_from_list(ptr);
740 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
741 sh_clearbit(buddy, list, sh.bittable);
742 sh_remove_from_list(buddy);
743
744 list--;
745
746 /* Zero the higher addressed block's free list pointers */
747 memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST));
748 if (ptr > buddy)
749 ptr = buddy;
750
751 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
752 sh_setbit(ptr, list, sh.bittable);
753 sh_add_to_list(&sh.freelist[list], ptr);
754 OPENSSL_assert(sh.freelist[list] == ptr);
755 }
756}
757
758static size_t sh_actual_size(char *ptr)
759{
760 int list;
761
762 OPENSSL_assert(WITHIN_ARENA(ptr));
763 if (!WITHIN_ARENA(ptr))
764 return 0;
765 list = sh_getlist(ptr);
766 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
767 return sh.arena_size / (ONE << list);
768}
769#endif /* OPENSSL_NO_SECURE_MEMORY */
770#endif /* VBOX */
Note: See TracBrowser for help on using the repository browser.

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