ex_data.c 10.6 KB
Newer Older
R
Rich Salz 已提交
1 2
/*
 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
B
Bodo Möller 已提交
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
B
Bodo Möller 已提交
8
 */
9

10
#include "internal/cryptlib_int.h"
11
#include "internal/thread_once.h"
12
#include <openssl/lhash.h>
13

14 15 16 17
/*
 * Each structure type (sometimes called a class), that supports
 * exdata has a stack of callbacks for each instance.
 */
18
struct ex_callback_st {
F
FdaSilvaYY 已提交
19 20
    long argl;                  /* Arbitrary long */
    void *argp;                 /* Arbitrary void * */
R
Rich Salz 已提交
21 22 23
    CRYPTO_EX_new *new_func;
    CRYPTO_EX_free *free_func;
    CRYPTO_EX_dup *dup_func;
24
};
25 26

/*
27 28
 * The state for each class.  This could just be a typedef, but
 * a structure allows future changes.
29
 */
30 31 32
typedef struct ex_callbacks_st {
    STACK_OF(EX_CALLBACK) *meth;
} EX_CALLBACKS;
33

34
static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
35

M
Matt Caswell 已提交
36
static CRYPTO_RWLOCK *ex_data_lock = NULL;
37 38
static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT;

39
DEFINE_RUN_ONCE_STATIC(do_ex_data_init)
40
{
M
Matt Caswell 已提交
41
    OPENSSL_init_crypto(0, NULL);
42
    ex_data_lock = CRYPTO_THREAD_lock_new();
43
    return ex_data_lock != NULL;
44 45
}

46
/*
47
 * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
R
Rich Salz 已提交
48
 * a given class.  On success, *holds the lock.*
49
 */
50
static EX_CALLBACKS *get_and_lock(int class_index)
51
{
52
    EX_CALLBACKS *ip;
R
Rich Salz 已提交
53 54

    if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
F
FdaSilvaYY 已提交
55
        CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);
R
Rich Salz 已提交
56 57 58
        return NULL;
    }

59 60 61 62
    if (!RUN_ONCE(&ex_data_init, do_ex_data_init)) {
        CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE);
        return NULL;
    }
63

M
Matt Caswell 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76
    if (ex_data_lock == NULL) {
        /*
         * This can happen in normal operation when using CRYPTO_mem_leaks().
         * The CRYPTO_mem_leaks() function calls OPENSSL_cleanup() which cleans
         * up the locks. Subsequently the BIO that CRYPTO_mem_leaks() uses gets
         * freed, which also attempts to free the ex_data. However
         * CRYPTO_mem_leaks() ensures that the ex_data is freed early (i.e.
         * before OPENSSL_cleanup() is called), so if we get here we can safely
         * ignore this operation. We just treat it as an error.
         */
         return NULL;
    }

R
Rich Salz 已提交
77
    ip = &ex_data[class_index];
78
    CRYPTO_THREAD_write_lock(ex_data_lock);
R
Rich Salz 已提交
79
    return ip;
80 81
}

82
static void cleanup_cb(EX_CALLBACK *funcs)
83 84 85
{
    OPENSSL_free(funcs);
}
86

87
/*
R
Rich Salz 已提交
88 89 90 91
 * Release all "ex_data" state to prevent memory leaks. This can't be made
 * thread-safe without overhauling a lot of stuff, and shouldn't really be
 * called under potential race-conditions anyway (it's for program shutdown
 * after all).
92
 */
93
void crypto_cleanup_all_ex_data_int(void)
94
{
R
Rich Salz 已提交
95
    int i;
96

R
Rich Salz 已提交
97
    for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
98
        EX_CALLBACKS *ip = &ex_data[i];
R
Rich Salz 已提交
99

100
        sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
R
Rich Salz 已提交
101
        ip->meth = NULL;
102
    }
M
Matt Caswell 已提交
103 104 105

    CRYPTO_THREAD_lock_free(ex_data_lock);
    ex_data_lock = NULL;
106 107
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

/*
 * Unregister a new index by replacing the callbacks with no-ops.
 * Any in-use instances are leaked.
 */
static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
                     long argl, void *argp)
{
}

static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
                       long argl, void *argp)
{
}

123
static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
                     void *from_d, int idx,
                     long argl, void *argp)
{
    return 0;
}

