Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/initthread.c
Line
Count
Source
1
/*
2
 * Copyright 2019-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 <openssl/crypto.h>
11
#include <openssl/core_dispatch.h>
12
#include "crypto/cryptlib.h"
13
#include "prov/providercommon.h"
14
#include "internal/thread_once.h"
15
#include "crypto/context.h"
16
17
#ifdef FIPS_MODULE
18
#include "prov/provider_ctx.h"
19
20
/*
21
 * Thread aware code may want to be told about thread stop events. We register
22
 * to hear about those thread stop events when we see a new thread has started.
23
 * We call the ossl_init_thread_start function to do that. In the FIPS provider
24
 * we have our own copy of ossl_init_thread_start, which cascades notifications
25
 * about threads stopping from libcrypto to all the code in the FIPS provider
26
 * that needs to know about it.
27
 *
28
 * The FIPS provider tells libcrypto about which threads it is interested in
29
 * by calling "c_thread_start" which is a function pointer created during
30
 * provider initialisation (i.e. OSSL_provider_init).
31
 */
32
extern OSSL_FUNC_core_thread_start_fn *c_thread_start;
33
#endif
34
35
typedef struct thread_event_handler_st THREAD_EVENT_HANDLER;
36
struct thread_event_handler_st {
37
#ifndef FIPS_MODULE
38
    const void *index;
39
#endif
40
    void *arg;
41
    OSSL_thread_stop_handler_fn handfn;
42
    THREAD_EVENT_HANDLER *next;
43
};
44
45
#ifndef FIPS_MODULE
46
DEFINE_SPECIAL_STACK_OF(THREAD_EVENT_HANDLER_PTR, THREAD_EVENT_HANDLER *)
47
48
typedef struct global_tevent_register_st GLOBAL_TEVENT_REGISTER;
49
struct global_tevent_register_st {
50
    STACK_OF(THREAD_EVENT_HANDLER_PTR) *skhands;
51
    CRYPTO_RWLOCK *lock;
52
};
53
54
static GLOBAL_TEVENT_REGISTER *glob_tevent_reg = NULL;
55
56
static CRYPTO_ONCE tevent_register_runonce = CRYPTO_ONCE_STATIC_INIT;
57
58
DEFINE_RUN_ONCE_STATIC(create_global_tevent_register)
59
305
{
60
305
    glob_tevent_reg = OPENSSL_zalloc(sizeof(*glob_tevent_reg));
61
305
    if (glob_tevent_reg == NULL)
62
0
        return 0;
63
64
305
    glob_tevent_reg->skhands = sk_THREAD_EVENT_HANDLER_PTR_new_null();
65
305
    glob_tevent_reg->lock = CRYPTO_THREAD_lock_new();
66
305
    if (glob_tevent_reg->skhands == NULL || glob_tevent_reg->lock == NULL) {
67
0
        sk_THREAD_EVENT_HANDLER_PTR_free(glob_tevent_reg->skhands);
68
0
        CRYPTO_THREAD_lock_free(glob_tevent_reg->lock);
69
0
        OPENSSL_free(glob_tevent_reg);
70
0
        glob_tevent_reg = NULL;
71
0
        return 0;
72
0
    }
73
74
305
    return 1;
75
305
}
76
77
static GLOBAL_TEVENT_REGISTER *get_global_tevent_register(void)
78
1.32k
{
79
1.32k
    if (!RUN_ONCE(&tevent_register_runonce, create_global_tevent_register))
80
0
        return NULL;
81
1.32k
    return glob_tevent_reg;
82
1.32k
}
83
#endif
84
85
#ifndef FIPS_MODULE
86
static int  init_thread_push_handlers(THREAD_EVENT_HANDLER **hands);
87
static void init_thread_remove_handlers(THREAD_EVENT_HANDLER **handsin);
88
static void init_thread_destructor(void *hands);
89
static int  init_thread_deregister(void *arg, int all);
90
#endif
91
static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands);
92
93
static THREAD_EVENT_HANDLER **
94
init_get_thread_local(CRYPTO_THREAD_LOCAL *local, int alloc, int keep)
95
647
{
96
647
    THREAD_EVENT_HANDLER **hands = CRYPTO_THREAD_get_local(local);
97
98
647
    if (alloc) {
99
254
        if (hands == NULL) {
100
101
199
            if ((hands = OPENSSL_zalloc(sizeof(*hands))) == NULL)
102
0
                return NULL;
103
104
199
            if (!CRYPTO_THREAD_set_local(local, hands)) {
105
0
                OPENSSL_free(hands);
106
0
                return NULL;
107
0
            }
108
109
199
#ifndef FIPS_MODULE
110
199
            if (!init_thread_push_handlers(hands)) {
111
0
                CRYPTO_THREAD_set_local(local, NULL);
112
0
                OPENSSL_free(hands);
113
0
                return NULL;
114
0
            }
115
199
#endif
116
199
        }
117
393
    } else if (!keep) {
118
199
        CRYPTO_THREAD_set_local(local, NULL);
119
199
    }
120
121
647
    return hands;
122
647
}
123
124
#ifndef FIPS_MODULE
125
/*
126
 * Since per-thread-specific-data destructors are not universally
127
 * available, i.e. not on Windows, only below CRYPTO_THREAD_LOCAL key
128
 * is assumed to have destructor associated. And then an effort is made
129
 * to call this single destructor on non-pthread platform[s].
130
 *
131
 * Initial value is "impossible". It is used as guard value to shortcut
132
 * destructor for threads terminating before libcrypto is initialized or
133
 * after it's de-initialized. Access to the key doesn't have to be
134
 * serialized for the said threads, because they didn't use libcrypto
135
 * and it doesn't matter if they pick "impossible" or dereference real
136
 * key value and pull NULL past initialization in the first thread that
137
 * intends to use libcrypto.
138
 */
