Coverage Report

Created: 2020-08-14 21:15

/src/openssl/crypto/ex_data.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include "crypto/cryptlib.h"
11
#include "internal/thread_once.h"
12
13
DEFINE_STACK_OF(void)
14
8
15
8
int do_ex_data_init(OPENSSL_CTX *ctx)
16
8
{
17
8
    OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
18
8
19
8
    if (global == NULL)
20
8
        return 0;
21
8
22
8
    global->ex_data_lock = CRYPTO_THREAD_lock_new();
23
    return global->ex_data_lock != NULL;
24
}
25
26
/*
27
 * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
28
 * a given class.  On success, *holds the lock.*
29
42.8k
 * The |global| parameter is assumed to be non null (checked by the caller).
30
42.8k
 */
31
42.8k
static EX_CALLBACKS *get_and_lock(OSSL_EX_DATA_GLOBAL *global, int class_index)
32
42.8k
{
33
42.8k
    EX_CALLBACKS *ip;
34
0
35
0
    if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
36
0
        CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);
37
42.8k
        return NULL;
38
42.8k
    }
39
42.8k
40
0
    if (global->ex_data_lock == NULL) {
41
0
        /*
42
0
         * If we get here, someone (who?) cleaned up the lock, so just
43
0
         * treat it as an error.
44
0
         */
45
0
         return NULL;
46
42.8k
    }
47
42.8k
48
42.8k
    CRYPTO_THREAD_write_lock(global->ex_data_lock);
49
42.8k
    ip = &global->ex_data[class_index];
50
42.8k
    return ip;
51
}
52
53
4
static void cleanup_cb(EX_CALLBACK *funcs)
54
4
{
55
4
    OPENSSL_free(funcs);
56
}
57
58
/*
59
 * Release all "ex_data" state to prevent memory leaks. This can't be made
60
 * thread-safe without overhauling a lot of stuff, and shouldn't really be
61
 * called under potential race-conditions anyway (it's for program shutdown
62
 * after all).
63
 */
64
4
void crypto_cleanup_all_ex_data_int(OPENSSL_CTX *ctx)
65
4
{
66
4
    int i;
67
4
    OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
68
4
69
4
    if (global == NULL)
70
4
        return;
71
72
72
68
    for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
73
68
        EX_CALLBACKS *ip = &global->ex_data[i];
74
68
75
68
        sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
76
68
        ip->meth = NULL;
77
4
    }
78
4
79
4
    CRYPTO_THREAD_lock_free(global->ex_data_lock);
80
4
    global->ex_data_lock = NULL;
81
}
82
83
84
/*
85
 * Unregister a new index by replacing the callbacks with no-ops.
86
 * Any in-use instances are leaked.
87
 */
88
static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
89
0
                     long argl, void *argp)
90
0
{
91
}
92
93
static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
94
0
                       long argl, void *argp)
95
0
{
96
}
97
98
static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
99
                     void **from_d, int idx,
100
0
                     long argl, void *argp)
101
0
{
102
0
    return 1;
103
}
104
105
8
int crypto_free_ex_index_ex(OPENSSL_CTX *ctx, int class_index, int idx)
106
8
{
107
8
    EX_CALLBACKS *ip;
108
8
    EX_CALLBACK *a;
109
8
    int toret = 0;
110
8
    OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
111
8
112
8
    if (global == NULL)
113
8
        return 0;
114
8
115
8
    ip = get_and_lock(global, class_index);
116
8
    if (ip == NULL)
117
8
        return 0;
118
8
119
0
    if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
120
0
        goto err;
121
0
    a = sk_EX_CALLBACK_value(ip->meth, idx);
122
0
    if (a == NULL)
123
0
        goto err;
124
0
    a->new_func = dummy_new;
125
0
    a->dup_func = dummy_dup;
126
8
    a->free_func = dummy_free;
127
8
    toret = 1;
128
8
err:
129
0
    CRYPTO_THREAD_unlock(global->ex_data_lock);
130
    return toret;
131
}
132
8
133
8
int CRYPTO_free_ex_index(int class_index, int idx)
134
8
{
135
    return crypto_free_ex_index_ex(NULL, class_index, idx);
136
}
137
138
/*
139
 * Register a new index.
140
 */
