2
0

initthread.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright 2019-2022 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_init_provider).
  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. void *ossl_thread_event_ctx_new(OSSL_LIB_CTX *libctx)
  216. {
  217. THREAD_EVENT_HANDLER **hands = NULL;
  218. CRYPTO_THREAD_LOCAL *tlocal = OPENSSL_zalloc(sizeof(*tlocal));
  219. if (tlocal == NULL)
  220. return NULL;
  221. if (!CRYPTO_THREAD_init_local(tlocal, NULL)) {
  222. goto err;
  223. }
  224. hands = OPENSSL_zalloc(sizeof(*hands));
  225. if (hands == NULL)
  226. goto err;
  227. if (!CRYPTO_THREAD_set_local(tlocal, hands))
  228. goto err;
  229. return tlocal;
  230. err:
  231. OPENSSL_free(hands);
  232. OPENSSL_free(tlocal);
  233. return NULL;
  234. }
  235. void ossl_thread_event_ctx_free(void *tlocal)
  236. {
  237. OPENSSL_free(tlocal);
  238. }
  239. static void ossl_arg_thread_stop(void *arg)
  240. {
  241. ossl_ctx_thread_stop((OSSL_LIB_CTX *)arg);
  242. }
  243. void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx)
  244. {
  245. THREAD_EVENT_HANDLER **hands;
  246. CRYPTO_THREAD_LOCAL *local
  247. = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX);
  248. if (local == NULL)
  249. return;
  250. hands = init_get_thread_local(local, 0, 0);
  251. init_thread_stop(ctx, hands);
  252. OPENSSL_free(hands);
  253. }
  254. #endif /* FIPS_MODULE */
  255. static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands)
  256. {
  257. THREAD_EVENT_HANDLER *curr, *prev = NULL, *tmp;
  258. #ifndef FIPS_MODULE
  259. GLOBAL_TEVENT_REGISTER *gtr;
  260. #endif
  261. /* Can't do much about this */
  262. if (hands == NULL)
  263. return;
  264. #ifndef FIPS_MODULE
  265. gtr = get_global_tevent_register();
  266. if (gtr == NULL)
  267. return;
  268. if (!CRYPTO_THREAD_write_lock(gtr->lock))
  269. return;
  270. #endif
  271. curr = *hands;
  272. while (curr != NULL) {
  273. if (arg != NULL && curr->arg != arg) {
  274. prev = curr;
  275. curr = curr->next;
  276. continue;
  277. }
  278. curr->handfn(curr->arg);
  279. if (prev == NULL)
  280. *hands = curr->next;
  281. else
  282. prev->next = curr->next;
  283. tmp = curr;
  284. curr = curr->next;
  285. OPENSSL_free(tmp);
  286. }
  287. #ifndef FIPS_MODULE
  288. CRYPTO_THREAD_unlock(gtr->lock);
  289. #endif
  290. }
  291. int ossl_init_thread_start(const void *index, void *arg,
  292. OSSL_thread_stop_handler_fn handfn)
  293. {
  294. THREAD_EVENT_HANDLER **hands;
  295. THREAD_EVENT_HANDLER *hand;
  296. #ifdef FIPS_MODULE
  297. OSSL_LIB_CTX *ctx = arg;
  298. /*
  299. * In FIPS mode the list of THREAD_EVENT_HANDLERs is unique per combination
  300. * of OSSL_LIB_CTX and thread. This is because in FIPS mode each
  301. * OSSL_LIB_CTX gets informed about thread stop events individually.
  302. */
  303. CRYPTO_THREAD_LOCAL *local
  304. = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX);
  305. #else
  306. /*
  307. * Outside of FIPS mode the list of THREAD_EVENT_HANDLERs is unique per
  308. * thread, but may hold multiple OSSL_LIB_CTXs. We only get told about
  309. * thread stop events globally, so we have to ensure all affected
  310. * OSSL_LIB_CTXs are informed.
  311. */
  312. CRYPTO_THREAD_LOCAL *local = &destructor_key.value;
  313. #endif
  314. hands = init_get_thread_local(local, 1, 0);
  315. if (hands == NULL)
  316. return 0;
  317. #ifdef FIPS_MODULE
  318. if (*hands == NULL) {
  319. /*
  320. * We've not yet registered any handlers for this thread. We need to get
  321. * libcrypto to tell us about later thread stop events. c_thread_start
  322. * is a callback to libcrypto defined in fipsprov.c
  323. */
  324. if (!c_thread_start(FIPS_get_core_handle(ctx), ossl_arg_thread_stop,
  325. ctx))
  326. return 0;
  327. }
  328. #endif
  329. hand = OPENSSL_malloc(sizeof(*hand));
  330. if (hand == NULL)
  331. return 0;
  332. hand->handfn = handfn;
  333. hand->arg = arg;
  334. #ifndef FIPS_MODULE
  335. hand->index = index;
  336. #endif
  337. hand->next = *hands;
  338. *hands = hand;
  339. return 1;
  340. }
  341. #ifndef FIPS_MODULE
  342. static int init_thread_deregister(void *index, int all)
  343. {
  344. GLOBAL_TEVENT_REGISTER *gtr;
  345. int i;
  346. gtr = get_global_tevent_register();
  347. if (gtr == NULL)
  348. return 0;
  349. if (!all) {
  350. if (!CRYPTO_THREAD_write_lock(gtr->lock))
  351. return 0;
  352. } else {
  353. glob_tevent_reg = NULL;
  354. }
  355. for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) {
  356. THREAD_EVENT_HANDLER **hands
  357. = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i);
  358. THREAD_EVENT_HANDLER *curr = NULL, *prev = NULL, *tmp;
  359. if (hands == NULL) {
  360. if (!all)
  361. CRYPTO_THREAD_unlock(gtr->lock);
  362. return 0;
  363. }
  364. curr = *hands;
  365. while (curr != NULL) {
  366. if (all || curr->index == index) {
  367. if (prev != NULL)
  368. prev->next = curr->next;
  369. else
  370. *hands = curr->next;
  371. tmp = curr;
  372. curr = curr->next;
  373. OPENSSL_free(tmp);
  374. continue;
  375. }
  376. prev = curr;
  377. curr = curr->next;
  378. }
  379. if (all)
  380. OPENSSL_free(hands);
  381. }
  382. if (all) {
  383. CRYPTO_THREAD_lock_free(gtr->lock);
  384. sk_THREAD_EVENT_HANDLER_PTR_free(gtr->skhands);
  385. OPENSSL_free(gtr);
  386. } else {
  387. CRYPTO_THREAD_unlock(gtr->lock);
  388. }
  389. return 1;
  390. }
  391. int ossl_init_thread_deregister(void *index)
  392. {
  393. return init_thread_deregister(index, 0);
  394. }
  395. #endif