xref: /openssl/doc/man3/OPENSSL_malloc.pod (revision 7ed6de99)
1=pod
2
3=head1 NAME
4
5OPENSSL_malloc_init,
6OPENSSL_malloc, OPENSSL_aligned_alloc, OPENSSL_zalloc, OPENSSL_realloc,
7OPENSSL_free, OPENSSL_clear_realloc, OPENSSL_clear_free, OPENSSL_cleanse,
8CRYPTO_malloc, CRYPTO_aligned_alloc, CRYPTO_zalloc, CRYPTO_realloc, CRYPTO_free,
9OPENSSL_strdup, OPENSSL_strndup,
10OPENSSL_memdup, OPENSSL_strlcpy, OPENSSL_strlcat, OPENSSL_strtoul,
11CRYPTO_strdup, CRYPTO_strndup,
12OPENSSL_mem_debug_push, OPENSSL_mem_debug_pop,
13CRYPTO_mem_debug_push, CRYPTO_mem_debug_pop,
14CRYPTO_clear_realloc, CRYPTO_clear_free,
15CRYPTO_malloc_fn, CRYPTO_realloc_fn, CRYPTO_free_fn,
16CRYPTO_get_mem_functions, CRYPTO_set_mem_functions,
17CRYPTO_get_alloc_counts,
18CRYPTO_set_mem_debug, CRYPTO_mem_ctrl,
19CRYPTO_mem_leaks, CRYPTO_mem_leaks_fp, CRYPTO_mem_leaks_cb,
20OPENSSL_MALLOC_FAILURES,
21OPENSSL_MALLOC_FD
22- Memory allocation functions
23
24=head1 SYNOPSIS
25
26 #include <openssl/crypto.h>
27
28 int OPENSSL_malloc_init(void);
29
30 void *OPENSSL_malloc(size_t num);
31 void *OPENSSL_aligned_alloc(size_t num, size_t alignment, void **freeptr);
32 void *OPENSSL_zalloc(size_t num);
33 void *OPENSSL_realloc(void *addr, size_t num);
34 void OPENSSL_free(void *addr);
35 char *OPENSSL_strdup(const char *str);
36 char *OPENSSL_strndup(const char *str, size_t s);
37 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size);
38 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size);
39 int OPENSSL_strtoul(char *src, char **endptr, int base, unsigned long *num);
40 void *OPENSSL_memdup(void *data, size_t s);
41 void *OPENSSL_clear_realloc(void *p, size_t old_len, size_t num);
42 void OPENSSL_clear_free(void *str, size_t num);
43 void OPENSSL_cleanse(void *ptr, size_t len);
44
45 void *CRYPTO_malloc(size_t num, const char *file, int line);
46 void *CRYPTO_aligned_alloc(size_t num, size_t align, void **freeptr,
47                            const char *file, int line);
48 void *CRYPTO_zalloc(size_t num, const char *file, int line);
49 void *CRYPTO_realloc(void *p, size_t num, const char *file, int line);
50 void CRYPTO_free(void *str, const char *, int);
51 char *CRYPTO_strdup(const char *p, const char *file, int line);
52 char *CRYPTO_strndup(const char *p, size_t num, const char *file, int line);
53 void *CRYPTO_clear_realloc(void *p, size_t old_len, size_t num,
54                            const char *file, int line);
55 void CRYPTO_clear_free(void *str, size_t num, const char *, int);
56
57 typedef void *(*CRYPTO_malloc_fn)(size_t num, const char *file, int line);
58 typedef void *(*CRYPTO_realloc_fn)(void *addr, size_t num, const char *file,
59                                    int line);
60 typedef void (*CRYPTO_free_fn)(void *addr, const char *file, int line);
61 void CRYPTO_get_mem_functions(CRYPTO_malloc_fn *malloc_fn,
62                               CRYPTO_realloc_fn *realloc_fn,
63                               CRYPTO_free_fn *free_fn);
64 int CRYPTO_set_mem_functions(CRYPTO_malloc_fn malloc_fn,
65                              CRYPTO_realloc_fn realloc_fn,
66                              CRYPTO_free_fn free_fn);
67
68 void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount);
69
70 env OPENSSL_MALLOC_FAILURES=... <application>
71 env OPENSSL_MALLOC_FD=... <application>
72
73The following functions have been deprecated since OpenSSL 3.0, and can be
74hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
75see L<openssl_user_macros(7)>:
76
77 int CRYPTO_mem_leaks(BIO *b);
78 int CRYPTO_mem_leaks_fp(FILE *fp);
79 int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u),
80                         void *u);
81
82 int CRYPTO_set_mem_debug(int onoff);
83 int CRYPTO_mem_ctrl(int mode);
84 int OPENSSL_mem_debug_push(const char *info);
85 int OPENSSL_mem_debug_pop(void);
86 int CRYPTO_mem_debug_push(const char *info, const char *file, int line);
87 int CRYPTO_mem_debug_pop(void);
88
89=head1 DESCRIPTION
90
91OpenSSL memory allocation is handled by the B<OPENSSL_xxx> API. These are
92generally macro's that add the standard C B<__FILE__> and B<__LINE__>
93parameters and call a lower-level B<CRYPTO_xxx> API.
94Some functions do not add those parameters, but exist for consistency.
95
96OPENSSL_malloc_init() does nothing and does not need to be called. It is
97included for compatibility with older versions of OpenSSL.
98
99OPENSSL_malloc(), OPENSSL_realloc(), and OPENSSL_free() are like the
100C malloc(), realloc(), and free() functions.
101OPENSSL_zalloc() calls memset() to zero the memory before returning.
102
103OPENSSL_aligned_alloc() operates just as OPENSSL_malloc does, but it
104allows for the caller to specify an alignment value, for instances in
105which the default alignment of malloc is insufficient for the callers
106needs.  Note, the alignment value must be a power of 2, and the size
107specified must be a multiple of the alignment.
108NOTE: The call to OPENSSL_aligned_alloc() accepts a 3rd argument, I<freeptr>
109which must point to a void pointer.  On some platforms, there is no available
110library call to obtain memory allocations greater than what malloc provides.  In
111this case, OPENSSL_aligned_alloc implements its own alignment routine,
112allocating additional memory and offsetting the returned pointer to be on the
113requested alignment boundary.  In order to safely free allocations made by this
114method, the caller must return the value in the I<freeptr> variable, rather than
115the returned pointer.
116
117OPENSSL_clear_realloc() and OPENSSL_clear_free() should be used
118when the buffer at B<addr> holds sensitive information.
119The old buffer is filled with zero's by calling OPENSSL_cleanse()
120before ultimately calling OPENSSL_free(). If the argument to OPENSSL_free() is
121NULL, nothing is done.
122
123OPENSSL_cleanse() fills B<ptr> of size B<len> with a string of 0's.
124Use OPENSSL_cleanse() with care if the memory is a mapping of a file.
125If the storage controller uses write compression, then it's possible
126that sensitive tail bytes will survive zeroization because the block of
127zeros will be compressed. If the storage controller uses wear leveling,
128then the old sensitive data will not be overwritten; rather, a block of
1290's will be written at a new physical location.
130
131OPENSSL_strdup(), OPENSSL_strndup() and OPENSSL_memdup() are like the
132equivalent C functions, except that memory is allocated by calling the
133OPENSSL_malloc() and should be released by calling OPENSSL_free().
134
135OPENSSL_strlcpy(),
136OPENSSL_strlcat() and OPENSSL_strnlen() are equivalents of the common C
137library functions and are provided for portability.
138
139OPENSSL_strtoul() is a wrapper around the POSIX function strtoul, with the same
140behaviors listed in the POSIX documentation, with the additional behavior that
141it validates the input I<str> and I<num> parameters for not being NULL, and confirms
142that at least a single byte of input has been consumed in the translation,
143returning an error in the event that no bytes were consumed.
144
145If no allocations have been done, it is possible to "swap out" the default
146implementations for OPENSSL_malloc(), OPENSSL_realloc() and OPENSSL_free()
147and replace them with alternate versions.
148CRYPTO_get_mem_functions() function fills in the given arguments with the
149function pointers for the current implementations.
150With CRYPTO_set_mem_functions(), you can specify a different set of functions.
151If any of B<malloc_fn>, B<realloc_fn>, or B<free_fn> are NULL, then
152the function is not changed.
153While it's permitted to swap out only a few and not all the functions
154with CRYPTO_set_mem_functions(), it's recommended to swap them all out
155at once.
156
157If the library is built with the C<crypto-mdebug> option, then one
158function, CRYPTO_get_alloc_counts(), and two additional environment
159variables, B<OPENSSL_MALLOC_FAILURES> and B<OPENSSL_MALLOC_FD>,
160are available.
161
162The function CRYPTO_get_alloc_counts() fills in the number of times
163each of CRYPTO_malloc(), CRYPTO_realloc(), and CRYPTO_free() have been
164called, into the values pointed to by B<mcount>, B<rcount>, and B<fcount>,
165respectively.  If a pointer is NULL, then the corresponding count is not stored.
166
167The variable
168B<OPENSSL_MALLOC_FAILURES> controls how often allocations should fail.
169It is a set of fields separated by semicolons, which each field is a count
170(defaulting to zero) and an optional atsign and percentage (defaulting
171to 100).  If the count is zero, then it lasts forever.  For example,
172C<100;@25> or C<100@0;0@25> means the first 100 allocations pass, then all
173other allocations (until the program exits or crashes) have a 25% chance of
174failing.
175
176If the variable B<OPENSSL_MALLOC_FD> is parsed as a positive integer, then
177it is taken as an open file descriptor. This is used in conjunction with
178B<OPENSSL_MALLOC_FAILURES> described above. For every allocation it will log
179details about how many allocations there have been so far, what percentage
180chance there is for this allocation failing, and whether it has actually failed.
181The following example in classic shell syntax shows how to use this (will not
182work on all platforms):
183
184  OPENSSL_MALLOC_FAILURES='200;@10'
185  export OPENSSL_MALLOC_FAILURES
186  OPENSSL_MALLOC_FD=3
187  export OPENSSL_MALLOC_FD
188  ...app invocation... 3>/tmp/log$$
189
190=head1 RETURN VALUES
191
192OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free()
193CRYPTO_free(), CRYPTO_clear_free() and CRYPTO_get_mem_functions()
194return no value.
195
196OPENSSL_malloc(), OPENSSL_aligned_alloc(), OPENSSL_zalloc(), OPENSSL_realloc(),
197OPENSSL_clear_realloc(),
198CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(),
199CRYPTO_clear_realloc(),
200OPENSSL_strdup(), and OPENSSL_strndup()
201return a pointer to allocated memory or NULL on error.
202
203CRYPTO_set_mem_functions() returns 1 on success or 0 on failure (almost
204always because allocations have already happened).
205
206CRYPTO_mem_leaks(), CRYPTO_mem_leaks_fp(), CRYPTO_mem_leaks_cb(),
207CRYPTO_set_mem_debug(), and CRYPTO_mem_ctrl() are deprecated and are no-ops that
208always return -1.
209OPENSSL_mem_debug_push(), OPENSSL_mem_debug_pop(),
210CRYPTO_mem_debug_push(), and CRYPTO_mem_debug_pop()
211are deprecated and are no-ops that always return 0.
212
213OPENSSL_strtoul() returns 1 on success and 0 in the event that an error has
214occurred. Specifically, 0 is returned in the following events:
215
216=over 4
217
218=item *
219
220If the underlying call to strtoul returned a non zero errno value
221
222=item *
223
224If the translation did not consume the entire input string, and the passed
225endptr value was NULL
226
227=item *
228
229If no characters were consumed in the translation
230
231=back
232
233Note that a success condition does not imply that the expected
234translation has been performed.  For instance calling
235
236    OPENSSL_strtoul("0x12345", &endptr, 10, &num);
237
238will result in a successful translation with num having the value 0, and
239*endptr = 'x'.  Be sure to validate how much data was consumed when calling this
240function.
241
242=head1 HISTORY
243
244OPENSSL_mem_debug_push(), OPENSSL_mem_debug_pop(),
245CRYPTO_mem_debug_push(), CRYPTO_mem_debug_pop(),
246CRYPTO_mem_leaks(), CRYPTO_mem_leaks_fp(),
247CRYPTO_mem_leaks_cb(), CRYPTO_set_mem_debug(), CRYPTO_mem_ctrl()
248were deprecated in OpenSSL 3.0.
249The memory-leak checking has been deprecated in OpenSSL 3.0 in favor of
250clang's memory and leak sanitizer.
251OPENSSL_aligned_alloc(), CRYPTO_aligned_alloc() were added in OpenSSL 3.4.0
252
253=head1 COPYRIGHT
254
255Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
256
257Licensed under the Apache License 2.0 (the "License").  You may not use
258this file except in compliance with the License.  You can obtain a copy
259in the file LICENSE in the source distribution or at
260L<https://www.openssl.org/source/license.html>.
261
262=cut
263