ex_data.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "internal/cryptlib_int.h"
  10. #include "internal/thread_once.h"
  11. /*
  12. * Each structure type (sometimes called a class), that supports
  13. * exdata has a stack of callbacks for each instance.
  14. */
  15. struct ex_callback_st {
  16. long argl; /* Arbitrary long */
  17. void *argp; /* Arbitrary void * */
  18. CRYPTO_EX_new *new_func;
  19. CRYPTO_EX_free *free_func;
  20. CRYPTO_EX_dup *dup_func;
  21. };
  22. /*
  23. * The state for each class. This could just be a typedef, but
  24. * a structure allows future changes.
  25. */
  26. typedef struct ex_callbacks_st {
  27. STACK_OF(EX_CALLBACK) *meth;
  28. } EX_CALLBACKS;
  29. static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
  30. static CRYPTO_RWLOCK *ex_data_lock = NULL;
  31. static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT;
  32. DEFINE_RUN_ONCE_STATIC(do_ex_data_init)
  33. {
  34. OPENSSL_init_crypto(0, NULL);
  35. ex_data_lock = CRYPTO_THREAD_lock_new();
  36. return ex_data_lock != NULL;
  37. }
  38. /*
  39. * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
  40. * a given class. On success, *holds the lock.*
  41. */
  42. static EX_CALLBACKS *get_and_lock(int class_index)
  43. {
  44. EX_CALLBACKS *ip;
  45. if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
  46. CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);
  47. return NULL;
  48. }
  49. if (!RUN_ONCE(&ex_data_init, do_ex_data_init)) {
  50. CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE);
  51. return NULL;
  52. }
  53. if (ex_data_lock == NULL) {
  54. /*
  55. * This can happen in normal operation when using CRYPTO_mem_leaks().
  56. * The CRYPTO_mem_leaks() function calls OPENSSL_cleanup() which cleans
  57. * up the locks. Subsequently the BIO that CRYPTO_mem_leaks() uses gets
  58. * freed, which also attempts to free the ex_data. However
  59. * CRYPTO_mem_leaks() ensures that the ex_data is freed early (i.e.
  60. * before OPENSSL_cleanup() is called), so if we get here we can safely
  61. * ignore this operation. We just treat it as an error.
  62. */
  63. return NULL;
  64. }
  65. ip = &ex_data[class_index];
  66. CRYPTO_THREAD_write_lock(ex_data_lock);
  67. return ip;
  68. }
  69. static void cleanup_cb(EX_CALLBACK *funcs)
  70. {
  71. OPENSSL_free(funcs);
  72. }
  73. /*
  74. * Release all "ex_data" state to prevent memory leaks. This can't be made
  75. * thread-safe without overhauling a lot of stuff, and shouldn't really be
  76. * called under potential race-conditions anyway (it's for program shutdown
  77. * after all).
  78. */
  79. void crypto_cleanup_all_ex_data_int(void)
  80. {
  81. int i;
  82. for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
  83. EX_CALLBACKS *ip = &ex_data[i];
  84. sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
  85. ip->meth = NULL;
  86. }
  87. CRYPTO_THREAD_lock_free(ex_data_lock);
  88. ex_data_lock = NULL;
  89. }
  90. /*
  91. * Unregister a new index by replacing the callbacks with no-ops.
  92. * Any in-use instances are leaked.
  93. */
  94. static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
  95. long argl, void *argp)
  96. {
  97. }
  98. static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
  99. long argl, void *argp)
  100. {
  101. }
  102. static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
  103. void *from_d, int idx,
  104. long argl, void *argp)
  105. {
  106. return 1;
  107. }
  108. int CRYPTO_free_ex_index(int class_index, int idx)
  109. {
  110. EX_CALLBACKS *ip = get_and_lock(class_index);
  111. EX_CALLBACK *a;
  112. int toret = 0;
  113. if (ip == NULL)
  114. return 0;
  115. if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
  116. goto err;
  117. a = sk_EX_CALLBACK_value(ip->meth, idx);
  118. if (a == NULL)
  119. goto err;
  120. a->new_func = dummy_new;
  121. a->dup_func = dummy_dup;
  122. a->free_func = dummy_free;
  123. toret = 1;
  124. err:
  125. CRYPTO_THREAD_unlock(ex_data_lock);
  126. return toret;
  127. }
  128. /*
  129. * Register a new index.
  130. */
  131. int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
  132. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  133. CRYPTO_EX_free *free_func)
  134. {
  135. int toret = -1;
  136. EX_CALLBACK *a;
  137. EX_CALLBACKS *ip = get_and_lock(class_index);
  138. if (ip == NULL)
  139. return -1;
  140. if (ip->meth == NULL) {
  141. ip->meth = sk_EX_CALLBACK_new_null();
  142. /* We push an initial value on the stack because the SSL
  143. * "app_data" routines use ex_data index zero. See RT 3710. */
  144. if (ip->meth == NULL
  145. || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
  146. CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
  147. goto err;
  148. }
  149. }
  150. a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
  151. if (a == NULL) {
  152. CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
  153. goto err;
  154. }
  155. a->argl = argl;
  156. a->argp = argp;
  157. a->new_func = new_func;
  158. a->dup_func = dup_func;
  159. a->free_func = free_func;
  160. if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
  161. CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
  162. OPENSSL_free(a);
  163. goto err;
  164. }
  165. toret = sk_EX_CALLBACK_num(ip->meth) - 1;
  166. (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
  167. err:
  168. CRYPTO_THREAD_unlock(ex_data_lock);
  169. return toret;
  170. }
  171. /*
  172. * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
  173. * calling new() callbacks for each index in the class used by this variable
  174. * Thread-safe by copying a class's array of "EX_CALLBACK" entries
  175. * in the lock, then using them outside the lock. Note this only applies
  176. * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
  177. */
  178. int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
  179. {
  180. int mx, i;
  181. void *ptr;
  182. EX_CALLBACK **storage = NULL;
  183. EX_CALLBACK *stack[10];
  184. EX_CALLBACKS *ip = get_and_lock(class_index);
  185. if (ip == NULL)
  186. return 0;
  187. ad->sk = NULL;
  188. mx = sk_EX_CALLBACK_num(ip->meth);
  189. if (mx > 0) {
  190. if (mx < (int)OSSL_NELEM(stack))
  191. storage = stack;
  192. else
  193. storage = OPENSSL_malloc(sizeof(*storage) * mx);
  194. if (storage != NULL)
  195. for (i = 0; i < mx; i++)
  196. storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
  197. }
  198. CRYPTO_THREAD_unlock(ex_data_lock);
  199. if (mx > 0 && storage == NULL) {
  200. CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
  201. return 0;
  202. }
  203. for (i = 0; i < mx; i++) {
  204. if (storage[i] && storage[i]->new_func) {
  205. ptr = CRYPTO_get_ex_data(ad, i);
  206. storage[i]->new_func(obj, ptr, ad, i,
  207. storage[i]->argl, storage[i]->argp);
  208. }
  209. }
  210. if (storage != stack)
  211. OPENSSL_free(storage);
  212. return 1;
  213. }
  214. /*
  215. * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
  216. * for each index in the class used by this variable
  217. */
  218. int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
  219. const CRYPTO_EX_DATA *from)
  220. {
  221. int mx, j, i;
  222. void *ptr;
  223. EX_CALLBACK *stack[10];
  224. EX_CALLBACK **storage = NULL;
  225. EX_CALLBACKS *ip;
  226. int toret = 0;
  227. if (from->sk == NULL)
  228. /* Nothing to copy over */
  229. return 1;
  230. if ((ip = get_and_lock(class_index)) == NULL)
  231. return 0;
  232. mx = sk_EX_CALLBACK_num(ip->meth);
  233. j = sk_void_num(from->sk);
  234. if (j < mx)
  235. mx = j;
  236. if (mx > 0) {
  237. if (mx < (int)OSSL_NELEM(stack))
  238. storage = stack;
  239. else
  240. storage = OPENSSL_malloc(sizeof(*storage) * mx);
  241. if (storage != NULL)
  242. for (i = 0; i < mx; i++)
  243. storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
  244. }
  245. CRYPTO_THREAD_unlock(ex_data_lock);
  246. if (mx == 0)
  247. return 1;
  248. if (storage == NULL) {
  249. CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
  250. return 0;
  251. }
  252. /*
  253. * Make sure the ex_data stack is at least |mx| elements long to avoid
  254. * issues in the for loop that follows; so go get the |mx|'th element
  255. * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
  256. * to itself. This is normally a no-op; but ensures the stack is the
  257. * proper size
  258. */
  259. if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
  260. goto err;
  261. for (i = 0; i < mx; i++) {
  262. ptr = CRYPTO_get_ex_data(from, i);
  263. if (storage[i] && storage[i]->dup_func)
  264. if (!storage[i]->dup_func(to, from, &ptr, i,
  265. storage[i]->argl, storage[i]->argp))
  266. goto err;
  267. CRYPTO_set_ex_data(to, i, ptr);
  268. }
  269. toret = 1;
  270. err:
  271. if (storage != stack)
  272. OPENSSL_free(storage);
  273. return toret;
  274. }
  275. /*
  276. * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
  277. * each index in the class used by this variable
  278. */
  279. void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
  280. {
  281. int mx, i;
  282. EX_CALLBACKS *ip;
  283. void *ptr;
  284. EX_CALLBACK *f;
  285. EX_CALLBACK *stack[10];
  286. EX_CALLBACK **storage = NULL;
  287. if ((ip = get_and_lock(class_index)) == NULL)
  288. goto err;
  289. mx = sk_EX_CALLBACK_num(ip->meth);
  290. if (mx > 0) {
  291. if (mx < (int)OSSL_NELEM(stack))
  292. storage = stack;
  293. else
  294. storage = OPENSSL_malloc(sizeof(*storage) * mx);
  295. if (storage != NULL)
  296. for (i = 0; i < mx; i++)
  297. storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
  298. }
  299. CRYPTO_THREAD_unlock(ex_data_lock);
  300. for (i = 0; i < mx; i++) {
  301. if (storage != NULL)
  302. f = storage[i];
  303. else {
  304. CRYPTO_THREAD_write_lock(ex_data_lock);
  305. f = sk_EX_CALLBACK_value(ip->meth, i);
  306. CRYPTO_THREAD_unlock(ex_data_lock);
  307. }
  308. if (f != NULL && f->free_func != NULL) {
  309. ptr = CRYPTO_get_ex_data(ad, i);
  310. f->free_func(obj, ptr, ad, i, f->argl, f->argp);
  311. }
  312. }
  313. if (storage != stack)
  314. OPENSSL_free(storage);
  315. err:
  316. sk_void_free(ad->sk);
  317. ad->sk = NULL;
  318. }
  319. /*
  320. * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
  321. * particular index in the class used by this variable
  322. */
  323. int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
  324. {
  325. int i;
  326. if (ad->sk == NULL) {
  327. if ((ad->sk = sk_void_new_null()) == NULL) {
  328. CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
  329. return 0;
  330. }
  331. }
  332. for (i = sk_void_num(ad->sk); i <= idx; ++i) {
  333. if (!sk_void_push(ad->sk, NULL)) {
  334. CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
  335. return 0;
  336. }
  337. }
  338. sk_void_set(ad->sk, idx, val);
  339. return 1;
  340. }
  341. /*
  342. * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
  343. * particular index in the class used by this variable
  344. */
  345. void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
  346. {
  347. if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
  348. return NULL;
  349. return sk_void_value(ad->sk, idx);
  350. }