Coverage Report

Created: 2023-04-12 06:22

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