dso_dlfcn.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. /*
  10. * We need to do this early, because stdio.h includes the header files that
  11. * handle _GNU_SOURCE and other similar macros. Defining it later is simply
  12. * too late, because those headers are protected from re- inclusion.
  13. */
  14. #ifndef _GNU_SOURCE
  15. # define _GNU_SOURCE /* make sure dladdr is declared */
  16. #endif
  17. #include "dso_locl.h"
  18. #ifdef DSO_DLFCN
  19. # ifdef HAVE_DLFCN_H
  20. # ifdef __osf__
  21. # define __EXTENSIONS__
  22. # endif
  23. # include <dlfcn.h>
  24. # define HAVE_DLINFO 1
  25. # if defined(_AIX) || defined(__CYGWIN__) || \
  26. defined(__SCO_VERSION__) || defined(_SCO_ELF) || \
  27. (defined(__osf__) && !defined(RTLD_NEXT)) || \
  28. (defined(__OpenBSD__) && !defined(RTLD_SELF)) || \
  29. defined(__ANDROID__)
  30. # undef HAVE_DLINFO
  31. # endif
  32. # endif
  33. /* Part of the hack in "dlfcn_load" ... */
  34. # define DSO_MAX_TRANSLATED_SIZE 256
  35. static int dlfcn_load(DSO *dso);
  36. static int dlfcn_unload(DSO *dso);
  37. static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname);
  38. static char *dlfcn_name_converter(DSO *dso, const char *filename);
  39. static char *dlfcn_merger(DSO *dso, const char *filespec1,
  40. const char *filespec2);
  41. static int dlfcn_pathbyaddr(void *addr, char *path, int sz);
  42. static void *dlfcn_globallookup(const char *name);
  43. static DSO_METHOD dso_meth_dlfcn = {
  44. "OpenSSL 'dlfcn' shared library method",
  45. dlfcn_load,
  46. dlfcn_unload,
  47. dlfcn_bind_func,
  48. NULL, /* ctrl */
  49. dlfcn_name_converter,
  50. dlfcn_merger,
  51. NULL, /* init */
  52. NULL, /* finish */
  53. dlfcn_pathbyaddr,
  54. dlfcn_globallookup
  55. };
  56. DSO_METHOD *DSO_METHOD_openssl(void)
  57. {
  58. return &dso_meth_dlfcn;
  59. }
  60. /*
  61. * Prior to using the dlopen() function, we should decide on the flag we
  62. * send. There's a few different ways of doing this and it's a messy
  63. * venn-diagram to match up which platforms support what. So as we don't have
  64. * autoconf yet, I'm implementing a hack that could be hacked further
  65. * relatively easily to deal with cases as we find them. Initially this is to
  66. * cope with OpenBSD.
  67. */
  68. # if defined(__OpenBSD__) || defined(__NetBSD__)
  69. # ifdef DL_LAZY
  70. # define DLOPEN_FLAG DL_LAZY
  71. # else
  72. # ifdef RTLD_NOW
  73. # define DLOPEN_FLAG RTLD_NOW
  74. # else
  75. # define DLOPEN_FLAG 0
  76. # endif
  77. # endif
  78. # else
  79. # define DLOPEN_FLAG RTLD_NOW /* Hope this works everywhere else */
  80. # endif
  81. /*
  82. * For this DSO_METHOD, our meth_data STACK will contain; (i) the handle
  83. * (void*) returned from dlopen().
  84. */
  85. static int dlfcn_load(DSO *dso)
  86. {
  87. void *ptr = NULL;
  88. /* See applicable comments in dso_dl.c */
  89. char *filename = DSO_convert_filename(dso, NULL);
  90. int flags = DLOPEN_FLAG;
  91. if (filename == NULL) {
  92. DSOerr(DSO_F_DLFCN_LOAD, DSO_R_NO_FILENAME);
  93. goto err;
  94. }
  95. # ifdef RTLD_GLOBAL
  96. if (dso->flags & DSO_FLAG_GLOBAL_SYMBOLS)
  97. flags |= RTLD_GLOBAL;
  98. # endif
  99. ptr = dlopen(filename, flags);
  100. if (ptr == NULL) {
  101. DSOerr(DSO_F_DLFCN_LOAD, DSO_R_LOAD_FAILED);
  102. ERR_add_error_data(4, "filename(", filename, "): ", dlerror());
  103. goto err;
  104. }
  105. if (!sk_void_push(dso->meth_data, (char *)ptr)) {
  106. DSOerr(DSO_F_DLFCN_LOAD, DSO_R_STACK_ERROR);
  107. goto err;
  108. }
  109. /* Success */
  110. dso->loaded_filename = filename;
  111. return (1);
  112. err:
  113. /* Cleanup! */
  114. OPENSSL_free(filename);
  115. if (ptr != NULL)
  116. dlclose(ptr);
  117. return (0);
  118. }
  119. static int dlfcn_unload(DSO *dso)
  120. {
  121. void *ptr;
  122. if (dso == NULL) {
  123. DSOerr(DSO_F_DLFCN_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
  124. return (0);
  125. }
  126. if (sk_void_num(dso->meth_data) < 1)
  127. return (1);
  128. ptr = sk_void_pop(dso->meth_data);
  129. if (ptr == NULL) {
  130. DSOerr(DSO_F_DLFCN_UNLOAD, DSO_R_NULL_HANDLE);
  131. /*
  132. * Should push the value back onto the stack in case of a retry.
  133. */
  134. sk_void_push(dso->meth_data, ptr);
  135. return (0);
  136. }
  137. /* For now I'm not aware of any errors associated with dlclose() */
  138. dlclose(ptr);
  139. return (1);
  140. }
  141. static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname)
  142. {
  143. void *ptr;
  144. union {
  145. DSO_FUNC_TYPE sym;
  146. void *dlret;
  147. } u;
  148. if ((dso == NULL) || (symname == NULL)) {
  149. DSOerr(DSO_F_DLFCN_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  150. return (NULL);
  151. }
  152. if (sk_void_num(dso->meth_data) < 1) {
  153. DSOerr(DSO_F_DLFCN_BIND_FUNC, DSO_R_STACK_ERROR);
  154. return (NULL);
  155. }
  156. ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
  157. if (ptr == NULL) {
  158. DSOerr(DSO_F_DLFCN_BIND_FUNC, DSO_R_NULL_HANDLE);
  159. return (NULL);
  160. }
  161. u.dlret = dlsym(ptr, symname);
  162. if (u.dlret == NULL) {
  163. DSOerr(DSO_F_DLFCN_BIND_FUNC, DSO_R_SYM_FAILURE);
  164. ERR_add_error_data(4, "symname(", symname, "): ", dlerror());
  165. return (NULL);
  166. }
  167. return u.sym;
  168. }
  169. static char *dlfcn_merger(DSO *dso, const char *filespec1,
  170. const char *filespec2)
  171. {
  172. char *merged;
  173. if (!filespec1 && !filespec2) {
  174. DSOerr(DSO_F_DLFCN_MERGER, ERR_R_PASSED_NULL_PARAMETER);
  175. return (NULL);
  176. }
  177. /*
  178. * If the first file specification is a rooted path, it rules. same goes
  179. * if the second file specification is missing.
  180. */
  181. if (!filespec2 || (filespec1 != NULL && filespec1[0] == '/')) {
  182. merged = OPENSSL_strdup(filespec1);
  183. if (merged == NULL) {
  184. DSOerr(DSO_F_DLFCN_MERGER, ERR_R_MALLOC_FAILURE);
  185. return (NULL);
  186. }
  187. }
  188. /*
  189. * If the first file specification is missing, the second one rules.
  190. */
  191. else if (!filespec1) {
  192. merged = OPENSSL_strdup(filespec2);
  193. if (merged == NULL) {
  194. DSOerr(DSO_F_DLFCN_MERGER, ERR_R_MALLOC_FAILURE);
  195. return (NULL);
  196. }
  197. } else {
  198. /*
  199. * This part isn't as trivial as it looks. It assumes that the
  200. * second file specification really is a directory, and makes no
  201. * checks whatsoever. Therefore, the result becomes the
  202. * concatenation of filespec2 followed by a slash followed by
  203. * filespec1.
  204. */
  205. int spec2len, len;
  206. spec2len = strlen(filespec2);
  207. len = spec2len + strlen(filespec1);
  208. if (spec2len && filespec2[spec2len - 1] == '/') {
  209. spec2len--;
  210. len--;
  211. }
  212. merged = OPENSSL_malloc(len + 2);
  213. if (merged == NULL) {
  214. DSOerr(DSO_F_DLFCN_MERGER, ERR_R_MALLOC_FAILURE);
  215. return (NULL);
  216. }
  217. strcpy(merged, filespec2);
  218. merged[spec2len] = '/';
  219. strcpy(&merged[spec2len + 1], filespec1);
  220. }
  221. return (merged);
  222. }
  223. static char *dlfcn_name_converter(DSO *dso, const char *filename)
  224. {
  225. char *translated;
  226. int len, rsize, transform;
  227. len = strlen(filename);
  228. rsize = len + 1;
  229. transform = (strstr(filename, "/") == NULL);
  230. if (transform) {
  231. /* We will convert this to "%s.so" or "lib%s.so" etc */
  232. rsize += strlen(DSO_EXTENSION); /* The length of ".so" */
  233. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  234. rsize += 3; /* The length of "lib" */
  235. }
  236. translated = OPENSSL_malloc(rsize);
  237. if (translated == NULL) {
  238. DSOerr(DSO_F_DLFCN_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
  239. return (NULL);
  240. }
  241. if (transform) {
  242. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  243. sprintf(translated, "lib%s" DSO_EXTENSION, filename);
  244. else
  245. sprintf(translated, "%s" DSO_EXTENSION, filename);
  246. } else
  247. sprintf(translated, "%s", filename);
  248. return (translated);
  249. }
  250. # ifdef __sgi
  251. /*-
  252. This is a quote from IRIX manual for dladdr(3c):
  253. <dlfcn.h> does not contain a prototype for dladdr or definition of
  254. Dl_info. The #include <dlfcn.h> in the SYNOPSIS line is traditional,
  255. but contains no dladdr prototype and no IRIX library contains an
  256. implementation. Write your own declaration based on the code below.
  257. The following code is dependent on internal interfaces that are not
  258. part of the IRIX compatibility guarantee; however, there is no future
  259. intention to change this interface, so on a practical level, the code
  260. below is safe to use on IRIX.
  261. */
  262. # include <rld_interface.h>
  263. # ifndef _RLD_INTERFACE_DLFCN_H_DLADDR
  264. # define _RLD_INTERFACE_DLFCN_H_DLADDR
  265. typedef struct Dl_info {
  266. const char *dli_fname;
  267. void *dli_fbase;
  268. const char *dli_sname;
  269. void *dli_saddr;
  270. int dli_version;
  271. int dli_reserved1;
  272. long dli_reserved[4];
  273. } Dl_info;
  274. # else
  275. typedef struct Dl_info Dl_info;
  276. # endif
  277. # define _RLD_DLADDR 14
  278. static int dladdr(void *address, Dl_info *dl)
  279. {
  280. void *v;
  281. v = _rld_new_interface(_RLD_DLADDR, address, dl);
  282. return (int)v;
  283. }
  284. # endif /* __sgi */
  285. static int dlfcn_pathbyaddr(void *addr, char *path, int sz)
  286. {
  287. # ifdef HAVE_DLINFO
  288. Dl_info dli;
  289. int len;
  290. if (addr == NULL) {
  291. union {
  292. int (*f) (void *, char *, int);
  293. void *p;
  294. } t = {
  295. dlfcn_pathbyaddr
  296. };
  297. addr = t.p;
  298. }
  299. if (dladdr(addr, &dli)) {
  300. len = (int)strlen(dli.dli_fname);
  301. if (sz <= 0)
  302. return len + 1;
  303. if (len >= sz)
  304. len = sz - 1;
  305. memcpy(path, dli.dli_fname, len);
  306. path[len++] = 0;
  307. return len;
  308. }
  309. ERR_add_error_data(2, "dlfcn_pathbyaddr(): ", dlerror());
  310. # endif
  311. return -1;
  312. }
  313. static void *dlfcn_globallookup(const char *name)
  314. {
  315. void *ret = NULL, *handle = dlopen(NULL, RTLD_LAZY);
  316. if (handle) {
  317. ret = dlsym(handle, name);
  318. dlclose(handle);
  319. }
  320. return ret;
  321. }
  322. #endif /* DSO_DLFCN */