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