Coverage Report

Created: 2025-06-13 06:58

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