Don't convert pre-existing providers into children
[openssl.git] / crypto / initthread.c
1 /*
2  * Copyright 2019-2021 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
16 #ifdef FIPS_MODULE
17 #include "prov/provider_ctx.h"
18
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.
26  *
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  */
31 extern OSSL_FUNC_core_thread_start_fn *c_thread_start;
32 #endif
33
34 typedef struct thread_event_handler_st THREAD_EVENT_HANDLER;
35 struct thread_event_handler_st {
36     const void *index;
37     void *arg;
38     OSSL_thread_stop_handler_fn handfn;
39     THREAD_EVENT_HANDLER *next;
40 };
41
42 #ifndef FIPS_MODULE
43 DEFINE_SPECIAL_STACK_OF(THREAD_EVENT_HANDLER_PTR, THREAD_EVENT_HANDLER *)
44
45 typedef struct global_tevent_register_st GLOBAL_TEVENT_REGISTER;
46 struct global_tevent_register_st {
47     STACK_OF(THREAD_EVENT_HANDLER_PTR) *skhands;
48     CRYPTO_RWLOCK *lock;
49 };
50
51 static GLOBAL_TEVENT_REGISTER *glob_tevent_reg = NULL;
52
53 static CRYPTO_ONCE tevent_register_runonce = CRYPTO_ONCE_STATIC_INIT;
54
55 DEFINE_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
74 static 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
82 #ifndef FIPS_MODULE
83 static int  init_thread_push_handlers(THREAD_EVENT_HANDLER **hands);
84 static void init_thread_remove_handlers(THREAD_EVENT_HANDLER **handsin);
85 static void init_thread_destructor(void *hands);
86 static int  init_thread_deregister(void *arg, int all);
87 #endif
88 static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands);
89
90 static THREAD_EVENT_HANDLER **
91 init_get_thread_local(CRYPTO_THREAD_LOCAL *local, int alloc, int keep)
92 {
93     THREAD_EVENT_HANDLER **hands = CRYPTO_THREAD_get_local(local);
94
95     if (alloc) {
96         if (hands == NULL) {
97
98             if ((hands = OPENSSL_zalloc(sizeof(*hands))) == NULL)
99                 return NULL;
100
101             if (!CRYPTO_THREAD_set_local(local, hands)) {
102                 OPENSSL_free(hands);
103                 return NULL;
104             }
105
106 #ifndef FIPS_MODULE
107             if (!init_thread_push_handlers(hands)) {
108                 CRYPTO_THREAD_set_local(local, NULL);
109                 OPENSSL_free(hands);
110                 return NULL;
111             }
112 #endif
113         }
114     } else if (!keep) {
115         CRYPTO_THREAD_set_local(local, NULL);
116     }
117
118     return hands;
119 }
120
121 #ifndef FIPS_MODULE
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
132  * and it doesn't matter if they pick "impossible" or dereference real
133  * key value and pull NULL past initialization in the first thread that
134  * intends to use libcrypto.
135  */
136 static union {
137     long sane;
138     CRYPTO_THREAD_LOCAL value;
139 } destructor_key = { -1 };
140
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  */
151 static 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
160     if (!CRYPTO_THREAD_write_lock(gtr->lock))
161         return 0;
162     ret = (sk_THREAD_EVENT_HANDLER_PTR_push(gtr->skhands, hands) != 0);
163     CRYPTO_THREAD_unlock(gtr->lock);
164
165     return ret;
166 }
167
168 static 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;
176     if (!CRYPTO_THREAD_write_lock(gtr->lock))
177         return;
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) {
183             sk_THREAD_EVENT_HANDLER_PTR_delete(gtr->skhands, i);
184             CRYPTO_THREAD_unlock(gtr->lock);
185             return;
186         }
187     }
188     CRYPTO_THREAD_unlock(gtr->lock);
189     return;
190 }
191
192 static void init_thread_destructor(void *hands)
193 {
194     init_thread_stop(NULL, (THREAD_EVENT_HANDLER **)hands);
195     init_thread_remove_handlers(hands);
196     OPENSSL_free(hands);
197 }
198
199 int ossl_init_thread(void)
200 {
201     if (!CRYPTO_THREAD_init_local(&destructor_key.value,
202                                   init_thread_destructor))
203         return 0;
204
205     return 1;
206 }
207
208 void ossl_cleanup_thread(void)
209 {
210     init_thread_deregister(NULL, 1);
211     CRYPTO_THREAD_cleanup_local(&destructor_key.value);
212     destructor_key.sane = -1;
213 }
214
215 void OPENSSL_thread_stop_ex(OSSL_LIB_CTX *ctx)
216 {
217     ctx = ossl_lib_ctx_get_concrete(ctx);
218     /*
219      * It would be nice if we could figure out a way to do this on all threads
220      * that have used the OSSL_LIB_CTX when the context is freed. This is
221      * currently not possible due to the use of thread local variables.
222      */
223     ossl_ctx_thread_stop(ctx);
224 }
225
226 void OPENSSL_thread_stop(void)
227 {
228     if (destructor_key.sane != -1) {
229         THREAD_EVENT_HANDLER **hands
230             = init_get_thread_local(&destructor_key.value, 0, 0);
231         init_thread_stop(NULL, hands);
232
233         init_thread_remove_handlers(hands);
234         OPENSSL_free(hands);
235     }
236 }
237
238 void ossl_ctx_thread_stop(void *arg)
239 {
240     if (destructor_key.sane != -1) {
241         THREAD_EVENT_HANDLER **hands
242             = init_get_thread_local(&destructor_key.value, 0, 1);
243         init_thread_stop(arg, hands);
244     }
245 }
246
247 #else
248
249 static void *thread_event_ossl_ctx_new(OSSL_LIB_CTX *libctx)
250 {
251     THREAD_EVENT_HANDLER **hands = NULL;
252     CRYPTO_THREAD_LOCAL *tlocal = OPENSSL_zalloc(sizeof(*tlocal));
253
254     if (tlocal == NULL)
255         return NULL;
256
257     if (!CRYPTO_THREAD_init_local(tlocal,  NULL)) {
258         goto err;
259     }
260
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
275 static void thread_event_ossl_ctx_free(void *tlocal)
276 {
277     OPENSSL_free(tlocal);
278 }
279
280 static const OSSL_LIB_CTX_METHOD thread_event_ossl_ctx_method = {
281     OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
282     thread_event_ossl_ctx_new,
283     thread_event_ossl_ctx_free,
284 };
285
286 void ossl_ctx_thread_stop(void *arg)
287 {
288     THREAD_EVENT_HANDLER **hands;
289     OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(arg);
290     CRYPTO_THREAD_LOCAL *local
291         = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX,
292                                 &thread_event_ossl_ctx_method);
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 {
305     THREAD_EVENT_HANDLER *curr, *prev = NULL, *tmp;
306
307     /* Can't do much about this */
308     if (hands == NULL)
309         return;
310
311     curr = *hands;
312     while (curr != NULL) {
313         if (arg != NULL && curr->arg != arg) {
314             prev = curr;
315             curr = curr->next;
316             continue;
317         }
318         curr->handfn(curr->arg);
319         if (prev == NULL)
320             *hands = curr->next;
321         else
322             prev->next = curr->next;
323
324         tmp = curr;
325         curr = curr->next;
326
327         OPENSSL_free(tmp);
328     }
329 }
330
331 int ossl_init_thread_start(const void *index, void *arg,
332                            OSSL_thread_stop_handler_fn handfn)
333 {
334     THREAD_EVENT_HANDLER **hands;
335     THREAD_EVENT_HANDLER *hand;
336 #ifdef FIPS_MODULE
337     OSSL_LIB_CTX *ctx = arg;
338
339     /*
340      * In FIPS mode the list of THREAD_EVENT_HANDLERs is unique per combination
341      * of OSSL_LIB_CTX and thread. This is because in FIPS mode each
342      * OSSL_LIB_CTX gets informed about thread stop events individually.
343      */
344     CRYPTO_THREAD_LOCAL *local
345         = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX,
346                                 &thread_event_ossl_ctx_method);
347 #else
348     /*
349      * Outside of FIPS mode the list of THREAD_EVENT_HANDLERs is unique per
350      * thread, but may hold multiple OSSL_LIB_CTXs. We only get told about
351      * thread stop events globally, so we have to ensure all affected
352      * OSSL_LIB_CTXs are informed.
353      */
354     CRYPTO_THREAD_LOCAL *local = &destructor_key.value;
355 #endif
356
357     hands = init_get_thread_local(local, 1, 0);
358     if (hands == NULL)
359         return 0;
360
361 #ifdef FIPS_MODULE
362     if (*hands == NULL) {
363         /*
364          * We've not yet registered any handlers for this thread. We need to get
365          * libcrypto to tell us about later thread stop events. c_thread_start
366          * is a callback to libcrypto defined in fipsprov.c
367          */
368         if (!c_thread_start(FIPS_get_core_handle(ctx), ossl_ctx_thread_stop))
369             return 0;
370     }
371 #endif
372
373     hand = OPENSSL_malloc(sizeof(*hand));
374     if (hand == NULL)
375         return 0;
376
377     hand->handfn = handfn;
378     hand->arg = arg;
379     hand->index = index;
380     hand->next = *hands;
381     *hands = hand;
382
383     return 1;
384 }
385
386 #ifndef FIPS_MODULE
387 static int init_thread_deregister(void *index, int all)
388 {
389     GLOBAL_TEVENT_REGISTER *gtr;
390     int i;
391
392     gtr = get_global_tevent_register();
393     if (gtr == NULL)
394         return 0;
395     if (!all) {
396         if (!CRYPTO_THREAD_write_lock(gtr->lock))
397             return 0;
398     } else {
399         glob_tevent_reg = NULL;
400     }
401     for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) {
402         THREAD_EVENT_HANDLER **hands
403             = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i);
404         THREAD_EVENT_HANDLER *curr = NULL, *prev = NULL, *tmp;
405
406         if (hands == NULL) {
407             if (!all)
408                 CRYPTO_THREAD_unlock(gtr->lock);
409             return 0;
410         }
411         curr = *hands;
412         while (curr != NULL) {
413             if (all || curr->index == index) {
414                 if (prev != NULL)
415                     prev->next = curr->next;
416                 else
417                     *hands = curr->next;
418                 tmp = curr;
419                 curr = curr->next;
420                 OPENSSL_free(tmp);
421                 continue;
422             }
423             prev = curr;
424             curr = curr->next;
425         }
426         if (all)
427             OPENSSL_free(hands);
428     }
429     if (all) {
430         CRYPTO_THREAD_lock_free(gtr->lock);
431         sk_THREAD_EVENT_HANDLER_PTR_free(gtr->skhands);
432         OPENSSL_free(gtr);
433     } else {
434         CRYPTO_THREAD_unlock(gtr->lock);
435     }
436     return 1;
437 }
438
439 int ossl_init_thread_deregister(void *index)
440 {
441     return init_thread_deregister(index, 0);
442 }
443 #endif