dso_dlfcn.c 12 KB

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