dso_lib.c 9.1 KB

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