Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/initthread.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2026 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
291
{
60
291
    glob_tevent_reg = OPENSSL_zalloc(sizeof(*glob_tevent_reg));
61
291
    if (glob_tevent_reg == NULL)
62
0
        return 0;
63
64
291
    glob_tevent_reg->skhands = sk_THREAD_EVENT_HANDLER_PTR_new_null();
65
291
    glob_tevent_reg->lock = CRYPTO_THREAD_lock_new();
66
291
    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
291
    return 1;
75
291
}
76
77
static GLOBAL_TEVENT_REGISTER *get_global_tevent_register(void)
78
805
{
79
805
    if (!RUN_ONCE(&tevent_register_runonce, create_global_tevent_register))
80
0
        return NULL;
81
805
    return glob_tevent_reg;
82
805
}
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
333
{
96
333
    THREAD_EVENT_HANDLER **hands = CRYPTO_THREAD_get_local(local);
97
98
333
    if (alloc) {
99
131
        if (hands == NULL) {
100
101
101
            if ((hands = OPENSSL_zalloc(sizeof(*hands))) == NULL)
102
0
                return NULL;
103
104
101
            if (!CRYPTO_THREAD_set_local(local, hands)) {
105
0
                OPENSSL_free(hands);
106
0
                return NULL;
107
0
            }
108
109
101
#ifndef FIPS_MODULE
110
101
            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
101
#endif
116
101
        }
117
202
    } else if (!keep) {
118
101
        CRYPTO_THREAD_set_local(local, NULL);
119
101
    }
120
121
333
    return hands;
122
333
}
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
291
{
156
291
    int ret;
157
291
    GLOBAL_TEVENT_REGISTER *gtr;
158
159
291
    gtr = get_global_tevent_register();
160
291
    if (gtr == NULL)
161
0
        return 0;
162
163
291
    if (!CRYPTO_THREAD_write_lock(gtr->lock))
164
0
        return 0;
165
291
    ret = (sk_THREAD_EVENT_HANDLER_PTR_push(gtr->skhands, hands) != 0);
166
291
    CRYPTO_THREAD_unlock(gtr->lock);
167
168
291
    return ret;
169
291
}
170
171
static void init_thread_remove_handlers(THREAD_EVENT_HANDLER **handsin)
172
154
{
173
154
    GLOBAL_TEVENT_REGISTER *gtr;
174
154
    int i;
175
176
154
    gtr = get_global_tevent_register();
177
154
    if (gtr == NULL)
178
0
        return;
179
154
    if (!CRYPTO_THREAD_write_lock(gtr->lock))
180
0
        return;
181
154
    for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) {
182
154
        THREAD_EVENT_HANDLER **hands
183
154
            = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i);
184
185
154
        if (hands == handsin) {
186
154
            sk_THREAD_EVENT_HANDLER_PTR_delete(gtr->skhands, i);
187
154
            CRYPTO_THREAD_unlock(gtr->lock);
188
154
            return;
189
154
        }
190
154
    }
191
0
    CRYPTO_THREAD_unlock(gtr->lock);
192
0
    return;
