mem.c 6.7 KB
Newer Older
R
Rich Salz 已提交
1 2
/*
 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
8 9 10 11
 */

#include <stdio.h>
#include <stdlib.h>
R
Rich Salz 已提交
12
#include <limits.h>
13
#include <openssl/crypto.h>
14
#include "internal/cryptlib.h"
R
Rich Salz 已提交
15 16 17 18
#include "internal/cryptlib_int.h"
#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
# include <execinfo.h>
#endif
19

20 21 22
/*
 * the following pointers may be changed as long as 'allow_customize' is set
 */
R
Rich Salz 已提交
23
static int allow_customize = 1;
24

25
static void *(*malloc_impl)(size_t, const char *, int)
R
Rich Salz 已提交
26
    = CRYPTO_malloc;
27
static void *(*realloc_impl)(void *, size_t, const char *, int)
R
Rich Salz 已提交
28
    = CRYPTO_realloc;
29
static void (*free_impl)(void *, const char *, int)
R
Rich Salz 已提交
30
    = CRYPTO_free;
R
Rich Salz 已提交
31

32
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
R
Rich Salz 已提交
33 34
static char *md_failstring;
static long md_count;
R
Rich Salz 已提交
35
static int md_fail_percent = 0;
R
Rich Salz 已提交
36
static int md_tracefd = -1;
R
Rich Salz 已提交
37
static int call_malloc_debug = 1;
R
Rich Salz 已提交
38 39 40 41 42 43

static void parseit(void);
static int shouldfail(void);

# define FAILTEST() if (shouldfail()) return NULL

44
#else
R
Rich Salz 已提交
45
static int call_malloc_debug = 0;
R
Rich Salz 已提交
46 47

# define FAILTEST() /* empty */
48
#endif
49

R
Rich Salz 已提交
50 51 52
int CRYPTO_set_mem_functions(
        void *(*m)(size_t, const char *, int),
        void *(*r)(void *, size_t, const char *, int),
53
        void (*f)(void *, const char *, int))
R
Rich Salz 已提交
54 55 56
{
    if (!allow_customize)
        return 0;
R
Rich Salz 已提交
57
    if (m)
58
        malloc_impl = m;
R
Rich Salz 已提交
59
    if (r)
60
        realloc_impl = r;
R
Rich Salz 已提交
61
    if (f)
62
        free_impl = f;
R
Rich Salz 已提交
63 64 65
    return 1;
}

R
Rich Salz 已提交
66
int CRYPTO_set_mem_debug(int flag)
R
Rich Salz 已提交
67 68 69
{
    if (!allow_customize)
        return 0;
R
Rich Salz 已提交
70
    call_malloc_debug = flag;
71 72 73
    return 1;
}

R
Rich Salz 已提交
74 75 76
void CRYPTO_get_mem_functions(
        void *(**m)(size_t, const char *, int),
        void *(**r)(void *, size_t, const char *, int),
77
        void (**f)(void *, const char *, int))
78 79
{
    if (m != NULL)
80
        *m = malloc_impl;
81
    if (r != NULL)
82
        *r = realloc_impl;
R
Rich Salz 已提交
83
    if (f != NULL)
84
        *f = free_impl;
85
}
86

R
Rich Salz 已提交
87 88 89 90 91
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
/*
 * Parse a "malloc failure spec" string.  This likes like a set of fields
 * separated by semicolons.  Each field has a count and an optional failure
 * percentage.  For example:
R
Rich Salz 已提交
92 93
 *          100@0;100@25;0@0
 *    or    100;100@25;0
R
Rich Salz 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107
 * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
 * all remaining (count is zero) succeed.
 */
static void parseit(void)
{
    char *semi = strchr(md_failstring, ';');
    char *atsign;

    if (semi != NULL)
        *semi++ = '\0';

    /* Get the count (atol will stop at the @ if there), and percentage */
    md_count = atol(md_failstring);
    atsign = strchr(md_failstring, '@');
R
Rich Salz 已提交
108
    md_fail_percent = atsign == NULL ? 0 : atoi(atsign + 1);
R
Rich Salz 已提交
109 110 111 112 113 114 115 116 117 118 119

    if (semi != NULL)
        md_failstring = semi;
}

/*
 * See if the current malloc should fail.
 */
