dso_dl.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright 2000-2021 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. #include "dso_local.h"
  10. #ifdef DSO_DL
  11. # include <dl.h>
  12. /* Part of the hack in "dl_load" ... */
  13. # define DSO_MAX_TRANSLATED_SIZE 256
  14. static int dl_load(DSO *dso);
  15. static int dl_unload(DSO *dso);
  16. static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname);
  17. static char *dl_name_converter(DSO *dso, const char *filename);
  18. static char *dl_merger(DSO *dso, const char *filespec1,
  19. const char *filespec2);
  20. static int dl_pathbyaddr(void *addr, char *path, int sz);
  21. static void *dl_globallookup(const char *name);
  22. static DSO_METHOD dso_meth_dl = {
  23. "OpenSSL 'dl' shared library method",
  24. dl_load,
  25. dl_unload,
  26. dl_bind_func,
  27. NULL, /* ctrl */
  28. dl_name_converter,
  29. dl_merger,
  30. NULL, /* init */
  31. NULL, /* finish */
  32. dl_pathbyaddr,
  33. dl_globallookup
  34. };
  35. DSO_METHOD *DSO_METHOD_openssl(void)
  36. {
  37. return &dso_meth_dl;
  38. }
  39. /*
  40. * For this DSO_METHOD, our meth_data STACK will contain; (i) the handle
  41. * (shl_t) returned from shl_load(). NB: I checked on HPUX11 and shl_t is
  42. * itself a pointer type so the cast is safe.
  43. */
  44. static int dl_load(DSO *dso)
  45. {
  46. shl_t ptr = NULL;
  47. /*
  48. * We don't do any fancy retries or anything, just take the method's (or
  49. * DSO's if it has the callback set) best translation of the
  50. * platform-independent filename and try once with that.
  51. */
  52. char *filename = DSO_convert_filename(dso, NULL);
  53. if (filename == NULL) {
  54. ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
  55. goto err;
  56. }
  57. ptr = shl_load(filename, BIND_IMMEDIATE |
  58. (dso->flags & DSO_FLAG_NO_NAME_TRANSLATION ? 0 :
  59. DYNAMIC_PATH), 0L);
  60. if (ptr == NULL) {
  61. char errbuf[160];
  62. if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
  63. ERR_raise_data(ERR_LIB_DSO, DSO_R_LOAD_FAILED,
  64. "filename(%s): %s", filename, errbuf);
  65. else
  66. ERR_raise_data(ERR_LIB_DSO, DSO_R_LOAD_FAILED,
  67. "filename(%s): errno %d", filename, errno);
  68. goto err;
  69. }
  70. if (!sk_push(dso->meth_data, (char *)ptr)) {
  71. ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
  72. goto err;
  73. }
  74. /*
  75. * Success, stick the converted filename we've loaded under into the DSO
  76. * (it also serves as the indicator that we are currently loaded).
  77. */
  78. dso->loaded_filename = filename;
  79. return 1;
  80. err:
  81. /* Cleanup! */
  82. OPENSSL_free(filename);
  83. if (ptr != NULL)
  84. shl_unload(ptr);
  85. return 0;
  86. }
  87. static int dl_unload(DSO *dso)
  88. {
  89. shl_t ptr;
  90. if (dso == NULL) {
  91. ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
  92. return 0;
  93. }
  94. if (sk_num(dso->meth_data) < 1)
  95. return 1;
  96. /* Is this statement legal? */
  97. ptr = (shl_t) sk_pop(dso->meth_data);
  98. if (ptr == NULL) {
  99. ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
  100. /*
  101. * Should push the value back onto the stack in case of a retry.
  102. */
  103. sk_push(dso->meth_data, (char *)ptr);
  104. return 0;
  105. }
  106. shl_unload(ptr);
  107. return 1;
  108. }
  109. static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname)
  110. {
  111. shl_t ptr;
  112. void *sym;
  113. if ((dso == NULL) || (symname == NULL)) {
  114. ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
  115. return NULL;
  116. }
  117. if (sk_num(dso->meth_data) < 1) {
  118. ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
  119. return NULL;
  120. }
  121. ptr = (shl_t) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  122. if (ptr == NULL) {
  123. ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
  124. return NULL;
  125. }
  126. if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0) {
  127. char errbuf[160];
  128. if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
  129. ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE,
  130. "symname(%s): %s", symname, errbuf);
  131. else
  132. ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE,
  133. "symname(%s): errno %d", symname, errno);
  134. return NULL;
  135. }
  136. return (DSO_FUNC_TYPE)sym;
  137. }
  138. static char *dl_merger(DSO *dso, const char *filespec1, const char *filespec2)
  139. {
  140. char *merged;
  141. if (!filespec1 && !filespec2) {
  142. ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
  143. return NULL;
  144. }
  145. /*
  146. * If the first file specification is a rooted path, it rules. same goes
  147. * if the second file specification is missing.
  148. */
  149. if (!filespec2 || filespec1[0] == '/') {
  150. merged = OPENSSL_strdup(filespec1);
  151. if (merged == NULL)
  152. return NULL;
  153. }
  154. /*
  155. * If the first file specification is missing, the second one rules.
  156. */
  157. else if (!filespec1) {
  158. merged = OPENSSL_strdup(filespec2);
  159. if (merged == NULL)
  160. return NULL;
  161. } else
  162. /*
  163. * This part isn't as trivial as it looks. It assumes that the
  164. * second file specification really is a directory, and makes no
  165. * checks whatsoever. Therefore, the result becomes the
  166. * concatenation of filespec2 followed by a slash followed by
  167. * filespec1.
  168. */
  169. {
  170. int spec2len, len;
  171. spec2len = (filespec2 ? strlen(filespec2) : 0);
  172. len = spec2len + (filespec1 ? strlen(filespec1) : 0);
  173. if (spec2len && filespec2[spec2len - 1] == '/') {
  174. spec2len--;
  175. len--;
  176. }
  177. merged = OPENSSL_malloc(len + 2);
  178. if (merged == NULL)
  179. return NULL;
  180. strcpy(merged, filespec2);
  181. merged[spec2len] = '/';
  182. strcpy(&merged[spec2len + 1], filespec1);
  183. }
  184. return merged;
  185. }
  186. /*
  187. * This function is identical to the one in dso_dlfcn.c, but as it is highly
  188. * unlikely that both the "dl" *and* "dlfcn" variants are being compiled at
  189. * the same time, there's no great duplicating the code. Figuring out an
  190. * elegant way to share one copy of the code would be more difficult and
  191. * would not leave the implementations independent.
  192. */
  193. static char *dl_name_converter(DSO *dso, const char *filename)
  194. {
  195. char *translated;
  196. int len, rsize, transform;
  197. len = strlen(filename);
  198. rsize = len + 1;
  199. transform = (strchr(filename, '/') == NULL);
  200. if (transform) {
  201. /* We will convert this to "%s.s?" or "lib%s.s?" */
  202. rsize += strlen(DSO_EXTENSION); /* The length of ".s?" */
  203. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  204. rsize += 3; /* The length of "lib" */
  205. }
  206. translated = OPENSSL_malloc(rsize);
  207. if (translated == NULL) {
  208. ERR_raise(ERR_LIB_DSO, DSO_R_NAME_TRANSLATION_FAILED);
  209. return NULL;
  210. }
  211. if (transform) {
  212. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  213. sprintf(translated, "lib%s%s", filename, DSO_EXTENSION);
  214. else
  215. sprintf(translated, "%s%s", filename, DSO_EXTENSION);
  216. } else
  217. sprintf(translated, "%s", filename);
  218. return translated;
  219. }
  220. static int dl_pathbyaddr(void *addr, char *path, int sz)
  221. {
  222. struct shl_descriptor inf;
  223. int i, len;
  224. if (addr == NULL) {
  225. union {
  226. int (*f) (void *, char *, int);
  227. void *p;
  228. } t = {
  229. dl_pathbyaddr
  230. };
  231. addr = t.p;
  232. }
  233. for (i = -1; shl_get_r(i, &inf) == 0; i++) {
  234. if (((size_t)addr >= inf.tstart && (size_t)addr < inf.tend) ||
  235. ((size_t)addr >= inf.dstart && (size_t)addr < inf.dend)) {
  236. len = (int)strlen(inf.filename);
  237. if (sz <= 0)
  238. return len + 1;
  239. if (len >= sz)
  240. len = sz - 1;
  241. memcpy(path, inf.filename, len);
  242. path[len++] = 0;
  243. return len;
  244. }
  245. }
  246. return -1;
  247. }
  248. static void *dl_globallookup(const char *name)
  249. {
  250. void *ret;
  251. shl_t h = NULL;
  252. return shl_findsym(&h, name, TYPE_UNDEFINED, &ret) ? NULL : ret;
  253. }
  254. #endif /* DSO_DL */