int CRYPTO_free_ex_index(int class_index, int idx)
{
    EX_CALLBACKS *ip = get_and_lock(class_index);
    EX_CALLBACK *a;
    int toret = 0;

    if (ip == NULL)
        return 0;
    if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
        goto err;
    a = sk_EX_CALLBACK_value(ip->meth, idx);
    if (a == NULL)
        goto err;
    a->new_func = dummy_new;
    a->dup_func = dummy_dup;
    a->free_func = dummy_free;
    toret = 1;
err:
148
    CRYPTO_THREAD_unlock(ex_data_lock);
149 150 151
    return toret;
}

152
/*
153
 * Register a new index.
154
 */
R
Rich Salz 已提交
155 156 157
int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
                            CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
                            CRYPTO_EX_free *free_func)
158 159
{
    int toret = -1;
160 161
    EX_CALLBACK *a;
    EX_CALLBACKS *ip = get_and_lock(class_index);
R
Rich Salz 已提交
162

163
    if (ip == NULL)
164
        return -1;
165 166 167 168 169 170 171

    if (ip->meth == NULL) {
        ip->meth = sk_EX_CALLBACK_new_null();
        /* We push an initial value on the stack because the SSL
         * "app_data" routines use ex_data index zero.  See RT 3710. */
        if (ip->meth == NULL
            || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
172
            CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
173 174 175 176
            goto err;
        }
    }

177
    a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
178
    if (a == NULL) {
R
Rich Salz 已提交
179 180
        CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
        goto err;
181 182 183 184 185 186
    }
    a->argl = argl;
    a->argp = argp;
    a->new_func = new_func;
    a->dup_func = dup_func;
    a->free_func = free_func;
187

188
    if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
R
Rich Salz 已提交
189 190 191 192
        CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
        OPENSSL_free(a);
        goto err;
    }
193 194
    toret = sk_EX_CALLBACK_num(ip->meth) - 1;
    (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
195

R
Rich Salz 已提交
196
 err:
197
    CRYPTO_THREAD_unlock(ex_data_lock);
198 199
    return toret;
}
200

201
/*
R
Rich Salz 已提交
202 203
 * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
 * calling new() callbacks for each index in the class used by this variable
204
 * Thread-safe by copying a class's array of "EX_CALLBACK" entries
R
Rich Salz 已提交
205 206
 * in the lock, then using them outside the lock. Note this only applies
 * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
207
 */
R
Rich Salz 已提交
208
int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
209 210 211
{
    int mx, i;
    void *ptr;
212 213 214
    EX_CALLBACK **storage = NULL;
    EX_CALLBACK *stack[10];
    EX_CALLBACKS *ip = get_and_lock(class_index);
R
Rich Salz 已提交
215

216
    if (ip == NULL)
217
        return 0;
R
Rich Salz 已提交
218

219
    ad->sk = NULL;
R
Rich Salz 已提交
220

221
    mx = sk_EX_CALLBACK_num(ip->meth);
222
    if (mx > 0) {
R
Rich Salz 已提交
223 224 225 226
        if (mx < (int)OSSL_NELEM(stack))
            storage = stack;
        else
            storage = OPENSSL_malloc(sizeof(*storage) * mx);
227
        if (storage != NULL)
R
Rich Salz 已提交
228
            for (i = 0; i < mx; i++)
229
                storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
230
    }
231
    CRYPTO_THREAD_unlock(ex_data_lock);
R
Rich Salz 已提交
232 233 234

    if (mx > 0 && storage == NULL) {
        CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
235 236 237 238 239 240 241 242 243
        return 0;
    }
    for (i = 0; i < mx; i++) {
        if (storage[i] && storage[i]->new_func) {
            ptr = CRYPTO_get_ex_data(ad, i);
            storage[i]->new_func(obj, ptr, ad, i,
                                 storage[i]->argl, storage[i]->argp);
        }
    }
R
Rich Salz 已提交
244 245
    if (storage != stack)
        OPENSSL_free(storage);
246 247
    return 1;
}
248

R
Rich Salz 已提交
249 250 251 252 253
/*
 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
 * for each index in the class used by this variable
 */
int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
254
                       const CRYPTO_EX_DATA *from)
