Blame doc/man3/OPENSSL_malloc.pod

Packit c4476c
=pod
Packit c4476c
Packit c4476c
=head1 NAME
Packit c4476c
Packit c4476c
OPENSSL_malloc_init,
Packit c4476c
OPENSSL_malloc, OPENSSL_zalloc, OPENSSL_realloc, OPENSSL_free,
Packit c4476c
OPENSSL_clear_realloc, OPENSSL_clear_free, OPENSSL_cleanse,
Packit c4476c
CRYPTO_malloc, CRYPTO_zalloc, CRYPTO_realloc, CRYPTO_free,
Packit c4476c
OPENSSL_strdup, OPENSSL_strndup,
Packit c4476c
OPENSSL_memdup, OPENSSL_strlcpy, OPENSSL_strlcat,
Packit c4476c
OPENSSL_hexstr2buf, OPENSSL_buf2hexstr, OPENSSL_hexchar2int,
Packit c4476c
CRYPTO_strdup, CRYPTO_strndup,
Packit c4476c
OPENSSL_mem_debug_push, OPENSSL_mem_debug_pop,
Packit c4476c
CRYPTO_mem_debug_push, CRYPTO_mem_debug_pop,
Packit c4476c
CRYPTO_clear_realloc, CRYPTO_clear_free,
Packit c4476c
CRYPTO_get_mem_functions, CRYPTO_set_mem_functions,
Packit c4476c
CRYPTO_get_alloc_counts,
Packit c4476c
CRYPTO_set_mem_debug, CRYPTO_mem_ctrl,
Packit c4476c
CRYPTO_mem_leaks, CRYPTO_mem_leaks_fp, CRYPTO_mem_leaks_cb,
Packit c4476c
OPENSSL_MALLOC_FAILURES,
Packit c4476c
OPENSSL_MALLOC_FD
Packit c4476c
- Memory allocation functions
Packit c4476c
Packit c4476c
=head1 SYNOPSIS
Packit c4476c
Packit c4476c
 #include <openssl/crypto.h>
Packit c4476c
Packit c4476c
 int OPENSSL_malloc_init(void)
Packit c4476c
Packit c4476c
 void *OPENSSL_malloc(size_t num)
Packit c4476c
 void *OPENSSL_zalloc(size_t num)
Packit c4476c
 void *OPENSSL_realloc(void *addr, size_t num)
Packit c4476c
 void OPENSSL_free(void *addr)
Packit c4476c
 char *OPENSSL_strdup(const char *str)
Packit c4476c
 char *OPENSSL_strndup(const char *str, size_t s)
Packit c4476c
 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size);
Packit c4476c
 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size);
Packit c4476c
 void *OPENSSL_memdup(void *data, size_t s)
Packit c4476c
 void *OPENSSL_clear_realloc(void *p, size_t old_len, size_t num)
Packit c4476c
 void OPENSSL_clear_free(void *str, size_t num)
Packit c4476c
 void OPENSSL_cleanse(void *ptr, size_t len);
Packit c4476c
Packit c4476c
 unsigned char *OPENSSL_hexstr2buf(const char *str, long *len);
Packit c4476c
 char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len);
Packit c4476c
 int OPENSSL_hexchar2int(unsigned char c);
Packit c4476c
Packit c4476c
 void *CRYPTO_malloc(size_t num, const char *file, int line)
Packit c4476c
 void *CRYPTO_zalloc(size_t num, const char *file, int line)
Packit c4476c
 void *CRYPTO_realloc(void *p, size_t num, const char *file, int line)
Packit c4476c
 void CRYPTO_free(void *str, const char *, int)
Packit c4476c
 char *CRYPTO_strdup(const char *p, const char *file, int line)
Packit c4476c
 char *CRYPTO_strndup(const char *p, size_t num, const char *file, int line)
Packit c4476c
 void *CRYPTO_clear_realloc(void *p, size_t old_len, size_t num,
Packit c4476c
                            const char *file, int line)
Packit c4476c
 void CRYPTO_clear_free(void *str, size_t num, const char *, int)
