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