mem.c 7.8 KB

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