Coverage Report

Created: 2025-06-13 06:58

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