139
static union {
140
    long sane;
141
    CRYPTO_THREAD_LOCAL value;
142
} destructor_key = { -1 };
143
144
/*
145
 * The thread event handler list is a thread specific linked list
146
 * of callback functions which are invoked in list order by the
147
 * current thread in case of certain events. (Currently, there is
148
 * only one type of event, the 'thread stop' event.)
149
 *
150
 * We also keep a global reference to that linked list, so that we
151
 * can deregister handlers if necessary before all the threads are
152
 * stopped.
153
 */
154
static int init_thread_push_handlers(THREAD_EVENT_HANDLER **hands)
155
305
{
156
305
    int ret;
157
305
    GLOBAL_TEVENT_REGISTER *gtr;
158
159
305
    gtr = get_global_tevent_register();
160
305
    if (gtr == NULL)
161
0
        return 0;
162
163
305
    if (!CRYPTO_THREAD_write_lock(gtr->lock))
164
0
        return 0;
165
305
    ret = (sk_THREAD_EVENT_HANDLER_PTR_push(gtr->skhands, hands) != 0);
166
305
    CRYPTO_THREAD_unlock(gtr->lock);
167
168
305
    return ret;
169
305
}
170
171
static void init_thread_remove_handlers(THREAD_EVENT_HANDLER **handsin)
172
305
{
173
305
    GLOBAL_TEVENT_REGISTER *gtr;
174
305
    int i;
175
176
305
    gtr = get_global_tevent_register();
177
305
    if (gtr == NULL)
178
0
        return;
179
305
    if (!CRYPTO_THREAD_write_lock(gtr->lock))
180
0
        return;
181
305
    for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) {
182
305
        THREAD_EVENT_HANDLER **hands
183
305
            = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i);
184
185
305
        if (hands == handsin) {
186
305
            sk_THREAD_EVENT_HANDLER_PTR_delete(gtr->skhands, i);
187
305
            CRYPTO_THREAD_unlock(gtr->lock);
188
305
            return;
189
305
        }
190
305
    }
191
0
    CRYPTO_THREAD_unlock(gtr->lock);
192
0
    return;
