]>
Commit | Line | Data |
---|---|---|
72592b86 | 1 | /* |
556009c5 | 2 | * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. |
72592b86 MC |
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> | |
23c48d94 | 11 | #include <openssl/core_dispatch.h> |
25f2138b | 12 | #include "crypto/cryptlib.h" |
ddd21319 | 13 | #include "prov/providercommon.h" |
6913f5fe | 14 | #include "internal/thread_once.h" |
927d0566 | 15 | #include "crypto/context.h" |
da747958 | 16 | |
f844f9eb | 17 | #ifdef FIPS_MODULE |
f6b72c7d MC |
18 | #include "prov/provider_ctx.h" |
19 | ||
da747958 MC |
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. | |
4bd8b240 | 27 | * |
da747958 MC |
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_init_provider). | |
31 | */ | |
363b1e5d | 32 | extern OSSL_FUNC_core_thread_start_fn *c_thread_start; |
da747958 | 33 | #endif |
72592b86 MC |
34 | |
35 | typedef struct thread_event_handler_st THREAD_EVENT_HANDLER; | |
36 | struct thread_event_handler_st { | |
ec91f1ae | 37 | #ifndef FIPS_MODULE |
6913f5fe | 38 | const void *index; |
ec91f1ae | 39 | #endif |
da747958 MC |
40 | void *arg; |
41 | OSSL_thread_stop_handler_fn handfn; | |
72592b86 MC |
42 | THREAD_EVENT_HANDLER *next; |
43 | }; | |
44 | ||
f844f9eb | 45 | #ifndef FIPS_MODULE |
6913f5fe MC |
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 | { | |
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 | ||
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 | ||
f844f9eb | 85 | #ifndef FIPS_MODULE |
3b438ef9 DMSP |
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 | |
2be8c56a | 91 | static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands); |
da747958 MC |
92 | |
93 | static THREAD_EVENT_HANDLER ** | |
2be8c56a | 94 | init_get_thread_local(CRYPTO_THREAD_LOCAL *local, int alloc, int keep) |
da747958 MC |
95 | { |
96 | THREAD_EVENT_HANDLER **hands = CRYPTO_THREAD_get_local(local); | |
97 | ||
98 | if (alloc) { | |
6913f5fe | 99 | if (hands == NULL) { |
6913f5fe | 100 | |
3b438ef9 | 101 | if ((hands = OPENSSL_zalloc(sizeof(*hands))) == NULL) |
6913f5fe | 102 | return NULL; |
6913f5fe | 103 | |
3b438ef9 | 104 | if (!CRYPTO_THREAD_set_local(local, hands)) { |
6913f5fe MC |
105 | OPENSSL_free(hands); |
106 | return NULL; | |
107 | } | |
3b438ef9 | 108 | |
f844f9eb | 109 | #ifndef FIPS_MODULE |
3b438ef9 DMSP |
110 | if (!init_thread_push_handlers(hands)) { |
111 | CRYPTO_THREAD_set_local(local, NULL); | |
6913f5fe | 112 | OPENSSL_free(hands); |
6913f5fe MC |
113 | return NULL; |
114 | } | |
6913f5fe | 115 | #endif |
da747958 MC |
116 | } |
117 | } else if (!keep) { | |
118 | CRYPTO_THREAD_set_local(local, NULL); | |
119 | } | |
120 | ||
121 | return hands; | |
122 | } | |
e41faf57 | 123 | |
f844f9eb | 124 | #ifndef FIPS_MODULE |
72592b86 MC |
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 | |
c2969ff6 | 135 | * and it doesn't matter if they pick "impossible" or dereference real |
72592b86 MC |
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 | ||
3b438ef9 DMSP |
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 | { | |
156 | int ret; | |
157 | GLOBAL_TEVENT_REGISTER *gtr; | |
158 | ||
159 | gtr = get_global_tevent_register(); | |
160 | if (gtr == NULL) | |
161 | return 0; | |
162 | ||
cd3f8c1b RS |
163 | if (!CRYPTO_THREAD_write_lock(gtr->lock)) |
164 | return 0; | |
3b438ef9 DMSP |
165 | ret = (sk_THREAD_EVENT_HANDLER_PTR_push(gtr->skhands, hands) != 0); |
166 | CRYPTO_THREAD_unlock(gtr->lock); | |
167 | ||
168 | return ret; | |
169 | } | |
170 | ||
6913f5fe MC |
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; | |
cd3f8c1b RS |
179 | if (!CRYPTO_THREAD_write_lock(gtr->lock)) |
180 | return; | |
6913f5fe MC |
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) { | |
30742e8e | 186 | sk_THREAD_EVENT_HANDLER_PTR_delete(gtr->skhands, i); |
6913f5fe MC |
187 | CRYPTO_THREAD_unlock(gtr->lock); |
188 | return; | |
189 | } | |
190 | } | |
191 | CRYPTO_THREAD_unlock(gtr->lock); | |
192 | return; | |
193 | } | |
194 | ||
2be8c56a | 195 | static void init_thread_destructor(void *hands) |
72592b86 | 196 | { |
2be8c56a | 197 | init_thread_stop(NULL, (THREAD_EVENT_HANDLER **)hands); |
6913f5fe | 198 | init_thread_remove_handlers(hands); |
da747958 | 199 | OPENSSL_free(hands); |
72592b86 MC |
200 | } |
201 | ||
2be8c56a | 202 | int ossl_init_thread(void) |
72592b86 | 203 | { |
72592b86 | 204 | if (!CRYPTO_THREAD_init_local(&destructor_key.value, |
2be8c56a | 205 | init_thread_destructor)) |
72592b86 MC |
206 | return 0; |
207 | ||
208 | return 1; | |
209 | } | |
210 | ||
2be8c56a | 211 | void ossl_cleanup_thread(void) |
72592b86 | 212 | { |
6913f5fe | 213 | init_thread_deregister(NULL, 1); |
72592b86 MC |
214 | CRYPTO_THREAD_cleanup_local(&destructor_key.value); |
215 | destructor_key.sane = -1; | |
216 | } | |
217 | ||
b4250010 | 218 | void OPENSSL_thread_stop_ex(OSSL_LIB_CTX *ctx) |
52b18ce1 | 219 | { |
b4250010 | 220 | ctx = ossl_lib_ctx_get_concrete(ctx); |
52b18ce1 | 221 | /* |
703c4d66 MC |
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. | |
52b18ce1 MC |
225 | */ |
226 | ossl_ctx_thread_stop(ctx); | |
227 | } | |
228 | ||
da747958 | 229 | void OPENSSL_thread_stop(void) |
72592b86 | 230 | { |
da747958 MC |
231 | if (destructor_key.sane != -1) { |
232 | THREAD_EVENT_HANDLER **hands | |
2be8c56a MC |
233 | = init_get_thread_local(&destructor_key.value, 0, 0); |
234 | init_thread_stop(NULL, hands); | |
6913f5fe MC |
235 | |
236 | init_thread_remove_handlers(hands); | |
da747958 | 237 | OPENSSL_free(hands); |
72592b86 | 238 | } |
72592b86 MC |
239 | } |
240 | ||
c9732f09 | 241 | void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx) |
e41faf57 | 242 | { |
da747958 MC |
243 | if (destructor_key.sane != -1) { |
244 | THREAD_EVENT_HANDLER **hands | |
2be8c56a | 245 | = init_get_thread_local(&destructor_key.value, 0, 1); |
c9732f09 | 246 | init_thread_stop(ctx, hands); |
da747958 | 247 | } |
e41faf57 | 248 | } |
da747958 | 249 | |
e41faf57 | 250 | #else |
da747958 | 251 | |
be203ea3 MC |
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 | ||
927d0566 | 261 | void *ossl_thread_event_ctx_new(OSSL_LIB_CTX *libctx) |
e41faf57 MC |
262 | { |
263 | THREAD_EVENT_HANDLER **hands = NULL; | |
da747958 | 264 | CRYPTO_THREAD_LOCAL *tlocal = OPENSSL_zalloc(sizeof(*tlocal)); |
e41faf57 MC |
265 | |
266 | if (tlocal == NULL) | |
267 | return NULL; | |
268 | ||
4cde7585 | 269 | if (!CRYPTO_THREAD_init_local(tlocal, NULL)) { |
da747958 MC |
270 | goto err; |
271 | } | |
272 | ||
e41faf57 MC |
273 | hands = OPENSSL_zalloc(sizeof(*hands)); |
274 | if (hands == NULL) | |
275 | goto err; | |
276 | ||
277 | if (!CRYPTO_THREAD_set_local(tlocal, hands)) | |
278 | goto err; | |
279 | ||
be203ea3 MC |
280 | /* |
281 | * We should ideally call ossl_thread_register_fips() here. This function | |
282 | * is called during the startup of the FIPS provider and we need to ensure | |
283 | * that the main thread is registered to receive thread callbacks in order | |
284 | * to free |hands| that we allocated above. However we are too early in | |
285 | * the FIPS provider initialisation that FIPS_get_core_handle() doesn't work | |
286 | * yet. So we defer this to the main provider OSSL_provider_init_int() | |
287 | * function. | |
288 | */ | |
289 | ||
e41faf57 MC |
290 | return tlocal; |
291 | err: | |
292 | OPENSSL_free(hands); | |
293 | OPENSSL_free(tlocal); | |
294 | return NULL; | |
295 | } | |
296 | ||
927d0566 | 297 | void ossl_thread_event_ctx_free(void *tlocal) |
e41faf57 | 298 | { |
e41faf57 MC |
299 | OPENSSL_free(tlocal); |
300 | } | |
301 | ||
c9732f09 MC |
302 | static void ossl_arg_thread_stop(void *arg) |
303 | { | |
304 | ossl_ctx_thread_stop((OSSL_LIB_CTX *)arg); | |
305 | } | |
306 | ||
307 | void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx) | |
e41faf57 MC |
308 | { |
309 | THREAD_EVENT_HANDLER **hands; | |
da747958 | 310 | CRYPTO_THREAD_LOCAL *local |
927d0566 | 311 | = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX); |
e41faf57 | 312 | |
da747958 MC |
313 | if (local == NULL) |
314 | return; | |
2be8c56a | 315 | hands = init_get_thread_local(local, 0, 0); |
f6b72c7d | 316 | init_thread_stop(ctx, hands); |
da747958 | 317 | OPENSSL_free(hands); |
e41faf57 | 318 | } |
f844f9eb | 319 | #endif /* FIPS_MODULE */ |
e41faf57 | 320 | |
da747958 | 321 | |
2be8c56a | 322 | static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands) |
72592b86 | 323 | { |
2dd04ca8 | 324 | THREAD_EVENT_HANDLER *curr, *prev = NULL, *tmp; |
3b9de0c9 MC |
325 | #ifndef FIPS_MODULE |
326 | GLOBAL_TEVENT_REGISTER *gtr; | |
327 | #endif | |
72592b86 MC |
328 | |
329 | /* Can't do much about this */ | |
330 | if (hands == NULL) | |
331 | return; | |
332 | ||
3b9de0c9 MC |
333 | #ifndef FIPS_MODULE |
334 | gtr = get_global_tevent_register(); | |
335 | if (gtr == NULL) | |
336 | return; | |
337 | ||
338 | if (!CRYPTO_THREAD_write_lock(gtr->lock)) | |
339 | return; | |
340 | #endif | |
341 | ||
72592b86 MC |
342 | curr = *hands; |
343 | while (curr != NULL) { | |
da747958 | 344 | if (arg != NULL && curr->arg != arg) { |
2dd04ca8 | 345 | prev = curr; |
da747958 MC |
346 | curr = curr->next; |
347 | continue; | |
348 | } | |
349 | curr->handfn(curr->arg); | |
2dd04ca8 MC |
350 | if (prev == NULL) |
351 | *hands = curr->next; | |
352 | else | |
353 | prev->next = curr->next; | |
354 | ||
355 | tmp = curr; | |
72592b86 | 356 | curr = curr->next; |
2dd04ca8 MC |
357 | |
358 | OPENSSL_free(tmp); | |
72592b86 | 359 | } |
3b9de0c9 MC |
360 | #ifndef FIPS_MODULE |
361 | CRYPTO_THREAD_unlock(gtr->lock); | |
362 | #endif | |
72592b86 MC |
363 | } |
364 | ||
6913f5fe MC |
365 | int ossl_init_thread_start(const void *index, void *arg, |
366 | OSSL_thread_stop_handler_fn handfn) | |
72592b86 MC |
367 | { |
368 | THREAD_EVENT_HANDLER **hands; | |
369 | THREAD_EVENT_HANDLER *hand; | |
f844f9eb | 370 | #ifdef FIPS_MODULE |
b4250010 | 371 | OSSL_LIB_CTX *ctx = arg; |
da747958 | 372 | |
e41faf57 MC |
373 | /* |
374 | * In FIPS mode the list of THREAD_EVENT_HANDLERs is unique per combination | |
b4250010 DMSP |
375 | * of OSSL_LIB_CTX and thread. This is because in FIPS mode each |
376 | * OSSL_LIB_CTX gets informed about thread stop events individually. | |
e41faf57 | 377 | */ |
da747958 | 378 | CRYPTO_THREAD_LOCAL *local |
927d0566 | 379 | = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX); |
e41faf57 MC |
380 | #else |
381 | /* | |
382 | * Outside of FIPS mode the list of THREAD_EVENT_HANDLERs is unique per | |
b4250010 | 383 | * thread, but may hold multiple OSSL_LIB_CTXs. We only get told about |
e41faf57 | 384 | * thread stop events globally, so we have to ensure all affected |
b4250010 | 385 | * OSSL_LIB_CTXs are informed. |
e41faf57 | 386 | */ |
da747958 | 387 | CRYPTO_THREAD_LOCAL *local = &destructor_key.value; |
e41faf57 | 388 | #endif |
72592b86 | 389 | |
2be8c56a | 390 | hands = init_get_thread_local(local, 1, 0); |
72592b86 MC |
391 | if (hands == NULL) |
392 | return 0; | |
393 | ||
f844f9eb | 394 | #ifdef FIPS_MODULE |
da747958 MC |
395 | if (*hands == NULL) { |
396 | /* | |
397 | * We've not yet registered any handlers for this thread. We need to get | |
398 | * libcrypto to tell us about later thread stop events. c_thread_start | |
399 | * is a callback to libcrypto defined in fipsprov.c | |
400 | */ | |
be203ea3 | 401 | if (!ossl_thread_register_fips(ctx)) |
da747958 MC |
402 | return 0; |
403 | } | |
404 | #endif | |
405 | ||
72592b86 MC |
406 | hand = OPENSSL_malloc(sizeof(*hand)); |
407 | if (hand == NULL) | |
408 | return 0; | |
409 | ||
410 | hand->handfn = handfn; | |
da747958 | 411 | hand->arg = arg; |
ec91f1ae | 412 | #ifndef FIPS_MODULE |
6913f5fe | 413 | hand->index = index; |
ec91f1ae | 414 | #endif |
72592b86 MC |
415 | hand->next = *hands; |
416 | *hands = hand; | |
417 | ||
418 | return 1; | |
419 | } | |
6913f5fe | 420 | |
f844f9eb | 421 | #ifndef FIPS_MODULE |
6913f5fe MC |
422 | static int init_thread_deregister(void *index, int all) |
423 | { | |
424 | GLOBAL_TEVENT_REGISTER *gtr; | |
425 | int i; | |
426 | ||
427 | gtr = get_global_tevent_register(); | |
3478a210 P |
428 | if (gtr == NULL) |
429 | return 0; | |
cd3f8c1b RS |
430 | if (!all) { |
431 | if (!CRYPTO_THREAD_write_lock(gtr->lock)) | |
432 | return 0; | |
433 | } else { | |
3d7e7e7c | 434 | glob_tevent_reg = NULL; |
cd3f8c1b | 435 | } |
6913f5fe MC |
436 | for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) { |
437 | THREAD_EVENT_HANDLER **hands | |
438 | = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i); | |
59ed7339 | 439 | THREAD_EVENT_HANDLER *curr = NULL, *prev = NULL, *tmp; |
6913f5fe MC |
440 | |
441 | if (hands == NULL) { | |
442 | if (!all) | |
443 | CRYPTO_THREAD_unlock(gtr->lock); | |
444 | return 0; | |
445 | } | |
59ed7339 | 446 | curr = *hands; |
6913f5fe MC |
447 | while (curr != NULL) { |
448 | if (all || curr->index == index) { | |
449 | if (prev != NULL) | |
450 | prev->next = curr->next; | |
451 | else | |
452 | *hands = curr->next; | |
453 | tmp = curr; | |
454 | curr = curr->next; | |
455 | OPENSSL_free(tmp); | |
456 | continue; | |
457 | } | |
458 | prev = curr; | |
459 | curr = curr->next; | |
460 | } | |
461 | if (all) | |
462 | OPENSSL_free(hands); | |
463 | } | |
464 | if (all) { | |
465 | CRYPTO_THREAD_lock_free(gtr->lock); | |
466 | sk_THREAD_EVENT_HANDLER_PTR_free(gtr->skhands); | |
467 | OPENSSL_free(gtr); | |
468 | } else { | |
469 | CRYPTO_THREAD_unlock(gtr->lock); | |
470 | } | |
471 | return 1; | |
472 | } | |
473 | ||
474 | int ossl_init_thread_deregister(void *index) | |
475 | { | |
476 | return init_thread_deregister(index, 0); | |
477 | } | |
478 | #endif |