141
int crypto_get_ex_new_index_ex(OPENSSL_CTX *ctx, int class_index, long argl,
142
                               void *argp, CRYPTO_EX_new *new_func,
143
8
                               CRYPTO_EX_dup *dup_func,
144
8
                               CRYPTO_EX_free *free_func)
145
8
{
146
8
    int toret = -1;
147
8
    EX_CALLBACK *a;
148
8
    EX_CALLBACKS *ip;
149
8
    OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
150
8
151
8
    if (global == NULL)
152
8
        return -1;
153
8
154
8
    ip = get_and_lock(global, class_index);
155
8
    if (ip == NULL)
156
8
        return -1;
157
8
158
8
    if (ip->meth == NULL) {
159
8
        ip->meth = sk_EX_CALLBACK_new_null();
160
8
        /* We push an initial value on the stack because the SSL
161
8
         * "app_data" routines use ex_data index zero.  See RT 3710. */
162
0
        if (ip->meth == NULL
163
0
            || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
164
0
            CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, ERR_R_MALLOC_FAILURE);
165
8
            goto err;
166
8
        }
167
8
    }
168
8
169
0
    a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
170
0
    if (a == NULL) {
171
0
        CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, ERR_R_MALLOC_FAILURE);
172
8
        goto err;
173
8
    }
174
8
    a->argl = argl;
175
8
    a->argp = argp;
176
8
    a->new_func = new_func;
177
8
    a->dup_func = dup_func;
178
8
    a->free_func = free_func;
179
0
180
0
    if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
181
0
        CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, ERR_R_MALLOC_FAILURE);
182
0
        OPENSSL_free(a);
183
8
        goto err;
184
8
    }
185
8
    toret = sk_EX_CALLBACK_num(ip->meth) - 1;
186
8
    (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
187
8
188
8
 err:
189
8
    CRYPTO_THREAD_unlock(global->ex_data_lock);
190
    return toret;
191
}
192
193
int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
194
0
                            CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
195
0
                            CRYPTO_EX_free *free_func)
196
0
{
197
0
    return crypto_get_ex_new_index_ex(NULL, class_index, argl, argp, new_func,
198
                                      dup_func, free_func);
199
}
200
201
/*
202
 * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
203
 * 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
205
 * in the lock, then using them outside the lock. Note this only applies
206
 * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
207
 */
208
21.4k
int crypto_new_ex_data_ex(OPENSSL_CTX *ctx, int class_index, void *obj,
209
21.4k
                          CRYPTO_EX_DATA *ad)
210
21.4k
{
211
21.4k
    int mx, i;
212
21.4k
    void *ptr;
213
21.4k
    EX_CALLBACK **storage = NULL;
214
21.4k
    EX_CALLBACK *stack[10];
215
21.4k
    EX_CALLBACKS *ip;
216
21.4k
    OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
217
21.4k
218
21.4k
    if (global == NULL)
219
21.4k
        return 0;
220
21.4k
221
21.4k
    ip = get_and_lock(global, class_index);
222
21.4k
    if (ip == NULL)
223
21.4k
        return 0;
224
21.4k
225
21.4k
    ad->ctx = ctx;
226
21.4k
    ad->sk = NULL;
227
21.4k
    mx = sk_EX_CALLBACK_num(ip->meth);
228
0
    if (mx > 0) {
229
0
        if (mx < (int)OSSL_NELEM(stack))
230
0
            storage = stack;
231
0
        else
232
0
            storage = OPENSSL_malloc(sizeof(*storage) * mx);
233
0
        if (storage != NULL)
234
0
            for (i = 0; i < mx; i++)
235
0
                storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
236
21.4k
    }
237
21.4k
    CRYPTO_THREAD_unlock(global->ex_data_lock);
238
21.4k
239
0
    if (mx > 0 && storage == NULL) {
240
0
        CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA_EX, ERR_R_MALLOC_FAILURE);
241
0
        return 0;
242
21.4k
    }