193
305
}
194
195
static void init_thread_destructor(void *hands)
196
0
{
197
0
    init_thread_stop(NULL, (THREAD_EVENT_HANDLER **)hands);
198
0
    init_thread_remove_handlers(hands);
199
0
    OPENSSL_free(hands);
200
0
}
201
202
static CRYPTO_ONCE ossl_init_thread_runonce = CRYPTO_ONCE_STATIC_INIT;
203
static CRYPTO_THREAD_ID recursion_guard = (CRYPTO_THREAD_ID)-1;
204
205
DEFINE_RUN_ONCE_STATIC(ossl_init_thread_once)
206
196
{
207
196
    recursion_guard = CRYPTO_THREAD_get_current_id();
208
196
    if (!CRYPTO_THREAD_init_local(&destructor_key.value,
209
196
                                  init_thread_destructor))
210
0
        return 0;
211
212
196
    recursion_guard = (CRYPTO_THREAD_ID)0;
213
196
    return 1;
214
196
}
215
216
int ossl_init_thread(void)
217
1.74k
{
218
1.74k
    if (CRYPTO_THREAD_compare_id(recursion_guard,
219
1.74k
                                 CRYPTO_THREAD_get_current_id()))
220
196
        return 1;
221
1.54k
    if (!RUN_ONCE(&ossl_init_thread_runonce, ossl_init_thread_once))
222
0
        return 0;
223
1.54k
    return 1;
224
1.54k
}
225
226
void ossl_cleanup_thread(void)
227
305
{
228
305
    init_thread_deregister(NULL, 1);
229
305
    CRYPTO_THREAD_cleanup_local(&destructor_key.value);
230
305
    destructor_key.sane = -1;
231
305
}
232
233
void OPENSSL_thread_stop_ex(OSSL_LIB_CTX *ctx)
234
0
{
235
0
    ctx = ossl_lib_ctx_get_concrete(ctx);
236
    /*
237
     * It would be nice if we could figure out a way to do this on all threads
238
     * that have used the OSSL_LIB_CTX when the context is freed. This is
239
     * currently not possible due to the use of thread local variables.
240
     */
241
0
    ossl_ctx_thread_stop(ctx);
242
0
}
243
244
void OPENSSL_thread_stop(void)
245
305
{
246
305
    if (destructor_key.sane != -1) {
247
305
        THREAD_EVENT_HANDLER **hands
248
305
            = init_get_thread_local(&destructor_key.value, 0, 0);
249
305
        init_thread_stop(NULL, hands);
250
251
305
        init_thread_remove_handlers(hands);
252
305
        OPENSSL_free(hands);
253
305
    }
254
305
}
255
256
void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx)
257
300
{
258
300
    if (destructor_key.sane != -1) {
259
300
        THREAD_EVENT_HANDLER **hands
260
300
            = init_get_thread_local(&destructor_key.value, 0, 1);
261
300
        init_thread_stop(ctx, hands);
262
300
    }
263
300
}
264
265
#else
266
267
static void ossl_arg_thread_stop(void *arg);
268
269
/* Register the current thread so that we are informed if it gets stopped */
270
int ossl_thread_register_fips(OSSL_LIB_CTX *libctx)
271
{
272
    return c_thread_start(FIPS_get_core_handle(libctx), ossl_arg_thread_stop,
273
                          libctx);
274
}
275
276
void *ossl_thread_event_ctx_new(OSSL_LIB_CTX *libctx)
277
{
278
    THREAD_EVENT_HANDLER **hands = NULL;
279
    CRYPTO_THREAD_LOCAL *tlocal = OPENSSL_zalloc(sizeof(*tlocal));
280
281
    if (tlocal == NULL)
282
        return NULL;
283
284
    if (!CRYPTO_THREAD_init_local(tlocal, NULL))
285
        goto deinit;
286
287
    hands = OPENSSL_zalloc(sizeof(*hands));
288
    if (hands == NULL)
289
        goto err;
290
291
    if (!CRYPTO_THREAD_set_local(tlocal, hands))
292
        goto err;
293
294
    /*
295
     * We should ideally call ossl_thread_register_fips() here. This function
296
     * is called during the startup of the FIPS provider and we need to ensure
297
     * that the main thread is registered to receive thread callbacks in order
298
     * to free |hands| that we allocated above. However we are too early in
299
     * the FIPS provider initialisation that FIPS_get_core_handle() doesn't work
300
     * yet. So we defer this to the main provider OSSL_provider_init_int()
301
     * function.
302
     */
303
304
    return tlocal;
305
 err:
306
    OPENSSL_free(hands);
307
    CRYPTO_THREAD_cleanup_local(tlocal);
308
 deinit:
309
    OPENSSL_free(tlocal);
310
    return NULL;
311
}
312
313
void ossl_thread_event_ctx_free(void *tlocal)
314
{
315
    CRYPTO_THREAD_cleanup_local(tlocal);
316
    OPENSSL_free(tlocal);
317
}
318
319
static void ossl_arg_thread_stop(void *arg)
320
{
321
    ossl_ctx_thread_stop((OSSL_LIB_CTX *)arg);
322
}
323
324
void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx)
325
{
326
    THREAD_EVENT_HANDLER **hands;
327
    CRYPTO_THREAD_LOCAL *local
328
        = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX);
329
330
    if (local == NULL)
331
        return;
332
    hands = init_get_thread_local(local, 0, 0);
333
    init_thread_stop(ctx, hands);
334
    OPENSSL_free(hands);
335
}
336
#endif /* FIPS_MODULE */
337
338
339
static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands)
340
605
{
341
605
    THREAD_EVENT_HANDLER *curr, *prev = NULL, *tmp;
342
605
#ifndef FIPS_MODULE
343
605
    GLOBAL_TEVENT_REGISTER *gtr;
344
605
#endif
345
346
    /* Can't do much about this */
347
605
    if (hands == NULL)
348
300
        return;
349
350
305
#ifndef FIPS_MODULE
351
305
    gtr = get_global_tevent_register();
352
305
    if (gtr == NULL)
353
0
        return;
354
355
305
    if (!CRYPTO_THREAD_write_lock(gtr->lock))
356
0
        return;
357
305
#endif
358
359
305
    curr = *hands;
360
699
    while (curr != NULL) {
361
394
        if (arg != NULL && curr->arg != arg) {
362
0
            prev = curr;
363
0
            curr = curr->next;
364
0
            continue;
365
0
        }
366
394
        curr->handfn(curr->arg);
367
394
        if (prev == NULL)
368
394
            *hands = curr->next;
369
0
        else
370
0
            prev->next = curr->next;
371
372
394
        tmp = curr;
373
394
        curr = curr->next;
374
375
394
        OPENSSL_free(tmp);
376
394
    }
377
305
#ifndef FIPS_MODULE
378
305
    CRYPTO_THREAD_unlock(gtr->lock);
379
305
#endif
380
305
}
381
382
int ossl_init_thread_start(const void *index, void *arg,
383
                           OSSL_thread_stop_handler_fn handfn)
