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 KimCRYPTO_strdup, CRYPTO_strndup, 12e71b7053SJung-uk KimOPENSSL_mem_debug_push, OPENSSL_mem_debug_pop, 13e71b7053SJung-uk KimCRYPTO_mem_debug_push, CRYPTO_mem_debug_pop, 14e71b7053SJung-uk KimCRYPTO_clear_realloc, CRYPTO_clear_free, 15b077aed3SPierre ProncheryCRYPTO_malloc_fn, CRYPTO_realloc_fn, CRYPTO_free_fn, 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 28b077aed3SPierre Pronchery int OPENSSL_malloc_init(void); 29e71b7053SJung-uk Kim 30b077aed3SPierre Pronchery void *OPENSSL_malloc(size_t num); 31b077aed3SPierre Pronchery void *OPENSSL_zalloc(size_t num); 32b077aed3SPierre Pronchery void *OPENSSL_realloc(void *addr, size_t num); 33b077aed3SPierre Pronchery void OPENSSL_free(void *addr); 34b077aed3SPierre Pronchery char *OPENSSL_strdup(const char *str); 35b077aed3SPierre Pronchery 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); 38b077aed3SPierre Pronchery void *OPENSSL_memdup(void *data, size_t s); 39b077aed3SPierre Pronchery void *OPENSSL_clear_realloc(void *p, size_t old_len, size_t num); 40b077aed3SPierre Pronchery void OPENSSL_clear_free(void *str, size_t num); 41e71b7053SJung-uk Kim void OPENSSL_cleanse(void *ptr, size_t len); 42e71b7053SJung-uk Kim 43b077aed3SPierre Pronchery void *CRYPTO_malloc(size_t num, const char *file, int line); 44b077aed3SPierre Pronchery void *CRYPTO_zalloc(size_t num, const char *file, int line); 45b077aed3SPierre Pronchery void *CRYPTO_realloc(void *p, size_t num, const char *file, int line); 46b077aed3SPierre Pronchery void CRYPTO_free(void *str, const char *, int); 47b077aed3SPierre Pronchery char *CRYPTO_strdup(const char *p, const char *file, int line); 48b077aed3SPierre Pronchery char *CRYPTO_strndup(const char *p, size_t num, const char *file, int line); 49e71b7053SJung-uk Kim void *CRYPTO_clear_realloc(void *p, size_t old_len, size_t num, 50b077aed3SPierre Pronchery const char *file, int line); 51b077aed3SPierre Pronchery void CRYPTO_clear_free(void *str, size_t num, const char *, int); 52e71b7053SJung-uk Kim 53b077aed3SPierre Pronchery typedef void *(*CRYPTO_malloc_fn)(size_t num, const char *file, int line); 54b077aed3SPierre Pronchery typedef void *(*CRYPTO_realloc_fn)(void *addr, size_t num, const char *file, 55b077aed3SPierre Pronchery int line); 56b077aed3SPierre Pronchery typedef void (*CRYPTO_free_fn)(void *addr, const char *file, int line); 57b077aed3SPierre Pronchery void CRYPTO_get_mem_functions(CRYPTO_malloc_fn *malloc_fn, 58b077aed3SPierre Pronchery CRYPTO_realloc_fn *realloc_fn, 59b077aed3SPierre Pronchery CRYPTO_free_fn *free_fn); 60b077aed3SPierre Pronchery int CRYPTO_set_mem_functions(CRYPTO_malloc_fn malloc_fn, 61b077aed3SPierre Pronchery CRYPTO_realloc_fn realloc_fn, 62b077aed3SPierre Pronchery CRYPTO_free_fn free_fn); 63e71b7053SJung-uk Kim 64b077aed3SPierre Pronchery void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount); 65e71b7053SJung-uk Kim 66e71b7053SJung-uk Kim env OPENSSL_MALLOC_FAILURES=... <application> 67e71b7053SJung-uk Kim env OPENSSL_MALLOC_FD=... <application> 68e71b7053SJung-uk Kim 69b077aed3SPierre ProncheryThe following functions have been deprecated since OpenSSL 3.0, and can be 70b077aed3SPierre Proncheryhidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value, 71b077aed3SPierre Proncherysee L<openssl_user_macros(7)>: 72e71b7053SJung-uk Kim 73e71b7053SJung-uk Kim int CRYPTO_mem_leaks(BIO *b); 74e71b7053SJung-uk Kim int CRYPTO_mem_leaks_fp(FILE *fp); 75e71b7053SJung-uk Kim int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u), 76e71b7053SJung-uk Kim void *u); 77e71b7053SJung-uk Kim 78b077aed3SPierre Pronchery int CRYPTO_set_mem_debug(int onoff); 79b077aed3SPierre Pronchery int CRYPTO_mem_ctrl(int mode); 80b077aed3SPierre Pronchery int OPENSSL_mem_debug_push(const char *info); 81b077aed3SPierre Pronchery int OPENSSL_mem_debug_pop(void); 82b077aed3SPierre Pronchery int CRYPTO_mem_debug_push(const char *info, const char *file, int line); 83b077aed3SPierre Pronchery int CRYPTO_mem_debug_pop(void); 84b077aed3SPierre Pronchery 85e71b7053SJung-uk Kim=head1 DESCRIPTION 86e71b7053SJung-uk Kim 87e71b7053SJung-uk KimOpenSSL memory allocation is handled by the B<OPENSSL_xxx> API. These are 88e71b7053SJung-uk Kimgenerally macro's that add the standard C B<__FILE__> and B<__LINE__> 89e71b7053SJung-uk Kimparameters and call a lower-level B<CRYPTO_xxx> API. 90e71b7053SJung-uk KimSome functions do not add those parameters, but exist for consistency. 91e71b7053SJung-uk Kim 926935a639SJung-uk KimOPENSSL_malloc_init() does nothing and does not need to be called. It is 936935a639SJung-uk Kimincluded for compatibility with older versions of OpenSSL. 94e71b7053SJung-uk Kim 95e71b7053SJung-uk KimOPENSSL_malloc(), OPENSSL_realloc(), and OPENSSL_free() are like the 96e71b7053SJung-uk KimC malloc(), realloc(), and free() functions. 97e71b7053SJung-uk KimOPENSSL_zalloc() calls memset() to zero the memory before returning. 98e71b7053SJung-uk Kim 99e71b7053SJung-uk KimOPENSSL_clear_realloc() and OPENSSL_clear_free() should be used 100e71b7053SJung-uk Kimwhen the buffer at B<addr> holds sensitive information. 101e71b7053SJung-uk KimThe old buffer is filled with zero's by calling OPENSSL_cleanse() 102*a7148ab3SEnji Cooperbefore ultimately calling OPENSSL_free(). If the argument to OPENSSL_free() is 103*a7148ab3SEnji CooperNULL, nothing is done. 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. 10788e852c0SJung-uk KimIf the storage controller uses write compression, then it's 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 KimIf no allocations have been done, it is possible to "swap out" the default 122b077aed3SPierre Proncheryimplementations for OPENSSL_malloc(), OPENSSL_realloc() and OPENSSL_free() 123b077aed3SPierre Proncheryand replace them with alternate versions. 124e71b7053SJung-uk KimCRYPTO_get_mem_functions() function fills in the given arguments with the 125e71b7053SJung-uk Kimfunction pointers for the current implementations. 126e71b7053SJung-uk KimWith CRYPTO_set_mem_functions(), you can specify a different set of functions. 127b077aed3SPierre ProncheryIf any of B<malloc_fn>, B<realloc_fn>, or B<free_fn> are NULL, then 128b077aed3SPierre Proncherythe function is not changed. 129b077aed3SPierre ProncheryWhile it's permitted to swap out only a few and not all the functions 130b077aed3SPierre Proncherywith CRYPTO_set_mem_functions(), it's recommended to swap them all out 131b077aed3SPierre Proncheryat once. 132e71b7053SJung-uk Kim 133e71b7053SJung-uk KimIf the library is built with the C<crypto-mdebug> option, then one 134e71b7053SJung-uk Kimfunction, CRYPTO_get_alloc_counts(), and two additional environment 135e71b7053SJung-uk Kimvariables, B<OPENSSL_MALLOC_FAILURES> and B<OPENSSL_MALLOC_FD>, 136e71b7053SJung-uk Kimare available. 137e71b7053SJung-uk Kim 138e71b7053SJung-uk KimThe function CRYPTO_get_alloc_counts() fills in the number of times 139e71b7053SJung-uk Kimeach of CRYPTO_malloc(), CRYPTO_realloc(), and CRYPTO_free() have been 140e71b7053SJung-uk Kimcalled, into the values pointed to by B<mcount>, B<rcount>, and B<fcount>, 141e71b7053SJung-uk Kimrespectively. If a pointer is NULL, then the corresponding count is not stored. 142e71b7053SJung-uk Kim 143e71b7053SJung-uk KimThe variable 144e71b7053SJung-uk KimB<OPENSSL_MALLOC_FAILURES> controls how often allocations should fail. 145e71b7053SJung-uk KimIt is a set of fields separated by semicolons, which each field is a count 146e71b7053SJung-uk Kim(defaulting to zero) and an optional atsign and percentage (defaulting 147e71b7053SJung-uk Kimto 100). If the count is zero, then it lasts forever. For example, 148e71b7053SJung-uk KimC<100;@25> or C<100@0;0@25> means the first 100 allocations pass, then all 149e71b7053SJung-uk Kimother allocations (until the program exits or crashes) have a 25% chance of 150e71b7053SJung-uk Kimfailing. 151e71b7053SJung-uk Kim 152e71b7053SJung-uk KimIf the variable B<OPENSSL_MALLOC_FD> is parsed as a positive integer, then 153b077aed3SPierre Proncheryit is taken as an open file descriptor. This is used in conjunction with 154b077aed3SPierre ProncheryB<OPENSSL_MALLOC_FAILURES> described above. For every allocation it will log 155b077aed3SPierre Proncherydetails about how many allocations there have been so far, what percentage 156b077aed3SPierre Proncherychance there is for this allocation failing, and whether it has actually failed. 157b077aed3SPierre ProncheryThe following example in classic shell syntax shows how to use this (will not 158b077aed3SPierre Proncherywork on all platforms): 159e71b7053SJung-uk Kim 160e71b7053SJung-uk Kim OPENSSL_MALLOC_FAILURES='200;@10' 161e71b7053SJung-uk Kim export OPENSSL_MALLOC_FAILURES 162e71b7053SJung-uk Kim OPENSSL_MALLOC_FD=3 163e71b7053SJung-uk Kim export OPENSSL_MALLOC_FD 164e71b7053SJung-uk Kim ...app invocation... 3>/tmp/log$$ 165e71b7053SJung-uk Kim 166e71b7053SJung-uk Kim=head1 RETURN VALUES 167e71b7053SJung-uk Kim 168e71b7053SJung-uk KimOPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free() 169e71b7053SJung-uk KimCRYPTO_free(), CRYPTO_clear_free() and CRYPTO_get_mem_functions() 170e71b7053SJung-uk Kimreturn no value. 171e71b7053SJung-uk Kim 172e71b7053SJung-uk KimOPENSSL_malloc(), OPENSSL_zalloc(), OPENSSL_realloc(), 173e71b7053SJung-uk KimOPENSSL_clear_realloc(), 174e71b7053SJung-uk KimCRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(), 175e71b7053SJung-uk KimCRYPTO_clear_realloc(), 176e71b7053SJung-uk KimOPENSSL_strdup(), and OPENSSL_strndup() 177e71b7053SJung-uk Kimreturn a pointer to allocated memory or NULL on error. 178e71b7053SJung-uk Kim 179b077aed3SPierre ProncheryCRYPTO_set_mem_functions() returns 1 on success or 0 on failure (almost 180e71b7053SJung-uk Kimalways because allocations have already happened). 181e71b7053SJung-uk Kim 182b077aed3SPierre ProncheryCRYPTO_mem_leaks(), CRYPTO_mem_leaks_fp(), CRYPTO_mem_leaks_cb(), 183b077aed3SPierre ProncheryCRYPTO_set_mem_debug(), and CRYPTO_mem_ctrl() are deprecated and are no-ops that 184b077aed3SPierre Proncheryalways return -1. 185b077aed3SPierre ProncheryOPENSSL_mem_debug_push(), OPENSSL_mem_debug_pop(), 186b077aed3SPierre ProncheryCRYPTO_mem_debug_push(), and CRYPTO_mem_debug_pop() 187b077aed3SPierre Proncheryare deprecated and are no-ops that always return 0. 188e71b7053SJung-uk Kim 189b077aed3SPierre Pronchery=head1 HISTORY 190e71b7053SJung-uk Kim 191b077aed3SPierre ProncheryOPENSSL_mem_debug_push(), OPENSSL_mem_debug_pop(), 192b077aed3SPierre ProncheryCRYPTO_mem_debug_push(), CRYPTO_mem_debug_pop(), 193b077aed3SPierre ProncheryCRYPTO_mem_leaks(), CRYPTO_mem_leaks_fp(), 194b077aed3SPierre ProncheryCRYPTO_mem_leaks_cb(), CRYPTO_set_mem_debug(), CRYPTO_mem_ctrl() 195b077aed3SPierre Proncherywere deprecated in OpenSSL 3.0. 196b077aed3SPierre ProncheryThe memory-leak checking has been deprecated in OpenSSL 3.0 in favor of 197b077aed3SPierre Proncheryclang's memory and leak sanitizer. 198e71b7053SJung-uk Kim 199e71b7053SJung-uk Kim 200e71b7053SJung-uk Kim=head1 COPYRIGHT 201e71b7053SJung-uk Kim 202*a7148ab3SEnji CooperCopyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved. 203e71b7053SJung-uk Kim 204b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 205e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 206e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 207e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 208e71b7053SJung-uk Kim 209e71b7053SJung-uk Kim=cut 210