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