Coverage Report

Created: 2025-10-28 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
16
{
61
16
    glob_tevent_reg = OPENSSL_zalloc(sizeof(*glob_tevent_reg));
62
16
    if (glob_tevent_reg == NULL)
63
0
        return 0;
64
65
16
    glob_tevent_reg->skhands = sk_THREAD_EVENT_HANDLER_PTR_new_null();
66
16
    glob_tevent_reg->lock = CRYPTO_THREAD_lock_new();
67
16
    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
16
    return 1;
76
16
}
77
78
static GLOBAL_TEVENT_REGISTER *get_global_tevent_register(void)
79
80
{
80
80
    if (!RUN_ONCE(&tevent_register_runonce, create_global_tevent_register))
81
0
        return NULL;
82
80
    return glob_tevent_reg;
83
80
}
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
64
{
115
#ifdef FIPS_MODULE
116
    return CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_TEVENT_KEY, ctx);
117
#else
118
64
    if (destructor_key.sane != -1)
119
64
        return CRYPTO_THREAD_get_local(&destructor_key.value);
120
0
    return NULL;
121
64
#endif
122
64
}
123
124
static int set_thread_event_handler(OSSL_LIB_CTX *ctx, THREAD_EVENT_HANDLER **hands)
125
32
{
126
#ifdef FIPS_MODULE
127
    return CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_TEVENT_KEY, ctx, hands);
128
#else
129
32
    if (destructor_key.sane != -1)
130
32
        return CRYPTO_THREAD_set_local(&destructor_key.value, hands);
131
0
    return 0;
132
32
#endif
133
32
}
134
135
static THREAD_EVENT_HANDLER **
136
manage_thread_local(OSSL_LIB_CTX *ctx, int alloc, int keep)
137
64
{
138
64
    THREAD_EVENT_HANDLER **hands = get_thread_event_handler(ctx);
139
140
64
    if (alloc) {
141
32
        if (hands == NULL) {
142
143
16
            if ((hands = OPENSSL_zalloc(sizeof(*hands))) == NULL)
144
0
                return NULL;
145
146
16
            if (!set_thread_event_handler(ctx, hands)) {
147
0
                OPENSSL_free(hands);
148
0
                return NULL;
149
0
            }
150
16
#ifndef FIPS_MODULE
151
16
            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
16
#endif
157
16
        }
158
32
    } else if (!keep) {
159
16
        set_thread_event_handler(ctx, NULL);
160
16
    }
161
162
64
    return hands;
163
64
}
164
165
static ossl_inline THREAD_EVENT_HANDLER **clear_thread_local(OSSL_LIB_CTX *ctx)
166
16
{
167
16
    return manage_thread_local(ctx, 0, 0);
168
16
}
169
170
static ossl_inline ossl_unused THREAD_EVENT_HANDLER **fetch_thread_local(OSSL_LIB_CTX *ctx)
171
16
{
172
16
    return manage_thread_local(ctx, 0, 1);
173
16
}
174
175
static ossl_inline THREAD_EVENT_HANDLER **alloc_thread_local(OSSL_LIB_CTX *ctx)
176
32
{
177
32
    return manage_thread_local(ctx, 1, 0);
178
32
}
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
16
{
193
16
    int ret;
194
16
    GLOBAL_TEVENT_REGISTER *gtr;
195
196
16
    gtr = get_global_tevent_register();
197
16
    if (gtr == NULL)
198
0
        return 0;
199
200
16
    if (!CRYPTO_THREAD_write_lock(gtr->lock))
201
0
        return 0;
202
16
    ret = (sk_THREAD_EVENT_HANDLER_PTR_push(gtr->skhands, hands) != 0);
203
16
    CRYPTO_THREAD_unlock(gtr->lock);
204
205
16
    return ret;
206
16
}
207
208
static void init_thread_remove_handlers(THREAD_EVENT_HANDLER **handsin)
209
16
{
210
16
    GLOBAL_TEVENT_REGISTER *gtr;
211
16
    int i;
212
213
16
    gtr = get_global_tevent_register();
214
16
    if (gtr == NULL)
215
0
        return;
216
16
    if (!CRYPTO_THREAD_write_lock(gtr->lock))
217
0
        return;
218
16
    for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) {
219
16
        THREAD_EVENT_HANDLER **hands
220
16
            = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i);
221
222
16
        if (hands == handsin) {
223
16
            sk_THREAD_EVENT_HANDLER_PTR_delete(gtr->skhands, i);
224
16
            CRYPTO_THREAD_unlock(gtr->lock);
225
16
            return;
226
16
        }