193
154
}
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
#if defined(OPENSSL_SYS_TANDEM) && (defined(_PUT_MODEL_) || defined(_KLT_MODEL_))
204
static CRYPTO_THREAD_ID recursion_guard = { (void *)-1, (short)-1, (short)-1 };
205
#else
206
static CRYPTO_THREAD_ID recursion_guard = (CRYPTO_THREAD_ID)0;
207
#endif
208
209
DEFINE_RUN_ONCE_STATIC(ossl_init_thread_once)
210
154
{
211
154
    recursion_guard = CRYPTO_THREAD_get_current_id();
212
154
    if (!CRYPTO_THREAD_init_local(&destructor_key.value,
213
154
            init_thread_destructor))
214
0
        return 0;
215
216
#if defined(OPENSSL_SYS_TANDEM)
217
    memset(&recursion_guard, 0, sizeof(recursion_guard));
218
#else
219
154
    recursion_guard = (CRYPTO_THREAD_ID)0;
220
154
#endif
221
154
    return 1;
222
154
}
223
224
int ossl_init_thread(void)
225
494
{
226
494
    if (CRYPTO_THREAD_compare_id(recursion_guard,
227
494
            CRYPTO_THREAD_get_current_id()))
228
48
        return 1;
229
446
    if (!RUN_ONCE(&ossl_init_thread_runonce, ossl_init_thread_once))
230
0
        return 0;
231
446
    return 1;
232
446
}
233
234
void ossl_cleanup_thread(void)
235
154
{
236
154
    init_thread_deregister(NULL, 1);
237
154
    CRYPTO_THREAD_cleanup_local(&destructor_key.value);
238
154
    destructor_key.sane = -1;
239
154
}
240
241
void OPENSSL_thread_stop_ex(OSSL_LIB_CTX *ctx)
242
0
{
243
0
    ctx = ossl_lib_ctx_get_concrete(ctx);
244
    /*
245
     * It would be nice if we could figure out a way to do this on all threads
246
     * that have used the OSSL_LIB_CTX when the context is freed. This is
247
     * currently not possible due to the use of thread local variables.
248
     */
249
0
    ossl_ctx_thread_stop(ctx);
250
0
}
251
252
void OPENSSL_thread_stop(void)
253
154
{
254
154
    if (destructor_key.sane != -1) {
255
154
        THREAD_EVENT_HANDLER **hands
256
154
            = init_get_thread_local(&destructor_key.value, 0, 0);
257
154
        init_thread_stop(NULL, hands);
258
259
154
        init_thread_remove_handlers(hands);
260
154
        OPENSSL_free(hands);
261
154
    }
262
154
}
263
264
void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx)
265
154
{
266
154
    if (destructor_key.sane != -1) {
267
154
        THREAD_EVENT_HANDLER **hands
268
154
            = init_get_thread_local(&destructor_key.value, 0, 1);
269
154
        init_thread_stop(ctx, hands);
270
154
    }
271
154
}
272
273
#else
274
275
static void ossl_arg_thread_stop(void *arg);
276
277
/* Register the current thread so that we are informed if it gets stopped */
278
int ossl_thread_register_fips(OSSL_LIB_CTX *libctx)
279
{
280
    return c_thread_start(FIPS_get_core_handle(libctx), ossl_arg_thread_stop,
281
        libctx);
282
}
283
284
void *ossl_thread_event_ctx_new(OSSL_LIB_CTX *libctx)
285
{
286
    THREAD_EVENT_HANDLER **hands = NULL;
287
    CRYPTO_THREAD_LOCAL *tlocal = OPENSSL_zalloc(sizeof(*tlocal));
288
289
    if (tlocal == NULL)
290
        return NULL;
291
292
    if (!CRYPTO_THREAD_init_local(tlocal, NULL))
293
        goto deinit;
294
295
    hands = OPENSSL_zalloc(sizeof(*hands));
296
    if (hands == NULL)
297
        goto err;
298
299
    if (!CRYPTO_THREAD_set_local(tlocal, hands))
300
        goto err;
301
302
    /*
303
     * We should ideally call ossl_thread_register_fips() here. This function
304
     * is called during the startup of the FIPS provider and we need to ensure
305
     * that the main thread is registered to receive thread callbacks in order
306
     * to free |hands| that we allocated above. However we are too early in
307
     * the FIPS provider initialisation that FIPS_get_core_handle() doesn't work
308
     * yet. So we defer this to the main provider OSSL_provider_init_int()
309
     * function.
310
     */
311
312
    return tlocal;
313
err:
314
    OPENSSL_free(hands);
315
    CRYPTO_THREAD_cleanup_local(tlocal);
316
deinit:
317
    OPENSSL_free(tlocal);
318
    return NULL;
319
}
320
321
void ossl_thread_event_ctx_free(void *tlocal)
322
{
323
    CRYPTO_THREAD_cleanup_local(tlocal);
324
    OPENSSL_free(tlocal);
325
}
326
327
static void ossl_arg_thread_stop(void *arg)
328
{
329
    ossl_ctx_thread_stop((OSSL_LIB_CTX *)arg);
330
}
331
332
void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx)
333
{
334
    THREAD_EVENT_HANDLER **hands;
335
    CRYPTO_THREAD_LOCAL *local
336
        = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX);
337
338
    if (local == NULL)
339
        return;
340
    hands = init_get_thread_local(local, 0, 0);
341
    init_thread_stop(ctx, hands);
342
    OPENSSL_free(hands);
343
}
344
#endif /* FIPS_MODULE */
345
346
static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands)
347
308
{
348
308
    THREAD_EVENT_HANDLER *curr, *prev = NULL, *tmp;
349
308
#ifndef FIPS_MODULE
350
308
    GLOBAL_TEVENT_REGISTER *gtr;
351
308
#endif
352
353
    /* Can't do much about this */
354
308
    if (hands == NULL)
355
154
        return;
356
357
154
#ifndef FIPS_MODULE
358
154
    gtr = get_global_tevent_register();
359
154
    if (gtr == NULL)
360
0
        return;
361
362
154
    if (!CRYPTO_THREAD_write_lock(gtr->lock))
363
0
        return;
364
154
#endif
365
366
154
    curr = *hands;
367
355
    while (curr != NULL) {
368
201
        if (arg != NULL && curr->arg != arg) {
369
0
            prev = curr;
370
0
            curr = curr->next;
371
0
            continue;
372
0
        }
373
201
        curr->handfn(curr->arg);
374
201
        if (prev == NULL)
375
201
            *hands = curr->next;
376
0
        else
377
0
            prev->next = curr->next;
378
379
201
        tmp = curr;
380
201
        curr = curr->next;
381
382
201
        OPENSSL_free(tmp);
383
201
    }
384
154
#ifndef FIPS_MODULE
385
154
    CRYPTO_THREAD_unlock(gtr->lock);
386
154
#endif
387
154
}
388
389
int ossl_init_thread_start(const void *index, void *arg,
390
    OSSL_thread_stop_handler_fn handfn)
