mem_sec.c 19 KB

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