2
0

dso_dlfcn.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /* dso_dlfcn.c -*- mode:C; c-file-style: "eay" -*- */
  2. /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
  3. * project 2000.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. /* We need to do this early, because stdio.h includes the header files
  59. that handle _GNU_SOURCE and other similar macros. Defining it later
  60. is simply too late, because those headers are protected from re-
  61. inclusion. */
  62. #ifdef __linux
  63. # ifndef _GNU_SOURCE
  64. # define _GNU_SOURCE /* make sure dladdr is declared */
  65. # endif
  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. /* Prior to using the dlopen() function, we should decide on the flag
  131. * we send. There's a few different ways of doing this and it's a
  132. * messy venn-diagram to match up which platforms support what. So
  133. * as we don't have autoconf yet, I'm implementing a hack that could
  134. * be hacked further relatively easily to deal with cases as we find
  135. * them. Initially this is to cope with OpenBSD. */
  136. #if defined(__OpenBSD__) || defined(__NetBSD__)
  137. # ifdef DL_LAZY
  138. # define DLOPEN_FLAG DL_LAZY
  139. # else
  140. # ifdef RTLD_NOW
  141. # define DLOPEN_FLAG RTLD_NOW
  142. # else
  143. # define DLOPEN_FLAG 0
  144. # endif
  145. # endif
  146. #else
  147. # ifdef OPENSSL_SYS_SUNOS
  148. # define DLOPEN_FLAG 1
  149. # else
  150. # define DLOPEN_FLAG RTLD_NOW /* Hope this works everywhere else */
  151. # endif
  152. #endif
  153. /* For this DSO_METHOD, our meth_data STACK will contain;
  154. * (i) the handle (void*) returned from dlopen().
  155. */
  156. static int dlfcn_load(DSO *dso)
  157. {
  158. void *ptr = NULL;
  159. /* See applicable comments in dso_dl.c */
  160. char *filename = DSO_convert_filename(dso, NULL);
  161. int flags = DLOPEN_FLAG;
  162. if(filename == NULL)
  163. {
  164. DSOerr(DSO_F_DLFCN_LOAD,DSO_R_NO_FILENAME);
  165. goto err;
  166. }
  167. #ifdef RTLD_GLOBAL
  168. if (dso->flags & DSO_FLAG_GLOBAL_SYMBOLS)
  169. flags |= RTLD_GLOBAL;
  170. #endif
  171. ptr = dlopen(filename, flags);
  172. if(ptr == NULL)
  173. {
  174. DSOerr(DSO_F_DLFCN_LOAD,DSO_R_LOAD_FAILED);
  175. ERR_add_error_data(4, "filename(", filename, "): ", dlerror());
  176. goto err;
  177. }
  178. if(!sk_void_push(dso->meth_data, (char *)ptr))
  179. {
  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. {
  199. DSOerr(DSO_F_DLFCN_UNLOAD,ERR_R_PASSED_NULL_PARAMETER);
  200. return(0);
  201. }
  202. if(sk_void_num(dso->meth_data) < 1)
  203. return(1);
  204. ptr = sk_void_pop(dso->meth_data);
  205. if(ptr == NULL)
  206. {
  207. DSOerr(DSO_F_DLFCN_UNLOAD,DSO_R_NULL_HANDLE);
  208. /* Should push the value back onto the stack in
  209. * case of a retry. */
  210. sk_void_push(dso->meth_data, ptr);
  211. return(0);
  212. }
  213. /* For now I'm not aware of any errors associated with dlclose() */
  214. dlclose(ptr);
  215. return(1);
  216. }
  217. static void *dlfcn_bind_var(DSO *dso, const char *symname)
  218. {
  219. void *ptr, *sym;
  220. if((dso == NULL) || (symname == NULL))
  221. {
  222. DSOerr(DSO_F_DLFCN_BIND_VAR,ERR_R_PASSED_NULL_PARAMETER);
  223. return(NULL);
  224. }
  225. if(sk_void_num(dso->meth_data) < 1)
  226. {
  227. DSOerr(DSO_F_DLFCN_BIND_VAR,DSO_R_STACK_ERROR);
  228. return(NULL);
  229. }
  230. ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
  231. if(ptr == NULL)
  232. {
  233. DSOerr(DSO_F_DLFCN_BIND_VAR,DSO_R_NULL_HANDLE);
  234. return(NULL);
  235. }
  236. sym = dlsym(ptr, symname);
  237. if(sym == NULL)
  238. {
  239. DSOerr(DSO_F_DLFCN_BIND_VAR,DSO_R_SYM_FAILURE);
  240. ERR_add_error_data(4, "symname(", symname, "): ", dlerror());
  241. return(NULL);
  242. }
  243. return(sym);
  244. }
  245. static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname)
  246. {
  247. void *ptr;
  248. union {
  249. DSO_FUNC_TYPE sym;
  250. void *dlret;
  251. } u;
  252. if((dso == NULL) || (symname == NULL))
  253. {
  254. DSOerr(DSO_F_DLFCN_BIND_FUNC,ERR_R_PASSED_NULL_PARAMETER);
  255. return(NULL);
  256. }
  257. if(sk_void_num(dso->meth_data) < 1)
  258. {
  259. DSOerr(DSO_F_DLFCN_BIND_FUNC,DSO_R_STACK_ERROR);
  260. return(NULL);
  261. }
  262. ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
  263. if(ptr == NULL)
  264. {
  265. DSOerr(DSO_F_DLFCN_BIND_FUNC,DSO_R_NULL_HANDLE);
  266. return(NULL);
  267. }
  268. u.dlret = dlsym(ptr, symname);
  269. if(u.dlret == NULL)
  270. {
  271. DSOerr(DSO_F_DLFCN_BIND_FUNC,DSO_R_SYM_FAILURE);
  272. ERR_add_error_data(4, "symname(", symname, "): ", dlerror());
  273. return(NULL);
  274. }
  275. return u.sym;
  276. }
  277. static char *dlfcn_merger(DSO *dso, const char *filespec1,
  278. const char *filespec2)
  279. {
  280. char *merged;
  281. if(!filespec1 && !filespec2)
  282. {
  283. DSOerr(DSO_F_DLFCN_MERGER,
  284. ERR_R_PASSED_NULL_PARAMETER);
  285. return(NULL);
  286. }
  287. /* If the first file specification is a rooted path, it rules.
  288. same goes if the second file specification is missing. */
  289. if (!filespec2 || (filespec1 != NULL && filespec1[0] == '/'))
  290. {
  291. merged = OPENSSL_malloc(strlen(filespec1) + 1);
  292. if(!merged)
  293. {
  294. DSOerr(DSO_F_DLFCN_MERGER, ERR_R_MALLOC_FAILURE);
  295. return(NULL);
  296. }
  297. strcpy(merged, filespec1);
  298. }
  299. /* If the first file specification is missing, the second one rules. */
  300. else if (!filespec1)
  301. {
  302. merged = OPENSSL_malloc(strlen(filespec2) + 1);
  303. if(!merged)
  304. {
  305. DSOerr(DSO_F_DLFCN_MERGER,
  306. ERR_R_MALLOC_FAILURE);
  307. return(NULL);
  308. }
  309. strcpy(merged, filespec2);
  310. }
  311. else
  312. /* This part isn't as trivial as it looks. It assumes that
  313. the second file specification really is a directory, and
  314. makes no checks whatsoever. Therefore, the result becomes
  315. the concatenation of filespec2 followed by a slash followed
  316. by filespec1. */
  317. {
  318. int spec2len, len;
  319. spec2len = strlen(filespec2);
  320. len = spec2len + (filespec1 ? strlen(filespec1) : 0);
  321. if(filespec2 && filespec2[spec2len - 1] == '/')
  322. {
  323. spec2len--;
  324. len--;
  325. }
  326. merged = OPENSSL_malloc(len + 2);
  327. if(!merged)
  328. {
  329. DSOerr(DSO_F_DLFCN_MERGER,
  330. ERR_R_MALLOC_FAILURE);
  331. return(NULL);
  332. }
  333. strcpy(merged, filespec2);
  334. merged[spec2len] = '/';
  335. strcpy(&merged[spec2len + 1], filespec1);
  336. }
  337. return(merged);
  338. }
  339. #ifdef OPENSSL_SYS_MACOSX
  340. #define DSO_ext ".dylib"
  341. #define DSO_extlen 6
  342. #else
  343. #define DSO_ext ".so"
  344. #define DSO_extlen 3
  345. #endif
  346. static char *dlfcn_name_converter(DSO *dso, const char *filename)
  347. {
  348. char *translated;
  349. int len, rsize, transform;
  350. len = strlen(filename);
  351. rsize = len + 1;
  352. transform = (strstr(filename, "/") == NULL);
  353. if(transform)
  354. {
  355. /* We will convert this to "%s.so" or "lib%s.so" etc */
  356. rsize += DSO_extlen; /* The length of ".so" */
  357. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  358. rsize += 3; /* The length of "lib" */
  359. }
  360. translated = OPENSSL_malloc(rsize);
  361. if(translated == NULL)
  362. {
  363. DSOerr(DSO_F_DLFCN_NAME_CONVERTER,
  364. DSO_R_NAME_TRANSLATION_FAILED);
  365. return(NULL);
  366. }
  367. if(transform)
  368. {
  369. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  370. sprintf(translated, "lib%s" DSO_ext, filename);
  371. else
  372. sprintf(translated, "%s" DSO_ext, filename);
  373. }
  374. else
  375. sprintf(translated, "%s", filename);
  376. return(translated);
  377. }
  378. #ifdef __sgi
  379. /*
  380. This is a quote from IRIX manual for dladdr(3c):
  381. <dlfcn.h> does not contain a prototype for dladdr or definition of
  382. Dl_info. The #include <dlfcn.h> in the SYNOPSIS line is traditional,
  383. but contains no dladdr prototype and no IRIX library contains an
  384. implementation. Write your own declaration based on the code below.
  385. The following code is dependent on internal interfaces that are not
  386. part of the IRIX compatibility guarantee; however, there is no future
  387. intention to change this interface, so on a practical level, the code
  388. below is safe to use on IRIX.
  389. */
  390. #include <rld_interface.h>
  391. #ifndef _RLD_INTERFACE_DLFCN_H_DLADDR
  392. #define _RLD_INTERFACE_DLFCN_H_DLADDR
  393. typedef struct Dl_info {
  394. const char * dli_fname;
  395. void * dli_fbase;
  396. const char * dli_sname;
  397. void * dli_saddr;
  398. int dli_version;
  399. int dli_reserved1;
  400. long dli_reserved[4];
  401. } Dl_info;
  402. #else
  403. typedef struct Dl_info Dl_info;
  404. #endif
  405. #define _RLD_DLADDR 14
  406. static int dladdr(void *address, Dl_info *dl)
  407. {
  408. void *v;
  409. v = _rld_new_interface(_RLD_DLADDR,address,dl);
  410. return (int)v;
  411. }
  412. #endif /* __sgi */
  413. static int dlfcn_pathbyaddr(void *addr,char *path,int sz)
  414. {
  415. #ifdef HAVE_DLINFO
  416. Dl_info dli;
  417. int len;
  418. if (addr == NULL)
  419. {
  420. union { int(*f)(void*,char*,int); void *p; } t =
  421. { dlfcn_pathbyaddr };
  422. addr = t.p;
  423. }
  424. if (dladdr(addr,&dli))
  425. {
  426. len = (int)strlen(dli.dli_fname);
  427. if (sz <= 0) return len+1;
  428. if (len >= sz) len=sz-1;
  429. memcpy(path,dli.dli_fname,len);
  430. path[len++]=0;
  431. return len;
  432. }
  433. ERR_add_error_data(4, "dlfcn_pathbyaddr(): ", dlerror());
  434. #endif
  435. return -1;
  436. }
  437. static void *dlfcn_globallookup(const char *name)
  438. {
  439. void *ret = NULL,*handle = dlopen(NULL,RTLD_LAZY);
  440. if (handle)
  441. {
  442. ret = dlsym(handle,name);
  443. dlclose(handle);
  444. }
  445. return ret;
  446. }
  447. #endif /* DSO_DLFCN */