2
0

initthread.c 13 KB

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