Packit c4476c
Packit c4476c
 void CRYPTO_get_mem_functions(
Packit c4476c
         void *(**m)(size_t, const char *, int),
Packit c4476c
         void *(**r)(void *, size_t, const char *, int),
Packit c4476c
         void (**f)(void *, const char *, int))
Packit c4476c
 int CRYPTO_set_mem_functions(
Packit c4476c
         void *(*m)(size_t, const char *, int),
Packit c4476c
         void *(*r)(void *, size_t, const char *, int),
Packit c4476c
         void (*f)(void *, const char *, int))
Packit c4476c
Packit c4476c
 void CRYPTO_get_alloc_counts(int *m, int *r, int *f)
Packit c4476c
Packit c4476c
 int CRYPTO_set_mem_debug(int onoff)
Packit c4476c
Packit c4476c
 env OPENSSL_MALLOC_FAILURES=... <application>
Packit c4476c
 env OPENSSL_MALLOC_FD=... <application>
Packit c4476c
Packit c4476c
 int CRYPTO_mem_ctrl(int mode);
Packit c4476c
Packit c4476c
 int OPENSSL_mem_debug_push(const char *info)
Packit c4476c
 int OPENSSL_mem_debug_pop(void);
Packit c4476c
Packit c4476c
 int CRYPTO_mem_debug_push(const char *info, const char *file, int line);
Packit c4476c
 int CRYPTO_mem_debug_pop(void);
Packit c4476c
Packit c4476c
 int CRYPTO_mem_leaks(BIO *b);
Packit c4476c
 int CRYPTO_mem_leaks_fp(FILE *fp);
Packit c4476c
 int CRYPTO_mem_leaks_cb(int (*cb)(const char *str, size_t len, void *u),
Packit c4476c
                         void *u);
