dso_dlfcn.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* dso_dlfcn.c */
  2. /*
  3. * Written by Geoff Thorpe (geoff@geoffthorpe.net) 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. /*
  60. * We need to do this early, because stdio.h includes the header files that
  61. * handle _GNU_SOURCE and other similar macros. Defining it later is simply
  62. * too late, because those headers are protected from re- inclusion.
  63. */
  64. #ifndef _GNU_SOURCE
  65. # define _GNU_SOURCE /* make sure dladdr is declared */
  66. #endif
  67. #include <stdio.h>
  68. #include "cryptlib.h"
  69. #include <openssl/dso.h>
  70. #ifndef DSO_DLFCN
  71. DSO_METHOD *DSO_METHOD_dlfcn(void)
  72. {
  73. return NULL;
  74. }
  75. #else
  76. # ifdef HAVE_DLFCN_H
  77. # ifdef __osf__
  78. # define __EXTENSIONS__
  79. # endif
  80. # include <dlfcn.h>
  81. # define HAVE_DLINFO 1
  82. # if defined(_AIX) || defined(__CYGWIN__) || \
  83. defined(__SCO_VERSION__) || defined(_SCO_ELF) || \
  84. (defined(__osf__) && !defined(RTLD_NEXT)) || \
  85. (defined(__OpenBSD__) && !defined(RTLD_SELF)) || \
  86. defined(__ANDROID__)
  87. # undef HAVE_DLINFO
  88. # endif
  89. # endif
  90. /* Part of the hack in "dlfcn_load" ... */
  91. # define DSO_MAX_TRANSLATED_SIZE 256
  92. static int dlfcn_load(DSO *dso);
  93. static int dlfcn_unload(DSO *dso);
  94. static void *dlfcn_bind_var(DSO *dso, const char *symname);
  95. static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname);
  96. # if 0
  97. static int dlfcn_unbind(DSO *dso, char *symname, void *symptr);
  98. static int dlfcn_init(DSO *dso);
  99. static int dlfcn_finish(DSO *dso);
  100. static long dlfcn_ctrl(DSO *dso, int cmd, long larg, void *parg);
  101. # endif
  102. static char *dlfcn_name_converter(DSO *dso, const char *filename);
  103. static char *dlfcn_merger(DSO *dso, const char *filespec1,
  104. const char *filespec2);
  105. static int dlfcn_pathbyaddr(void *addr, char *path, int sz);
  106. static void *dlfcn_globallookup(const char *name);
  107. static DSO_METHOD dso_meth_dlfcn = {
  108. "OpenSSL 'dlfcn' shared library method",
  109. dlfcn_load,
  110. dlfcn_unload,
  111. dlfcn_bind_var,
  112. dlfcn_bind_func,
  113. /* For now, "unbind" doesn't exist */
  114. # if 0
  115. NULL, /* unbind_var */
  116. NULL, /* unbind_func */
  117. # endif
  118. NULL, /* ctrl */
  119. dlfcn_name_converter,
  120. dlfcn_merger,
  121. NULL, /* init */
  122. NULL, /* finish */
  123. dlfcn_pathbyaddr,
  124. dlfcn_globallookup
  125. };
  126. DSO_METHOD *DSO_METHOD_dlfcn(void)
  127. {
  128. return (&dso_meth_dlfcn);
  129. }
  130. /*
  131. * Prior to using the dlopen() function, we should decide on the flag we
  132. * send. There's a few different ways of doing this and it's a messy
  133. * venn-diagram to match up which platforms support what. So as we don't have
  134. * autoconf yet, I'm implementing a hack that could be hacked further
  135. * relatively easily to deal with cases as we find them. Initially this is to
  136. * cope with OpenBSD.
  137. */
  138. # if defined(__OpenBSD__) || defined(__NetBSD__)
  139. # ifdef DL_LAZY
  140. # define DLOPEN_FLAG DL_LAZY
  141. # else
  142. # ifdef RTLD_NOW
  143. # define DLOPEN_FLAG RTLD_NOW
  144. # else
  145. # define DLOPEN_FLAG 0
  146. # endif
  147. # endif
  148. # else
  149. # ifdef OPENSSL_SYS_SUNOS
  150. # define DLOPEN_FLAG 1
  151. # else
  152. # define DLOPEN_FLAG RTLD_NOW /* Hope this works everywhere else */
  153. # endif
  154. # endif
  155. /*
  156. * For this DSO_METHOD, our meth_data STACK will contain; (i) the handle
  157. * (void*) returned from dlopen().
  158. */
  159. static int dlfcn_load(DSO *dso)
  160. {
  161. void *ptr = NULL;
  162. /* See applicable comments in dso_dl.c */
  163. char *filename = DSO_convert_filename(dso, NULL);
  164. int flags = DLOPEN_FLAG;
  165. if (filename == NULL) {
  166. DSOerr(DSO_F_DLFCN_LOAD, DSO_R_NO_FILENAME);
  167. goto err;
  168. }
  169. # ifdef RTLD_GLOBAL
  170. if (dso->flags & DSO_FLAG_GLOBAL_SYMBOLS)
  171. flags |= RTLD_GLOBAL;
  172. # endif
  173. ptr = dlopen(filename, flags);
  174. if (ptr == NULL) {
  175. DSOerr(DSO_F_DLFCN_LOAD, DSO_R_LOAD_FAILED);
  176. ERR_add_error_data(4, "filename(", filename, "): ", dlerror());
  177. goto err;
  178. }
  179. if (!sk_void_push(dso->meth_data, (char *)ptr)) {
  180. DSOerr(DSO_F_DLFCN_LOAD, DSO_R_STACK_ERROR);
  181. goto err;
  182. }
  183. /* Success */
  184. dso->loaded_filename = filename;
  185. return (1);
  186. err:
  187. /* Cleanup! */
  188. if (filename != NULL)
  189. OPENSSL_free(filename);
  190. if (ptr != NULL)
  191. dlclose(ptr);
  192. return (0);
  193. }
  194. static int dlfcn_unload(DSO *dso)
  195. {
  196. void *ptr;
  197. if (dso == NULL) {
  198. DSOerr(DSO_F_DLFCN_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
  199. return (0);
  200. }
  201. if (sk_void_num(dso->meth_data) < 1)
  202. return (1);
  203. ptr = sk_void_pop(dso->meth_data);
  204. if (ptr == NULL) {
  205. DSOerr(DSO_F_DLFCN_UNLOAD, DSO_R_NULL_HANDLE);
  206. /*
  207. * Should push the value back onto the stack in case of a retry.
  208. */
  209. sk_void_push(dso->meth_data, ptr);
  210. return (0);
  211. }
  212. /* For now I'm not aware of any errors associated with dlclose() */
  213. dlclose(ptr);
  214. return (1);
  215. }
  216. static void *dlfcn_bind_var(DSO *dso, const char *symname)
  217. {
  218. void *ptr, *sym;
  219. if ((dso == NULL) || (symname == NULL)) {
  220. DSOerr(DSO_F_DLFCN_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
  221. return (NULL);
  222. }
  223. if (sk_void_num(dso->meth_data) < 1) {
  224. DSOerr(DSO_F_DLFCN_BIND_VAR, DSO_R_STACK_ERROR);
  225. return (NULL);
  226. }
  227. ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
  228. if (ptr == NULL) {
  229. DSOerr(DSO_F_DLFCN_BIND_VAR, DSO_R_NULL_HANDLE);
  230. return (NULL);
  231. }
  232. sym = dlsym(ptr, symname);
  233. if (sym == NULL) {
  234. DSOerr(DSO_F_DLFCN_BIND_VAR, DSO_R_SYM_FAILURE);
  235. ERR_add_error_data(4, "symname(", symname, "): ", dlerror());
  236. return (NULL);
  237. }
  238. return (sym);
  239. }
  240. static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname)
  241. {
  242. void *ptr;
  243. union {
  244. DSO_FUNC_TYPE sym;
  245. void *dlret;
  246. } u;
  247. if ((dso == NULL) || (symname == NULL)) {
  248. DSOerr(DSO_F_DLFCN_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  249. return (NULL);
  250. }
  251. if (sk_void_num(dso->meth_data) < 1) {
  252. DSOerr(DSO_F_DLFCN_BIND_FUNC, DSO_R_STACK_ERROR);
  253. return (NULL);
  254. }
  255. ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
  256. if (ptr == NULL) {
  257. DSOerr(DSO_F_DLFCN_BIND_FUNC, DSO_R_NULL_HANDLE);
  258. return (NULL);
  259. }
  260. u.dlret = dlsym(ptr, symname);
  261. if (u.dlret == NULL) {
  262. DSOerr(DSO_F_DLFCN_BIND_FUNC, DSO_R_SYM_FAILURE);
  263. ERR_add_error_data(4, "symname(", symname, "): ", dlerror());
  264. return (NULL);
  265. }
  266. return u.sym;
  267. }
  268. static char *dlfcn_merger(DSO *dso, const char *filespec1,
  269. const char *filespec2)
  270. {
  271. char *merged;
  272. if (!filespec1 && !filespec2) {
  273. DSOerr(DSO_F_DLFCN_MERGER, ERR_R_PASSED_NULL_PARAMETER);
  274. return (NULL);
  275. }
  276. /*
  277. * If the first file specification is a rooted path, it rules. same goes
  278. * if the second file specification is missing.
  279. */
  280. if (!filespec2 || (filespec1 != NULL && filespec1[0] == '/')) {
  281. merged = OPENSSL_malloc(strlen(filespec1) + 1);
  282. if (!merged) {
  283. DSOerr(DSO_F_DLFCN_MERGER, ERR_R_MALLOC_FAILURE);
  284. return (NULL);
  285. }
  286. strcpy(merged, filespec1);
  287. }
  288. /*
  289. * If the first file specification is missing, the second one rules.
  290. */
  291. else if (!filespec1) {
  292. merged = OPENSSL_malloc(strlen(filespec2) + 1);
  293. if (!merged) {
  294. DSOerr(DSO_F_DLFCN_MERGER, ERR_R_MALLOC_FAILURE);
  295. return (NULL);
  296. }
  297. strcpy(merged, filespec2);
  298. } else {
  299. /*
  300. * This part isn't as trivial as it looks. It assumes that the
  301. * second file specification really is a directory, and makes no
  302. * checks whatsoever. Therefore, the result becomes the
  303. * concatenation of filespec2 followed by a slash followed by
  304. * filespec1.
  305. */
  306. int spec2len, len;
  307. spec2len = strlen(filespec2);
  308. len = spec2len + strlen(filespec1);
  309. if (spec2len && filespec2[spec2len - 1] == '/') {
  310. spec2len--;
  311. len--;
  312. }
  313. merged = OPENSSL_malloc(len + 2);
  314. if (!merged) {
  315. DSOerr(DSO_F_DLFCN_MERGER, ERR_R_MALLOC_FAILURE);
  316. return (NULL);
  317. }
  318. strcpy(merged, filespec2);
  319. merged[spec2len] = '/';
  320. strcpy(&merged[spec2len + 1], filespec1);
  321. }
  322. return (merged);
  323. }
  324. # ifdef OPENSSL_SYS_MACOSX
  325. # define DSO_ext ".dylib"
  326. # define DSO_extlen 6
  327. # else
  328. # define DSO_ext ".so"
  329. # define DSO_extlen 3
  330. # endif
  331. static char *dlfcn_name_converter(DSO *dso, const char *filename)
  332. {
  333. char *translated;
  334. int len, rsize, transform;
  335. len = strlen(filename);
  336. rsize = len + 1;
  337. transform = (strstr(filename, "/") == NULL);
  338. if (transform) {
  339. /* We will convert this to "%s.so" or "lib%s.so" etc */
  340. rsize += DSO_extlen; /* The length of ".so" */
  341. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  342. rsize += 3; /* The length of "lib" */
  343. }
  344. translated = OPENSSL_malloc(rsize);
  345. if (translated == NULL) {
  346. DSOerr(DSO_F_DLFCN_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
  347. return (NULL);
  348. }
  349. if (transform) {
  350. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  351. sprintf(translated, "lib%s" DSO_ext, filename);
  352. else
  353. sprintf(translated, "%s" DSO_ext, filename);
  354. } else
  355. sprintf(translated, "%s", filename);
  356. return (translated);
  357. }
  358. # ifdef __sgi
  359. /*-
  360. This is a quote from IRIX manual for dladdr(3c):
  361. <dlfcn.h> does not contain a prototype for dladdr or definition of
  362. Dl_info. The #include <dlfcn.h> in the SYNOPSIS line is traditional,
  363. but contains no dladdr prototype and no IRIX library contains an
  364. implementation. Write your own declaration based on the code below.
  365. The following code is dependent on internal interfaces that are not
  366. part of the IRIX compatibility guarantee; however, there is no future
  367. intention to change this interface, so on a practical level, the code
  368. below is safe to use on IRIX.
  369. */
  370. # include <rld_interface.h>
  371. # ifndef _RLD_INTERFACE_DLFCN_H_DLADDR
  372. # define _RLD_INTERFACE_DLFCN_H_DLADDR
  373. typedef struct Dl_info {
  374. const char *dli_fname;
  375. void *dli_fbase;
  376. const char *dli_sname;
  377. void *dli_saddr;
  378. int dli_version;
  379. int dli_reserved1;
  380. long dli_reserved[4];
  381. } Dl_info;
  382. # else
  383. typedef struct Dl_info Dl_info;
  384. # endif
  385. # define _RLD_DLADDR 14
  386. static int dladdr(void *address, Dl_info *dl)
  387. {
  388. void *v;
  389. v = _rld_new_interface(_RLD_DLADDR, address, dl);
  390. return (int)v;
  391. }
  392. # endif /* __sgi */
  393. static int dlfcn_pathbyaddr(void *addr, char *path, int sz)
  394. {
  395. # ifdef HAVE_DLINFO
  396. Dl_info dli;
  397. int len;
  398. if (addr == NULL) {
  399. union {
  400. int (*f) (void *, char *, int);
  401. void *p;
  402. } t = {
  403. dlfcn_pathbyaddr
  404. };
  405. addr = t.p;
  406. }
  407. if (dladdr(addr, &dli)) {
  408. len = (int)strlen(dli.dli_fname);
  409. if (sz <= 0)
  410. return len + 1;
  411. if (len >= sz)
  412. len = sz - 1;
  413. memcpy(path, dli.dli_fname, len);
  414. path[len++] = 0;
  415. return len;
  416. }
  417. ERR_add_error_data(2, "dlfcn_pathbyaddr(): ", dlerror());
  418. # endif
  419. return -1;
  420. }
  421. static void *dlfcn_globallookup(const char *name)
  422. {
  423. void *ret = NULL, *handle = dlopen(NULL, RTLD_LAZY);
  424. if (handle) {
  425. ret = dlsym(handle, name);
  426. dlclose(handle);
  427. }
  428. return ret;
  429. }
  430. #endif /* DSO_DLFCN */