2
0

mem_sec.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 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. /*
  11. * This file is in two halves. The first half implements the public API
  12. * to be used by external consumers, and to be used by OpenSSL to store
  13. * data in a "secure arena." The second half implements the secure arena.
  14. * For details on that implementation, see below (look for uppercase
  15. * "SECURE HEAP IMPLEMENTATION").
  16. */
  17. #include "e_os.h"
  18. #include <openssl/crypto.h>
  19. #include <string.h>
  20. /* e_os.h defines OPENSSL_SECURE_MEMORY if secure memory can be implemented */
  21. #ifdef OPENSSL_SECURE_MEMORY
  22. # include <stdlib.h>
  23. # include <assert.h>
  24. # include <unistd.h>
  25. # include <sys/types.h>
  26. # include <sys/mman.h>
  27. # if defined(OPENSSL_SYS_LINUX)
  28. # include <sys/syscall.h>
  29. # if defined(SYS_mlock2)
  30. # include <linux/mman.h>
  31. # include <errno.h>
  32. # endif
  33. # endif
  34. # if defined(__FreeBSD__)
  35. # define MADV_DONTDUMP MADV_NOCORE
  36. # endif
  37. # if !defined(MAP_CONCEAL)
  38. # define MAP_CONCEAL 0
  39. # endif
  40. # include <sys/param.h>
  41. # include <sys/stat.h>
  42. # include <fcntl.h>
  43. #endif
  44. #define CLEAR(p, s) OPENSSL_cleanse(p, s)
  45. #ifndef PAGE_SIZE
  46. # define PAGE_SIZE 4096
  47. #endif
  48. #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
  49. # define MAP_ANON MAP_ANONYMOUS
  50. #endif
  51. #ifdef OPENSSL_SECURE_MEMORY
  52. static size_t secure_mem_used;
  53. static int secure_mem_initialized;
  54. static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
  55. /*
  56. * These are the functions that must be implemented by a secure heap (sh).
  57. */
  58. static int sh_init(size_t size, int minsize);
  59. static void *sh_malloc(size_t size);
  60. static void sh_free(void *ptr);
  61. static void sh_done(void);
  62. static size_t sh_actual_size(char *ptr);
  63. static int sh_allocated(const char *ptr);
  64. #endif
  65. int CRYPTO_secure_malloc_init(size_t size, int minsize)
  66. {
  67. #ifdef OPENSSL_SECURE_MEMORY
  68. int ret = 0;
  69. if (!secure_mem_initialized) {
  70. sec_malloc_lock = CRYPTO_THREAD_lock_new();
  71. if (sec_malloc_lock == NULL)
  72. return 0;
  73. if ((ret = sh_init(size, minsize)) != 0) {
  74. secure_mem_initialized = 1;
  75. } else {
  76. CRYPTO_THREAD_lock_free(sec_malloc_lock);
  77. sec_malloc_lock = NULL;
  78. }
  79. }
  80. return ret;
  81. #else
  82. return 0;
  83. #endif /* OPENSSL_SECURE_MEMORY */
  84. }
  85. int CRYPTO_secure_malloc_done(void)
  86. {
  87. #ifdef OPENSSL_SECURE_MEMORY
  88. if (secure_mem_used == 0) {
  89. sh_done();
  90. secure_mem_initialized = 0;
  91. CRYPTO_THREAD_lock_free(sec_malloc_lock);
  92. sec_malloc_lock = NULL;
  93. return 1;
  94. }
  95. #endif /* OPENSSL_SECURE_MEMORY */
  96. return 0;
  97. }
  98. int CRYPTO_secure_malloc_initialized(void)
  99. {
  100. #ifdef OPENSSL_SECURE_MEMORY
  101. return secure_mem_initialized;
  102. #else
  103. return 0;
  104. #endif /* OPENSSL_SECURE_MEMORY */
  105. }
  106. void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
  107. {
  108. #ifdef OPENSSL_SECURE_MEMORY
  109. void *ret;
  110. size_t actual_size;
  111. if (!secure_mem_initialized) {
  112. return CRYPTO_malloc(num, file, line);
  113. }
  114. CRYPTO_THREAD_write_lock(sec_malloc_lock);
  115. ret = sh_malloc(num);
  116. actual_size = ret ? sh_actual_size(ret) : 0;
  117. secure_mem_used += actual_size;
  118. CRYPTO_THREAD_unlock(sec_malloc_lock);
  119. return ret;
  120. #else
  121. return CRYPTO_malloc(num, file, line);
  122. #endif /* OPENSSL_SECURE_MEMORY */
  123. }
  124. void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
  125. {
  126. #ifdef OPENSSL_SECURE_MEMORY
  127. if (secure_mem_initialized)
  128. /* CRYPTO_secure_malloc() zeroes allocations when it is implemented */
  129. return CRYPTO_secure_malloc(num, file, line);
  130. #endif
  131. return CRYPTO_zalloc(num, file, line);
  132. }
  133. void CRYPTO_secure_free(void *ptr, const char *file, int line)
  134. {
  135. #ifdef OPENSSL_SECURE_MEMORY
  136. size_t actual_size;
  137. if (ptr == NULL)
  138. return;
  139. if (!CRYPTO_secure_allocated(ptr)) {
  140. CRYPTO_free(ptr, file, line);
  141. return;
  142. }
  143. CRYPTO_THREAD_write_lock(sec_malloc_lock);
  144. actual_size = sh_actual_size(ptr);
  145. CLEAR(ptr, actual_size);
  146. secure_mem_used -= actual_size;
  147. sh_free(ptr);
  148. CRYPTO_THREAD_unlock(sec_malloc_lock);
  149. #else
  150. CRYPTO_free(ptr, file, line);
  151. #endif /* OPENSSL_SECURE_MEMORY */
  152. }
  153. void CRYPTO_secure_clear_free(void *ptr, size_t num,
  154. const char *file, int line)
  155. {
  156. #ifdef OPENSSL_SECURE_MEMORY
  157. size_t actual_size;
  158. if (ptr == NULL)
  159. return;
  160. if (!CRYPTO_secure_allocated(ptr)) {
  161. OPENSSL_cleanse(ptr, num);
  162. CRYPTO_free(ptr, file, line);
  163. return;
  164. }
  165. CRYPTO_THREAD_write_lock(sec_malloc_lock);
  166. actual_size = sh_actual_size(ptr);
  167. CLEAR(ptr, actual_size);
  168. secure_mem_used -= actual_size;
  169. sh_free(ptr);
  170. CRYPTO_THREAD_unlock(sec_malloc_lock);
  171. #else
  172. if (ptr == NULL)
  173. return;
  174. OPENSSL_cleanse(ptr, num);
  175. CRYPTO_free(ptr, file, line);
  176. #endif /* OPENSSL_SECURE_MEMORY */
  177. }
  178. int CRYPTO_secure_allocated(const void *ptr)
  179. {
  180. #ifdef OPENSSL_SECURE_MEMORY
  181. int ret;
  182. if (!secure_mem_initialized)
  183. return 0;
  184. CRYPTO_THREAD_write_lock(sec_malloc_lock);
  185. ret = sh_allocated(ptr);
  186. CRYPTO_THREAD_unlock(sec_malloc_lock);
  187. return ret;
  188. #else
  189. return 0;
  190. #endif /* OPENSSL_SECURE_MEMORY */
  191. }
  192. size_t CRYPTO_secure_used(void)
  193. {
  194. #ifdef OPENSSL_SECURE_MEMORY
  195. return secure_mem_used;
  196. #else
  197. return 0;
  198. #endif /* OPENSSL_SECURE_MEMORY */
  199. }
  200. size_t CRYPTO_secure_actual_size(void *ptr)
  201. {
  202. #ifdef OPENSSL_SECURE_MEMORY
  203. size_t actual_size;
  204. CRYPTO_THREAD_write_lock(sec_malloc_lock);
  205. actual_size = sh_actual_size(ptr);
  206. CRYPTO_THREAD_unlock(sec_malloc_lock);
  207. return actual_size;
  208. #else
  209. return 0;
  210. #endif
  211. }
  212. /* END OF PAGE ...
  213. ... START OF PAGE */
  214. /*
  215. * SECURE HEAP IMPLEMENTATION
  216. */
  217. #ifdef OPENSSL_SECURE_MEMORY
  218. /*
  219. * The implementation provided here uses a fixed-sized mmap() heap,
  220. * which is locked into memory, not written to core files, and protected
  221. * on either side by an unmapped page, which will catch pointer overruns
  222. * (or underruns) and an attempt to read data out of the secure heap.
  223. * Free'd memory is zero'd or otherwise cleansed.
  224. *
  225. * This is a pretty standard buddy allocator. We keep areas in a multiple
  226. * of "sh.minsize" units. The freelist and bitmaps are kept separately,
  227. * so all (and only) data is kept in the mmap'd heap.
  228. *
  229. * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
  230. * place.
  231. */
  232. #define ONE ((size_t)1)
  233. # define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
  234. # define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
  235. # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
  236. #define WITHIN_ARENA(p) \
  237. ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
  238. #define WITHIN_FREELIST(p) \
  239. ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
  240. typedef struct sh_list_st
  241. {
  242. struct sh_list_st *next;
  243. struct sh_list_st **p_next;
  244. } SH_LIST;
  245. typedef struct sh_st
  246. {
  247. char* map_result;
  248. size_t map_size;
  249. char *arena;
  250. size_t arena_size;
  251. char **freelist;
  252. ossl_ssize_t freelist_size;
  253. size_t minsize;
  254. unsigned char *bittable;
  255. unsigned char *bitmalloc;
  256. size_t bittable_size; /* size in bits */
  257. } SH;
  258. static SH sh;
  259. static size_t sh_getlist(char *ptr)
  260. {
  261. ossl_ssize_t list = sh.freelist_size - 1;
  262. size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
  263. for (; bit; bit >>= 1, list--) {
  264. if (TESTBIT(sh.bittable, bit))
  265. break;
  266. OPENSSL_assert((bit & 1) == 0);
  267. }
  268. return list;
  269. }
  270. static int sh_testbit(char *ptr, int list, unsigned char *table)
  271. {
  272. size_t bit;
  273. OPENSSL_assert(list >= 0 && list < sh.freelist_size);
  274. OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
  275. bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
  276. OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
  277. return TESTBIT(table, bit);
  278. }
  279. static void sh_clearbit(char *ptr, int list, unsigned char *table)
  280. {
  281. size_t bit;
  282. OPENSSL_assert(list >= 0 && list < sh.freelist_size);
  283. OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
  284. bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
  285. OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
  286. OPENSSL_assert(TESTBIT(table, bit));
  287. CLEARBIT(table, bit);
  288. }
  289. static void sh_setbit(char *ptr, int list, unsigned char *table)
  290. {
  291. size_t bit;
  292. OPENSSL_assert(list >= 0 && list < sh.freelist_size);
  293. OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
  294. bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
  295. OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
  296. OPENSSL_assert(!TESTBIT(table, bit));
  297. SETBIT(table, bit);
  298. }
  299. static void sh_add_to_list(char **list, char *ptr)
  300. {
  301. SH_LIST *temp;
  302. OPENSSL_assert(WITHIN_FREELIST(list));
  303. OPENSSL_assert(WITHIN_ARENA(ptr));
  304. temp = (SH_LIST *)ptr;
  305. temp->next = *(SH_LIST **)list;
  306. OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
  307. temp->p_next = (SH_LIST **)list;
  308. if (temp->next != NULL) {
  309. OPENSSL_assert((char **)temp->next->p_next == list);
  310. temp->next->p_next = &(temp->next);
  311. }
  312. *list = ptr;
  313. }
  314. static void sh_remove_from_list(char *ptr)
  315. {
  316. SH_LIST *temp, *temp2;
  317. temp = (SH_LIST *)ptr;
  318. if (temp->next != NULL)
  319. temp->next->p_next = temp->p_next;
  320. *temp->p_next = temp->next;
  321. if (temp->next == NULL)
  322. return;
  323. temp2 = temp->next;
  324. OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
  325. }
  326. static int sh_init(size_t size, int minsize)
  327. {
  328. int ret;
  329. size_t i;
  330. size_t pgsize;
  331. size_t aligned;
  332. memset(&sh, 0, sizeof(sh));
  333. /* make sure size and minsize are powers of 2 */
  334. OPENSSL_assert(size > 0);
  335. OPENSSL_assert((size & (size - 1)) == 0);
  336. OPENSSL_assert(minsize > 0);
  337. OPENSSL_assert((minsize & (minsize - 1)) == 0);
  338. if (size <= 0 || (size & (size - 1)) != 0)
  339. goto err;
  340. if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
  341. goto err;
  342. while (minsize < (int)sizeof(SH_LIST))
  343. minsize *= 2;
  344. sh.arena_size = size;
  345. sh.minsize = minsize;
  346. sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
  347. /* Prevent allocations of size 0 later on */
  348. if (sh.bittable_size >> 3 == 0)
  349. goto err;
  350. sh.freelist_size = -1;
  351. for (i = sh.bittable_size; i; i >>= 1)
  352. sh.freelist_size++;
  353. sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *));
  354. OPENSSL_assert(sh.freelist != NULL);
  355. if (sh.freelist == NULL)
  356. goto err;
  357. sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
  358. OPENSSL_assert(sh.bittable != NULL);
  359. if (sh.bittable == NULL)
  360. goto err;
  361. sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
  362. OPENSSL_assert(sh.bitmalloc != NULL);
  363. if (sh.bitmalloc == NULL)
  364. goto err;
  365. /* Allocate space for heap, and two extra pages as guards */
  366. #if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
  367. {
  368. # if defined(_SC_PAGE_SIZE)
  369. long tmppgsize = sysconf(_SC_PAGE_SIZE);
  370. # else
  371. long tmppgsize = sysconf(_SC_PAGESIZE);
  372. # endif
  373. if (tmppgsize < 1)
  374. pgsize = PAGE_SIZE;
  375. else
  376. pgsize = (size_t)tmppgsize;
  377. }
  378. #else
  379. pgsize = PAGE_SIZE;
  380. #endif
  381. sh.map_size = pgsize + sh.arena_size + pgsize;
  382. if (1) {
  383. #ifdef MAP_ANON
  384. sh.map_result = mmap(NULL, sh.map_size,
  385. PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|MAP_CONCEAL, -1, 0);
  386. } else {
  387. #endif
  388. int fd;
  389. sh.map_result = MAP_FAILED;
  390. if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
  391. sh.map_result = mmap(NULL, sh.map_size,
  392. PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  393. close(fd);
  394. }
  395. }
  396. if (sh.map_result == MAP_FAILED)
  397. goto err;
  398. sh.arena = (char *)(sh.map_result + pgsize);
  399. sh_setbit(sh.arena, 0, sh.bittable);
  400. sh_add_to_list(&sh.freelist[0], sh.arena);
  401. /* Now try to add guard pages and lock into memory. */
  402. ret = 1;
  403. /* Starting guard is already aligned from mmap. */
  404. if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
  405. ret = 2;
  406. /* Ending guard page - need to round up to page boundary */
  407. aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
  408. if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
  409. ret = 2;
  410. #if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
  411. if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
  412. if (errno == ENOSYS) {
  413. if (mlock(sh.arena, sh.arena_size) < 0)
  414. ret = 2;
  415. } else {
  416. ret = 2;
  417. }
  418. }
  419. #else
  420. if (mlock(sh.arena, sh.arena_size) < 0)
  421. ret = 2;
  422. #endif
  423. #ifdef MADV_DONTDUMP
  424. if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
  425. ret = 2;
  426. #endif
  427. return ret;
  428. err:
  429. sh_done();
  430. return 0;
  431. }
  432. static void sh_done(void)
  433. {
  434. OPENSSL_free(sh.freelist);
  435. OPENSSL_free(sh.bittable);
  436. OPENSSL_free(sh.bitmalloc);
  437. if (sh.map_result != MAP_FAILED && sh.map_size)
  438. munmap(sh.map_result, sh.map_size);
  439. memset(&sh, 0, sizeof(sh));
  440. }
  441. static int sh_allocated(const char *ptr)
  442. {
  443. return WITHIN_ARENA(ptr) ? 1 : 0;
  444. }
  445. static char *sh_find_my_buddy(char *ptr, int list)
  446. {
  447. size_t bit;
  448. char *chunk = NULL;
  449. bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
  450. bit ^= 1;
  451. if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
  452. chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
  453. return chunk;
  454. }
  455. static void *sh_malloc(size_t size)
  456. {
  457. ossl_ssize_t list, slist;
  458. size_t i;
  459. char *chunk;
  460. if (size > sh.arena_size)
  461. return NULL;
  462. list = sh.freelist_size - 1;
  463. for (i = sh.minsize; i < size; i <<= 1)
  464. list--;
  465. if (list < 0)
  466. return NULL;
  467. /* try to find a larger entry to split */
  468. for (slist = list; slist >= 0; slist--)
  469. if (sh.freelist[slist] != NULL)
  470. break;
  471. if (slist < 0)
  472. return NULL;
  473. /* split larger entry */
  474. while (slist != list) {
  475. char *temp = sh.freelist[slist];
  476. /* remove from bigger list */
  477. OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
  478. sh_clearbit(temp, slist, sh.bittable);
  479. sh_remove_from_list(temp);
  480. OPENSSL_assert(temp != sh.freelist[slist]);
  481. /* done with bigger list */
  482. slist++;
  483. /* add to smaller list */
  484. OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
  485. sh_setbit(temp, slist, sh.bittable);
  486. sh_add_to_list(&sh.freelist[slist], temp);
  487. OPENSSL_assert(sh.freelist[slist] == temp);
  488. /* split in 2 */
  489. temp += sh.arena_size >> slist;
  490. OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
  491. sh_setbit(temp, slist, sh.bittable);
  492. sh_add_to_list(&sh.freelist[slist], temp);
  493. OPENSSL_assert(sh.freelist[slist] == temp);
  494. OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
  495. }
  496. /* peel off memory to hand back */
  497. chunk = sh.freelist[list];
  498. OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
  499. sh_setbit(chunk, list, sh.bitmalloc);
  500. sh_remove_from_list(chunk);
  501. OPENSSL_assert(WITHIN_ARENA(chunk));
  502. /* zero the free list header as a precaution against information leakage */
  503. memset(chunk, 0, sizeof(SH_LIST));
  504. return chunk;
  505. }
  506. static void sh_free(void *ptr)
  507. {
  508. size_t list;
  509. void *buddy;
  510. if (ptr == NULL)
  511. return;
  512. OPENSSL_assert(WITHIN_ARENA(ptr));
  513. if (!WITHIN_ARENA(ptr))
  514. return;
  515. list = sh_getlist(ptr);
  516. OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
  517. sh_clearbit(ptr, list, sh.bitmalloc);
  518. sh_add_to_list(&sh.freelist[list], ptr);
  519. /* Try to coalesce two adjacent free areas. */
  520. while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
  521. OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
  522. OPENSSL_assert(ptr != NULL);
  523. OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
  524. sh_clearbit(ptr, list, sh.bittable);
  525. sh_remove_from_list(ptr);
  526. OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
  527. sh_clearbit(buddy, list, sh.bittable);
  528. sh_remove_from_list(buddy);
  529. list--;
  530. /* Zero the higher addressed block's free list pointers */
  531. memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST));
  532. if (ptr > buddy)
  533. ptr = buddy;
  534. OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
  535. sh_setbit(ptr, list, sh.bittable);
  536. sh_add_to_list(&sh.freelist[list], ptr);
  537. OPENSSL_assert(sh.freelist[list] == ptr);
  538. }
  539. }
  540. static size_t sh_actual_size(char *ptr)
  541. {
  542. int list;
  543. OPENSSL_assert(WITHIN_ARENA(ptr));
  544. if (!WITHIN_ARENA(ptr))
  545. return 0;
  546. list = sh_getlist(ptr);
  547. OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
  548. return sh.arena_size / (ONE << list);
  549. }
  550. #endif /* OPENSSL_SECURE_MEMORY */