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