summaryrefslogtreecommitdiffstats
path: root/doc/crypto/OPENSSL_malloc.pod
blob: eef8c6439384ccf7a06eb06618f77924e8a29e16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
=pod

=head1 NAME

OPENSSL_malloc_init,
OPENSSL_malloc, OPENSSL_zalloc, OPENSSL_realloc, OPENSSL_free,
OPENSSL_clear_realloc, OPENSSL_clear_free,
CRYPTO_malloc, CRYPTO_zalloc, CRYPTO_realloc, CRYPTO_free,
OPENSSL_strdup, OPENSSL_strndup,
OPENSSL_memdup, OPENSSL_strlcpy, OPENSSL_strlcat,
CRYPTO_clear_realloc, CRYPTO_clear_free,
CRYPTO_get_mem_functions, CRYPTO_set_mem_functions,
CRYPTO_set_mem_debug, CRYPTO_mem_ctrl,
OPENSSL_mem_debug_push, OPENSSL_mem_debug_pop,
CRYPTO_mem_debug_push, CRYPTO_mem_debug_pop,
CRYPTO_mem_leaks, CRYPTO_mem_leaks_fp - Memory allocation functions

=head1 SYNOPSIS

 #include <openssl/crypto.h>

 int OPENSSL_malloc_init(void)

 void *OPENSSL_malloc(size_t num)
 void *OPENSSL_zalloc(size_t num)
 void *OPENSSL_realloc(void *addr, size_t num)
 void OPENSSL_free(void *addr)
 char *OPENSSL_strdup(const char *str)
 char *OPENSSL_strndup(const char *str, size_t s)
 void *OPENSSL_clear_realloc(void *p, size_t old_len, size_t num)
 void OPENSSL_clear_free(void *str, size_t num)
 void OPENSSL_cleanse(void *ptr, size_t len);

 void *CRYPTO_malloc(size_t num, const char *file, int line)
 void *CRYPTO_zalloc(size_t num, const char *file, int line)
 void *CRYPTO_realloc(void *p, size_t num, const char *file, int line)
 void CRYPTO_free(void *str)
 char *CRYPTO_strdup(const char *p, const char *file, int line)
 char *CRYPTO_strndup(const char *p, size_t num, const char *file, int line)
 void *CRYPTO_clear_realloc(void *p, size_t old_len, size_t num, const char *file, int line)
 void CRYPTO_clear_free(void *str, size_t num)

 void CRYPTO_get_mem_functions(
         void *(**m)(size_t, const char *, int),
         void *(**r)(void *, size_t, const char *, int),
         void (**f)(void *))
 int CRYPTO_set_mem_functions(
         void *(*m)(size_t, const char *, int),
         void *(*r)(void *, size_t, const char *, int),
         void (*f)(void *))

 int CRYPTO_set_mem_debug(int onoff)

 #define CRYPTO_MEM_CHECK_OFF
 #define CRYPTO_MEM_CHECK_ON
 #define CRYPTO_MEM_CHECK_DISABLE
 #define CRYPTO_MEM_CHECK_ENABLE

 int CRYPTO_mem_ctrl(int mode);

 int OPENSSL_mem_debug_push(const char *info)
 int OPENSLS_mem_debug_pop)(void)

 int CRYPTO_mem_debug_push(const char *info, const char *file, int line);

 void CRYPTO_mem_leaks(BIO *b);
 void CRYPTO_mem_leaks(FILE *fp);

=head1 DESCRIPTION

OpenSSL memory allocation is handled by the B<OPENSSL_xxx> API. These are
generally macro's that add the standard C B<__FILE__> and B<__LINE__>
parameters and call a lower-level B<CRYPTO_xxx> API.
Some functions do not add those parameters, but exist for consistency.

OPENSSL_malloc_init() sets the lower-level memory allocation functions
to their default implementation.
It is generally not necessary to call this, except perhaps in certain
shared-library situations.

OPENSSL_malloc(), OPENSSL_realloc(), and OPENSSL_free() are like the
C malloc(), realloc(), and free() functions.
OPENSSL_zalloc() calls memset() to zero the memory before returning.

OPENSSL_clear_realloc() and OPENSSL_clear_free() should be used
when the buffer at B<addr> holds sensitive information.
The old buffer is filled with arbitrary data by calling OPENSSL_cleanse()
before ultimately calling OPENSSL_free().

OPENSSL_strdup(), OPENSSL_strndup() and OPENSSL_memdup() are like the
equivalent C functions, except that memory is allocated by calling the
OPENSSL_malloc() and should be releaed by calling OPENSSL_free().

OPENSSL_strlcpy(),
OPENSSL_strlcat() and OPENSSL_strnlen() are equivalents of the common C
library functions and are provided for portability.

If no allocations have been done, it is possible to "swap out" the default
implementations and replace them with alternate versions, or wrappers that
do some additional housekeeping and then defer to the OpenSSL implementation.
The CRYPTO_get_mem_functions() function fills in the function pointers for
with the current functions (normally, and by default,
CRYPTO_malloc(), CRYPTO_realloc(), and CRYPTO_free()).
The CRYPTO_set_mem_functions() specifies a different set of functions.
If any of B<m>, B<r>, or B<f> are NULL, then the function is not changed.

The default implementation can include some debugging capability (if enabled
at build-time).
This adds some overhead by keeping a list of all memory allocations, and
removes items from the list when they are free'd.
This is most useful for identifying memory leaks.
CRYPTO_set_mem_debug() turns this tracking on and off.  It is normally
called at startup, but can be called at any time.

CRYPTO_mem_ctrl() provides fine-grained control of memory leak tracking.
To enable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
the B<CRYPTO_MEM_CHECK_ON>.
To disable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
the B<CRYPTO_MEM_CHECK_OFF>.
The B<CRYPTO_MEM_CHECK_DISABLE> and B<CRYPTO_MEM_CHECK_ENABLE> modes
are used internally within OpenSSL to temporarily suspend and resume
tracking.

While checking memory, it can be useful to store additional context
about what is being done.
For example, identifying the field names when parsing a complicated
data structure.
OPENSSL_mem_debug_push() (which calls CRYPTO_mem_debug_push())
attachs an identifying string to the allocation stack.
This must be a global or other static string; it is not copied.
OPENSSL_mem_debug_pop() removes identifying state from the stack.

At the end of the program, calling CRYPTO_mem_leaks() or
CRYPTO_mem_leaks_fp() will report all "leaked" memory, writing it
to the specified BIO B<b> or FILE B<fp>.
It will then L<abort(3)> if there were any unfree'd allocations.

=head1 RETURN VALUES

OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free()
CRYPTO_free(), CRYPTO_clear_free(),
CRYPTO_get_mem_functions(), and
CRYPTO_mem_leaks()
return no value.

OPENSSL_malloc(), OPENSSL_zalloc(), OPENSSL_realloc(),
OPENSSL_clear_realloc(),
CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(),
CRYPTO_clear_realloc(),
OPENSSL_strdup(), and OPENSSL_strndup()
return a pointer to allocated memory or NULL on error.

CRYPTO_set_mem_functions() and CRYPTO_set_mem_debug()
return 1 on success or 0 on failure (almost
always because allocations have already happened).

CRYPTO_mem_ctrl() returns the previous value of the mode.

OPENSSL_mem_debug_push() and OPENSSL_mem_debug_pop()
return 1 on success or 0 on failure.

=cut