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