dso_lib.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright 2000-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 "dso_local.h"
  10. #include "internal/refcount.h"
  11. DEFINE_STACK_OF(void)
  12. static DSO_METHOD *default_DSO_meth = NULL;
  13. static DSO *DSO_new_method(DSO_METHOD *meth)
  14. {
  15. DSO *ret;
  16. if (default_DSO_meth == NULL) {
  17. /*
  18. * We default to DSO_METH_openssl() which in turn defaults to
  19. * stealing the "best available" method. Will fallback to
  20. * DSO_METH_null() in the worst case.
  21. */
  22. default_DSO_meth = DSO_METHOD_openssl();
  23. }
  24. ret = OPENSSL_zalloc(sizeof(*ret));
  25. if (ret == NULL) {
  26. DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  27. return NULL;
  28. }
  29. ret->meth_data = sk_void_new_null();
  30. if (ret->meth_data == NULL) {
  31. /* sk_new doesn't generate any errors so we do */
  32. DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  33. OPENSSL_free(ret);
  34. return NULL;
  35. }
  36. ret->meth = default_DSO_meth;
  37. ret->references = 1;
  38. ret->lock = CRYPTO_THREAD_lock_new();
  39. if (ret->lock == NULL) {
  40. DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  41. sk_void_free(ret->meth_data);
  42. OPENSSL_free(ret);
  43. return NULL;
  44. }
  45. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  46. DSO_free(ret);
  47. ret = NULL;
  48. }
  49. return ret;
  50. }
  51. DSO *DSO_new(void)
  52. {
  53. return DSO_new_method(NULL);
  54. }
  55. int DSO_free(DSO *dso)
  56. {
  57. int i;
  58. if (dso == NULL)
  59. return 1;
  60. if (CRYPTO_DOWN_REF(&dso->references, &i, dso->lock) <= 0)
  61. return 0;
  62. REF_PRINT_COUNT("DSO", dso);
  63. if (i > 0)
  64. return 1;
  65. REF_ASSERT_ISNT(i < 0);
  66. if ((dso->flags & DSO_FLAG_NO_UNLOAD_ON_FREE) == 0) {
  67. if ((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso)) {
  68. DSOerr(DSO_F_DSO_FREE, DSO_R_UNLOAD_FAILED);
  69. return 0;
  70. }
  71. }
  72. if ((dso->meth->finish != NULL) && !dso->meth->finish(dso)) {
  73. DSOerr(DSO_F_DSO_FREE, DSO_R_FINISH_FAILED);
  74. return 0;
  75. }
  76. sk_void_free(dso->meth_data);
  77. OPENSSL_free(dso->filename);
  78. OPENSSL_free(dso->loaded_filename);
  79. CRYPTO_THREAD_lock_free(dso->lock);
  80. OPENSSL_free(dso);
  81. return 1;
  82. }
  83. int DSO_flags(DSO *dso)
  84. {
  85. return ((dso == NULL) ? 0 : dso->flags);
  86. }
  87. int DSO_up_ref(DSO *dso)
  88. {
  89. int i;
  90. if (dso == NULL) {
  91. DSOerr(DSO_F_DSO_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
  92. return 0;
  93. }
  94. if (CRYPTO_UP_REF(&dso->references, &i, dso->lock) <= 0)
  95. return 0;
  96. REF_PRINT_COUNT("DSO", dso);
  97. REF_ASSERT_ISNT(i < 2);
  98. return ((i > 1) ? 1 : 0);
  99. }
  100. DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
  101. {
  102. DSO *ret;
  103. int allocated = 0;
  104. if (dso == NULL) {
  105. ret = DSO_new_method(meth);
  106. if (ret == NULL) {
  107. DSOerr(DSO_F_DSO_LOAD, ERR_R_MALLOC_FAILURE);
  108. goto err;
  109. }
  110. allocated = 1;
  111. /* Pass the provided flags to the new DSO object */
  112. if (DSO_ctrl(ret, DSO_CTRL_SET_FLAGS, flags, NULL) < 0) {
  113. DSOerr(DSO_F_DSO_LOAD, DSO_R_CTRL_FAILED);
  114. goto err;
  115. }
  116. } else
  117. ret = dso;
  118. /* Don't load if we're currently already loaded */
  119. if (ret->filename != NULL) {
  120. DSOerr(DSO_F_DSO_LOAD, DSO_R_DSO_ALREADY_LOADED);
  121. goto err;
  122. }
  123. /*
  124. * filename can only be NULL if we were passed a dso that already has one
  125. * set.
  126. */
  127. if (filename != NULL)
  128. if (!DSO_set_filename(ret, filename)) {
  129. DSOerr(DSO_F_DSO_LOAD, DSO_R_SET_FILENAME_FAILED);
  130. goto err;
  131. }
  132. filename = ret->filename;
  133. if (filename == NULL) {
  134. DSOerr(DSO_F_DSO_LOAD, DSO_R_NO_FILENAME);
  135. goto err;
  136. }
  137. if (ret->meth->dso_load == NULL) {
  138. DSOerr(DSO_F_DSO_LOAD, DSO_R_UNSUPPORTED);
  139. goto err;
  140. }
  141. if (!ret->meth->dso_load(ret)) {
  142. DSOerr(DSO_F_DSO_LOAD, DSO_R_LOAD_FAILED);
  143. goto err;
  144. }
  145. /* Load succeeded */
  146. return ret;
  147. err:
  148. if (allocated)
  149. DSO_free(ret);
  150. return NULL;
  151. }
  152. DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname)
  153. {
  154. DSO_FUNC_TYPE ret = NULL;
  155. if ((dso == NULL) || (symname == NULL)) {
  156. DSOerr(DSO_F_DSO_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  157. return NULL;
  158. }
  159. if (dso->meth->dso_bind_func == NULL) {
  160. DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_UNSUPPORTED);
  161. return NULL;
  162. }
  163. if ((ret = dso->meth->dso_bind_func(dso, symname)) == NULL) {
  164. DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_SYM_FAILURE);
  165. return NULL;
  166. }
  167. /* Success */
  168. return ret;
  169. }
  170. /*
  171. * I don't really like these *_ctrl functions very much to be perfectly
  172. * honest. For one thing, I think I have to return a negative value for any
  173. * error because possible DSO_ctrl() commands may return values such as
  174. * "size"s that can legitimately be zero (making the standard
  175. * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
  176. * times. I'd prefer "output" values to be passed by reference and the return
  177. * value as success/failure like usual ... but we conform when we must... :-)
  178. */
  179. long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg)
  180. {
  181. if (dso == NULL) {
  182. DSOerr(DSO_F_DSO_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  183. return -1;
  184. }
  185. /*
  186. * We should intercept certain generic commands and only pass control to
  187. * the method-specific ctrl() function if it's something we don't handle.
  188. */
  189. switch (cmd) {
  190. case DSO_CTRL_GET_FLAGS:
  191. return dso->flags;
  192. case DSO_CTRL_SET_FLAGS:
  193. dso->flags = (int)larg;
  194. return 0;
  195. case DSO_CTRL_OR_FLAGS:
  196. dso->flags |= (int)larg;
  197. return 0;
  198. default:
  199. break;
  200. }
  201. if ((dso->meth == NULL) || (dso->meth->dso_ctrl == NULL)) {
  202. DSOerr(DSO_F_DSO_CTRL, DSO_R_UNSUPPORTED);
  203. return -1;
  204. }
  205. return dso->meth->dso_ctrl(dso, cmd, larg, parg);
  206. }
  207. const char *DSO_get_filename(DSO *dso)
  208. {
  209. if (dso == NULL) {
  210. DSOerr(DSO_F_DSO_GET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
  211. return NULL;
  212. }
  213. return dso->filename;
  214. }
  215. int DSO_set_filename(DSO *dso, const char *filename)
  216. {
  217. char *copied;
  218. if ((dso == NULL) || (filename == NULL)) {
  219. DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
  220. return 0;
  221. }
  222. if (dso->loaded_filename) {
  223. DSOerr(DSO_F_DSO_SET_FILENAME, DSO_R_DSO_ALREADY_LOADED);
  224. return 0;
  225. }
  226. /* We'll duplicate filename */
  227. copied = OPENSSL_strdup(filename);
  228. if (copied == NULL) {
  229. DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_MALLOC_FAILURE);
  230. return 0;
  231. }
  232. OPENSSL_free(dso->filename);
  233. dso->filename = copied;
  234. return 1;
  235. }
  236. char *DSO_merge(DSO *dso, const char *filespec1, const char *filespec2)
  237. {
  238. char *result = NULL;
  239. if (dso == NULL || filespec1 == NULL) {
  240. DSOerr(DSO_F_DSO_MERGE, ERR_R_PASSED_NULL_PARAMETER);
  241. return NULL;
  242. }
  243. if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
  244. if (dso->merger != NULL)
  245. result = dso->merger(dso, filespec1, filespec2);
  246. else if (dso->meth->dso_merger != NULL)
  247. result = dso->meth->dso_merger(dso, filespec1, filespec2);
  248. }
  249. return result;
  250. }
  251. char *DSO_convert_filename(DSO *dso, const char *filename)
  252. {
  253. char *result = NULL;
  254. if (dso == NULL) {
  255. DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
  256. return NULL;
  257. }
  258. if (filename == NULL)
  259. filename = dso->filename;
  260. if (filename == NULL) {
  261. DSOerr(DSO_F_DSO_CONVERT_FILENAME, DSO_R_NO_FILENAME);
  262. return NULL;
  263. }
  264. if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
  265. if (dso->name_converter != NULL)
  266. result = dso->name_converter(dso, filename);
  267. else if (dso->meth->dso_name_converter != NULL)
  268. result = dso->meth->dso_name_converter(dso, filename);
  269. }
  270. if (result == NULL) {
  271. result = OPENSSL_strdup(filename);
  272. if (result == NULL) {
  273. DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_MALLOC_FAILURE);
  274. return NULL;
  275. }
  276. }
  277. return result;
  278. }
  279. int DSO_pathbyaddr(void *addr, char *path, int sz)
  280. {
  281. DSO_METHOD *meth = default_DSO_meth;
  282. if (meth == NULL)
  283. meth = DSO_METHOD_openssl();
  284. if (meth->pathbyaddr == NULL) {
  285. DSOerr(DSO_F_DSO_PATHBYADDR, DSO_R_UNSUPPORTED);
  286. return -1;
  287. }
  288. return (*meth->pathbyaddr) (addr, path, sz);
  289. }
  290. DSO *DSO_dsobyaddr(void *addr, int flags)
  291. {
  292. DSO *ret = NULL;
  293. char *filename = NULL;
  294. int len = DSO_pathbyaddr(addr, NULL, 0);
  295. if (len < 0)
  296. return NULL;
  297. filename = OPENSSL_malloc(len);
  298. if (filename != NULL
  299. && DSO_pathbyaddr(addr, filename, len) == len)
  300. ret = DSO_load(NULL, filename, NULL, flags);
  301. OPENSSL_free(filename);
  302. return ret;
  303. }
  304. void *DSO_global_lookup(const char *name)
  305. {
  306. DSO_METHOD *meth = default_DSO_meth;
  307. if (meth == NULL)
  308. meth = DSO_METHOD_openssl();
  309. if (meth->globallookup == NULL) {
  310. DSOerr(DSO_F_DSO_GLOBAL_LOOKUP, DSO_R_UNSUPPORTED);
  311. return NULL;
  312. }
  313. return (*meth->globallookup) (name);
  314. }