initthread.c 12 KB

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