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