ex_data.c 11.1 KB
Newer Older
R
Rich Salz 已提交
1
/*
M
Matt Caswell 已提交
2
 * Copyright 1995-2020 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 "crypto/cryptlib.h"
11
#include "internal/thread_once.h"
12

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

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

33
static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
34

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

38
DEFINE_RUN_ONCE_STATIC(do_ex_data_init)
39
{
40 41
    if (!OPENSSL_init_crypto(0, NULL))
        return 0;
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
                     void *from_d, int idx,
                     long argl, void *argp)
{
127
    return 1;
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
}

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
        return 0;
    }
    for (i = 0; i < mx; i++) {
F
FdaSilvaYY 已提交
238
        if (storage[i] != NULL && storage[i]->new_func != NULL) {
239 240 241 242 243
            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
{
    int mx, j, i;
257
    void *ptr;
258 259 260
    EX_CALLBACK *stack[10];
    EX_CALLBACK **storage = NULL;
    EX_CALLBACKS *ip;
261
    int toret = 0;
R
Rich Salz 已提交
262 263 264

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

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

284 285 286
    if (mx == 0)
        return 1;
    if (storage == NULL) {
R
Rich Salz 已提交
287
        CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
288 289
        return 0;
    }
T
Todd Short 已提交
290 291 292 293 294 295 296 297
    /*
     * Make sure the ex_data stack is at least |mx| elements long to avoid
     * issues in the for loop that follows; so go get the |mx|'th element
     * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
     * to itself. This is normally a no-op; but ensures the stack is the
     * proper size
     */
    if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
298
        goto err;
R
Rich Salz 已提交
299

300 301
    for (i = 0; i < mx; i++) {
        ptr = CRYPTO_get_ex_data(from, i);
F
FdaSilvaYY 已提交
302
        if (storage[i] != NULL && storage[i]->dup_func != NULL)
303 304 305
            if (!storage[i]->dup_func(to, from, &ptr, i,
                                      storage[i]->argl, storage[i]->argp))
                goto err;
306 307
        CRYPTO_set_ex_data(to, i, ptr);
    }
308 309
    toret = 1;
 err:
R
Rich Salz 已提交
310 311
    if (storage != stack)
        OPENSSL_free(storage);
312
    return toret;
313
}
314

R
Rich Salz 已提交
315 316 317 318 319 320

/*
 * 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)
321 322
{
    int mx, i;
323
    EX_CALLBACKS *ip;
324
    void *ptr;
325
    EX_CALLBACK *f;
326 327
    EX_CALLBACK *stack[10];
    EX_CALLBACK **storage = NULL;
R
Rich Salz 已提交
328

329
    if ((ip = get_and_lock(class_index)) == NULL)
330
        goto err;
R
Rich Salz 已提交
331

332
    mx = sk_EX_CALLBACK_num(ip->meth);
333
    if (mx > 0) {
R
Rich Salz 已提交
334 335 336 337
        if (mx < (int)OSSL_NELEM(stack))
            storage = stack;
        else
            storage = OPENSSL_malloc(sizeof(*storage) * mx);
338
        if (storage != NULL)
R
Rich Salz 已提交
339
            for (i = 0; i < mx; i++)
340
                storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
341
    }
342
    CRYPTO_THREAD_unlock(ex_data_lock);
R
Rich Salz 已提交
343

344
    for (i = 0; i < mx; i++) {
345 346 347 348 349 350 351 352
        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) {
353
            ptr = CRYPTO_get_ex_data(ad, i);
354
            f->free_func(obj, ptr, ad, i, f->argl, f->argp);
355 356
        }
    }
R
Rich Salz 已提交
357 358 359

    if (storage != stack)
        OPENSSL_free(storage);
360
 err:
R
Rich Salz 已提交
361 362
    sk_void_free(ad->sk);
    ad->sk = NULL;
363
}
364

365 366 367 368
/*
 * 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 已提交
369
int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
370 371 372 373 374 375
{
    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 已提交
376
            return 0;
377 378 379
        }
    }

R
Rich Salz 已提交
380
    for (i = sk_void_num(ad->sk); i <= idx; ++i) {
381 382
        if (!sk_void_push(ad->sk, NULL)) {
            CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
R
Rich Salz 已提交
383
            return 0;
384 385 386
        }
    }
    sk_void_set(ad->sk, idx, val);
R
Rich Salz 已提交
387
    return 1;
388 389 390 391 392 393
}

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