initthread.c 12 KB

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