dso_lib.c 8.7 KB

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