Packit c4476c
Packit c4476c
=head1 DESCRIPTION
Packit c4476c
Packit c4476c
OpenSSL memory allocation is handled by the B<OPENSSL_xxx> API. These are
Packit c4476c
generally macro's that add the standard C B<__FILE__> and B<__LINE__>
Packit c4476c
parameters and call a lower-level B<CRYPTO_xxx> API.
Packit c4476c
Some functions do not add those parameters, but exist for consistency.
Packit c4476c
Packit c4476c
OPENSSL_malloc_init() does nothing and does not need to be called. It is
Packit c4476c
included for compatibility with older versions of OpenSSL.
Packit c4476c
Packit c4476c
OPENSSL_malloc(), OPENSSL_realloc(), and OPENSSL_free() are like the
Packit c4476c
C malloc(), realloc(), and free() functions.
Packit c4476c
OPENSSL_zalloc() calls memset() to zero the memory before returning.
Packit c4476c
Packit c4476c
OPENSSL_clear_realloc() and OPENSSL_clear_free() should be used
Packit c4476c
when the buffer at B<addr> holds sensitive information.
Packit c4476c
The old buffer is filled with zero's by calling OPENSSL_cleanse()
Packit c4476c
before ultimately calling OPENSSL_free().
Packit c4476c
Packit c4476c
OPENSSL_cleanse() fills B<ptr> of size B<len> with a string of 0's.
Packit c4476c
Use OPENSSL_cleanse() with care if the memory is a mapping of a file.
Packit c4476c
If the storage controller uses write compression, then its possible
Packit c4476c
that sensitive tail bytes will survive zeroization because the block of
Packit c4476c
zeros will be compressed. If the storage controller uses wear leveling,
Packit c4476c
then the old sensitive data will not be overwritten; rather, a block of
Packit c4476c
0's will be written at a new physical location.
Packit c4476c
Packit c4476c
OPENSSL_strdup(), OPENSSL_strndup() and OPENSSL_memdup() are like the
Packit c4476c
equivalent C functions, except that memory is allocated by calling the
Packit c4476c
OPENSSL_malloc() and should be released by calling OPENSSL_free().
Packit c4476c
Packit c4476c
OPENSSL_strlcpy(),
Packit c4476c
OPENSSL_strlcat() and OPENSSL_strnlen() are equivalents of the common C
Packit c4476c
library functions and are provided for portability.
Packit c4476c
Packit c4476c
OPENSSL_hexstr2buf() parses B<str> as a hex string and returns a
Packit c4476c
pointer to the parsed value. The memory is allocated by calling
Packit c4476c
OPENSSL_malloc() and should be released by calling OPENSSL_free().
Packit c4476c
If B<len> is not NULL, it is filled in with the output length.
Packit c4476c
Colons between two-character hex "bytes" are ignored.
Packit c4476c
An odd number of hex digits is an error.
Packit c4476c
Packit c4476c
OPENSSL_buf2hexstr() takes the specified buffer and length, and returns
Packit c4476c
a hex string for value, or NULL on error.
Packit c4476c
B<Buffer> cannot be NULL; if B<len> is 0 an empty string is returned.
Packit c4476c
Packit c4476c
OPENSSL_hexchar2int() converts a character to the hexadecimal equivalent,
Packit c4476c
or returns -1 on error.
Packit c4476c
Packit c4476c
If no allocations have been done, it is possible to "swap out" the default
Packit c4476c
implementations for OPENSSL_malloc(), OPENSSL_realloc and OPENSSL_free()
Packit c4476c
and replace them with alternate versions (hooks).
Packit c4476c
CRYPTO_get_mem_functions() function fills in the given arguments with the
Packit c4476c
function pointers for the current implementations.
Packit c4476c
With CRYPTO_set_mem_functions(), you can specify a different set of functions.
Packit c4476c
If any of B<m>, B<r>, or B<f> are NULL, then the function is not changed.
Packit c4476c
Packit c4476c
The default implementation can include some debugging capability (if enabled
Packit c4476c
at build-time).
Packit c4476c
This adds some overhead by keeping a list of all memory allocations, and
Packit c4476c
removes items from the list when they are free'd.
Packit c4476c
This is most useful for identifying memory leaks.
Packit c4476c
CRYPTO_set_mem_debug() turns this tracking on and off.  In order to have
Packit c4476c
any effect, is must be called before any of the allocation functions
Packit c4476c
(e.g., CRYPTO_malloc()) are called, and is therefore normally one of the
Packit c4476c
first lines of main() in an application.
Packit c4476c
CRYPTO_mem_ctrl() provides fine-grained control of memory leak tracking.
Packit c4476c
To enable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
Packit c4476c
the B<CRYPTO_MEM_CHECK_ON>.
Packit c4476c
To disable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
Packit c4476c
the B<CRYPTO_MEM_CHECK_OFF>.
Packit c4476c
Packit c4476c
While checking memory, it can be useful to store additional context
Packit c4476c
about what is being done.
Packit c4476c
For example, identifying the field names when parsing a complicated
Packit c4476c
data structure.
Packit c4476c
OPENSSL_mem_debug_push() (which calls CRYPTO_mem_debug_push())
Packit c4476c
attaches an identifying string to the allocation stack.
Packit c4476c
This must be a global or other static string; it is not copied.
Packit c4476c
OPENSSL_mem_debug_pop() removes identifying state from the stack.
Packit c4476c
Packit c4476c
At the end of the program, calling CRYPTO_mem_leaks() or
Packit c4476c
CRYPTO_mem_leaks_fp() will report all "leaked" memory, writing it
Packit c4476c
to the specified BIO B or FILE B<fp>. These functions return 1 if
Packit c4476c
there are no leaks, 0 if there are leaks and -1 if an error occurred.
Packit c4476c
Packit c4476c
CRYPTO_mem_leaks_cb() does the same as CRYPTO_mem_leaks(), but instead
Packit c4476c
of writing to a given BIO, the callback function is called for each
Packit c4476c
output string with the string, length, and userdata B<u> as the callback
Packit c4476c
parameters.
Packit c4476c
Packit c4476c
If the library is built with the C<crypto-mdebug> option, then one
Packit c4476c
function, CRYPTO_get_alloc_counts(), and two additional environment
Packit c4476c
variables, B<OPENSSL_MALLOC_FAILURES> and B<OPENSSL_MALLOC_FD>,
Packit c4476c
are available.
Packit c4476c
Packit c4476c
The function CRYPTO_get_alloc_counts() fills in the number of times
Packit c4476c
each of CRYPTO_malloc(), CRYPTO_realloc(), and CRYPTO_free() have been
Packit c4476c
called, into the values pointed to by B<mcount>, B<rcount>, and B<fcount>,
Packit c4476c
respectively.  If a pointer is NULL, then the corresponding count is not stored.
Packit c4476c
Packit c4476c
The variable
Packit c4476c
B<OPENSSL_MALLOC_FAILURES> controls how often allocations should fail.
Packit c4476c
It is a set of fields separated by semicolons, which each field is a count
Packit c4476c
(defaulting to zero) and an optional atsign and percentage (defaulting
Packit c4476c
to 100).  If the count is zero, then it lasts forever.  For example,
Packit c4476c
C<100;@25> or C<100@0;0@25> means the first 100 allocations pass, then all
Packit c4476c
other allocations (until the program exits or crashes) have a 25% chance of
Packit c4476c
failing.
Packit c4476c
Packit c4476c
If the variable B<OPENSSL_MALLOC_FD> is parsed as a positive integer, then
Packit c4476c
it is taken as an open file descriptor, and a record of all allocations is
Packit c4476c
written to that descriptor.  If an allocation will fail, and the platform
Packit c4476c
supports it, then a backtrace will be written to the descriptor.  This can
Packit c4476c
be useful because a malloc may fail but not be checked, and problems will
Packit c4476c
only occur later.  The following example in classic shell syntax shows how
Packit c4476c
to use this (will not work on all platforms):
Packit c4476c
Packit c4476c
  OPENSSL_MALLOC_FAILURES='200;@10'