227
16
    }
228
0
    CRYPTO_THREAD_unlock(gtr->lock);
229
0
    return;
230
16
}
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
int ossl_init_thread(void)
240
16
{
241
16
    if (!CRYPTO_THREAD_init_local(&destructor_key.value,
242
16
                                  init_thread_destructor))
243
0
        return 0;
244
245
16
    return 1;
246
16
}
247
248
void ossl_cleanup_thread(void)
249
16
{
250
16
    init_thread_deregister(NULL, 1);
251
16
    CRYPTO_THREAD_cleanup_local(&destructor_key.value);
252
16
    destructor_key.sane = -1;
253
16
}
254
255
void OPENSSL_thread_stop_ex(OSSL_LIB_CTX *ctx)
256
0
{
257
0
    ctx = ossl_lib_ctx_get_concrete(ctx);
258
    /*
259
     * It would be nice if we could figure out a way to do this on all threads
260
     * that have used the OSSL_LIB_CTX when the context is freed. This is
261
     * currently not possible due to the use of thread local variables.
262
     */
263
0
    ossl_ctx_thread_stop(ctx);
264
0
}
265
266
void OPENSSL_thread_stop(void)
267
16
{
268
16
    if (destructor_key.sane != -1) {
269
16
        THREAD_EVENT_HANDLER **hands = clear_thread_local(NULL);
270
271
16
        init_thread_stop(NULL, hands);
272
273
16
        init_thread_remove_handlers(hands);
274
16
        OPENSSL_free(hands);
275
276
16
        CRYPTO_THREAD_clean_local();
277
16
    }
278
16
}
279
280
void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx)
281
16
{
282
16
    if (destructor_key.sane != -1) {
283
16
        THREAD_EVENT_HANDLER **hands = fetch_thread_local(ctx);
284
285
16
        init_thread_stop(ctx, hands);
286
16
    }
287
16
}
288
289
#else
290
291
static void ossl_arg_thread_stop(void *arg);
292
293
/* Register the current thread so that we are informed if it gets stopped */
294
int ossl_thread_register_fips(OSSL_LIB_CTX *libctx)
295
{
296
    return c_thread_start(FIPS_get_core_handle(libctx), ossl_arg_thread_stop,
297
                          libctx);
298
}
299
300
int ossl_thread_event_ctx_new(OSSL_LIB_CTX *libctx)
301
{
302
    THREAD_EVENT_HANDLER **hands = NULL;
303
304
    hands = OPENSSL_zalloc(sizeof(*hands));
305
    if (hands == NULL)
306
        goto err;
307
308
    if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_TEVENT_KEY, libctx, hands))
309
        goto err;
310
311
    /*
312
     * We should ideally call ossl_thread_register_fips() here. This function
313
     * is called during the startup of the FIPS provider and we need to ensure
314
     * that the main thread is registered to receive thread callbacks in order
315
     * to free |hands| that we allocated above. However we are too early in
316
     * the FIPS provider initialisation that FIPS_get_core_handle() doesn't work
317
     * yet. So we defer this to the main provider OSSL_provider_init_int()
318
     * function.
319
     */
320
321
    return 1;
322
 err:
323
    OPENSSL_free(hands);
324
    return 0;
325
}
326
327
void ossl_thread_event_ctx_free(OSSL_LIB_CTX *ctx)
328
{
329
    CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_TEVENT_KEY, ctx, NULL);
330
}
331
332
static void ossl_arg_thread_stop(void *arg)
333
{
334
    ossl_ctx_thread_stop((OSSL_LIB_CTX *)arg);
335
}
336
337
void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx)
338
{
339
    THREAD_EVENT_HANDLER **hands;
340
341
    hands = clear_thread_local(ctx);
342
    init_thread_stop(ctx, hands);
343
    OPENSSL_free(hands);
344
}
345
#endif /* FIPS_MODULE */
346
347
348
static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands)
349
32
{
350
32
    THREAD_EVENT_HANDLER *curr, *prev = NULL, *tmp;
351
32
#ifndef FIPS_MODULE
352
32
    GLOBAL_TEVENT_REGISTER *gtr;
353
32
#endif
354
355
    /* Can't do much about this */
356
32
    if (hands == NULL)
357
16
        return;
358
359
16
#ifndef FIPS_MODULE
360
16
    gtr = get_global_tevent_register();
361
16
    if (gtr == NULL)
362
0
        return;
363
364
16
    if (!CRYPTO_THREAD_write_lock(gtr->lock))
365
0
        return;
366
16
#endif
367
368
16
    curr = *hands;
369
48
    while (curr != NULL) {
370
32
        if (arg != NULL && curr->arg != arg) {
371
0
            prev = curr;
372
0
            curr = curr->next;
373
0
            continue;
374
0
        }
375
32
        curr->handfn(curr->arg);
376
32
        if (prev == NULL)
377
32
            *hands = curr->next;
378
0
        else
379
0
            prev->next = curr->next;
380
381
32
        tmp = curr;
382
32
        curr = curr->next;
383
384
32
        OPENSSL_free(tmp);
385
32
    }
386
16
#ifndef FIPS_MODULE
387
16
    CRYPTO_THREAD_unlock(gtr->lock);
388
16
#endif
389
16
}
390
391
int ossl_init_thread_start(const void *index, void *arg,
392
                           OSSL_thread_stop_handler_fn handfn)
