dso_dl.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. ERR_raise(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE);
  153. return NULL;
  154. }
  155. }
  156. /*
  157. * If the first file specification is missing, the second one rules.
  158. */
  159. else if (!filespec1) {
  160. merged = OPENSSL_strdup(filespec2);
  161. if (merged == NULL) {
  162. ERR_raise(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE);
  163. return NULL;
  164. }
  165. } else
  166. /*
  167. * This part isn't as trivial as it looks. It assumes that the
  168. * second file specification really is a directory, and makes no
  169. * checks whatsoever. Therefore, the result becomes the
  170. * concatenation of filespec2 followed by a slash followed by
  171. * filespec1.
  172. */
  173. {
  174. int spec2len, len;
  175. spec2len = (filespec2 ? strlen(filespec2) : 0);
  176. len = spec2len + (filespec1 ? strlen(filespec1) : 0);
  177. if (spec2len && filespec2[spec2len - 1] == '/') {
  178. spec2len--;
  179. len--;
  180. }
  181. merged = OPENSSL_malloc(len + 2);
  182. if (merged == NULL) {
  183. ERR_raise(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE);
  184. return NULL;
  185. }
  186. strcpy(merged, filespec2);
  187. merged[spec2len] = '/';
  188. strcpy(&merged[spec2len + 1], filespec1);
  189. }
  190. return merged;
  191. }
  192. /*
  193. * This function is identical to the one in dso_dlfcn.c, but as it is highly
  194. * unlikely that both the "dl" *and* "dlfcn" variants are being compiled at
  195. * the same time, there's no great duplicating the code. Figuring out an
  196. * elegant way to share one copy of the code would be more difficult and
  197. * would not leave the implementations independent.
  198. */
  199. static char *dl_name_converter(DSO *dso, const char *filename)
  200. {
  201. char *translated;
  202. int len, rsize, transform;
  203. len = strlen(filename);
  204. rsize = len + 1;
  205. transform = (strstr(filename, "/") == NULL);
  206. if (transform) {
  207. /* We will convert this to "%s.s?" or "lib%s.s?" */
  208. rsize += strlen(DSO_EXTENSION); /* The length of ".s?" */
  209. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  210. rsize += 3; /* The length of "lib" */
  211. }
  212. translated = OPENSSL_malloc(rsize);
  213. if (translated == NULL) {
  214. ERR_raise(ERR_LIB_DSO, DSO_R_NAME_TRANSLATION_FAILED);
  215. return NULL;
  216. }
  217. if (transform) {
  218. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  219. sprintf(translated, "lib%s%s", filename, DSO_EXTENSION);
  220. else
  221. sprintf(translated, "%s%s", filename, DSO_EXTENSION);
  222. } else
  223. sprintf(translated, "%s", filename);
  224. return translated;
  225. }
  226. static int dl_pathbyaddr(void *addr, char *path, int sz)
  227. {
  228. struct shl_descriptor inf;
  229. int i, len;
  230. if (addr == NULL) {
  231. union {
  232. int (*f) (void *, char *, int);
  233. void *p;
  234. } t = {
  235. dl_pathbyaddr
  236. };
  237. addr = t.p;
  238. }
  239. for (i = -1; shl_get_r(i, &inf) == 0; i++) {
  240. if (((size_t)addr >= inf.tstart && (size_t)addr < inf.tend) ||
  241. ((size_t)addr >= inf.dstart && (size_t)addr < inf.dend)) {
  242. len = (int)strlen(inf.filename);
  243. if (sz <= 0)
  244. return len + 1;
  245. if (len >= sz)
  246. len = sz - 1;
  247. memcpy(path, inf.filename, len);
  248. path[len++] = 0;
  249. return len;
  250. }
  251. }
  252. return -1;
  253. }
  254. static void *dl_globallookup(const char *name)
  255. {
  256. void *ret;
  257. shl_t h = NULL;
  258. return shl_findsym(&h, name, TYPE_UNDEFINED, &ret) ? NULL : ret;
  259. }
  260. #endif /* DSO_DL */