dso_dl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* dso_dl.c -*- mode:C; c-file-style: "eay" -*- */
  2. /*
  3. * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include "cryptlib.h"
  61. #include <openssl/dso.h>
  62. #ifndef DSO_DL
  63. DSO_METHOD *DSO_METHOD_dl(void)
  64. {
  65. return NULL;
  66. }
  67. #else
  68. # include <dl.h>
  69. /* Part of the hack in "dl_load" ... */
  70. # define DSO_MAX_TRANSLATED_SIZE 256
  71. static int dl_load(DSO *dso);
  72. static int dl_unload(DSO *dso);
  73. static void *dl_bind_var(DSO *dso, const char *symname);
  74. static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname);
  75. # if 0
  76. static int dl_unbind_var(DSO *dso, char *symname, void *symptr);
  77. static int dl_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
  78. static int dl_init(DSO *dso);
  79. static int dl_finish(DSO *dso);
  80. static int dl_ctrl(DSO *dso, int cmd, long larg, void *parg);
  81. # endif
  82. static char *dl_name_converter(DSO *dso, const char *filename);
  83. static char *dl_merger(DSO *dso, const char *filespec1,
  84. const char *filespec2);
  85. static int dl_pathbyaddr(void *addr, char *path, int sz);
  86. static void *dl_globallookup(const char *name);
  87. static DSO_METHOD dso_meth_dl = {
  88. "OpenSSL 'dl' shared library method",
  89. dl_load,
  90. dl_unload,
  91. dl_bind_var,
  92. dl_bind_func,
  93. /* For now, "unbind" doesn't exist */
  94. # if 0
  95. NULL, /* unbind_var */
  96. NULL, /* unbind_func */
  97. # endif
  98. NULL, /* ctrl */
  99. dl_name_converter,
  100. dl_merger,
  101. NULL, /* init */
  102. NULL, /* finish */
  103. dl_pathbyaddr,
  104. dl_globallookup
  105. };
  106. DSO_METHOD *DSO_METHOD_dl(void)
  107. {
  108. return (&dso_meth_dl);
  109. }
  110. /*
  111. * For this DSO_METHOD, our meth_data STACK will contain; (i) the handle
  112. * (shl_t) returned from shl_load(). NB: I checked on HPUX11 and shl_t is
  113. * itself a pointer type so the cast is safe.
  114. */
  115. static int dl_load(DSO *dso)
  116. {
  117. shl_t ptr = NULL;
  118. /*
  119. * We don't do any fancy retries or anything, just take the method's (or
  120. * DSO's if it has the callback set) best translation of the
  121. * platform-independant filename and try once with that.
  122. */
  123. char *filename = DSO_convert_filename(dso, NULL);
  124. if (filename == NULL) {
  125. DSOerr(DSO_F_DL_LOAD, DSO_R_NO_FILENAME);
  126. goto err;
  127. }
  128. ptr = shl_load(filename, BIND_IMMEDIATE |
  129. (dso->flags & DSO_FLAG_NO_NAME_TRANSLATION ? 0 :
  130. DYNAMIC_PATH), 0L);
  131. if (ptr == NULL) {
  132. DSOerr(DSO_F_DL_LOAD, DSO_R_LOAD_FAILED);
  133. ERR_add_error_data(4, "filename(", filename, "): ", strerror(errno));
  134. goto err;
  135. }
  136. if (!sk_push(dso->meth_data, (char *)ptr)) {
  137. DSOerr(DSO_F_DL_LOAD, DSO_R_STACK_ERROR);
  138. goto err;
  139. }
  140. /*
  141. * Success, stick the converted filename we've loaded under into the DSO
  142. * (it also serves as the indicator that we are currently loaded).
  143. */
  144. dso->loaded_filename = filename;
  145. return (1);
  146. err:
  147. /* Cleanup! */
  148. if (filename != NULL)
  149. OPENSSL_free(filename);
  150. if (ptr != NULL)
  151. shl_unload(ptr);
  152. return (0);
  153. }
  154. static int dl_unload(DSO *dso)
  155. {
  156. shl_t ptr;
  157. if (dso == NULL) {
  158. DSOerr(DSO_F_DL_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
  159. return (0);
  160. }
  161. if (sk_num(dso->meth_data) < 1)
  162. return (1);
  163. /* Is this statement legal? */
  164. ptr = (shl_t) sk_pop(dso->meth_data);
  165. if (ptr == NULL) {
  166. DSOerr(DSO_F_DL_UNLOAD, DSO_R_NULL_HANDLE);
  167. /*
  168. * Should push the value back onto the stack in case of a retry.
  169. */
  170. sk_push(dso->meth_data, (char *)ptr);
  171. return (0);
  172. }
  173. shl_unload(ptr);
  174. return (1);
  175. }
  176. static void *dl_bind_var(DSO *dso, const char *symname)
  177. {
  178. shl_t ptr;
  179. void *sym;
  180. if ((dso == NULL) || (symname == NULL)) {
  181. DSOerr(DSO_F_DL_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
  182. return (NULL);
  183. }
  184. if (sk_num(dso->meth_data) < 1) {
  185. DSOerr(DSO_F_DL_BIND_VAR, DSO_R_STACK_ERROR);
  186. return (NULL);
  187. }
  188. ptr = (shl_t) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  189. if (ptr == NULL) {
  190. DSOerr(DSO_F_DL_BIND_VAR, DSO_R_NULL_HANDLE);
  191. return (NULL);
  192. }
  193. if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0) {
  194. DSOerr(DSO_F_DL_BIND_VAR, DSO_R_SYM_FAILURE);
  195. ERR_add_error_data(4, "symname(", symname, "): ", strerror(errno));
  196. return (NULL);
  197. }
  198. return (sym);
  199. }
  200. static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname)
  201. {
  202. shl_t ptr;
  203. void *sym;
  204. if ((dso == NULL) || (symname == NULL)) {
  205. DSOerr(DSO_F_DL_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  206. return (NULL);
  207. }
  208. if (sk_num(dso->meth_data) < 1) {
  209. DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_STACK_ERROR);
  210. return (NULL);
  211. }
  212. ptr = (shl_t) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  213. if (ptr == NULL) {
  214. DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_NULL_HANDLE);
  215. return (NULL);
  216. }
  217. if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0) {
  218. DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_SYM_FAILURE);
  219. ERR_add_error_data(4, "symname(", symname, "): ", strerror(errno));
  220. return (NULL);
  221. }
  222. return ((DSO_FUNC_TYPE)sym);
  223. }
  224. static char *dl_merger(DSO *dso, const char *filespec1, const char *filespec2)
  225. {
  226. char *merged;
  227. if (!filespec1 && !filespec2) {
  228. DSOerr(DSO_F_DL_MERGER, ERR_R_PASSED_NULL_PARAMETER);
  229. return (NULL);
  230. }
  231. /*
  232. * If the first file specification is a rooted path, it rules. same goes
  233. * if the second file specification is missing.
  234. */
  235. if (!filespec2 || filespec1[0] == '/') {
  236. merged = OPENSSL_malloc(strlen(filespec1) + 1);
  237. if (!merged) {
  238. DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
  239. return (NULL);
  240. }
  241. strcpy(merged, filespec1);
  242. }
  243. /*
  244. * If the first file specification is missing, the second one rules.
  245. */
  246. else if (!filespec1) {
  247. merged = OPENSSL_malloc(strlen(filespec2) + 1);
  248. if (!merged) {
  249. DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
  250. return (NULL);
  251. }
  252. strcpy(merged, filespec2);
  253. } else
  254. /*
  255. * This part isn't as trivial as it looks. It assumes that the
  256. * second file specification really is a directory, and makes no
  257. * checks whatsoever. Therefore, the result becomes the
  258. * concatenation of filespec2 followed by a slash followed by
  259. * filespec1.
  260. */
  261. {
  262. int spec2len, len;
  263. spec2len = (filespec2 ? strlen(filespec2) : 0);
  264. len = spec2len + (filespec1 ? strlen(filespec1) : 0);
  265. if (filespec2 && filespec2[spec2len - 1] == '/') {
  266. spec2len--;
  267. len--;
  268. }
  269. merged = OPENSSL_malloc(len + 2);
  270. if (!merged) {
  271. DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
  272. return (NULL);
  273. }
  274. strcpy(merged, filespec2);
  275. merged[spec2len] = '/';
  276. strcpy(&merged[spec2len + 1], filespec1);
  277. }
  278. return (merged);
  279. }
  280. /*
  281. * This function is identical to the one in dso_dlfcn.c, but as it is highly
  282. * unlikely that both the "dl" *and* "dlfcn" variants are being compiled at
  283. * the same time, there's no great duplicating the code. Figuring out an
  284. * elegant way to share one copy of the code would be more difficult and
  285. * would not leave the implementations independant.
  286. */
  287. # if defined(__hpux)
  288. static const char extension[] = ".sl";
  289. # else
  290. static const char extension[] = ".so";
  291. # endif
  292. static char *dl_name_converter(DSO *dso, const char *filename)
  293. {
  294. char *translated;
  295. int len, rsize, transform;
  296. len = strlen(filename);
  297. rsize = len + 1;
  298. transform = (strstr(filename, "/") == NULL);
  299. {
  300. /* We will convert this to "%s.s?" or "lib%s.s?" */
  301. rsize += strlen(extension); /* The length of ".s?" */
  302. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  303. rsize += 3; /* The length of "lib" */
  304. }
  305. translated = OPENSSL_malloc(rsize);
  306. if (translated == NULL) {
  307. DSOerr(DSO_F_DL_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
  308. return (NULL);
  309. }
  310. if (transform) {
  311. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  312. sprintf(translated, "lib%s%s", filename, extension);
  313. else
  314. sprintf(translated, "%s%s", filename, extension);
  315. } else
  316. sprintf(translated, "%s", filename);
  317. return (translated);
  318. }
  319. static int dl_pathbyaddr(void *addr, char *path, int sz)
  320. {
  321. struct shl_descriptor inf;
  322. int i, len;
  323. if (addr == NULL) {
  324. union {
  325. int (*f) (void *, char *, int);
  326. void *p;
  327. } t = {
  328. dl_pathbyaddr
  329. };
  330. addr = t.p;
  331. }
  332. for (i = -1; shl_get_r(i, &inf) == 0; i++) {
  333. if (((size_t)addr >= inf.tstart && (size_t)addr < inf.tend) ||
  334. ((size_t)addr >= inf.dstart && (size_t)addr < inf.dend)) {
  335. len = (int)strlen(inf.filename);
  336. if (sz <= 0)
  337. return len + 1;
  338. if (len >= sz)
  339. len = sz - 1;
  340. memcpy(path, inf.filename, len);
  341. path[len++] = 0;
  342. return len;
  343. }
  344. }
  345. return -1;
  346. }
  347. static void *dl_globallookup(const char *name)
  348. {
  349. void *ret;
  350. shl_t h = NULL;
  351. return shl_findsym(&h, name, TYPE_UNDEFINED, &ret) ? NULL : ret;
  352. }
  353. #endif /* DSO_DL */