mem_sec.c 17 KB

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