mem.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright 1995-2018 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. #include "e_os.h"
  10. #include "internal/cryptlib.h"
  11. #include "internal/cryptlib_int.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <limits.h>
  15. #include <openssl/crypto.h>
  16. #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
  17. # include <execinfo.h>
  18. #endif
  19. /*
  20. * the following pointers may be changed as long as 'allow_customize' is set
  21. */
  22. static int allow_customize = 1;
  23. static void *(*malloc_impl)(size_t, const char *, int)
  24. = CRYPTO_malloc;
  25. static void *(*realloc_impl)(void *, size_t, const char *, int)
  26. = CRYPTO_realloc;
  27. static void (*free_impl)(void *, const char *, int)
  28. = CRYPTO_free;
  29. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  30. static int malloc_count;
  31. static int realloc_count;
  32. static int free_count;
  33. static int dummy;
  34. # define INCREMENT(x) CRYPTO_atomic_add(&x, 1, &dummy, memdbg_lock)
  35. # define GET(ret, val) CRYPTO_atomic_read(&val, ret, memdbg_lock)
  36. static char *md_failstring;
  37. static long md_count;
  38. static int md_fail_percent = 0;
  39. static int md_tracefd = -1;
  40. static int call_malloc_debug = 1;
  41. static void parseit(void);
  42. static int shouldfail(void);
  43. # define FAILTEST() if (shouldfail()) return NULL
  44. #else
  45. static int call_malloc_debug = 0;
  46. # define INCREMENT(x) /* empty */
  47. # define FAILTEST() /* empty */
  48. #endif
  49. int CRYPTO_set_mem_functions(
  50. void *(*m)(size_t, const char *, int),
  51. void *(*r)(void *, size_t, const char *, int),
  52. void (*f)(void *, const char *, int))
  53. {
  54. if (!allow_customize)
  55. return 0;
  56. if (m)
  57. malloc_impl = m;
  58. if (r)
  59. realloc_impl = r;
  60. if (f)
  61. free_impl = f;
  62. return 1;
  63. }
  64. int CRYPTO_set_mem_debug(int flag)
  65. {
  66. if (!allow_customize)
  67. return 0;
  68. call_malloc_debug = flag;
  69. return 1;
  70. }
  71. void CRYPTO_get_mem_functions(
  72. void *(**m)(size_t, const char *, int),
  73. void *(**r)(void *, size_t, const char *, int),
  74. void (**f)(void *, const char *, int))
  75. {
  76. if (m != NULL)
  77. *m = malloc_impl;
  78. if (r != NULL)
  79. *r = realloc_impl;
  80. if (f != NULL)
  81. *f = free_impl;
  82. }
  83. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  84. void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount)
  85. {
  86. if (mcount != NULL)
  87. GET(mcount, malloc_count);
  88. if (rcount != NULL)
  89. GET(rcount, realloc_count);
  90. if (fcount != NULL)
  91. GET(fcount, free_count);
  92. }
  93. /*
  94. * Parse a "malloc failure spec" string. This likes like a set of fields
  95. * separated by semicolons. Each field has a count and an optional failure
  96. * percentage. For example:
  97. * 100@0;100@25;0@0
  98. * or 100;100@25;0
  99. * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
  100. * all remaining (count is zero) succeed.
  101. */
  102. static void parseit(void)
  103. {
  104. char *semi = strchr(md_failstring, ';');
  105. char *atsign;
  106. if (semi != NULL)
  107. *semi++ = '\0';
  108. /* Get the count (atol will stop at the @ if there), and percentage */
  109. md_count = atol(md_failstring);
  110. atsign = strchr(md_failstring, '@');
  111. md_fail_percent = atsign == NULL ? 0 : atoi(atsign + 1);
  112. if (semi != NULL)
  113. md_failstring = semi;
  114. }
  115. /*
  116. * Windows doesn't have random(), but it has rand()
  117. * Some rand() implementations aren't good, but we're not
  118. * dealing with secure randomness here.
  119. */
  120. # ifdef _WIN32
  121. # define random() rand()
  122. # endif
  123. /*
  124. * See if the current malloc should fail.
  125. */
  126. static int shouldfail(void)
  127. {
  128. int roll = (int)(random() % 100);
  129. int shoulditfail = roll < md_fail_percent;
  130. # ifndef _WIN32
  131. /* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
  132. int len;
  133. char buff[80];
  134. if (md_tracefd > 0) {
  135. BIO_snprintf(buff, sizeof(buff),
  136. "%c C%ld %%%d R%d\n",
  137. shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
  138. len = strlen(buff);
  139. if (write(md_tracefd, buff, len) != len)
  140. perror("shouldfail write failed");
  141. # ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
  142. if (shoulditfail) {
  143. void *addrs[30];
  144. int num = backtrace(addrs, OSSL_NELEM(addrs));
  145. backtrace_symbols_fd(addrs, num, md_tracefd);
  146. }
  147. # endif
  148. }
  149. # endif
  150. if (md_count) {
  151. /* If we used up this one, go to the next. */
  152. if (--md_count == 0)
  153. parseit();
  154. }
  155. return shoulditfail;
  156. }
  157. void ossl_malloc_setup_failures(void)
  158. {
  159. const char *cp = getenv("OPENSSL_MALLOC_FAILURES");
  160. if (cp != NULL && (md_failstring = strdup(cp)) != NULL)
  161. parseit();
  162. if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
  163. md_tracefd = atoi(cp);
  164. }
  165. #endif
  166. void *CRYPTO_malloc(size_t num, const char *file, int line)
  167. {
  168. void *ret = NULL;
  169. INCREMENT(malloc_count);
  170. if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
  171. return malloc_impl(num, file, line);
  172. if (num == 0)
  173. return NULL;
  174. FAILTEST();
  175. if (allow_customize) {
  176. /*
  177. * Disallow customization after the first allocation. We only set this
  178. * if necessary to avoid a store to the same cache line on every
  179. * allocation.
  180. */
  181. allow_customize = 0;
  182. }
  183. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  184. if (call_malloc_debug) {
  185. CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);
  186. ret = malloc(num);
  187. CRYPTO_mem_debug_malloc(ret, num, 1, file, line);
  188. } else {
  189. ret = malloc(num);
  190. }
  191. #else
  192. (void)(file); (void)(line);
  193. ret = malloc(num);
  194. #endif
  195. return ret;
  196. }
  197. void *CRYPTO_zalloc(size_t num, const char *file, int line)
  198. {
  199. void *ret = CRYPTO_malloc(num, file, line);
  200. FAILTEST();
  201. if (ret != NULL)
  202. memset(ret, 0, num);
  203. return ret;
  204. }
  205. void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
  206. {
  207. INCREMENT(realloc_count);
  208. if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)
  209. return realloc_impl(str, num, file, line);
  210. FAILTEST();
  211. if (str == NULL)
  212. return CRYPTO_malloc(num, file, line);
  213. if (num == 0) {
  214. CRYPTO_free(str, file, line);
  215. return NULL;
  216. }
  217. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  218. if (call_malloc_debug) {
  219. void *ret;
  220. CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
  221. ret = realloc(str, num);
  222. CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
  223. return ret;
  224. }
  225. #else
  226. (void)(file); (void)(line);
  227. #endif
  228. return realloc(str, num);
  229. }
  230. void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
  231. const char *file, int line)
  232. {
  233. void *ret = NULL;
  234. if (str == NULL)
  235. return CRYPTO_malloc(num, file, line);
  236. if (num == 0) {
  237. CRYPTO_clear_free(str, old_len, file, line);
  238. return NULL;
  239. }
  240. /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
  241. if (num < old_len) {
  242. OPENSSL_cleanse((char*)str + num, old_len - num);
  243. return str;
  244. }
  245. ret = CRYPTO_malloc(num, file, line);
  246. if (ret != NULL) {
  247. memcpy(ret, str, old_len);
  248. CRYPTO_clear_free(str, old_len, file, line);
  249. }
  250. return ret;
  251. }
  252. void CRYPTO_free(void *str, const char *file, int line)
  253. {
  254. INCREMENT(free_count);
  255. if (free_impl != NULL && free_impl != &CRYPTO_free) {
  256. free_impl(str, file, line);
  257. return;
  258. }
  259. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  260. if (call_malloc_debug) {
  261. CRYPTO_mem_debug_free(str, 0, file, line);
  262. free(str);
  263. CRYPTO_mem_debug_free(str, 1, file, line);
  264. } else {
  265. free(str);
  266. }
  267. #else
  268. free(str);
  269. #endif
  270. }
  271. void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
  272. {
  273. if (str == NULL)
  274. return;
  275. if (num)
  276. OPENSSL_cleanse(str, num);
  277. CRYPTO_free(str, file, line);
  278. }