Coverage Report

Created: 2023-09-25 06:45

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