384
394
{
385
394
    THREAD_EVENT_HANDLER **hands;
386
394
    THREAD_EVENT_HANDLER *hand;
387
#ifdef FIPS_MODULE
388
    OSSL_LIB_CTX *ctx = arg;
389
390
    /*
391
     * In FIPS mode the list of THREAD_EVENT_HANDLERs is unique per combination
392
     * of OSSL_LIB_CTX and thread. This is because in FIPS mode each
393
     * OSSL_LIB_CTX gets informed about thread stop events individually.
394
     */
395
    CRYPTO_THREAD_LOCAL *local
396
        = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX);
397
#else
398
    /*
399
     * Outside of FIPS mode the list of THREAD_EVENT_HANDLERs is unique per
400
     * thread, but may hold multiple OSSL_LIB_CTXs. We only get told about
401
     * thread stop events globally, so we have to ensure all affected
402
     * OSSL_LIB_CTXs are informed.
403
     */
404
394
    CRYPTO_THREAD_LOCAL *local = &destructor_key.value;
405
394
#endif
406
407
394
    hands = init_get_thread_local(local, 1, 0);
408
394
    if (hands == NULL)
409
0
        return 0;
410
411
#ifdef FIPS_MODULE
412
    if (*hands == NULL) {
413
        /*
414
         * We've not yet registered any handlers for this thread. We need to get
415
         * libcrypto to tell us about later thread stop events. c_thread_start
416
         * is a callback to libcrypto defined in fipsprov.c
417
         */
418
        if (!ossl_thread_register_fips(ctx))
419
            return 0;
420
    }
421
#endif
422
423
394
    hand = OPENSSL_malloc(sizeof(*hand));
424
394
    if (hand == NULL)
425
0
        return 0;
426
427
394
    hand->handfn = handfn;
428
394
    hand->arg = arg;
429
394
#ifndef FIPS_MODULE
430
394
    hand->index = index;
431
394
#endif
432
394
    hand->next = *hands;
433
394
    *hands = hand;
434
435
394
    return 1;
436
394
}
437
438
#ifndef FIPS_MODULE
439
static int init_thread_deregister(void *index, int all)
440
406
{
441
406
    GLOBAL_TEVENT_REGISTER *gtr;
442
406
    int i;
443
444
406
    gtr = get_global_tevent_register();
445
406
    if (gtr == NULL)
446
0
        return 0;
447
406
    if (!all) {
448
101
        if (!CRYPTO_THREAD_write_lock(gtr->lock))
449
0
            return 0;
450
305
    } else {
451
305
        glob_tevent_reg = NULL;
452
305
    }
453
406
    for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) {
454
0
        THREAD_EVENT_HANDLER **hands
455
0
            = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i);
456
0
        THREAD_EVENT_HANDLER *curr = NULL, *prev = NULL, *tmp;
457
458
0
        if (hands == NULL) {
459
0
            if (!all)
460
0
                CRYPTO_THREAD_unlock(gtr->lock);
461
0
            return 0;
462
0
        }
463
0
        curr = *hands;
464
0
        while (curr != NULL) {
465
0
            if (all || curr->index == index) {
466
0
                if (prev != NULL)
467
0
                    prev->next = curr->next;
468
0
                else
469
0
                    *hands = curr->next;
470
0
                tmp = curr;
471
0
                curr = curr->next;
472
0
                OPENSSL_free(tmp);
473
0
                continue;
474
0
            }
475
0
            prev = curr;
476
0
            curr = curr->next;
477
0
        }
478
0
        if (all)
479
0
            OPENSSL_free(hands);
480
0
    }
481
406
    if (all) {
482
305
        CRYPTO_THREAD_lock_free(gtr->lock);
483
305
        sk_THREAD_EVENT_HANDLER_PTR_free(gtr->skhands);
484
305
        OPENSSL_free(gtr);
485
305
    } else {
486
101
        CRYPTO_THREAD_unlock(gtr->lock);
487
101
    }
488
406
    return 1;
489
406
}
490
491
int ossl_init_thread_deregister(void *index)
492
101
{
493
101
    return init_thread_deregister(index, 0);
494
101
}
495
#endif