mem.c 8.0 KB

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