Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/ex_data.c
Line
Count
Source (jump to first uncovered line)
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
185
{
16
185
    OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
17
18
185
    if (global == NULL)
19
0
        return 0;
20
21
185
    global->ex_data_lock = CRYPTO_THREAD_lock_new();
22
185
    return global->ex_data_lock != NULL;
23
185
}
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.62M
{
32
9.62M
    EX_CALLBACKS *ip;
33
34
9.62M
    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.62M
    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.62M
    if (!CRYPTO_THREAD_write_lock(global->ex_data_lock))
48
0
        return NULL;
49
9.62M
    ip = &global->ex_data[class_index];
50
9.62M
    return ip;
51
9.62M
}
52
53
static void cleanup_cb(EX_CALLBACK *funcs)
54
137
{
55
137
    OPENSSL_free(funcs);
56
137
}
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
123
{
66
123
    int i;
67
123
    OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
68
69
123
    if (global == NULL)
70
0
        return;
71
72
2.33k
    for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
73
2.21k
        EX_CALLBACKS *ip = &global->ex_data[i];
74
75
2.21k
        sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
76
2.21k
        ip->meth = NULL;
77
2.21k
    }
78
79
123
    CRYPTO_THREAD_lock_free(global->ex_data_lock);
80
123
    global->ex_data_lock = NULL;
