1*4ec2eca9Schristos /*
2*4ec2eca9Schristos  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3a89c9211Schristos  *
4*4ec2eca9Schristos  * Licensed under the OpenSSL license (the "License").  You may not use
5*4ec2eca9Schristos  * this file except in compliance with the License.  You can obtain a copy
6*4ec2eca9Schristos  * in the file LICENSE in the source distribution or at
7*4ec2eca9Schristos  * https://www.openssl.org/source/license.html
8a89c9211Schristos  */
9a89c9211Schristos 
10a89c9211Schristos #include <stdio.h>
11a89c9211Schristos #include <stdlib.h>
12*4ec2eca9Schristos #include <limits.h>
13a89c9211Schristos #include <openssl/crypto.h>
14*4ec2eca9Schristos #include "internal/cryptlib.h"
15a89c9211Schristos 
16d572d25fSspz /*
17d572d25fSspz  * the following pointers may be changed as long as 'allow_customize' is set
18d572d25fSspz  */
19*4ec2eca9Schristos static int allow_customize = 1;
20a89c9211Schristos 
21*4ec2eca9Schristos static void *(*malloc_impl)(size_t, const char *, int)
22*4ec2eca9Schristos     = CRYPTO_malloc;
23*4ec2eca9Schristos static void *(*realloc_impl)(void *, size_t, const char *, int)
24*4ec2eca9Schristos     = CRYPTO_realloc;
25*4ec2eca9Schristos static void (*free_impl)(void *, const char *, int)
26*4ec2eca9Schristos     = CRYPTO_free;
27*4ec2eca9Schristos 
28*4ec2eca9Schristos #ifndef OPENSSL_NO_CRYPTO_MDEBUG
29*4ec2eca9Schristos static int call_malloc_debug = 1;
30*4ec2eca9Schristos #else
31*4ec2eca9Schristos static int call_malloc_debug = 0;
32*4ec2eca9Schristos #endif
33*4ec2eca9Schristos 
34*4ec2eca9Schristos int CRYPTO_set_mem_functions(
35*4ec2eca9Schristos         void *(*m)(size_t, const char *, int),
36*4ec2eca9Schristos         void *(*r)(void *, size_t, const char *, int),
37*4ec2eca9Schristos         void (*f)(void *, const char *, int))
38d572d25fSspz {
39*4ec2eca9Schristos     if (!allow_customize)
40*4ec2eca9Schristos         return 0;
41*4ec2eca9Schristos     if (m)
42*4ec2eca9Schristos         malloc_impl = m;
43*4ec2eca9Schristos     if (r)
44*4ec2eca9Schristos         realloc_impl = r;
45*4ec2eca9Schristos     if (f)
46*4ec2eca9Schristos         free_impl = f;
47*4ec2eca9Schristos     return 1;
48d572d25fSspz }
49d572d25fSspz 
50*4ec2eca9Schristos int CRYPTO_set_mem_debug(int flag)
51*4ec2eca9Schristos {
52*4ec2eca9Schristos     if (!allow_customize)
53*4ec2eca9Schristos         return 0;
54*4ec2eca9Schristos     call_malloc_debug = flag;
55*4ec2eca9Schristos     return 1;
56*4ec2eca9Schristos }
57a89c9211Schristos 
58*4ec2eca9Schristos void CRYPTO_get_mem_functions(
59*4ec2eca9Schristos         void *(**m)(size_t, const char *, int),
60*4ec2eca9Schristos         void *(**r)(void *, size_t, const char *, int),
61*4ec2eca9Schristos         void (**f)(void *, const char *, int))
62*4ec2eca9Schristos {
63*4ec2eca9Schristos     if (m != NULL)
64*4ec2eca9Schristos         *m = malloc_impl;
65*4ec2eca9Schristos     if (r != NULL)
66*4ec2eca9Schristos         *r = realloc_impl;
67*4ec2eca9Schristos     if (f != NULL)
68*4ec2eca9Schristos         *f = free_impl;
69*4ec2eca9Schristos }
70*4ec2eca9Schristos 
71*4ec2eca9Schristos void *CRYPTO_malloc(size_t num, const char *file, int line)
72*4ec2eca9Schristos {
73*4ec2eca9Schristos     void *ret = NULL;
74*4ec2eca9Schristos 
75*4ec2eca9Schristos     if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
76*4ec2eca9Schristos         return malloc_impl(num, file, line);
77*4ec2eca9Schristos 
78*4ec2eca9Schristos     if (num == 0)
79*4ec2eca9Schristos         return NULL;
80*4ec2eca9Schristos 
81*4ec2eca9Schristos     allow_customize = 0;
82*4ec2eca9Schristos #ifndef OPENSSL_NO_CRYPTO_MDEBUG
83*4ec2eca9Schristos     if (call_malloc_debug) {
84*4ec2eca9Schristos         CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);
85*4ec2eca9Schristos         ret = malloc(num);
86*4ec2eca9Schristos         CRYPTO_mem_debug_malloc(ret, num, 1, file, line);
87*4ec2eca9Schristos     } else {
88*4ec2eca9Schristos         ret = malloc(num);
89*4ec2eca9Schristos     }
90*4ec2eca9Schristos #else
91*4ec2eca9Schristos     osslargused(file); osslargused(line);
92*4ec2eca9Schristos     ret = malloc(num);
937fa80bafSspz #endif
947fa80bafSspz 
95*4ec2eca9Schristos     return ret;
96*4ec2eca9Schristos }
97*4ec2eca9Schristos 
98*4ec2eca9Schristos void *CRYPTO_zalloc(size_t num, const char *file, int line)
99*4ec2eca9Schristos {
100*4ec2eca9Schristos     void *ret = CRYPTO_malloc(num, file, line);
101*4ec2eca9Schristos 
102*4ec2eca9Schristos     if (ret != NULL)
103*4ec2eca9Schristos         memset(ret, 0, num);
104*4ec2eca9Schristos     return ret;
105*4ec2eca9Schristos }
106*4ec2eca9Schristos 
107*4ec2eca9Schristos void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
108*4ec2eca9Schristos {
109*4ec2eca9Schristos     if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)
110*4ec2eca9Schristos         return realloc_impl(str, num, file, line);
111*4ec2eca9Schristos 
112*4ec2eca9Schristos     if (str == NULL)
113*4ec2eca9Schristos         return CRYPTO_malloc(num, file, line);
114*4ec2eca9Schristos 
115*4ec2eca9Schristos     if (num == 0) {
116*4ec2eca9Schristos         CRYPTO_free(str, file, line);
117*4ec2eca9Schristos         return NULL;
118*4ec2eca9Schristos     }
119*4ec2eca9Schristos 
120*4ec2eca9Schristos     allow_customize = 0;
121*4ec2eca9Schristos #ifndef OPENSSL_NO_CRYPTO_MDEBUG
122*4ec2eca9Schristos     if (call_malloc_debug) {
123*4ec2eca9Schristos         void *ret;
124*4ec2eca9Schristos         CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
125*4ec2eca9Schristos         ret = realloc(str, num);
126*4ec2eca9Schristos         CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
127*4ec2eca9Schristos         return ret;
128*4ec2eca9Schristos     }
129*4ec2eca9Schristos #else
130*4ec2eca9Schristos     osslargused(file); osslargused(line);
131*4ec2eca9Schristos #endif
132*4ec2eca9Schristos     return realloc(str, num);
133*4ec2eca9Schristos 
134*4ec2eca9Schristos }
135*4ec2eca9Schristos 
136*4ec2eca9Schristos void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
137a89c9211Schristos                            const char *file, int line)
138d572d25fSspz {
139a89c9211Schristos     void *ret = NULL;
140a89c9211Schristos 
141a89c9211Schristos     if (str == NULL)
142a89c9211Schristos         return CRYPTO_malloc(num, file, line);
143a89c9211Schristos 
144*4ec2eca9Schristos     if (num == 0) {
145*4ec2eca9Schristos         CRYPTO_clear_free(str, old_len, file, line);
146d572d25fSspz         return NULL;
147a89c9211Schristos     }
148a89c9211Schristos 
149*4ec2eca9Schristos     /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
150*4ec2eca9Schristos     if (num < old_len) {
151*4ec2eca9Schristos         OPENSSL_cleanse((char*)str + num, old_len - num);
152*4ec2eca9Schristos         return str;
153*4ec2eca9Schristos     }
154a89c9211Schristos 
155*4ec2eca9Schristos     ret = CRYPTO_malloc(num, file, line);
156*4ec2eca9Schristos     if (ret != NULL) {
157a89c9211Schristos         memcpy(ret, str, old_len);
158*4ec2eca9Schristos         CRYPTO_clear_free(str, old_len, file, line);
159a89c9211Schristos     }
160a89c9211Schristos     return ret;
161a89c9211Schristos }
162a89c9211Schristos 
163*4ec2eca9Schristos void CRYPTO_free(void *str, const char *file, int line)
164a89c9211Schristos {
165*4ec2eca9Schristos     if (free_impl != NULL && free_impl != &CRYPTO_free) {
166*4ec2eca9Schristos         free_impl(str, file, line);
167*4ec2eca9Schristos         return;
168*4ec2eca9Schristos     }
169*4ec2eca9Schristos 
170*4ec2eca9Schristos #ifndef OPENSSL_NO_CRYPTO_MDEBUG
171*4ec2eca9Schristos     if (call_malloc_debug) {
172*4ec2eca9Schristos         CRYPTO_mem_debug_free(str, 0, file, line);
173*4ec2eca9Schristos         free(str);
174*4ec2eca9Schristos         CRYPTO_mem_debug_free(str, 1, file, line);
175*4ec2eca9Schristos     } else {
176*4ec2eca9Schristos         free(str);
177*4ec2eca9Schristos     }
178*4ec2eca9Schristos #else
179*4ec2eca9Schristos     free(str);
180a89c9211Schristos #endif
181a89c9211Schristos }
182a89c9211Schristos 
183*4ec2eca9Schristos void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
184a89c9211Schristos {
185*4ec2eca9Schristos     if (str == NULL)
186*4ec2eca9Schristos         return;
187*4ec2eca9Schristos     if (num)
188*4ec2eca9Schristos         OPENSSL_cleanse(str, num);
189*4ec2eca9Schristos     CRYPTO_free(str, file, line);
190a89c9211Schristos }
191