OPENSSL_malloc.3ossl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. .\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42)
  2. .\"
  3. .\" Standard preamble:
  4. .\" ========================================================================
  5. .de Sp \" Vertical space (when we can't use .PP)
  6. .if t .sp .5v
  7. .if n .sp
  8. ..
  9. .de Vb \" Begin verbatim text
  10. .ft CW
  11. .nf
  12. .ne \\$1
  13. ..
  14. .de Ve \" End verbatim text
  15. .ft R
  16. .fi
  17. ..
  18. .\" Set up some character translations and predefined strings. \*(-- will
  19. .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
  20. .\" double quote, and \*(R" will give a right double quote. \*(C+ will
  21. .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
  22. .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
  23. .\" nothing in troff, for use with C<>.
  24. .tr \(*W-
  25. .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
  26. .ie n \{\
  27. . ds -- \(*W-
  28. . ds PI pi
  29. . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
  30. . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
  31. . ds L" ""
  32. . ds R" ""
  33. . ds C` ""
  34. . ds C' ""
  35. 'br\}
  36. .el\{\
  37. . ds -- \|\(em\|
  38. . ds PI \(*p
  39. . ds L" ``
  40. . ds R" ''
  41. . ds C`
  42. . ds C'
  43. 'br\}
  44. .\"
  45. .\" Escape single quotes in literal strings from groff's Unicode transform.
  46. .ie \n(.g .ds Aq \(aq
  47. .el .ds Aq '
  48. .\"
  49. .\" If the F register is >0, we'll generate index entries on stderr for
  50. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  51. .\" entries marked with X<> in POD. Of course, you'll have to process the
  52. .\" output yourself in some meaningful fashion.
  53. .\"
  54. .\" Avoid warning from groff about undefined register 'F'.
  55. .de IX
  56. ..
  57. .nr rF 0
  58. .if \n(.g .if rF .nr rF 1
  59. .if (\n(rF:(\n(.g==0)) \{\
  60. . if \nF \{\
  61. . de IX
  62. . tm Index:\\$1\t\\n%\t"\\$2"
  63. ..
  64. . if !\nF==2 \{\
  65. . nr % 0
  66. . nr F 2
  67. . \}
  68. . \}
  69. .\}
  70. .rr rF
  71. .\"
  72. .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
  73. .\" Fear. Run. Save yourself. No user-serviceable parts.
  74. . \" fudge factors for nroff and troff
  75. .if n \{\
  76. . ds #H 0
  77. . ds #V .8m
  78. . ds #F .3m
  79. . ds #[ \f1
  80. . ds #] \fP
  81. .\}
  82. .if t \{\
  83. . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
  84. . ds #V .6m
  85. . ds #F 0
  86. . ds #[ \&
  87. . ds #] \&
  88. .\}
  89. . \" simple accents for nroff and troff
  90. .if n \{\
  91. . ds ' \&
  92. . ds ` \&
  93. . ds ^ \&
  94. . ds , \&
  95. . ds ~ ~
  96. . ds /
  97. .\}
  98. .if t \{\
  99. . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
  100. . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
  101. . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
  102. . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
  103. . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
  104. . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
  105. .\}
  106. . \" troff and (daisy-wheel) nroff accents
  107. .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
  108. .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
  109. .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
  110. .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
  111. .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
  112. .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
  113. .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
  114. .ds ae a\h'-(\w'a'u*4/10)'e
  115. .ds Ae A\h'-(\w'A'u*4/10)'E
  116. . \" corrections for vroff
  117. .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
  118. .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
  119. . \" for low resolution devices (crt and lpr)
  120. .if \n(.H>23 .if \n(.V>19 \
  121. \{\
  122. . ds : e
  123. . ds 8 ss
  124. . ds o a
  125. . ds d- d\h'-1'\(ga
  126. . ds D- D\h'-1'\(hy
  127. . ds th \o'bp'
  128. . ds Th \o'LP'
  129. . ds ae ae
  130. . ds Ae AE
  131. .\}
  132. .rm #[ #] #H #V #F C
  133. .\" ========================================================================
  134. .\"
  135. .IX Title "OPENSSL_MALLOC 3ossl"
  136. .TH OPENSSL_MALLOC 3ossl "2024-09-03" "3.3.2" "OpenSSL"
  137. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  138. .\" way too many mistakes in technical documents.
  139. .if n .ad l
  140. .nh
  141. .SH "NAME"
  142. OPENSSL_malloc_init,
  143. OPENSSL_malloc, OPENSSL_zalloc, OPENSSL_realloc, OPENSSL_free,
  144. OPENSSL_clear_realloc, OPENSSL_clear_free, OPENSSL_cleanse,
  145. CRYPTO_malloc, CRYPTO_zalloc, CRYPTO_realloc, CRYPTO_free,
  146. OPENSSL_strdup, OPENSSL_strndup,
  147. OPENSSL_memdup, OPENSSL_strlcpy, OPENSSL_strlcat,
  148. CRYPTO_strdup, CRYPTO_strndup,
  149. OPENSSL_mem_debug_push, OPENSSL_mem_debug_pop,
  150. CRYPTO_mem_debug_push, CRYPTO_mem_debug_pop,
  151. CRYPTO_clear_realloc, CRYPTO_clear_free,
  152. CRYPTO_malloc_fn, CRYPTO_realloc_fn, CRYPTO_free_fn,
  153. CRYPTO_get_mem_functions, CRYPTO_set_mem_functions,
  154. CRYPTO_get_alloc_counts,
  155. CRYPTO_set_mem_debug, CRYPTO_mem_ctrl,
  156. CRYPTO_mem_leaks, CRYPTO_mem_leaks_fp, CRYPTO_mem_leaks_cb,
  157. OPENSSL_MALLOC_FAILURES,
  158. OPENSSL_MALLOC_FD
  159. \&\- Memory allocation functions
  160. .SH "SYNOPSIS"
  161. .IX Header "SYNOPSIS"
  162. .Vb 1
  163. \& #include <openssl/crypto.h>
  164. \&
  165. \& int OPENSSL_malloc_init(void);
  166. \&
  167. \& void *OPENSSL_malloc(size_t num);
  168. \& void *OPENSSL_zalloc(size_t num);
  169. \& void *OPENSSL_realloc(void *addr, size_t num);
  170. \& void OPENSSL_free(void *addr);
  171. \& char *OPENSSL_strdup(const char *str);
  172. \& char *OPENSSL_strndup(const char *str, size_t s);
  173. \& size_t OPENSSL_strlcat(char *dst, const char *src, size_t size);
  174. \& size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size);
  175. \& void *OPENSSL_memdup(void *data, size_t s);
  176. \& void *OPENSSL_clear_realloc(void *p, size_t old_len, size_t num);
  177. \& void OPENSSL_clear_free(void *str, size_t num);
  178. \& void OPENSSL_cleanse(void *ptr, size_t len);
  179. \&
  180. \& void *CRYPTO_malloc(size_t num, const char *file, int line);
  181. \& void *CRYPTO_zalloc(size_t num, const char *file, int line);
  182. \& void *CRYPTO_realloc(void *p, size_t num, const char *file, int line);
  183. \& void CRYPTO_free(void *str, const char *, int);
  184. \& char *CRYPTO_strdup(const char *p, const char *file, int line);
  185. \& char *CRYPTO_strndup(const char *p, size_t num, const char *file, int line);
  186. \& void *CRYPTO_clear_realloc(void *p, size_t old_len, size_t num,
  187. \& const char *file, int line);
  188. \& void CRYPTO_clear_free(void *str, size_t num, const char *, int);
  189. \&
  190. \& typedef void *(*CRYPTO_malloc_fn)(size_t num, const char *file, int line);
  191. \& typedef void *(*CRYPTO_realloc_fn)(void *addr, size_t num, const char *file,
  192. \& int line);
  193. \& typedef void (*CRYPTO_free_fn)(void *addr, const char *file, int line);
  194. \& void CRYPTO_get_mem_functions(CRYPTO_malloc_fn *malloc_fn,
  195. \& CRYPTO_realloc_fn *realloc_fn,
  196. \& CRYPTO_free_fn *free_fn);
  197. \& int CRYPTO_set_mem_functions(CRYPTO_malloc_fn malloc_fn,
  198. \& CRYPTO_realloc_fn realloc_fn,
  199. \& CRYPTO_free_fn free_fn);
  200. \&
  201. \& void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount);
  202. \&
  203. \& env OPENSSL_MALLOC_FAILURES=... <application>
  204. \& env OPENSSL_MALLOC_FD=... <application>
  205. .Ve
  206. .PP
  207. The following functions have been deprecated since OpenSSL 3.0, and can be
  208. hidden entirely by defining \fB\s-1OPENSSL_API_COMPAT\s0\fR with a suitable version value,
  209. see \fBopenssl_user_macros\fR\|(7):
  210. .PP
  211. .Vb 4
  212. \& int CRYPTO_mem_leaks(BIO *b);
  213. \& int CRYPTO_mem_leaks_fp(FILE *fp);
  214. \& int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u),
  215. \& void *u);
  216. \&
  217. \& int CRYPTO_set_mem_debug(int onoff);
  218. \& int CRYPTO_mem_ctrl(int mode);
  219. \& int OPENSSL_mem_debug_push(const char *info);
  220. \& int OPENSSL_mem_debug_pop(void);
  221. \& int CRYPTO_mem_debug_push(const char *info, const char *file, int line);
  222. \& int CRYPTO_mem_debug_pop(void);
  223. .Ve
  224. .SH "DESCRIPTION"
  225. .IX Header "DESCRIPTION"
  226. OpenSSL memory allocation is handled by the \fBOPENSSL_xxx\fR \s-1API.\s0 These are
  227. generally macro's that add the standard C \fB_\|_FILE_\|_\fR and \fB_\|_LINE_\|_\fR
  228. parameters and call a lower-level \fBCRYPTO_xxx\fR \s-1API.\s0
  229. Some functions do not add those parameters, but exist for consistency.
  230. .PP
  231. \&\fBOPENSSL_malloc_init()\fR does nothing and does not need to be called. It is
  232. included for compatibility with older versions of OpenSSL.
  233. .PP
  234. \&\fBOPENSSL_malloc()\fR, \fBOPENSSL_realloc()\fR, and \fBOPENSSL_free()\fR are like the
  235. C \fBmalloc()\fR, \fBrealloc()\fR, and \fBfree()\fR functions.
  236. \&\fBOPENSSL_zalloc()\fR calls \fBmemset()\fR to zero the memory before returning.
  237. .PP
  238. \&\fBOPENSSL_clear_realloc()\fR and \fBOPENSSL_clear_free()\fR should be used
  239. when the buffer at \fBaddr\fR holds sensitive information.
  240. The old buffer is filled with zero's by calling \fBOPENSSL_cleanse()\fR
  241. before ultimately calling \fBOPENSSL_free()\fR. If the argument to \fBOPENSSL_free()\fR is
  242. \&\s-1NULL,\s0 nothing is done.
  243. .PP
  244. \&\fBOPENSSL_cleanse()\fR fills \fBptr\fR of size \fBlen\fR with a string of 0's.
  245. Use \fBOPENSSL_cleanse()\fR with care if the memory is a mapping of a file.
  246. If the storage controller uses write compression, then it's possible
  247. that sensitive tail bytes will survive zeroization because the block of
  248. zeros will be compressed. If the storage controller uses wear leveling,
  249. then the old sensitive data will not be overwritten; rather, a block of
  250. 0's will be written at a new physical location.
  251. .PP
  252. \&\fBOPENSSL_strdup()\fR, \fBOPENSSL_strndup()\fR and \fBOPENSSL_memdup()\fR are like the
  253. equivalent C functions, except that memory is allocated by calling the
  254. \&\fBOPENSSL_malloc()\fR and should be released by calling \fBOPENSSL_free()\fR.
  255. .PP
  256. \&\fBOPENSSL_strlcpy()\fR,
  257. \&\fBOPENSSL_strlcat()\fR and \fBOPENSSL_strnlen()\fR are equivalents of the common C
  258. library functions and are provided for portability.
  259. .PP
  260. If no allocations have been done, it is possible to \*(L"swap out\*(R" the default
  261. implementations for \fBOPENSSL_malloc()\fR, \fBOPENSSL_realloc()\fR and \fBOPENSSL_free()\fR
  262. and replace them with alternate versions.
  263. \&\fBCRYPTO_get_mem_functions()\fR function fills in the given arguments with the
  264. function pointers for the current implementations.
  265. With \fBCRYPTO_set_mem_functions()\fR, you can specify a different set of functions.
  266. If any of \fBmalloc_fn\fR, \fBrealloc_fn\fR, or \fBfree_fn\fR are \s-1NULL,\s0 then
  267. the function is not changed.
  268. While it's permitted to swap out only a few and not all the functions
  269. with \fBCRYPTO_set_mem_functions()\fR, it's recommended to swap them all out
  270. at once.
  271. .PP
  272. If the library is built with the \f(CW\*(C`crypto\-mdebug\*(C'\fR option, then one
  273. function, \fBCRYPTO_get_alloc_counts()\fR, and two additional environment
  274. variables, \fB\s-1OPENSSL_MALLOC_FAILURES\s0\fR and \fB\s-1OPENSSL_MALLOC_FD\s0\fR,
  275. are available.
  276. .PP
  277. The function \fBCRYPTO_get_alloc_counts()\fR fills in the number of times
  278. each of \fBCRYPTO_malloc()\fR, \fBCRYPTO_realloc()\fR, and \fBCRYPTO_free()\fR have been
  279. called, into the values pointed to by \fBmcount\fR, \fBrcount\fR, and \fBfcount\fR,
  280. respectively. If a pointer is \s-1NULL,\s0 then the corresponding count is not stored.
  281. .PP
  282. The variable
  283. \&\fB\s-1OPENSSL_MALLOC_FAILURES\s0\fR controls how often allocations should fail.
  284. It is a set of fields separated by semicolons, which each field is a count
  285. (defaulting to zero) and an optional atsign and percentage (defaulting
  286. to 100). If the count is zero, then it lasts forever. For example,
  287. \&\f(CW\*(C`100;@25\*(C'\fR or \f(CW\*(C`100@0;0@25\*(C'\fR means the first 100 allocations pass, then all
  288. other allocations (until the program exits or crashes) have a 25% chance of
  289. failing.
  290. .PP
  291. If the variable \fB\s-1OPENSSL_MALLOC_FD\s0\fR is parsed as a positive integer, then
  292. it is taken as an open file descriptor. This is used in conjunction with
  293. \&\fB\s-1OPENSSL_MALLOC_FAILURES\s0\fR described above. For every allocation it will log
  294. details about how many allocations there have been so far, what percentage
  295. chance there is for this allocation failing, and whether it has actually failed.
  296. The following example in classic shell syntax shows how to use this (will not
  297. work on all platforms):
  298. .PP
  299. .Vb 5
  300. \& OPENSSL_MALLOC_FAILURES=\*(Aq200;@10\*(Aq
  301. \& export OPENSSL_MALLOC_FAILURES
  302. \& OPENSSL_MALLOC_FD=3
  303. \& export OPENSSL_MALLOC_FD
  304. \& ...app invocation... 3>/tmp/log$$
  305. .Ve
  306. .SH "RETURN VALUES"
  307. .IX Header "RETURN VALUES"
  308. \&\fBOPENSSL_malloc_init()\fR, \fBOPENSSL_free()\fR, \fBOPENSSL_clear_free()\fR
  309. \&\fBCRYPTO_free()\fR, \fBCRYPTO_clear_free()\fR and \fBCRYPTO_get_mem_functions()\fR
  310. return no value.
  311. .PP
  312. \&\fBOPENSSL_malloc()\fR, \fBOPENSSL_zalloc()\fR, \fBOPENSSL_realloc()\fR,
  313. \&\fBOPENSSL_clear_realloc()\fR,
  314. \&\fBCRYPTO_malloc()\fR, \fBCRYPTO_zalloc()\fR, \fBCRYPTO_realloc()\fR,
  315. \&\fBCRYPTO_clear_realloc()\fR,
  316. \&\fBOPENSSL_strdup()\fR, and \fBOPENSSL_strndup()\fR
  317. return a pointer to allocated memory or \s-1NULL\s0 on error.
  318. .PP
  319. \&\fBCRYPTO_set_mem_functions()\fR returns 1 on success or 0 on failure (almost
  320. always because allocations have already happened).
  321. .PP
  322. \&\fBCRYPTO_mem_leaks()\fR, \fBCRYPTO_mem_leaks_fp()\fR, \fBCRYPTO_mem_leaks_cb()\fR,
  323. \&\fBCRYPTO_set_mem_debug()\fR, and \fBCRYPTO_mem_ctrl()\fR are deprecated and are no-ops that
  324. always return \-1.
  325. \&\fBOPENSSL_mem_debug_push()\fR, \fBOPENSSL_mem_debug_pop()\fR,
  326. \&\fBCRYPTO_mem_debug_push()\fR, and \fBCRYPTO_mem_debug_pop()\fR
  327. are deprecated and are no-ops that always return 0.
  328. .SH "HISTORY"
  329. .IX Header "HISTORY"
  330. \&\fBOPENSSL_mem_debug_push()\fR, \fBOPENSSL_mem_debug_pop()\fR,
  331. \&\fBCRYPTO_mem_debug_push()\fR, \fBCRYPTO_mem_debug_pop()\fR,
  332. \&\fBCRYPTO_mem_leaks()\fR, \fBCRYPTO_mem_leaks_fp()\fR,
  333. \&\fBCRYPTO_mem_leaks_cb()\fR, \fBCRYPTO_set_mem_debug()\fR, \fBCRYPTO_mem_ctrl()\fR
  334. were deprecated in OpenSSL 3.0.
  335. The memory-leak checking has been deprecated in OpenSSL 3.0 in favor of
  336. clang's memory and leak sanitizer.
  337. .SH "COPYRIGHT"
  338. .IX Header "COPYRIGHT"
  339. Copyright 2016\-2024 The OpenSSL Project Authors. All Rights Reserved.
  340. .PP
  341. Licensed under the Apache License 2.0 (the \*(L"License\*(R"). You may not use
  342. this file except in compliance with the License. You can obtain a copy
  343. in the file \s-1LICENSE\s0 in the source distribution or at
  344. <https://www.openssl.org/source/license.html>.