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