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