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. 124It is useful in cases when it is needed to ensure that memory (that contains 125sensitive information) is overwritten (for example, before it is reclaimed, 126or when it is stored on stack), and such operation is not optimised out 127by compiler optimisations such as dead store elimination (as memset(3) may be). 128Use OPENSSL_cleanse() with care if the memory is a mapping of a file. 129If the storage controller uses write compression, then it's possible 130that sensitive tail bytes will survive zeroization because the block of 131zeros will be compressed. If the storage controller uses wear leveling, 132then the old sensitive data will not be overwritten; rather, a block of 1330's will be written at a new physical location. 134 135OPENSSL_strdup(), OPENSSL_strndup() and OPENSSL_memdup() are like the 136equivalent C functions, except that memory is allocated by calling the 137OPENSSL_malloc() and should be released by calling OPENSSL_free(). 138 139OPENSSL_strlcpy(), 140OPENSSL_strlcat() and OPENSSL_strnlen() are equivalents of the common C 141library functions and are provided for portability. 142 143OPENSSL_strtoul() is a wrapper around the POSIX function strtoul, with the same 144behaviors listed in the POSIX documentation, with the additional behavior that 145it validates the input I<str> and I<num> parameters for not being NULL, and confirms 146that at least a single byte of input has been consumed in the translation, 147returning an error in the event that no bytes were consumed. 148 149If no allocations have been done, it is possible to "swap out" the default 150implementations for OPENSSL_malloc(), OPENSSL_realloc() and OPENSSL_free() 151and replace them with alternate versions. 152CRYPTO_get_mem_functions() function fills in the given arguments with the 153function pointers for the current implementations. 154With CRYPTO_set_mem_functions(), you can specify a different set of functions. 155If any of B<malloc_fn>, B<realloc_fn>, or B<free_fn> are NULL, then 156the function is not changed. 157While it's permitted to swap out only a few and not all the functions 158with CRYPTO_set_mem_functions(), it's recommended to swap them all out 159at once. 160 161If the library is built with the C<crypto-mdebug> option, then one 162function, CRYPTO_get_alloc_counts(), and two additional environment 163variables, B<OPENSSL_MALLOC_FAILURES> and B<OPENSSL_MALLOC_FD>, 164are available. 165 166The function CRYPTO_get_alloc_counts() fills in the number of times 167each of CRYPTO_malloc(), CRYPTO_realloc(), and CRYPTO_free() have been 168called, into the values pointed to by B<mcount>, B<rcount>, and B<fcount>, 169respectively. If a pointer is NULL, then the corresponding count is not stored. 170 171The variable 172B<OPENSSL_MALLOC_FAILURES> controls how often allocations should fail. 173It is a set of fields separated by semicolons, which each field is a count 174(defaulting to zero) and an optional atsign and percentage (defaulting 175to 100). If the count is zero, then it lasts forever. For example, 176C<100;@25> or C<100@0;0@25> means the first 100 allocations pass, then all 177other allocations (until the program exits or crashes) have a 25% chance of 178failing. The length of the value of B<OPENSSL_MALLOC_FAILURES> must be 256 or 179fewer characters. 180 181If the variable B<OPENSSL_MALLOC_FD> is parsed as a positive integer, then 182it is taken as an open file descriptor. This is used in conjunction with 183B<OPENSSL_MALLOC_FAILURES> described above. For every allocation it will log 184details about how many allocations there have been so far, what percentage 185chance there is for this allocation failing, and whether it has actually failed. 186The following example in classic shell syntax shows how to use this (will not 187work on all platforms): 188 189 OPENSSL_MALLOC_FAILURES='200;@10' 190 export OPENSSL_MALLOC_FAILURES 191 OPENSSL_MALLOC_FD=3 192 export OPENSSL_MALLOC_FD 193 ...app invocation... 3>/tmp/log$$ 194 195=head1 RETURN VALUES 196 197OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free() 198CRYPTO_free(), CRYPTO_clear_free() and CRYPTO_get_mem_functions() 199return no value. 200 201OPENSSL_malloc(), OPENSSL_aligned_alloc(), OPENSSL_zalloc(), OPENSSL_realloc(), 202OPENSSL_clear_realloc(), 203CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(), 204CRYPTO_clear_realloc(), 205OPENSSL_strdup(), and OPENSSL_strndup() 206return a pointer to allocated memory or NULL on error. 207 208CRYPTO_set_mem_functions() returns 1 on success or 0 on failure (almost 209always because allocations have already happened). 210 211CRYPTO_mem_leaks(), CRYPTO_mem_leaks_fp(), CRYPTO_mem_leaks_cb(), 212CRYPTO_set_mem_debug(), and CRYPTO_mem_ctrl() are deprecated and are no-ops that 213always return -1. 214OPENSSL_mem_debug_push(), OPENSSL_mem_debug_pop(), 215CRYPTO_mem_debug_push(), and CRYPTO_mem_debug_pop() 216are deprecated and are no-ops that always return 0. 217 218OPENSSL_strtoul() returns 1 on success and 0 in the event that an error has 219occurred. Specifically, 0 is returned in the following events: 220 221=over 4 222 223=item * 224 225If the underlying call to strtoul returned a non zero errno value 226 227=item * 228 229If the translation did not consume the entire input string, and the passed 230endptr value was NULL 231 232=item * 233 234If no characters were consumed in the translation 235 236=back 237 238Note that a success condition does not imply that the expected 239translation has been performed. For instance calling 240 241 OPENSSL_strtoul("0x12345", &endptr, 10, &num); 242 243will result in a successful translation with num having the value 0, and 244*endptr = 'x'. Be sure to validate how much data was consumed when calling this 245function. 246 247=head1 HISTORY 248 249OPENSSL_mem_debug_push(), OPENSSL_mem_debug_pop(), 250CRYPTO_mem_debug_push(), CRYPTO_mem_debug_pop(), 251CRYPTO_mem_leaks(), CRYPTO_mem_leaks_fp(), 252CRYPTO_mem_leaks_cb(), CRYPTO_set_mem_debug(), CRYPTO_mem_ctrl() 253were deprecated in OpenSSL 3.0. 254The memory-leak checking has been deprecated in OpenSSL 3.0 in favor of 255clang's memory and leak sanitizer. 256OPENSSL_aligned_alloc(), CRYPTO_aligned_alloc(), OPENSSL_strtoul() were 257added in OpenSSL 3.4. 258 259=head1 COPYRIGHT 260 261Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved. 262 263Licensed under the Apache License 2.0 (the "License"). You may not use 264this file except in compliance with the License. You can obtain a copy 265in the file LICENSE in the source distribution or at 266L<https://www.openssl.org/source/license.html>. 267 268=cut 269