Coverage Report

Created: 2025-12-31 06:58

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