Packit c4476c
  export OPENSSL_MALLOC_FAILURES
Packit c4476c
  OPENSSL_MALLOC_FD=3
Packit c4476c
  export OPENSSL_MALLOC_FD
Packit c4476c
  ...app invocation... 3>/tmp/log$$
Packit c4476c
Packit c4476c
Packit c4476c
=head1 RETURN VALUES
Packit c4476c
Packit c4476c
OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free()
Packit c4476c
CRYPTO_free(), CRYPTO_clear_free() and CRYPTO_get_mem_functions()
Packit c4476c
return no value.
Packit c4476c
Packit c4476c
CRYPTO_mem_leaks(), CRYPTO_mem_leaks_fp() and CRYPTO_mem_leaks_cb() return 1 if
Packit c4476c
there are no leaks, 0 if there are leaks and -1 if an error occurred.
Packit c4476c
Packit c4476c
OPENSSL_malloc(), OPENSSL_zalloc(), OPENSSL_realloc(),
Packit c4476c
OPENSSL_clear_realloc(),
Packit c4476c
CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(),
Packit c4476c
CRYPTO_clear_realloc(),
Packit c4476c
OPENSSL_buf2hexstr(), OPENSSL_hexstr2buf(),
Packit c4476c
OPENSSL_strdup(), and OPENSSL_strndup()
Packit c4476c
return a pointer to allocated memory or NULL on error.
Packit c4476c
Packit c4476c
CRYPTO_set_mem_functions() and CRYPTO_set_mem_debug()
Packit c4476c
return 1 on success or 0 on failure (almost
Packit c4476c
always because allocations have already happened).
Packit c4476c
Packit c4476c
CRYPTO_mem_ctrl() returns -1 if an error occurred, otherwise the
Packit c4476c
previous value of the mode.
Packit c4476c
Packit c4476c
OPENSSL_mem_debug_push() and OPENSSL_mem_debug_pop()
Packit c4476c
return 1 on success or 0 on failure.
Packit c4476c
Packit c4476c
=head1 NOTES
Packit c4476c
Packit c4476c
While it's permitted to swap out only a few and not all the functions
Packit c4476c
with CRYPTO_set_mem_functions(), it's recommended to swap them all out
Packit c4476c
at once.  I
Packit c4476c
configuration option> C<crypto-mdebug> I
Packit c4476c
only, say, the malloc() implementation is outright dangerous.>
Packit c4476c
Packit c4476c
=head1 COPYRIGHT
Packit c4476c
Packit c4476c
Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
Packit c4476c
Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
this file except in compliance with the License.  You can obtain a copy
Packit c4476c
in the file LICENSE in the source distribution or at
Packit c4476c
L<https://www.openssl.org/source/license.html>.
Packit c4476c
Packit c4476c
=cut