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