Coverage Report

Created: 2025-06-13 06:58

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