391
373
{
392
373
    THREAD_EVENT_HANDLER **hands;
393
373
    THREAD_EVENT_HANDLER *hand;
394
#ifdef FIPS_MODULE
395
    OSSL_LIB_CTX *ctx = arg;
396
397
    /*
398
     * In FIPS mode the list of THREAD_EVENT_HANDLERs is unique per combination
399
     * of OSSL_LIB_CTX and thread. This is because in FIPS mode each
400
     * OSSL_LIB_CTX gets informed about thread stop events individually.
401
     */
402
    CRYPTO_THREAD_LOCAL *local
403
        = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX);
404
#else
405
    /*
406
     * Outside of FIPS mode the list of THREAD_EVENT_HANDLERs is unique per
407
     * thread, but may hold multiple OSSL_LIB_CTXs. We only get told about
408
     * thread stop events globally, so we have to ensure all affected
409
     * OSSL_LIB_CTXs are informed.
410
     */
411
373
    CRYPTO_THREAD_LOCAL *local = &destructor_key.value;
412
373
#endif
413
414
373
    hands = init_get_thread_local(local, 1, 0);
415
373
    if (hands == NULL)
416
0
        return 0;
417
418
#ifdef FIPS_MODULE
419
    if (*hands == NULL) {
420
        /*
421
         * We've not yet registered any handlers for this thread. We need to get
422
         * libcrypto to tell us about later thread stop events. c_thread_start
423
         * is a callback to libcrypto defined in fipsprov.c
424
         */
425
        if (!ossl_thread_register_fips(ctx))
426
            return 0;
427
    }
428
#endif
429
430
373
    hand = OPENSSL_malloc(sizeof(*hand));
431
373
    if (hand == NULL)
432
0
        return 0;
433
434
373
    hand->handfn = handfn;
435
373
    hand->arg = arg;
436
373
#ifndef FIPS_MODULE
437
373
    hand->index = index;
438
373
#endif
439
373
    hand->next = *hands;
440
373
    *hands = hand;
441
442
373
    return 1;
443
373
}
444
445
#ifndef FIPS_MODULE
446
static int init_thread_deregister(void *index, int all)
447
206
{
448
206
    GLOBAL_TEVENT_REGISTER *gtr;
449
206
    int i;
450
451
206
    gtr = get_global_tevent_register();
452
206
    if (gtr == NULL)
453
0
        return 0;
454
206
    if (!all) {
455
52
        if (!CRYPTO_THREAD_write_lock(gtr->lock))
456
0
            return 0;
457
154
    } else {
458
154
        glob_tevent_reg = NULL;
459
154
    }
460
206
    for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) {
461
0
        THREAD_EVENT_HANDLER **hands
462
0
            = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i);
463
0
        THREAD_EVENT_HANDLER *curr = NULL, *prev = NULL, *tmp;
464
465
0
        if (hands == NULL) {
466
0
            if (!all)
467
0
                CRYPTO_THREAD_unlock(gtr->lock);
468
0
            return 0;
469
0
        }
470
0
        curr = *hands;
471
0
        while (curr != NULL) {
472
0
            if (all || curr->index == index) {
473
0
                if (prev != NULL)
474
0
                    prev->next = curr->next;
475
0
                else
476
0
                    *hands = curr->next;
477
0
                tmp = curr;
478
0
                curr = curr->next;
479
0
                OPENSSL_free(tmp);
480
0
                continue;
481
0
            }
482
0
            prev = curr;
483
0
            curr = curr->next;
484
0
        }
485
0
        if (all)
486
0
            OPENSSL_free(hands);
487
0
    }
488
206
    if (all) {
489
154
        CRYPTO_THREAD_lock_free(gtr->lock);
490
154
        sk_THREAD_EVENT_HANDLER_PTR_free(gtr->skhands);
491
154
        OPENSSL_free(gtr);
492
154
    } else {
493
52
        CRYPTO_THREAD_unlock(gtr->lock);
494
52
    }
495
206
    return 1;
496
206
}
497
498
int ossl_init_thread_deregister(void *index)
499
52
{
500
52
    return init_thread_deregister(index, 0);
501
52
}
502
#endif