mem.c 7.9 KB
Newer Older
R
Rich Salz 已提交
1
/*
2
 * Copyright 1995-2018 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 12
#include "e_os.h"
#include "internal/cryptlib.h"
#include "internal/cryptlib_int.h"
13 14
#include <stdio.h>
#include <stdlib.h>
R
Rich Salz 已提交
15
#include <limits.h>
16
#include <openssl/crypto.h>
R
Rich Salz 已提交
17 18 19
#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
# include <execinfo.h>
#endif
20

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

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

33
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
34
# include "internal/tsan_assist.h"
R
Rich Salz 已提交
35

36 37 38 39 40
static TSAN_QUALIFIER int malloc_count;
static TSAN_QUALIFIER int realloc_count;
static TSAN_QUALIFIER int free_count;

# define INCREMENT(x) tsan_counter(&(x))
R
Rich Salz 已提交
41

R
Rich Salz 已提交
42 43
static char *md_failstring;
static long md_count;
R
Rich Salz 已提交
44
static int md_fail_percent = 0;
R
Rich Salz 已提交
45
static int md_tracefd = -1;
R
Rich Salz 已提交
46
static int call_malloc_debug = 1;
R
Rich Salz 已提交
47 48 49 50 51 52

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

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

53
#else
R
Rich Salz 已提交
54
static int call_malloc_debug = 0;
R
Rich Salz 已提交
55

R
Rich Salz 已提交
56
# define INCREMENT(x) /* empty */
R
Rich Salz 已提交
57
# define FAILTEST() /* empty */
58
#endif
59

R
Rich Salz 已提交
60 61 62
int CRYPTO_set_mem_functions(
        void *(*m)(size_t, const char *, int),
        void *(*r)(void *, size_t, const char *, int),
63
        void (*f)(void *, const char *, int))
R
Rich Salz 已提交
64 65 66
{
    if (!allow_customize)
        return 0;
R
Rich Salz 已提交
67
    if (m)
68
        malloc_impl = m;
R
Rich Salz 已提交
69
    if (r)
70
        realloc_impl = r;
R
Rich Salz 已提交
71
    if (f)
72
        free_impl = f;
R
Rich Salz 已提交
73 74 75
    return 1;
}

R
Rich Salz 已提交
76
int CRYPTO_set_mem_debug(int flag)
R
Rich Salz 已提交
77 78 79
{
    if (!allow_customize)
        return 0;
R
Rich Salz 已提交
80
    call_malloc_debug = flag;
81 82 83
    return 1;
}

R
Rich Salz 已提交
84 85 86
void CRYPTO_get_mem_functions(
        void *(**m)(size_t, const char *, int),
        void *(**r)(void *, size_t, const char *, int),
87
        void (**f)(void *, const char *, int))
88 89
{
    if (m != NULL)
90
        *m = malloc_impl;
91
    if (r != NULL)
92
        *r = realloc_impl;
R
Rich Salz 已提交
93
    if (f != NULL)
94
        *f = free_impl;
95
}
96

R
Rich Salz 已提交
97
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
R
Rich Salz 已提交
98 99 100
void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount)
{
    if (mcount != NULL)
101
        *mcount = tsan_load(&malloc_count);
R
Rich Salz 已提交
102
    if (rcount != NULL)
103
        *rcount = tsan_load(&realloc_count);
R
Rich Salz 已提交
104
    if (fcount != NULL)
105
        *fcount = tsan_load(&free_count);
R
Rich Salz 已提交
106 107
}

R
Rich Salz 已提交
108 109 110 111
/*
 * 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 已提交
112 113
 *          100@0;100@25;0@0
 *    or    100;100@25;0
R
Rich Salz 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127
 * 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 已提交
128
    md_fail_percent = atsign == NULL ? 0 : atoi(atsign + 1);
R
Rich Salz 已提交
129 130 131 132 133

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

134 135 136 137 138
/*
 * Windows doesn't have random(), but it has rand()
 * Some rand() implementations aren't good, but we're not
 * dealing with secure randomness here.
 */
139 140 141
# ifdef _WIN32
#  define random() rand()
# endif
R
Rich Salz 已提交
142 143 144 145 146 147
/*
 * See if the current malloc should fail.
 */
