mem.c 8.6 KB

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