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