VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.7/crypto/mem_sec.c@ 105963

Last change on this file since 105963 was 105643, checked in by vboxsync, 4 months ago

IPRT,libs/openssl-3.1.5: Added RTMEMSAFER_F_NO_SUPLIB_ALLOC and made OpenSSL use it. OpenSSL can't use the SUPLIB secure memory allocator, as SUPR3Term may be invoked while the memory is still being used. bugref:10638

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