dso_dl.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright 2000-2016 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. DSOerr(DSO_F_DL_LOAD, 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. DSOerr(DSO_F_DL_LOAD, DSO_R_LOAD_FAILED);
  63. if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
  64. ERR_add_error_data(4, "filename(", filename, "): ", errbuf);
  65. goto err;
  66. }
  67. if (!sk_push(dso->meth_data, (char *)ptr)) {
  68. DSOerr(DSO_F_DL_LOAD, DSO_R_STACK_ERROR);
  69. goto err;
  70. }
  71. /*
  72. * Success, stick the converted filename we've loaded under into the DSO
  73. * (it also serves as the indicator that we are currently loaded).
  74. */
  75. dso->loaded_filename = filename;
  76. return 1;
  77. err:
  78. /* Cleanup! */
  79. OPENSSL_free(filename);
  80. if (ptr != NULL)
  81. shl_unload(ptr);
  82. return 0;
  83. }
  84. static int dl_unload(DSO *dso)
  85. {
  86. shl_t ptr;
  87. if (dso == NULL) {
  88. DSOerr(DSO_F_DL_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
  89. return 0;
  90. }
  91. if (sk_num(dso->meth_data) < 1)
  92. return 1;
  93. /* Is this statement legal? */
  94. ptr = (shl_t) sk_pop(dso->meth_data);
  95. if (ptr == NULL) {
  96. DSOerr(DSO_F_DL_UNLOAD, DSO_R_NULL_HANDLE);
  97. /*
  98. * Should push the value back onto the stack in case of a retry.
  99. */
  100. sk_push(dso->meth_data, (char *)ptr);
  101. return 0;
  102. }
  103. shl_unload(ptr);
  104. return 1;
  105. }
  106. static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname)
  107. {
  108. shl_t ptr;
  109. void *sym;
  110. if ((dso == NULL) || (symname == NULL)) {
  111. DSOerr(DSO_F_DL_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  112. return NULL;
  113. }
  114. if (sk_num(dso->meth_data) < 1) {
  115. DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_STACK_ERROR);
  116. return NULL;
  117. }
  118. ptr = (shl_t) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  119. if (ptr == NULL) {
  120. DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_NULL_HANDLE);
  121. return NULL;
  122. }
  123. if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0) {
  124. char errbuf[160];
  125. DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_SYM_FAILURE);
  126. if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
  127. ERR_add_error_data(4, "symname(", symname, "): ", errbuf);
  128. return NULL;
  129. }
  130. return (DSO_FUNC_TYPE)sym;
  131. }
  132. static char *dl_merger(DSO *dso, const char *filespec1, const char *filespec2)
  133. {
  134. char *merged;
  135. if (!filespec1 && !filespec2) {
  136. DSOerr(DSO_F_DL_MERGER, ERR_R_PASSED_NULL_PARAMETER);
  137. return NULL;
  138. }
  139. /*
  140. * If the first file specification is a rooted path, it rules. same goes
  141. * if the second file specification is missing.
  142. */
  143. if (!filespec2 || filespec1[0] == '/') {
  144. merged = OPENSSL_strdup(filespec1);
  145. if (merged == NULL) {
  146. DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
  147. return NULL;
  148. }
  149. }
  150. /*
  151. * If the first file specification is missing, the second one rules.
  152. */
  153. else if (!filespec1) {
  154. merged = OPENSSL_strdup(filespec2);
  155. if (merged == NULL) {
  156. DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
  157. return NULL;
  158. }
  159. } else
  160. /*
  161. * This part isn't as trivial as it looks. It assumes that the
  162. * second file specification really is a directory, and makes no
  163. * checks whatsoever. Therefore, the result becomes the
  164. * concatenation of filespec2 followed by a slash followed by
  165. * filespec1.
  166. */
  167. {
  168. int spec2len, len;
  169. spec2len = (filespec2 ? strlen(filespec2) : 0);
  170. len = spec2len + (filespec1 ? strlen(filespec1) : 0);
  171. if (spec2len && filespec2[spec2len - 1] == '/') {
  172. spec2len--;
  173. len--;
  174. }
  175. merged = OPENSSL_malloc(len + 2);
  176. if (merged == NULL) {
  177. DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
  178. return NULL;
  179. }
  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 = (strstr(filename, "/") == NULL);
  200. {
  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. DSOerr(DSO_F_DL_NAME_CONVERTER, 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 */