initthread.c 12 KB

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