dso_lib.c 9.3 KB

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