Coverage Report

Created: 2023-06-08 06:40

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