dso_dlfcn.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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_local.h"
  18. #include "internal/e_os.h"
  19. #ifdef DSO_DLFCN
  20. # ifdef HAVE_DLFCN_H
  21. # ifdef __osf__
  22. # define __EXTENSIONS__
  23. # endif
  24. # include <dlfcn.h>
  25. # define HAVE_DLINFO 1
  26. # if defined(__SCO_VERSION__) || defined(_SCO_ELF) || \
  27. (defined(__osf__) && !defined(RTLD_NEXT)) || \
  28. (defined(__OpenBSD__) && !defined(RTLD_SELF)) || \
  29. defined(__ANDROID__) || defined(__TANDEM)
  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. int saveerrno = get_last_sys_error();
  92. if (filename == NULL) {
  93. ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
  94. goto err;
  95. }
  96. # ifdef RTLD_GLOBAL
  97. if (dso->flags & DSO_FLAG_GLOBAL_SYMBOLS)
  98. flags |= RTLD_GLOBAL;
  99. # endif
  100. # ifdef _AIX
  101. if (filename[strlen(filename) - 1] == ')')
  102. flags |= RTLD_MEMBER;
  103. # endif
  104. ptr = dlopen(filename, flags);
  105. if (ptr == NULL) {
  106. ERR_raise_data(ERR_LIB_DSO, DSO_R_LOAD_FAILED,
  107. "filename(%s): %s", filename, dlerror());
  108. goto err;
  109. }
  110. /*
  111. * Some dlopen() implementations (e.g. solaris) do no preserve errno, even
  112. * on a successful call.
  113. */
  114. set_sys_error(saveerrno);
  115. if (!sk_void_push(dso->meth_data, (char *)ptr)) {
  116. ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
  117. goto err;
  118. }
  119. /* Success */
  120. dso->loaded_filename = filename;
  121. return 1;
  122. err:
  123. /* Cleanup! */
  124. OPENSSL_free(filename);
  125. if (ptr != NULL)
  126. dlclose(ptr);
  127. return 0;
  128. }
  129. static int dlfcn_unload(DSO *dso)
  130. {
  131. void *ptr;
  132. if (dso == NULL) {
  133. ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
  134. return 0;
  135. }
  136. if (sk_void_num(dso->meth_data) < 1)
  137. return 1;
  138. ptr = sk_void_pop(dso->meth_data);
  139. if (ptr == NULL) {
  140. ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
  141. /*
  142. * Should push the value back onto the stack in case of a retry.
  143. */
  144. sk_void_push(dso->meth_data, ptr);
  145. return 0;
  146. }
  147. /* For now I'm not aware of any errors associated with dlclose() */
  148. dlclose(ptr);
  149. return 1;
  150. }
  151. static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname)
  152. {
  153. void *ptr;
  154. union {
  155. DSO_FUNC_TYPE sym;
  156. void *dlret;
  157. } u;
  158. if ((dso == NULL) || (symname == NULL)) {
  159. ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
  160. return NULL;
  161. }
  162. if (sk_void_num(dso->meth_data) < 1) {
  163. ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
  164. return NULL;
  165. }
  166. ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
  167. if (ptr == NULL) {
  168. ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
  169. return NULL;
  170. }
  171. u.dlret = dlsym(ptr, symname);
  172. if (u.dlret == NULL) {
  173. ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE,
  174. "symname(%s): %s", symname, dlerror());
  175. return NULL;
  176. }
  177. return u.sym;
  178. }
  179. static char *dlfcn_merger(DSO *dso, const char *filespec1,
  180. const char *filespec2)
  181. {
  182. char *merged;
  183. if (!filespec1 && !filespec2) {
  184. ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
  185. return NULL;
  186. }
  187. /*
  188. * If the first file specification is a rooted path, it rules. same goes
  189. * if the second file specification is missing.
  190. */
  191. if (!filespec2 || (filespec1 != NULL && filespec1[0] == '/')) {
  192. merged = OPENSSL_strdup(filespec1);
  193. if (merged == NULL) {
  194. ERR_raise(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE);
  195. return NULL;
  196. }
  197. }
  198. /*
  199. * If the first file specification is missing, the second one rules.
  200. */
  201. else if (!filespec1) {
  202. merged = OPENSSL_strdup(filespec2);
  203. if (merged == NULL) {
  204. ERR_raise(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE);
  205. return NULL;
  206. }
  207. } else {
  208. /*
  209. * This part isn't as trivial as it looks. It assumes that the
  210. * second file specification really is a directory, and makes no
  211. * checks whatsoever. Therefore, the result becomes the
  212. * concatenation of filespec2 followed by a slash followed by
  213. * filespec1.
  214. */
  215. int spec2len, len;
  216. spec2len = strlen(filespec2);
  217. len = spec2len + strlen(filespec1);
  218. if (spec2len && filespec2[spec2len - 1] == '/') {
  219. spec2len--;
  220. len--;
  221. }
  222. merged = OPENSSL_malloc(len + 2);
  223. if (merged == NULL) {
  224. ERR_raise(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE);
  225. return NULL;
  226. }
  227. strcpy(merged, filespec2);
  228. merged[spec2len] = '/';
  229. strcpy(&merged[spec2len + 1], filespec1);
  230. }
  231. return merged;
  232. }
  233. static char *dlfcn_name_converter(DSO *dso, const char *filename)
  234. {
  235. char *translated;
  236. int len, rsize, transform;
  237. len = strlen(filename);
  238. rsize = len + 1;
  239. transform = (strstr(filename, "/") == NULL);
  240. if (transform) {
  241. /* We will convert this to "%s.so" or "lib%s.so" etc */
  242. rsize += strlen(DSO_EXTENSION); /* The length of ".so" */
  243. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  244. rsize += 3; /* The length of "lib" */
  245. }
  246. translated = OPENSSL_malloc(rsize);
  247. if (translated == NULL) {
  248. ERR_raise(ERR_LIB_DSO, DSO_R_NAME_TRANSLATION_FAILED);
  249. return NULL;
  250. }
  251. if (transform) {
  252. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  253. sprintf(translated, "lib%s" DSO_EXTENSION, filename);
  254. else
  255. sprintf(translated, "%s" DSO_EXTENSION, filename);
  256. } else
  257. sprintf(translated, "%s", filename);
  258. return translated;
  259. }
  260. # ifdef __sgi
  261. /*-
  262. This is a quote from IRIX manual for dladdr(3c):
  263. <dlfcn.h> does not contain a prototype for dladdr or definition of
  264. Dl_info. The #include <dlfcn.h> in the SYNOPSIS line is traditional,
  265. but contains no dladdr prototype and no IRIX library contains an
  266. implementation. Write your own declaration based on the code below.
  267. The following code is dependent on internal interfaces that are not
  268. part of the IRIX compatibility guarantee; however, there is no future
  269. intention to change this interface, so on a practical level, the code
  270. below is safe to use on IRIX.
  271. */
  272. # include <rld_interface.h>
  273. # ifndef _RLD_INTERFACE_DLFCN_H_DLADDR
  274. # define _RLD_INTERFACE_DLFCN_H_DLADDR
  275. typedef struct Dl_info {
  276. const char *dli_fname;
  277. void *dli_fbase;
  278. const char *dli_sname;
  279. void *dli_saddr;
  280. int dli_version;
  281. int dli_reserved1;
  282. long dli_reserved[4];
  283. } Dl_info;
  284. # else
  285. typedef struct Dl_info Dl_info;
  286. # endif
  287. # define _RLD_DLADDR 14
  288. static int dladdr(void *address, Dl_info *dl)
  289. {
  290. void *v;
  291. v = _rld_new_interface(_RLD_DLADDR, address, dl);
  292. return (int)v;
  293. }
  294. # endif /* __sgi */
  295. # ifdef _AIX
  296. /*-
  297. * See IBM's AIX Version 7.2, Technical Reference:
  298. * Base Operating System and Extensions, Volume 1 and 2
  299. * https://www.ibm.com/support/knowledgecenter/ssw_aix_72/com.ibm.aix.base/technicalreferences.htm
  300. */
  301. # include <sys/ldr.h>
  302. # include <errno.h>
  303. /* ~ 64 * (sizeof(struct ld_info) + _XOPEN_PATH_MAX + _XOPEN_NAME_MAX) */
  304. # define DLFCN_LDINFO_SIZE 86976
  305. typedef struct Dl_info {
  306. const char *dli_fname;
  307. } Dl_info;
  308. /*
  309. * This dladdr()-implementation will also find the ptrgl (Pointer Glue) virtual
  310. * address of a function, which is just located in the DATA segment instead of
  311. * the TEXT segment.
  312. */
  313. static int dladdr(void *ptr, Dl_info *dl)
  314. {
  315. uintptr_t addr = (uintptr_t)ptr;
  316. unsigned int found = 0;
  317. struct ld_info *ldinfos, *next_ldi, *this_ldi;
  318. if ((ldinfos = OPENSSL_malloc(DLFCN_LDINFO_SIZE)) == NULL) {
  319. errno = ENOMEM;
  320. dl->dli_fname = NULL;
  321. return 0;
  322. }
  323. if ((loadquery(L_GETINFO, (void *)ldinfos, DLFCN_LDINFO_SIZE)) < 0) {
  324. /*-
  325. * Error handling is done through errno and dlerror() reading errno:
  326. * ENOMEM (ldinfos buffer is too small),
  327. * EINVAL (invalid flags),
  328. * EFAULT (invalid ldinfos ptr)
  329. */
  330. OPENSSL_free((void *)ldinfos);
  331. dl->dli_fname = NULL;
  332. return 0;
  333. }
  334. next_ldi = ldinfos;
  335. do {
  336. this_ldi = next_ldi;
  337. if (((addr >= (uintptr_t)this_ldi->ldinfo_textorg)
  338. && (addr < ((uintptr_t)this_ldi->ldinfo_textorg +
  339. this_ldi->ldinfo_textsize)))
  340. || ((addr >= (uintptr_t)this_ldi->ldinfo_dataorg)
  341. && (addr < ((uintptr_t)this_ldi->ldinfo_dataorg +
  342. this_ldi->ldinfo_datasize)))) {
  343. char *buffer, *member;
  344. size_t buffer_sz, member_len;
  345. buffer_sz = strlen(this_ldi->ldinfo_filename) + 1;
  346. member = this_ldi->ldinfo_filename + buffer_sz;
  347. if ((member_len = strlen(member)) > 0)
  348. buffer_sz += 1 + member_len + 1;
  349. found = 1;
  350. if ((buffer = OPENSSL_malloc(buffer_sz)) != NULL) {
  351. OPENSSL_strlcpy(buffer, this_ldi->ldinfo_filename, buffer_sz);
  352. if (member_len > 0) {
  353. /*
  354. * Need to respect a possible member name and not just
  355. * returning the path name in this case. See docs:
  356. * sys/ldr.h, loadquery() and dlopen()/RTLD_MEMBER.
  357. */
  358. OPENSSL_strlcat(buffer, "(", buffer_sz);
  359. OPENSSL_strlcat(buffer, member, buffer_sz);
  360. OPENSSL_strlcat(buffer, ")", buffer_sz);
  361. }
  362. dl->dli_fname = buffer;
  363. } else {
  364. errno = ENOMEM;
  365. }
  366. } else {
  367. next_ldi = (struct ld_info *)((uintptr_t)this_ldi +
  368. this_ldi->ldinfo_next);
  369. }
  370. } while (this_ldi->ldinfo_next && !found);
  371. OPENSSL_free((void *)ldinfos);
  372. return (found && dl->dli_fname != NULL);
  373. }
  374. # endif /* _AIX */
  375. static int dlfcn_pathbyaddr(void *addr, char *path, int sz)
  376. {
  377. # ifdef HAVE_DLINFO
  378. Dl_info dli;
  379. int len;
  380. if (addr == NULL) {
  381. union {
  382. int (*f) (void *, char *, int);
  383. void *p;
  384. } t = {
  385. dlfcn_pathbyaddr
  386. };
  387. addr = t.p;
  388. }
  389. if (dladdr(addr, &dli)) {
  390. len = (int)strlen(dli.dli_fname);
  391. if (sz <= 0) {
  392. # ifdef _AIX
  393. OPENSSL_free((void *)dli.dli_fname);
  394. # endif
  395. return len + 1;
  396. }
  397. if (len >= sz)
  398. len = sz - 1;
  399. memcpy(path, dli.dli_fname, len);
  400. path[len++] = 0;
  401. # ifdef _AIX
  402. OPENSSL_free((void *)dli.dli_fname);
  403. # endif
  404. return len;
  405. }
  406. ERR_add_error_data(2, "dlfcn_pathbyaddr(): ", dlerror());
  407. # endif
  408. return -1;
  409. }
  410. static void *dlfcn_globallookup(const char *name)
  411. {
  412. void *ret = NULL, *handle = dlopen(NULL, RTLD_LAZY);
  413. if (handle) {
  414. ret = dlsym(handle, name);
  415. dlclose(handle);
  416. }
  417. return ret;
  418. }
  419. #endif /* DSO_DLFCN */