mem_sec.c 15 KB

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