static int shouldfail(void)
{
    int roll = (int)(random() % 100);
R
Richard Levitte 已提交
148
    int shoulditfail = roll < md_fail_percent;
149 150
# ifndef _WIN32
/* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
P
Pauli 已提交
151
    int len;
R
Rich Salz 已提交
152 153 154 155 156
    char buff[80];

    if (md_tracefd > 0) {
        BIO_snprintf(buff, sizeof(buff),
                     "%c C%ld %%%d R%d\n",
R
Richard Levitte 已提交
157
                     shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
P
Pauli 已提交
158 159 160
        len = strlen(buff);
        if (write(md_tracefd, buff, len) != len)
            perror("shouldfail write failed");
161
#  ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
R
Richard Levitte 已提交
162
        if (shoulditfail) {
R
Rich Salz 已提交
163 164 165 166 167
            void *addrs[30];
            int num = backtrace(addrs, OSSL_NELEM(addrs));

            backtrace_symbols_fd(addrs, num, md_tracefd);
        }
168
#  endif
R
Rich Salz 已提交
169
    }
170
# endif
R
Rich Salz 已提交
171 172 173 174 175 176 177

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

R
Richard Levitte 已提交
178
    return shoulditfail;
R
Rich Salz 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191
}

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

192
void *CRYPTO_malloc(size_t num, const char *file, int line)
193 194 195
{
    void *ret = NULL;

R
Rich Salz 已提交
196
    INCREMENT(malloc_count);
197 198 199
    if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
        return malloc_impl(num, file, line);

200
    if (num == 0)
201 202
        return NULL;

R
Rich Salz 已提交
203
    FAILTEST();
204 205 206 207 208 209 210 211
    if (allow_customize) {
        /*
         * Disallow customization after the first allocation. We only set this
         * if necessary to avoid a store to the same cache line on every
         * allocation.
         */
        allow_customize = 0;
    }
212
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
R
Rich Salz 已提交
213 214 215 216 217 218
    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);
219
    }
R
Rich Salz 已提交
220
#else
R
Rich Salz 已提交
221
    (void)(file); (void)(line);
R
Rich Salz 已提交
222 223
    ret = malloc(num);
#endif
224

225 226 227
    return ret;
}

228
void *CRYPTO_zalloc(size_t num, const char *file, int line)
R
Rich Salz 已提交
229 230 231
{
    void *ret = CRYPTO_malloc(num, file, line);

R
Rich Salz 已提交
232
    FAILTEST();
R
Rich Salz 已提交
233 234 235 236 237
    if (ret != NULL)
        memset(ret, 0, num);
    return ret;
}

238
void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
239
{
R
Rich Salz 已提交
240
    INCREMENT(realloc_count);
241 242 243
    if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)
        return realloc_impl(str, num, file, line);

R
Rich Salz 已提交
244
    FAILTEST();
245 246
    if (str == NULL)
        return CRYPTO_malloc(num, file, line);
247

R
Rich Salz 已提交
248
    if (num == 0) {
249
        CRYPTO_free(str, file, line);
250
        return NULL;
R
Rich Salz 已提交
251
    }
252

253
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
R
Rich Salz 已提交
254 255 256 257 258 259 260 261
    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
R
Rich Salz 已提交
262
    (void)(file); (void)(line);
R
Rich Salz 已提交
263 264
#endif
    return realloc(str, num);
265

266
}
267

268
void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
269
                           const char *file, int line)
270 271 272 273 274 275
{
    void *ret = NULL;

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

R
Rich Salz 已提交
276
    if (num == 0) {
277
        CRYPTO_clear_free(str, old_len, file, line);
278
        return NULL;
R
Rich Salz 已提交
279
    }
280

R
Rich Salz 已提交
281 282
    /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
    if (num < old_len) {
283
        OPENSSL_cleanse((char*)str + num, old_len - num);
R
Rich Salz 已提交
284 285
        return str;
    }
286

287
    ret = CRYPTO_malloc(num, file, line);
288
    if (ret != NULL) {
R
Rich Salz 已提交
289
        memcpy(ret, str, old_len);
290 291
        CRYPTO_clear_free(str, old_len, file, line);
    }
292 293
    return ret;
}
294

295
void CRYPTO_free(void *str, const char *file, int line)
296
{
R
Rich Salz 已提交
297
    INCREMENT(free_count);
298 299 300 301 302
    if (free_impl != NULL && free_impl != &CRYPTO_free) {
        free_impl(str, file, line);
        return;
    }

303
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
R
Rich Salz 已提交
304
    if (call_malloc_debug) {
305
        CRYPTO_mem_debug_free(str, 0, file, line);
R
Rich Salz 已提交
306
        free(str);
307
        CRYPTO_mem_debug_free(str, 1, file, line);
R
Rich Salz 已提交
308 309 310 311 312 313
    } else {
        free(str);
    }
#else
    free(str);
#endif
314
}
315

316
void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
R
Rich Salz 已提交
317
{
R
Rich Salz 已提交
318
    if (str == NULL)
R
Rich Salz 已提交
319 320 321
        return;
    if (num)
        OPENSSL_cleanse(str, num);
322
    CRYPTO_free(str, file, line);
R
Rich Salz 已提交
323
}