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