xref: /freebsd/crypto/openssl/doc/man3/OPENSSL_malloc.pod (revision 6935a639f0f999de98b970a3cf26b0dc80b1798b)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5e71b7053SJung-uk KimOPENSSL_malloc_init,
6e71b7053SJung-uk KimOPENSSL_malloc, OPENSSL_zalloc, OPENSSL_realloc, OPENSSL_free,
7e71b7053SJung-uk KimOPENSSL_clear_realloc, OPENSSL_clear_free, OPENSSL_cleanse,
8e71b7053SJung-uk KimCRYPTO_malloc, CRYPTO_zalloc, CRYPTO_realloc, CRYPTO_free,
9e71b7053SJung-uk KimOPENSSL_strdup, OPENSSL_strndup,
10e71b7053SJung-uk KimOPENSSL_memdup, OPENSSL_strlcpy, OPENSSL_strlcat,
11e71b7053SJung-uk KimOPENSSL_hexstr2buf, OPENSSL_buf2hexstr, OPENSSL_hexchar2int,
12e71b7053SJung-uk KimCRYPTO_strdup, CRYPTO_strndup,
13e71b7053SJung-uk KimOPENSSL_mem_debug_push, OPENSSL_mem_debug_pop,
14e71b7053SJung-uk KimCRYPTO_mem_debug_push, CRYPTO_mem_debug_pop,
15e71b7053SJung-uk KimCRYPTO_clear_realloc, CRYPTO_clear_free,
16e71b7053SJung-uk KimCRYPTO_get_mem_functions, CRYPTO_set_mem_functions,
17e71b7053SJung-uk KimCRYPTO_get_alloc_counts,
18e71b7053SJung-uk KimCRYPTO_set_mem_debug, CRYPTO_mem_ctrl,
19e71b7053SJung-uk KimCRYPTO_mem_leaks, CRYPTO_mem_leaks_fp, CRYPTO_mem_leaks_cb,
20e71b7053SJung-uk KimOPENSSL_MALLOC_FAILURES,
21e71b7053SJung-uk KimOPENSSL_MALLOC_FD
22e71b7053SJung-uk Kim- Memory allocation functions
23e71b7053SJung-uk Kim
24e71b7053SJung-uk Kim=head1 SYNOPSIS
25e71b7053SJung-uk Kim
26e71b7053SJung-uk Kim #include <openssl/crypto.h>
27e71b7053SJung-uk Kim
28e71b7053SJung-uk Kim int OPENSSL_malloc_init(void)
29e71b7053SJung-uk Kim
30e71b7053SJung-uk Kim void *OPENSSL_malloc(size_t num)
31e71b7053SJung-uk Kim void *OPENSSL_zalloc(size_t num)
32e71b7053SJung-uk Kim void *OPENSSL_realloc(void *addr, size_t num)
33e71b7053SJung-uk Kim void OPENSSL_free(void *addr)
34e71b7053SJung-uk Kim char *OPENSSL_strdup(const char *str)
35e71b7053SJung-uk Kim char *OPENSSL_strndup(const char *str, size_t s)
36e71b7053SJung-uk Kim size_t OPENSSL_strlcat(char *dst, const char *src, size_t size);
37e71b7053SJung-uk Kim size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size);
38e71b7053SJung-uk Kim void *OPENSSL_memdup(void *data, size_t s)
39e71b7053SJung-uk Kim void *OPENSSL_clear_realloc(void *p, size_t old_len, size_t num)
40e71b7053SJung-uk Kim void OPENSSL_clear_free(void *str, size_t num)
41e71b7053SJung-uk Kim void OPENSSL_cleanse(void *ptr, size_t len);
42e71b7053SJung-uk Kim
43e71b7053SJung-uk Kim unsigned char *OPENSSL_hexstr2buf(const char *str, long *len);
44e71b7053SJung-uk Kim char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len);
45e71b7053SJung-uk Kim int OPENSSL_hexchar2int(unsigned char c);
46e71b7053SJung-uk Kim
47e71b7053SJung-uk Kim void *CRYPTO_malloc(size_t num, const char *file, int line)
48e71b7053SJung-uk Kim void *CRYPTO_zalloc(size_t num, const char *file, int line)
49e71b7053SJung-uk Kim void *CRYPTO_realloc(void *p, size_t num, const char *file, int line)
50e71b7053SJung-uk Kim void CRYPTO_free(void *str, const char *, int)
51e71b7053SJung-uk Kim char *CRYPTO_strdup(const char *p, const char *file, int line)
52e71b7053SJung-uk Kim char *CRYPTO_strndup(const char *p, size_t num, const char *file, int line)
53e71b7053SJung-uk Kim void *CRYPTO_clear_realloc(void *p, size_t old_len, size_t num,
54e71b7053SJung-uk Kim                            const char *file, int line)
55e71b7053SJung-uk Kim void CRYPTO_clear_free(void *str, size_t num, const char *, int)
56e71b7053SJung-uk Kim
57e71b7053SJung-uk Kim void CRYPTO_get_mem_functions(
58e71b7053SJung-uk Kim         void *(**m)(size_t, const char *, int),
59e71b7053SJung-uk Kim         void *(**r)(void *, size_t, const char *, int),
60e71b7053SJung-uk Kim         void (**f)(void *, const char *, int))
61e71b7053SJung-uk Kim int CRYPTO_set_mem_functions(
62e71b7053SJung-uk Kim         void *(*m)(size_t, const char *, int),
63e71b7053SJung-uk Kim         void *(*r)(void *, size_t, const char *, int),
64e71b7053SJung-uk Kim         void (*f)(void *, const char *, int))
65e71b7053SJung-uk Kim
66e71b7053SJung-uk Kim void CRYPTO_get_alloc_counts(int *m, int *r, int *f)
67e71b7053SJung-uk Kim
68e71b7053SJung-uk Kim int CRYPTO_set_mem_debug(int onoff)
69e71b7053SJung-uk Kim
70e71b7053SJung-uk Kim env OPENSSL_MALLOC_FAILURES=... <application>
71e71b7053SJung-uk Kim env OPENSSL_MALLOC_FD=... <application>
72e71b7053SJung-uk Kim
73e71b7053SJung-uk Kim int CRYPTO_mem_ctrl(int mode);
74e71b7053SJung-uk Kim
75e71b7053SJung-uk Kim int OPENSSL_mem_debug_push(const char *info)
76e71b7053SJung-uk Kim int OPENSSL_mem_debug_pop(void);
77e71b7053SJung-uk Kim
78e71b7053SJung-uk Kim int CRYPTO_mem_debug_push(const char *info, const char *file, int line);
79e71b7053SJung-uk Kim int CRYPTO_mem_debug_pop(void);
80e71b7053SJung-uk Kim
81e71b7053SJung-uk Kim int CRYPTO_mem_leaks(BIO *b);
82e71b7053SJung-uk Kim int CRYPTO_mem_leaks_fp(FILE *fp);
83e71b7053SJung-uk Kim int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u),
84e71b7053SJung-uk Kim                         void *u);
85e71b7053SJung-uk Kim
86e71b7053SJung-uk Kim=head1 DESCRIPTION
87e71b7053SJung-uk Kim
88e71b7053SJung-uk KimOpenSSL memory allocation is handled by the B<OPENSSL_xxx> API. These are
89e71b7053SJung-uk Kimgenerally macro's that add the standard C B<__FILE__> and B<__LINE__>
90e71b7053SJung-uk Kimparameters and call a lower-level B<CRYPTO_xxx> API.
91e71b7053SJung-uk KimSome functions do not add those parameters, but exist for consistency.
92e71b7053SJung-uk Kim
93*6935a639SJung-uk KimOPENSSL_malloc_init() does nothing and does not need to be called. It is
94*6935a639SJung-uk Kimincluded for compatibility with older versions of OpenSSL.
95e71b7053SJung-uk Kim
96e71b7053SJung-uk KimOPENSSL_malloc(), OPENSSL_realloc(), and OPENSSL_free() are like the
97e71b7053SJung-uk KimC malloc(), realloc(), and free() functions.
98e71b7053SJung-uk KimOPENSSL_zalloc() calls memset() to zero the memory before returning.
99e71b7053SJung-uk Kim
100e71b7053SJung-uk KimOPENSSL_clear_realloc() and OPENSSL_clear_free() should be used
101e71b7053SJung-uk Kimwhen the buffer at B<addr> holds sensitive information.
102e71b7053SJung-uk KimThe old buffer is filled with zero's by calling OPENSSL_cleanse()
103e71b7053SJung-uk Kimbefore ultimately calling OPENSSL_free().
104e71b7053SJung-uk Kim
105e71b7053SJung-uk KimOPENSSL_cleanse() fills B<ptr> of size B<len> with a string of 0's.
106e71b7053SJung-uk KimUse OPENSSL_cleanse() with care if the memory is a mapping of a file.
107e71b7053SJung-uk KimIf the storage controller uses write compression, then its possible
108e71b7053SJung-uk Kimthat sensitive tail bytes will survive zeroization because the block of
109e71b7053SJung-uk Kimzeros will be compressed. If the storage controller uses wear leveling,
110e71b7053SJung-uk Kimthen the old sensitive data will not be overwritten; rather, a block of
111e71b7053SJung-uk Kim0's will be written at a new physical location.
112e71b7053SJung-uk Kim
113e71b7053SJung-uk KimOPENSSL_strdup(), OPENSSL_strndup() and OPENSSL_memdup() are like the
114e71b7053SJung-uk Kimequivalent C functions, except that memory is allocated by calling the
115e71b7053SJung-uk KimOPENSSL_malloc() and should be released by calling OPENSSL_free().
116e71b7053SJung-uk Kim
117e71b7053SJung-uk KimOPENSSL_strlcpy(),
118e71b7053SJung-uk KimOPENSSL_strlcat() and OPENSSL_strnlen() are equivalents of the common C
119e71b7053SJung-uk Kimlibrary functions and are provided for portability.
120e71b7053SJung-uk Kim
121e71b7053SJung-uk KimOPENSSL_hexstr2buf() parses B<str> as a hex string and returns a
122e71b7053SJung-uk Kimpointer to the parsed value. The memory is allocated by calling
123e71b7053SJung-uk KimOPENSSL_malloc() and should be released by calling OPENSSL_free().
124e71b7053SJung-uk KimIf B<len> is not NULL, it is filled in with the output length.
125e71b7053SJung-uk KimColons between two-character hex "bytes" are ignored.
126e71b7053SJung-uk KimAn odd number of hex digits is an error.
127e71b7053SJung-uk Kim
128e71b7053SJung-uk KimOPENSSL_buf2hexstr() takes the specified buffer and length, and returns
129e71b7053SJung-uk Kima hex string for value, or NULL on error.
130e71b7053SJung-uk KimB<Buffer> cannot be NULL; if B<len> is 0 an empty string is returned.
131e71b7053SJung-uk Kim
132e71b7053SJung-uk KimOPENSSL_hexchar2int() converts a character to the hexadecimal equivalent,
133e71b7053SJung-uk Kimor returns -1 on error.
134e71b7053SJung-uk Kim
135e71b7053SJung-uk KimIf no allocations have been done, it is possible to "swap out" the default
136e71b7053SJung-uk Kimimplementations for OPENSSL_malloc(), OPENSSL_realloc and OPENSSL_free()
137e71b7053SJung-uk Kimand replace them with alternate versions (hooks).
138e71b7053SJung-uk KimCRYPTO_get_mem_functions() function fills in the given arguments with the
139e71b7053SJung-uk Kimfunction pointers for the current implementations.
140e71b7053SJung-uk KimWith CRYPTO_set_mem_functions(), you can specify a different set of functions.
141e71b7053SJung-uk KimIf any of B<m>, B<r>, or B<f> are NULL, then the function is not changed.
142e71b7053SJung-uk Kim
143e71b7053SJung-uk KimThe default implementation can include some debugging capability (if enabled
144e71b7053SJung-uk Kimat build-time).
145e71b7053SJung-uk KimThis adds some overhead by keeping a list of all memory allocations, and
146e71b7053SJung-uk Kimremoves items from the list when they are free'd.
147e71b7053SJung-uk KimThis is most useful for identifying memory leaks.
148e71b7053SJung-uk KimCRYPTO_set_mem_debug() turns this tracking on and off.  In order to have
149e71b7053SJung-uk Kimany effect, is must be called before any of the allocation functions
150e71b7053SJung-uk Kim(e.g., CRYPTO_malloc()) are called, and is therefore normally one of the
151e71b7053SJung-uk Kimfirst lines of main() in an application.
152e71b7053SJung-uk KimCRYPTO_mem_ctrl() provides fine-grained control of memory leak tracking.
153e71b7053SJung-uk KimTo enable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
154e71b7053SJung-uk Kimthe B<CRYPTO_MEM_CHECK_ON>.
155e71b7053SJung-uk KimTo disable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
156e71b7053SJung-uk Kimthe B<CRYPTO_MEM_CHECK_OFF>.
157e71b7053SJung-uk Kim
158e71b7053SJung-uk KimWhile checking memory, it can be useful to store additional context
159e71b7053SJung-uk Kimabout what is being done.
160e71b7053SJung-uk KimFor example, identifying the field names when parsing a complicated
161e71b7053SJung-uk Kimdata structure.
162e71b7053SJung-uk KimOPENSSL_mem_debug_push() (which calls CRYPTO_mem_debug_push())
163e71b7053SJung-uk Kimattachs an identifying string to the allocation stack.
164e71b7053SJung-uk KimThis must be a global or other static string; it is not copied.
165e71b7053SJung-uk KimOPENSSL_mem_debug_pop() removes identifying state from the stack.
166e71b7053SJung-uk Kim
167e71b7053SJung-uk KimAt the end of the program, calling CRYPTO_mem_leaks() or
168e71b7053SJung-uk KimCRYPTO_mem_leaks_fp() will report all "leaked" memory, writing it
169e71b7053SJung-uk Kimto the specified BIO B<b> or FILE B<fp>. These functions return 1 if
170e71b7053SJung-uk Kimthere are no leaks, 0 if there are leaks and -1 if an error occurred.
171e71b7053SJung-uk Kim
172e71b7053SJung-uk KimCRYPTO_mem_leaks_cb() does the same as CRYPTO_mem_leaks(), but instead
173e71b7053SJung-uk Kimof writing to a given BIO, the callback function is called for each
174e71b7053SJung-uk Kimoutput string with the string, length, and userdata B<u> as the callback
175e71b7053SJung-uk Kimparameters.
176e71b7053SJung-uk Kim
177e71b7053SJung-uk KimIf the library is built with the C<crypto-mdebug> option, then one
178e71b7053SJung-uk Kimfunction, CRYPTO_get_alloc_counts(), and two additional environment
179e71b7053SJung-uk Kimvariables, B<OPENSSL_MALLOC_FAILURES> and B<OPENSSL_MALLOC_FD>,
180e71b7053SJung-uk Kimare available.
181e71b7053SJung-uk Kim
182e71b7053SJung-uk KimThe function CRYPTO_get_alloc_counts() fills in the number of times
183e71b7053SJung-uk Kimeach of CRYPTO_malloc(), CRYPTO_realloc(), and CRYPTO_free() have been
184e71b7053SJung-uk Kimcalled, into the values pointed to by B<mcount>, B<rcount>, and B<fcount>,
185e71b7053SJung-uk Kimrespectively.  If a pointer is NULL, then the corresponding count is not stored.
186e71b7053SJung-uk Kim
187e71b7053SJung-uk KimThe variable
188e71b7053SJung-uk KimB<OPENSSL_MALLOC_FAILURES> controls how often allocations should fail.
189e71b7053SJung-uk KimIt is a set of fields separated by semicolons, which each field is a count
190e71b7053SJung-uk Kim(defaulting to zero) and an optional atsign and percentage (defaulting
191e71b7053SJung-uk Kimto 100).  If the count is zero, then it lasts forever.  For example,
192e71b7053SJung-uk KimC<100;@25> or C<100@0;0@25> means the first 100 allocations pass, then all
193e71b7053SJung-uk Kimother allocations (until the program exits or crashes) have a 25% chance of
194e71b7053SJung-uk Kimfailing.
195e71b7053SJung-uk Kim
196e71b7053SJung-uk KimIf the variable B<OPENSSL_MALLOC_FD> is parsed as a positive integer, then
197e71b7053SJung-uk Kimit is taken as an open file descriptor, and a record of all allocations is
198e71b7053SJung-uk Kimwritten to that descriptor.  If an allocation will fail, and the platform
199e71b7053SJung-uk Kimsupports it, then a backtrace will be written to the descriptor.  This can
200e71b7053SJung-uk Kimbe useful because a malloc may fail but not be checked, and problems will
201e71b7053SJung-uk Kimonly occur later.  The following example in classic shell syntax shows how
202e71b7053SJung-uk Kimto use this (will not work on all platforms):
203e71b7053SJung-uk Kim
204e71b7053SJung-uk Kim  OPENSSL_MALLOC_FAILURES='200;@10'
205e71b7053SJung-uk Kim  export OPENSSL_MALLOC_FAILURES
206e71b7053SJung-uk Kim  OPENSSL_MALLOC_FD=3
207e71b7053SJung-uk Kim  export OPENSSL_MALLOC_FD
208e71b7053SJung-uk Kim  ...app invocation... 3>/tmp/log$$
209e71b7053SJung-uk Kim
210e71b7053SJung-uk Kim
211e71b7053SJung-uk Kim=head1 RETURN VALUES
212e71b7053SJung-uk Kim
213e71b7053SJung-uk KimOPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free()
214e71b7053SJung-uk KimCRYPTO_free(), CRYPTO_clear_free() and CRYPTO_get_mem_functions()
215e71b7053SJung-uk Kimreturn no value.
216e71b7053SJung-uk Kim
217e71b7053SJung-uk KimCRYPTO_mem_leaks(), CRYPTO_mem_leaks_fp() and CRYPTO_mem_leaks_cb() return 1 if
218e71b7053SJung-uk Kimthere are no leaks, 0 if there are leaks and -1 if an error occurred.
219e71b7053SJung-uk Kim
220e71b7053SJung-uk KimOPENSSL_malloc(), OPENSSL_zalloc(), OPENSSL_realloc(),
221e71b7053SJung-uk KimOPENSSL_clear_realloc(),
222e71b7053SJung-uk KimCRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(),
223e71b7053SJung-uk KimCRYPTO_clear_realloc(),
224e71b7053SJung-uk KimOPENSSL_buf2hexstr(), OPENSSL_hexstr2buf(),
225e71b7053SJung-uk KimOPENSSL_strdup(), and OPENSSL_strndup()
226e71b7053SJung-uk Kimreturn a pointer to allocated memory or NULL on error.
227e71b7053SJung-uk Kim
228e71b7053SJung-uk KimCRYPTO_set_mem_functions() and CRYPTO_set_mem_debug()
229e71b7053SJung-uk Kimreturn 1 on success or 0 on failure (almost
230e71b7053SJung-uk Kimalways because allocations have already happened).
231e71b7053SJung-uk Kim
232e71b7053SJung-uk KimCRYPTO_mem_ctrl() returns -1 if an error occurred, otherwise the
233e71b7053SJung-uk Kimprevious value of the mode.
234e71b7053SJung-uk Kim
235e71b7053SJung-uk KimOPENSSL_mem_debug_push() and OPENSSL_mem_debug_pop()
236e71b7053SJung-uk Kimreturn 1 on success or 0 on failure.
237e71b7053SJung-uk Kim
238e71b7053SJung-uk Kim=head1 NOTES
239e71b7053SJung-uk Kim
240e71b7053SJung-uk KimWhile it's permitted to swap out only a few and not all the functions
241e71b7053SJung-uk Kimwith CRYPTO_set_mem_functions(), it's recommended to swap them all out
242e71b7053SJung-uk Kimat once.  I<This applies specially if OpenSSL was built with the
243e71b7053SJung-uk Kimconfiguration option> C<crypto-mdebug> I<enabled.  In case, swapping out
244e71b7053SJung-uk Kimonly, say, the malloc() implementation is outright dangerous.>
245e71b7053SJung-uk Kim
246e71b7053SJung-uk Kim=head1 COPYRIGHT
247e71b7053SJung-uk Kim
248*6935a639SJung-uk KimCopyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
249e71b7053SJung-uk Kim
250e71b7053SJung-uk KimLicensed under the OpenSSL license (the "License").  You may not use
251e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
252e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
253e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
254e71b7053SJung-uk Kim
255e71b7053SJung-uk Kim=cut
256