2
0

dso_dlfcn.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright 2000-2024 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. return NULL;
  195. }
  196. /*
  197. * If the first file specification is missing, the second one rules.
  198. */
  199. else if (!filespec1) {
  200. merged = OPENSSL_strdup(filespec2);
  201. if (merged == NULL)
  202. return NULL;
  203. } else {
  204. /*
  205. * This part isn't as trivial as it looks. It assumes that the
  206. * second file specification really is a directory, and makes no
  207. * checks whatsoever. Therefore, the result becomes the
  208. * concatenation of filespec2 followed by a slash followed by
  209. * filespec1.
  210. */
  211. int spec2len, len;
  212. spec2len = strlen(filespec2);
  213. len = spec2len + strlen(filespec1);
  214. if (spec2len && filespec2[spec2len - 1] == '/') {
  215. spec2len--;
  216. len--;
  217. }
  218. merged = OPENSSL_malloc(len + 2);
  219. if (merged == NULL)
  220. return NULL;
  221. strcpy(merged, filespec2);
  222. merged[spec2len] = '/';
  223. strcpy(&merged[spec2len + 1], filespec1);
  224. }
  225. return merged;
  226. }
  227. static char *dlfcn_name_converter(DSO *dso, const char *filename)
  228. {
  229. char *translated;
  230. int len, rsize, transform;
  231. len = strlen(filename);
  232. rsize = len + 1;
  233. transform = (strchr(filename, '/') == NULL);
  234. if (transform) {
  235. /* We will convert this to "%s.so" or "lib%s.so" etc */
  236. rsize += strlen(DSO_EXTENSION); /* The length of ".so" */
  237. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  238. rsize += 3; /* The length of "lib" */
  239. }
  240. translated = OPENSSL_malloc(rsize);
  241. if (translated == NULL) {
  242. ERR_raise(ERR_LIB_DSO, DSO_R_NAME_TRANSLATION_FAILED);
  243. return NULL;
  244. }
  245. if (transform) {
  246. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  247. sprintf(translated, "lib%s" DSO_EXTENSION, filename);
  248. else
  249. sprintf(translated, "%s" DSO_EXTENSION, filename);
  250. } else
  251. sprintf(translated, "%s", filename);
  252. return translated;
  253. }
  254. # ifdef __sgi
  255. /*-
  256. This is a quote from IRIX manual for dladdr(3c):
  257. <dlfcn.h> does not contain a prototype for dladdr or definition of
  258. Dl_info. The #include <dlfcn.h> in the SYNOPSIS line is traditional,
  259. but contains no dladdr prototype and no IRIX library contains an
  260. implementation. Write your own declaration based on the code below.
  261. The following code is dependent on internal interfaces that are not
  262. part of the IRIX compatibility guarantee; however, there is no future
  263. intention to change this interface, so on a practical level, the code
  264. below is safe to use on IRIX.
  265. */
  266. # include <rld_interface.h>
  267. # ifndef _RLD_INTERFACE_DLFCN_H_DLADDR
  268. # define _RLD_INTERFACE_DLFCN_H_DLADDR
  269. typedef struct Dl_info {
  270. const char *dli_fname;
  271. void *dli_fbase;
  272. const char *dli_sname;
  273. void *dli_saddr;
  274. int dli_version;
  275. int dli_reserved1;
  276. long dli_reserved[4];
  277. } Dl_info;
  278. # else
  279. typedef struct Dl_info Dl_info;
  280. # endif
  281. # define _RLD_DLADDR 14
  282. static int dladdr(void *address, Dl_info *dl)
  283. {
  284. void *v;
  285. v = _rld_new_interface(_RLD_DLADDR, address, dl);
  286. return (int)v;
  287. }
  288. # endif /* __sgi */
  289. # ifdef _AIX
  290. /*-
  291. * See IBM's AIX Version 7.2, Technical Reference:
  292. * Base Operating System and Extensions, Volume 1 and 2
  293. * https://www.ibm.com/support/knowledgecenter/ssw_aix_72/com.ibm.aix.base/technicalreferences.htm
  294. */
  295. # include <sys/ldr.h>
  296. # include <errno.h>
  297. /* ~ 64 * (sizeof(struct ld_info) + _XOPEN_PATH_MAX + _XOPEN_NAME_MAX) */
  298. # define DLFCN_LDINFO_SIZE 86976
  299. typedef struct Dl_info {
  300. const char *dli_fname;
  301. } Dl_info;
  302. /*
  303. * This dladdr()-implementation will also find the ptrgl (Pointer Glue) virtual
  304. * address of a function, which is just located in the DATA segment instead of
  305. * the TEXT segment.
  306. */
  307. static int dladdr(void *ptr, Dl_info *dl)
  308. {
  309. uintptr_t addr = (uintptr_t)ptr;
  310. unsigned int found = 0;
  311. struct ld_info *ldinfos, *next_ldi, *this_ldi;
  312. if ((ldinfos = OPENSSL_malloc(DLFCN_LDINFO_SIZE)) == NULL) {
  313. errno = ENOMEM;
  314. dl->dli_fname = NULL;
  315. return 0;
  316. }
  317. if ((loadquery(L_GETINFO, (void *)ldinfos, DLFCN_LDINFO_SIZE)) < 0) {
  318. /*-
  319. * Error handling is done through errno and dlerror() reading errno:
  320. * ENOMEM (ldinfos buffer is too small),
  321. * EINVAL (invalid flags),
  322. * EFAULT (invalid ldinfos ptr)
  323. */
  324. OPENSSL_free((void *)ldinfos);
  325. dl->dli_fname = NULL;
  326. return 0;
  327. }
  328. next_ldi = ldinfos;
  329. do {
  330. this_ldi = next_ldi;
  331. if (((addr >= (uintptr_t)this_ldi->ldinfo_textorg)
  332. && (addr < ((uintptr_t)this_ldi->ldinfo_textorg +
  333. this_ldi->ldinfo_textsize)))
  334. || ((addr >= (uintptr_t)this_ldi->ldinfo_dataorg)
  335. && (addr < ((uintptr_t)this_ldi->ldinfo_dataorg +
  336. this_ldi->ldinfo_datasize)))) {
  337. char *buffer, *member;
  338. size_t buffer_sz, member_len;
  339. buffer_sz = strlen(this_ldi->ldinfo_filename) + 1;
  340. member = this_ldi->ldinfo_filename + buffer_sz;
  341. if ((member_len = strlen(member)) > 0)
  342. buffer_sz += 1 + member_len + 1;
  343. found = 1;
  344. if ((buffer = OPENSSL_malloc(buffer_sz)) != NULL) {
  345. OPENSSL_strlcpy(buffer, this_ldi->ldinfo_filename, buffer_sz);
  346. if (member_len > 0) {
  347. /*
  348. * Need to respect a possible member name and not just
  349. * returning the path name in this case. See docs:
  350. * sys/ldr.h, loadquery() and dlopen()/RTLD_MEMBER.
  351. */
  352. OPENSSL_strlcat(buffer, "(", buffer_sz);
  353. OPENSSL_strlcat(buffer, member, buffer_sz);
  354. OPENSSL_strlcat(buffer, ")", buffer_sz);
  355. }
  356. dl->dli_fname = buffer;
  357. } else {
  358. errno = ENOMEM;
  359. }
  360. } else {
  361. next_ldi = (struct ld_info *)((uintptr_t)this_ldi +
  362. this_ldi->ldinfo_next);
  363. }
  364. } while (this_ldi->ldinfo_next && !found);
  365. OPENSSL_free((void *)ldinfos);
  366. return (found && dl->dli_fname != NULL);
  367. }
  368. # endif /* _AIX */
  369. static int dlfcn_pathbyaddr(void *addr, char *path, int sz)
  370. {
  371. # ifdef HAVE_DLINFO
  372. Dl_info dli;
  373. int len;
  374. if (addr == NULL) {
  375. union {
  376. int (*f) (void *, char *, int);
  377. void *p;
  378. } t = {
  379. dlfcn_pathbyaddr
  380. };
  381. addr = t.p;
  382. }
  383. if (dladdr(addr, &dli)) {
  384. len = (int)strlen(dli.dli_fname);
  385. if (sz <= 0) {
  386. # ifdef _AIX
  387. OPENSSL_free((void *)dli.dli_fname);
  388. # endif
  389. return len + 1;
  390. }
  391. if (len >= sz)
  392. len = sz - 1;
  393. memcpy(path, dli.dli_fname, len);
  394. path[len++] = 0;
  395. # ifdef _AIX
  396. OPENSSL_free((void *)dli.dli_fname);
  397. # endif
  398. return len;
  399. }
  400. ERR_add_error_data(2, "dlfcn_pathbyaddr(): ", dlerror());
  401. # endif
  402. return -1;
  403. }
  404. static void *dlfcn_globallookup(const char *name)
  405. {
  406. void *ret = NULL, *handle = dlopen(NULL, RTLD_LAZY);
  407. if (handle) {
  408. ret = dlsym(handle, name);
  409. dlclose(handle);
  410. }
  411. return ret;
  412. }
  413. #endif /* DSO_DLFCN */