Coverage Report

Created: 2025-12-31 06:58

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