dso_dlfcn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Copyright 2000-2018 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(__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. # ifdef _AIX
  286. /*-
  287. * See IBM's AIX Version 7.2, Technical Reference:
  288. * Base Operating System and Extensions, Volume 1 and 2
  289. * https://www.ibm.com/support/knowledgecenter/ssw_aix_72/com.ibm.aix.base/technicalreferences.htm
  290. */
  291. # include <sys/ldr.h>
  292. # include <errno.h>
  293. /* ~ 64 * (sizeof(struct ld_info) + _XOPEN_PATH_MAX + _XOPEN_NAME_MAX) */
  294. # define DLFCN_LDINFO_SIZE 86976
  295. typedef struct Dl_info {
  296. const char *dli_fname;
  297. } Dl_info;
  298. /*
  299. * This dladdr()-implementation will also find the ptrgl (Pointer Glue) virtual
  300. * address of a function, which is just located in the DATA segment instead of
  301. * the TEXT segment.
  302. */
  303. static int dladdr(void *ptr, Dl_info *dl)
  304. {
  305. uintptr_t addr = (uintptr_t)ptr;
  306. unsigned int found = 0;
  307. struct ld_info *ldinfos, *next_ldi, *this_ldi;
  308. if ((ldinfos = (struct ld_info *)OPENSSL_malloc(DLFCN_LDINFO_SIZE)) == NULL) {
  309. errno = ENOMEM;
  310. dl->dli_fname = NULL;
  311. return 0;
  312. }
  313. if ((loadquery(L_GETINFO, (void *)ldinfos, DLFCN_LDINFO_SIZE)) < 0) {
  314. /*-
  315. * Error handling is done through errno and dlerror() reading errno:
  316. * ENOMEM (ldinfos buffer is too small),
  317. * EINVAL (invalid flags),
  318. * EFAULT (invalid ldinfos ptr)
  319. */
  320. OPENSSL_free((void *)ldinfos);
  321. dl->dli_fname = NULL;
  322. return 0;
  323. }
  324. next_ldi = ldinfos;
  325. do {
  326. this_ldi = next_ldi;
  327. if (((addr >= (uintptr_t)this_ldi->ldinfo_textorg)
  328. && (addr < ((uintptr_t)this_ldi->ldinfo_textorg +
  329. this_ldi->ldinfo_textsize)))
  330. || ((addr >= (uintptr_t)this_ldi->ldinfo_dataorg)
  331. && (addr < ((uintptr_t)this_ldi->ldinfo_dataorg +
  332. this_ldi->ldinfo_datasize)))) {
  333. found = 1;
  334. /*
  335. * Ignoring the possibility of a member name and just returning
  336. * the path name. See docs: sys/ldr.h, loadquery() and
  337. * dlopen()/RTLD_MEMBER.
  338. */
  339. if ((dl->dli_fname =
  340. OPENSSL_strdup(this_ldi->ldinfo_filename)) == NULL)
  341. errno = ENOMEM;
  342. } else {
  343. next_ldi =
  344. (struct ld_info *)((uintptr_t)this_ldi + this_ldi->ldinfo_next);
  345. }
  346. } while (this_ldi->ldinfo_next && !found);
  347. OPENSSL_free((void *)ldinfos);
  348. return (found && dl->dli_fname != NULL);
  349. }
  350. # endif /* _AIX */
  351. static int dlfcn_pathbyaddr(void *addr, char *path, int sz)
  352. {
  353. # ifdef HAVE_DLINFO
  354. Dl_info dli;
  355. int len;
  356. if (addr == NULL) {
  357. union {
  358. int (*f) (void *, char *, int);
  359. void *p;
  360. } t = {
  361. dlfcn_pathbyaddr
  362. };
  363. addr = t.p;
  364. }
  365. if (dladdr(addr, &dli)) {
  366. len = (int)strlen(dli.dli_fname);
  367. if (sz <= 0) {
  368. # ifdef _AIX
  369. OPENSSL_free((void *)dli.dli_fname);
  370. # endif
  371. return len + 1;
  372. }
  373. if (len >= sz)
  374. len = sz - 1;
  375. memcpy(path, dli.dli_fname, len);
  376. path[len++] = 0;
  377. # ifdef _AIX
  378. OPENSSL_free((void *)dli.dli_fname);
  379. # endif
  380. return len;
  381. }
  382. ERR_add_error_data(2, "dlfcn_pathbyaddr(): ", dlerror());
  383. # endif
  384. return -1;
  385. }
  386. static void *dlfcn_globallookup(const char *name)
  387. {
  388. void *ret = NULL, *handle = dlopen(NULL, RTLD_LAZY);
  389. if (handle) {
  390. ret = dlsym(handle, name);
  391. dlclose(handle);
  392. }
  393. return ret;
  394. }
  395. #endif /* DSO_DLFCN */