243
0
    for (i = 0; i < mx; i++) {
244
0
        if (storage[i] != NULL && storage[i]->new_func != NULL) {
245
0
            ptr = CRYPTO_get_ex_data(ad, i);
246
0
            storage[i]->new_func(obj, ptr, ad, i,
247
0
                                 storage[i]->argl, storage[i]->argp);
248
0
        }
249
21.4k
    }
250
21.4k
    if (storage != stack)
251
21.4k
        OPENSSL_free(storage);
252
21.4k
    return 1;
253
}
254
255
21.2k
int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
256
21.2k
{
257
21.2k
    return crypto_new_ex_data_ex(NULL, class_index, obj, ad);
258
}
259
260
/*
261
 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
262
 * for each index in the class used by this variable
263
 */
264
int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
265
0
                       const CRYPTO_EX_DATA *from)
266
0
{
267
0
    int mx, j, i;
268
0
    void *ptr;
269
0
    EX_CALLBACK *stack[10];
270
0
    EX_CALLBACK **storage = NULL;
271
0
    EX_CALLBACKS *ip;
272
0
    int toret = 0;
273
0
    OSSL_EX_DATA_GLOBAL *global;
274
0
275
0
    to->ctx = from->ctx;
276
0
    if (from->sk == NULL)
277
0
        /* Nothing to copy over */
278
0
        return 1;
279
0
280
0
    global = openssl_ctx_get_ex_data_global(from->ctx);
281
0
    if (global == NULL)
282
0
        return 0;
283
0
284
0
    ip = get_and_lock(global, class_index);
285
0
    if (ip == NULL)
286
0
        return 0;
287
0
288
0
    mx = sk_EX_CALLBACK_num(ip->meth);
289
0
    j = sk_void_num(from->sk);
290
0
    if (j < mx)
291
0
        mx = j;
292
0
    if (mx > 0) {
293
0
        if (mx < (int)OSSL_NELEM(stack))
294
0
            storage = stack;
295
0
        else
296
0
            storage = OPENSSL_malloc(sizeof(*storage) * mx);
297
0
        if (storage != NULL)
298
0
            for (i = 0; i < mx; i++)
299
0
                storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
300
0
    }
301
0
    CRYPTO_THREAD_unlock(global->ex_data_lock);
302
0
303
0
    if (mx == 0)
304
0
        return 1;
305
0
    if (storage == NULL) {
306
0
        CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
307
0
        return 0;
308
0
    }
309
0
    /*
310
0
     * Make sure the ex_data stack is at least |mx| elements long to avoid
311
0
     * issues in the for loop that follows; so go get the |mx|'th element
312
0
     * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
313
0
     * to itself. This is normally a no-op; but ensures the stack is the
314
0
     * proper size
315
0
     */
316
0
    if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
317
0
        goto err;
318
0
319
0
    for (i = 0; i < mx; i++) {
320
0
        ptr = CRYPTO_get_ex_data(from, i);
321
0
        if (storage[i] != NULL && storage[i]->dup_func != NULL)
322
0
            if (!storage[i]->dup_func(to, from, &ptr, i,
323
0
                                      storage[i]->argl, storage[i]->argp))
324
0
                goto err;
325
0
        CRYPTO_set_ex_data(to, i, ptr);
326
0
    }
327
0
    toret = 1;
328
0
 err:
329
    if (storage != stack)
330
        OPENSSL_free(storage);
331
    return toret;
332
}
333
334
335
/*
336
21.4k
 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
337
21.4k
 * each index in the class used by this variable
338
21.4k
 */
339
21.4k
void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
340
21.4k
{
341
21.4k
    int mx, i;
342
21.4k
    EX_CALLBACKS *ip;
343
21.4k
    void *ptr;
344
21.4k
    EX_CALLBACK *f;
345
21.4k
    EX_CALLBACK *stack[10];
346
21.4k
    EX_CALLBACK **storage = NULL;
347
21.4k
    OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ad->ctx);
348
21.4k
349
21.4k
    if (global == NULL)
350
21.4k
        goto err;
351
21.4k
352
21.4k
    ip = get_and_lock(global, class_index);
353
4
    if (ip == NULL)