81
123
}
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
92
{
107
92
    EX_CALLBACKS *ip;
108
92
    EX_CALLBACK *a;
109
92
    int toret = 0;
110
92
    OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
111
112
92
    if (global == NULL)
113
0
        return 0;
114
115
92
    ip = get_and_lock(global, class_index);
116
92
    if (ip == NULL)
117
0
        return 0;
118
119
92
    if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
120
92
        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
92
err:
129
92
    CRYPTO_THREAD_unlock(global->ex_data_lock);
130
92
    return toret;
131
0
}
132
133
int CRYPTO_free_ex_index(int class_index, int idx)
134
92
{
135
92
    return ossl_crypto_free_ex_index_ex(NULL, class_index, idx);
136
92
}
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
147
{
148
147
    int toret = -1;
149
147
    EX_CALLBACK *a;
150
147
    EX_CALLBACKS *ip;
151
147
    OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
152
153
147
    if (global == NULL)
154
0
        return -1;
155
156
147
    ip = get_and_lock(global, class_index);
157
147
    if (ip == NULL)
158
0
        return -1;
159
160
147
    if (ip->meth == NULL) {
161
60
        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
60
        if (ip->meth == NULL
165
60
            || !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
60
    }
172
173
147
    a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
174
147
    if (a == NULL) {
175
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
176
0
        goto err;
177
0
    }
178
147
    a->argl = argl;
179
147
    a->argp = argp;
180
147
    a->new_func = new_func;
181
147
    a->dup_func = dup_func;
182
147
    a->free_func = free_func;
183
147
    a->priority = priority;
184
185
147
    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
147
    toret = sk_EX_CALLBACK_num(ip->meth) - 1;
191
147
    (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
192
193
147
 err:
194
147
    CRYPTO_THREAD_unlock(global->ex_data_lock);
195
147
    return toret;
196
147
}
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
26
{
202
26
    return ossl_crypto_get_ex_new_index_ex(NULL, class_index, argl, argp,
203
26
                                           new_func, dup_func, free_func, 0);
204
26
}
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
21.4M
{
216
21.4M
    int mx, i;
217
21.4M
    void *ptr;
218
21.4M
    EX_CALLBACK **storage = NULL;
219
21.4M
    EX_CALLBACK *stack[10];
220
21.4M
    EX_CALLBACKS *ip;
221
21.4M
    OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
222
223
21.4M
    if (global == NULL)
224
0
        return 0;
225
226
21.4M
    ip = get_and_lock(global, class_index);
227
21.4M
    if (ip == NULL)
228
0
        return 0;
229
230
21.4M
    ad->ctx = ctx;
231
21.4M
    ad->sk = NULL;
232
21.4M
    mx = sk_EX_CALLBACK_num(ip->meth);
233
21.4M
    if (mx > 0) {
234
27.5k
        if (mx < (int)OSSL_NELEM(stack))
235
27.5k
            storage = stack;
236
0
        else
237
0
            storage = OPENSSL_malloc(sizeof(*storage) * mx);
238
27.5k
        if (storage != NULL)
239
82.7k
            for (i = 0; i < mx; i++)
240
55.1k
                storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
241
27.5k
    }
242
21.4M
    CRYPTO_THREAD_unlock(global->ex_data_lock);
243
244
21.4M
    if (mx > 0 && storage == NULL) {
245
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
246
0
        return 0;
247
0
    }
248
21.5M
    for (i = 0; i < mx; i++) {
249
55.1k
        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
55.1k
    }
255
21.4M
    if (storage != stack)
256
21.4M
        OPENSSL_free(storage);
257
21.4M
    return 1;
258
21.4M
}
259
260
int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
261
21.0M
{
262
21.0M
    return ossl_crypto_new_ex_data_ex(NULL, class_index, obj, ad);
263
21.0M
}
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
20.0k
{
272
20.0k
    int mx, j, i;
273
20.0k
    void *ptr;
274
20.0k
    EX_CALLBACK *stack[10];
275
20.0k
    EX_CALLBACK **storage = NULL;
276
20.0k
    EX_CALLBACKS *ip;
277
20.0k
    int toret = 0;
278
20.0k
    OSSL_EX_DATA_GLOBAL *global;
279
280
20.0k
    to->ctx = from->ctx;
281
20.0k
    if (from->sk == NULL)
282
        /* Nothing to copy over */
283
20.0k
        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
55.3k
{
346
55.3k
    const struct ex_callback_entry *ap = (const struct ex_callback_entry *)a;
347
55.3k
    const struct ex_callback_entry *bp = (const struct ex_callback_entry *)b;
348
349
55.3k
    if (ap->excb == bp->excb)
350
0
        return 0;
351
352
55.3k
    if (ap->excb == NULL)
353
55.1k
        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
21.4M
{
367
21.4M
    int mx, i;
368
21.4M
    EX_CALLBACKS *ip;
369
21.4M
    void *ptr;
370
21.4M
    const EX_CALLBACK *f;
371
21.4M
    struct ex_callback_entry stack[10];
372
21.4M
    struct ex_callback_entry *storage = NULL;
373
21.4M
    OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ad->ctx);
374
375
21.4M
    if (global == NULL)
376
0
        goto err;
377
378
21.4M
    ip = get_and_lock(global, class_index);
379
21.4M
    if (ip == NULL)
380
0
        goto err;
381
382
21.4M
    mx = sk_EX_CALLBACK_num(ip->meth);
383
21.4M
    if (mx > 0) {
384
55.1k
        if (mx < (int)OSSL_NELEM(stack))
385
55.1k
            storage = stack;
386
5
        else
387
5
            storage = OPENSSL_malloc(sizeof(*storage) * mx);
388
55.1k
        if (storage != NULL)
389
165k
            for (i = 0; i < mx; i++) {
390
110k
                storage[i].excb = sk_EX_CALLBACK_value(ip->meth, i);
391
110k
                storage[i].index = i;
392
110k
            }
393
55.1k
    }
394
21.4M
    CRYPTO_THREAD_unlock(global->ex_data_lock);
395
396
21.4M
    if (storage != NULL) {
397
        /* Sort according to priority. High priority first */
398
55.1k
        qsort(storage, mx, sizeof(*storage), ex_callback_compare);
399
165k
        for (i = 0; i < mx; i++) {
400
110k
            f = storage[i].excb;
401
402
110k
            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
110k
        }
407
55.1k
    }
408
409
21.4M
    if (storage != stack)
410
21.4M
        OPENSSL_free(storage);
411
21.4M
 err:
412
21.4M
    sk_void_free(ad->sk);
413
21.4M
    ad->sk = NULL;
414
21.4M
    ad->ctx = NULL;
415
21.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
14.4k
{
469
14.4k
    int i;
470
471
14.4k
    if (ad->sk == NULL) {
472
14.3k
        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
14.3k
    }
477
478
43.3k
    for (i = sk_void_num(ad->sk); i <= idx; ++i) {
479
28.8k
        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
28.8k
    }
484
14.4k
    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
14.4k
    return 1;
490
14.4k
}
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
255M
{
498
255M
    if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
499
0
        return NULL;
500
255M
    return sk_void_value(ad->sk, idx);
501
255M
}
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
}