static int shouldfail(void)
{
    int roll = (int)(random() % 100);
R
Richard Levitte 已提交
120
    int shoulditfail = roll < md_fail_percent;
R
Rich Salz 已提交
121 122 123 124 125
    char buff[80];

    if (md_tracefd > 0) {
        BIO_snprintf(buff, sizeof(buff),
                     "%c C%ld %%%d R%d\n",
R
Richard Levitte 已提交
126
                     shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
R
Rich Salz 已提交
127 128
        write(md_tracefd, buff, strlen(buff));
#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
R
Richard Levitte 已提交
129
        if (shoulditfail) {
R
Rich Salz 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143
            void *addrs[30];
            int num = backtrace(addrs, OSSL_NELEM(addrs));

            backtrace_symbols_fd(addrs, num, md_tracefd);
        }
#endif
    }

    if (md_count) {
        /* If we used up this one, go to the next. */
        if (--md_count == 0)
            parseit();
    }

R
Richard Levitte 已提交
144
    return shoulditfail;
R
Rich Salz 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157
}

void ossl_malloc_setup_failures(void)
{
    const char *cp = getenv("OPENSSL_MALLOC_FAILURES");

    if (cp != NULL && (md_failstring = strdup(cp)) != NULL)
        parseit();
    if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
        md_tracefd = atoi(cp);
}
#endif

158
void *CRYPTO_malloc(size_t num, const char *file, int line)
159 160 161
{
    void *ret = NULL;

162 163 164
    if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
        return malloc_impl(num, file, line);

165
    if (num == 0)
166 167
        return NULL;

R
Rich Salz 已提交
168
    FAILTEST();
R
Rich Salz 已提交
169
    allow_customize = 0;
170
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
R
Rich Salz 已提交
171 172 173 174 175 176
    if (call_malloc_debug) {
        CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);
        ret = malloc(num);
        CRYPTO_mem_debug_malloc(ret, num, 1, file, line);
    } else {
        ret = malloc(num);
177
    }
R
Rich Salz 已提交
178
#else
179
    osslargused(file); osslargused(line);
R
Rich Salz 已提交
180 181
    ret = malloc(num);
#endif
182

183 184 185
    return ret;
}

186
void *CRYPTO_zalloc(size_t num, const char *file, int line)
R
Rich Salz 已提交
187 188 189
{
    void *ret = CRYPTO_malloc(num, file, line);

R
Rich Salz 已提交
190
    FAILTEST();
R
Rich Salz 已提交
191 192 193 194 195
    if (ret != NULL)
        memset(ret, 0, num);
    return ret;
}

196
void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
197
{
198 199 200
    if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)
        return realloc_impl(str, num, file, line);

R
Rich Salz 已提交
201
    FAILTEST();
202 203
    if (str == NULL)
        return CRYPTO_malloc(num, file, line);
204

R
Rich Salz 已提交
205
    if (num == 0) {
206
        CRYPTO_free(str, file, line);
207
        return NULL;
R
Rich Salz 已提交
208
    }
209

R
Rich Salz 已提交
210
    allow_customize = 0;
211
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
R
Rich Salz 已提交
212 213 214 215 216 217 218 219
    if (call_malloc_debug) {
        void *ret;
        CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
        ret = realloc(str, num);
        CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
        return ret;
    }
#else
220
    osslargused(file); osslargused(line);
R
Rich Salz 已提交
221 222
#endif
    return realloc(str, num);
223

224
}
225

226
void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
227
                           const char *file, int line)
228 229 230 231 232 233
{
    void *ret = NULL;

    if (str == NULL)
        return CRYPTO_malloc(num, file, line);

R
Rich Salz 已提交
234
    if (num == 0) {
235
        CRYPTO_clear_free(str, old_len, file, line);
236
        return NULL;
R
Rich Salz 已提交
237
    }
238

R
Rich Salz 已提交
239 240
    /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
    if (num < old_len) {
241
        OPENSSL_cleanse((char*)str + num, old_len - num);
R
Rich Salz 已提交
242 243
        return str;
    }
244

245
    ret = CRYPTO_malloc(num, file, line);
246
    if (ret != NULL) {
R
Rich Salz 已提交
247
        memcpy(ret, str, old_len);
248 249
        CRYPTO_clear_free(str, old_len, file, line);
    }
250 251
    return ret;
}
252

253
void CRYPTO_free(void *str, const char *file, int line)
254
{
255 256 257 258 259
    if (free_impl != NULL && free_impl != &CRYPTO_free) {
        free_impl(str, file, line);
        return;
    }

260
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
R
Rich Salz 已提交
261
    if (call_malloc_debug) {
262
        CRYPTO_mem_debug_free(str, 0, file, line);
R
Rich Salz 已提交
263
        free(str);
264
        CRYPTO_mem_debug_free(str, 1, file, line);
R
Rich Salz 已提交
265 266 267 268 269 270
    } else {
        free(str);
    }
#else
    free(str);
#endif
271
}
272

273
void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
R
Rich Salz 已提交
274
{
R
Rich Salz 已提交
275
    if (str == NULL)
R
Rich Salz 已提交
276 277 278
        return;
    if (num)
        OPENSSL_cleanse(str, num);
279
    CRYPTO_free(str, file, line);
R
Rich Salz 已提交
280
}