354
4
        goto err;
355
0
356
0
    mx = sk_EX_CALLBACK_num(ip->meth);
357
4
    if (mx > 0) {
358
12
        if (mx < (int)OSSL_NELEM(stack))
359
8
            storage = stack;
360
4
        else
361
21.4k
            storage = OPENSSL_malloc(sizeof(*storage) * mx);
362
21.4k
        if (storage != NULL)
363
21.4k
            for (i = 0; i < mx; i++)
364
8
                storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
365
8
    }
366
0
    CRYPTO_THREAD_unlock(global->ex_data_lock);
367
0
368
0
    for (i = 0; i < mx; i++) {
369
0
        if (storage != NULL)
370
0
            f = storage[i];
371
8
        else {
372
4
            CRYPTO_THREAD_write_lock(global->ex_data_lock);
373
4
            f = sk_EX_CALLBACK_value(ip->meth, i);
374
4
            CRYPTO_THREAD_unlock(global->ex_data_lock);
375
8
        }
376
21.4k
        if (f != NULL && f->free_func != NULL) {
377
21.4k
            ptr = CRYPTO_get_ex_data(ad, i);
378
21.4k
            f->free_func(obj, ptr, ad, i, f->argl, f->argp);
379
21.4k
        }
380
21.4k
    }
381
21.4k
382
21.4k
    if (storage != stack)
383
21.4k
        OPENSSL_free(storage);
384
 err:
385
    sk_void_free(ad->sk);
386
    ad->sk = NULL;
387
    ad->ctx = NULL;
388
}
389
390
/*
391
8
 * Allocate a given CRYPTO_EX_DATA item using the class specific allocation
392
8
 * function
393
8
 */
394
8
int CRYPTO_alloc_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad,
395
8
                         int idx)
396
8
{
397
8
    EX_CALLBACK *f;
398
8
    EX_CALLBACKS *ip;
399
8
    void *curval;
400
8
    OSSL_EX_DATA_GLOBAL *global;
401
8
402
8
    curval = CRYPTO_get_ex_data(ad, idx);
403
8
    /* Already there, no need to allocate */
404
8
    if (curval != NULL)
405
8
        return 1;
406
8
407
8
    global = openssl_ctx_get_ex_data_global(ad->ctx);
408
8
    if (global == NULL)
409
8
        return 0;
410
8
411
8
    ip = get_and_lock(global, class_index);
412
8
    if (ip == NULL)
413
8
        return 0;
414
8
    f = sk_EX_CALLBACK_value(ip->meth, idx);
415
8
    CRYPTO_THREAD_unlock(global->ex_data_lock);
416
8
417
8
    /*
418
8
     * This should end up calling CRYPTO_set_ex_data(), which allocates
419
8
     * everything necessary to support placing the new data in the right spot.
420
8
     */
421
8
    if (f->new_func == NULL)
422
8
        return 0;
423
424
    f->new_func(obj, NULL, ad, idx, f->argl, f->argp);
425
426
    return 1;
427
}
428
429
8
/*
430
8
 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
431
8
 * particular index in the class used by this variable
432
8
 */
433
8
int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
434
0
{
435
0
    int i;
436
0
437
8
    if (ad->sk == NULL) {
438
8
        if ((ad->sk = sk_void_new_null()) == NULL) {
439
24
            CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
440
16
            return 0;
441
0
        }
442
0
    }
443
0
444
16
    for (i = sk_void_num(ad->sk); i <= idx; ++i) {
445
8
        if (!sk_void_push(ad->sk, NULL)) {
446
8
            CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
447
8
            return 0;
448
        }
449
    }
450
    sk_void_set(ad->sk, idx, val);
451
    return 1;
452
}
453
454
92
/*
455
92
 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
456
8
 * particular index in the class used by this variable
457
84
 */
458
84
void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
459
{
460
    if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
461
8
        return NULL;
462
8
    return sk_void_value(ad->sk, idx);
463
8
}
464
465
OPENSSL_CTX *crypto_ex_data_get_openssl_ctx(const CRYPTO_EX_DATA *ad)
466
{
467
    return ad->ctx;
468
}