255 256 257
{
    int mx, j, i;
    char *ptr;
258 259 260
    EX_CALLBACK *stack[10];
    EX_CALLBACK **storage = NULL;
    EX_CALLBACKS *ip;
R
Rich Salz 已提交
261 262 263

    if (from->sk == NULL)
        /* Nothing to copy over */
264
        return 1;
265
    if ((ip = get_and_lock(class_index)) == NULL)
266
        return 0;
R
Rich Salz 已提交
267

268
    mx = sk_EX_CALLBACK_num(ip->meth);
269 270 271 272
    j = sk_void_num(from->sk);
    if (j < mx)
        mx = j;
    if (mx > 0) {
R
Rich Salz 已提交
273 274 275 276
        if (mx < (int)OSSL_NELEM(stack))
            storage = stack;
        else
            storage = OPENSSL_malloc(sizeof(*storage) * mx);
277
        if (storage != NULL)
R
Rich Salz 已提交
278
            for (i = 0; i < mx; i++)
279
                storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
280
    }
281
    CRYPTO_THREAD_unlock(ex_data_lock);
R
Rich Salz 已提交
282 283 284

    if (mx > 0 && storage == NULL) {
        CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
285 286
        return 0;
    }
R
Rich Salz 已提交
287

288 289 290 291 292 293 294
    for (i = 0; i < mx; i++) {
        ptr = CRYPTO_get_ex_data(from, i);
        if (storage[i] && storage[i]->dup_func)
            storage[i]->dup_func(to, from, &ptr, i,
                                 storage[i]->argl, storage[i]->argp);
        CRYPTO_set_ex_data(to, i, ptr);
    }
R
Rich Salz 已提交
295 296
    if (storage != stack)
        OPENSSL_free(storage);
297 298
    return 1;
}
299

R
Rich Salz 已提交
300 301 302 303 304 305

/*
 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
 * each index in the class used by this variable
 */
void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
306 307
{
    int mx, i;
308
    EX_CALLBACKS *ip;
309
    void *ptr;
310
    EX_CALLBACK *f;
311 312
    EX_CALLBACK *stack[10];
    EX_CALLBACK **storage = NULL;
R
Rich Salz 已提交
313

314
    if ((ip = get_and_lock(class_index)) == NULL)
315
        goto err;
R
Rich Salz 已提交
316

317
    mx = sk_EX_CALLBACK_num(ip->meth);
318
    if (mx > 0) {
R
Rich Salz 已提交
319 320 321 322
        if (mx < (int)OSSL_NELEM(stack))
            storage = stack;
        else
            storage = OPENSSL_malloc(sizeof(*storage) * mx);
323
        if (storage != NULL)
R
Rich Salz 已提交
324
            for (i = 0; i < mx; i++)
325
                storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
326
    }
327
    CRYPTO_THREAD_unlock(ex_data_lock);
R
Rich Salz 已提交
328

329
    for (i = 0; i < mx; i++) {
330 331 332 333 334 335 336 337
        if (storage != NULL)
            f = storage[i];
        else {
            CRYPTO_THREAD_write_lock(ex_data_lock);
            f = sk_EX_CALLBACK_value(ip->meth, i);
            CRYPTO_THREAD_unlock(ex_data_lock);
        }
        if (f != NULL && f->free_func != NULL) {
338
            ptr = CRYPTO_get_ex_data(ad, i);
339
            f->free_func(obj, ptr, ad, i, f->argl, f->argp);
340 341
        }
    }
R
Rich Salz 已提交
342 343 344

    if (storage != stack)
        OPENSSL_free(storage);
345
 err:
R
Rich Salz 已提交
346 347
    sk_void_free(ad->sk);
    ad->sk = NULL;
348
}
349

350 351 352 353
/*
 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
 * particular index in the class used by this variable
 */
D
 
Dr. Stephen Henson 已提交
354
int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
355 356 357 358 359 360
{
    int i;

    if (ad->sk == NULL) {
        if ((ad->sk = sk_void_new_null()) == NULL) {
            CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
R
Rich Salz 已提交
361
            return 0;
362 363 364
        }
    }

R
Rich Salz 已提交
365
    for (i = sk_void_num(ad->sk); i <= idx; ++i) {
366 367
        if (!sk_void_push(ad->sk, NULL)) {
            CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
R
Rich Salz 已提交
368
            return 0;
369 370 371
        }
    }
    sk_void_set(ad->sk, idx, val);
R
Rich Salz 已提交
372
    return 1;
373 374 375 376 377 378
}

/*
 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
 * particular index in the class used by this variable
 */
379
void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
380
{
R
Rich Salz 已提交
381 382 383
    if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
        return NULL;
    return sk_void_value(ad->sk, idx);
384
}