393
32
{
394
32
    THREAD_EVENT_HANDLER **hands;
395
32
    THREAD_EVENT_HANDLER *hand;
396
32
    OSSL_LIB_CTX *ctx = NULL;
397
#ifdef FIPS_MODULE
398
    /*
399
     * In FIPS mode the list of THREAD_EVENT_HANDLERs is unique per combination
400
     * of OSSL_LIB_CTX and thread. This is because in FIPS mode each
401
     * OSSL_LIB_CTX gets informed about thread stop events individually.
402
     */
403
404
    ctx = arg;
405
#endif
406
407
32
    hands = alloc_thread_local(ctx);
408
409
32
    if (hands == NULL)
410
0
        return 0;
411
412
#ifdef FIPS_MODULE
413
    if (*hands == NULL) {
414
        /*
415
         * We've not yet registered any handlers for this thread. We need to get
416
         * libcrypto to tell us about later thread stop events. c_thread_start
417
         * is a callback to libcrypto defined in fipsprov.c
418
         */
419
        if (!ossl_thread_register_fips(ctx))
420
            return 0;
421
    }
422
#endif
423
424
32
    hand = OPENSSL_malloc(sizeof(*hand));
425
32
    if (hand == NULL)
426
0
        return 0;
427
428
32
    hand->handfn = handfn;
429
32
    hand->arg = arg;
430
32
#ifndef FIPS_MODULE
431
32
    hand->index = index;
432
32
#endif
433
32
    hand->next = *hands;
434
32
    *hands = hand;
435
436
32
    return 1;
437
32
}
438
439
#ifndef FIPS_MODULE
440
static int init_thread_deregister(void *index, int all)
441
32
{
442
32
    GLOBAL_TEVENT_REGISTER *gtr;
443
32
    int i;
444
445
32
    gtr = get_global_tevent_register();
446
32
    if (gtr == NULL)
447
0
        return 0;
448
32
    if (!all) {
449
16
        if (!CRYPTO_THREAD_write_lock(gtr->lock))
450
0
            return 0;
451
16
    } else {
452
16
        glob_tevent_reg = NULL;
453
16
    }
454
32
    for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) {
455
0
        THREAD_EVENT_HANDLER **hands
456
0
            = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i);
457
0
        THREAD_EVENT_HANDLER *curr = NULL, *prev = NULL, *tmp;
458
459
0
        if (hands == NULL) {
460
0
            if (!all)
461
0
                CRYPTO_THREAD_unlock(gtr->lock);
462
0
            return 0;
463
0
        }
464
0
        curr = *hands;
465
0
        while (curr != NULL) {
466
0
            if (all || curr->index == index) {
467
0
                if (prev != NULL)
468
0
                    prev->next = curr->next;
469
0
                else
470
0
                    *hands = curr->next;
471
0
                tmp = curr;
472
0
                curr = curr->next;
473
0
                OPENSSL_free(tmp);
474
0
                continue;
475
0
            }
476
0
            prev = curr;
477
0
            curr = curr->next;
478
0
        }
479
0
        if (all)
480
0
            OPENSSL_free(hands);
481
0
    }
482
32
    if (all) {
483
16
        CRYPTO_THREAD_lock_free(gtr->lock);
484
16
        sk_THREAD_EVENT_HANDLER_PTR_free(gtr->skhands);
485
16
        OPENSSL_free(gtr);
486
16
    } else {
487
16
        CRYPTO_THREAD_unlock(gtr->lock);
488
16
    }
489
32
    return 1;
490
32
}
491
492
int ossl_init_thread_deregister(void *index)
493
16
{
494
16
    return init_thread_deregister(index, 0);
495
16
}
496
#endif