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