shlibloadtest.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright 2016-2018 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 <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <openssl/opensslv.h>
  13. #include <openssl/ssl.h>
  14. #include <openssl/ossl_typ.h>
  15. #include "internal/dso_conf.h"
  16. typedef void DSO;
  17. typedef const SSL_METHOD * (*TLS_method_t)(void);
  18. typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
  19. typedef void (*SSL_CTX_free_t)(SSL_CTX *);
  20. typedef int (*OPENSSL_init_crypto_t)(uint64_t, void *);
  21. typedef int (*OPENSSL_atexit_t)(void (*handler)(void));
  22. typedef unsigned long (*ERR_get_error_t)(void);
  23. typedef unsigned long (*OPENSSL_version_major_t)(void);
  24. typedef unsigned long (*OPENSSL_version_minor_t)(void);
  25. typedef unsigned long (*OPENSSL_version_patch_t)(void);
  26. typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags);
  27. typedef int (*DSO_free_t)(DSO *dso);
  28. typedef enum test_types_en {
  29. CRYPTO_FIRST,
  30. SSL_FIRST,
  31. JUST_CRYPTO,
  32. DSO_REFTEST,
  33. NO_ATEXIT
  34. } TEST_TYPE;
  35. static TEST_TYPE test_type;
  36. static const char *path_crypto;
  37. static const char *path_ssl;
  38. static const char *path_atexit;
  39. #ifdef DSO_DLFCN
  40. # include <dlfcn.h>
  41. # define SHLIB_INIT NULL
  42. typedef void *SHLIB;
  43. typedef void *SHLIB_SYM;
  44. static int shlib_load(const char *filename, SHLIB *lib)
  45. {
  46. int dl_flags = (RTLD_GLOBAL|RTLD_LAZY);
  47. #ifdef _AIX
  48. if (filename[strlen(filename) - 1] == ')')
  49. dl_flags |= RTLD_MEMBER;
  50. #endif
  51. *lib = dlopen(filename, dl_flags);
  52. return *lib == NULL ? 0 : 1;
  53. }
  54. static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
  55. {
  56. *sym = dlsym(lib, symname);
  57. return *sym != NULL;
  58. }
  59. static int shlib_close(SHLIB lib)
  60. {
  61. return dlclose(lib) != 0 ? 0 : 1;
  62. }
  63. #endif
  64. #ifdef DSO_WIN32
  65. # include <windows.h>
  66. # define SHLIB_INIT 0
  67. typedef HINSTANCE SHLIB;
  68. typedef void *SHLIB_SYM;
  69. static int shlib_load(const char *filename, SHLIB *lib)
  70. {
  71. *lib = LoadLibraryA(filename);
  72. return *lib == NULL ? 0 : 1;
  73. }
  74. static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
  75. {
  76. *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
  77. return *sym != NULL;
  78. }
  79. static int shlib_close(SHLIB lib)
  80. {
  81. return FreeLibrary(lib) == 0 ? 0 : 1;
  82. }
  83. #endif
  84. #if defined(DSO_DLFCN) || defined(DSO_WIN32)
  85. static int atexit_handler_done = 0;
  86. static void atexit_handler(void)
  87. {
  88. FILE *atexit_file = fopen(path_atexit, "w");
  89. if (atexit_file == NULL)
  90. return;
  91. fprintf(atexit_file, "atexit() run\n");
  92. fclose(atexit_file);
  93. atexit_handler_done++;
  94. }
  95. static int test_lib(void)
  96. {
  97. SHLIB ssllib = SHLIB_INIT;
  98. SHLIB cryptolib = SHLIB_INIT;
  99. SSL_CTX *ctx;
  100. union {
  101. void (*func)(void);
  102. SHLIB_SYM sym;
  103. } symbols[5];
  104. TLS_method_t myTLS_method;
  105. SSL_CTX_new_t mySSL_CTX_new;
  106. SSL_CTX_free_t mySSL_CTX_free;
  107. ERR_get_error_t myERR_get_error;
  108. OPENSSL_version_major_t myOPENSSL_version_major;
  109. OPENSSL_version_minor_t myOPENSSL_version_minor;
  110. OPENSSL_version_patch_t myOPENSSL_version_patch;
  111. OPENSSL_atexit_t myOPENSSL_atexit;
  112. int result = 0;
  113. switch (test_type) {
  114. case JUST_CRYPTO:
  115. case DSO_REFTEST:
  116. case NO_ATEXIT:
  117. case CRYPTO_FIRST:
  118. if (!shlib_load(path_crypto, &cryptolib)) {
  119. fprintf(stderr, "Failed to load libcrypto\n");
  120. goto end;
  121. }
  122. if (test_type != CRYPTO_FIRST)
  123. break;
  124. /* Fall through */
  125. case SSL_FIRST:
  126. if (!shlib_load(path_ssl, &ssllib)) {
  127. fprintf(stderr, "Failed to load libssl\n");
  128. goto end;
  129. }
  130. if (test_type != SSL_FIRST)
  131. break;
  132. if (!shlib_load(path_crypto, &cryptolib)) {
  133. fprintf(stderr, "Failed to load libcrypto\n");
  134. goto end;
  135. }
  136. break;
  137. }
  138. if (test_type == NO_ATEXIT) {
  139. OPENSSL_init_crypto_t myOPENSSL_init_crypto;
  140. if (!shlib_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) {
  141. fprintf(stderr, "Failed to load OPENSSL_init_crypto symbol\n");
  142. goto end;
  143. }
  144. myOPENSSL_init_crypto = (OPENSSL_init_crypto_t)symbols[0].func;
  145. if (!myOPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, NULL)) {
  146. fprintf(stderr, "Failed to initialise libcrypto\n");
  147. goto end;
  148. }
  149. }
  150. if (test_type != JUST_CRYPTO
  151. && test_type != DSO_REFTEST
  152. && test_type != NO_ATEXIT) {
  153. if (!shlib_sym(ssllib, "TLS_method", &symbols[0].sym)
  154. || !shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym)
  155. || !shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)) {
  156. fprintf(stderr, "Failed to load libssl symbols\n");
  157. goto end;
  158. }
  159. myTLS_method = (TLS_method_t)symbols[0].func;
  160. mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
  161. mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
  162. ctx = mySSL_CTX_new(myTLS_method());
  163. if (ctx == NULL) {
  164. fprintf(stderr, "Failed to create SSL_CTX\n");
  165. goto end;
  166. }
  167. mySSL_CTX_free(ctx);
  168. }
  169. if (!shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym)
  170. || !shlib_sym(cryptolib, "OPENSSL_version_major", &symbols[1].sym)
  171. || !shlib_sym(cryptolib, "OPENSSL_version_minor", &symbols[2].sym)
  172. || !shlib_sym(cryptolib, "OPENSSL_version_patch", &symbols[3].sym)
  173. || !shlib_sym(cryptolib, "OPENSSL_atexit", &symbols[4].sym)) {
  174. fprintf(stderr, "Failed to load libcrypto symbols\n");
  175. goto end;
  176. }
  177. myERR_get_error = (ERR_get_error_t)symbols[0].func;
  178. if (myERR_get_error() != 0) {
  179. fprintf(stderr, "Unexpected ERR_get_error() response\n");
  180. goto end;
  181. }
  182. /* Library and header version should be identical in this test */
  183. myOPENSSL_version_major = (OPENSSL_version_major_t)symbols[1].func;
  184. myOPENSSL_version_minor = (OPENSSL_version_minor_t)symbols[2].func;
  185. myOPENSSL_version_patch = (OPENSSL_version_patch_t)symbols[3].func;
  186. if (myOPENSSL_version_major() != OPENSSL_VERSION_MAJOR
  187. || myOPENSSL_version_minor() != OPENSSL_VERSION_MINOR
  188. || myOPENSSL_version_patch() != OPENSSL_VERSION_PATCH) {
  189. fprintf(stderr, "Invalid library version number\n");
  190. goto end;
  191. }
  192. myOPENSSL_atexit = (OPENSSL_atexit_t)symbols[4].func;
  193. if (!myOPENSSL_atexit(atexit_handler)) {
  194. fprintf(stderr, "Failed to register atexit handler\n");
  195. goto end;
  196. }
  197. if (test_type == DSO_REFTEST) {
  198. # ifdef DSO_DLFCN
  199. DSO_dsobyaddr_t myDSO_dsobyaddr;
  200. DSO_free_t myDSO_free;
  201. /*
  202. * This is resembling the code used in ossl_init_base() and
  203. * OPENSSL_atexit() to block unloading the library after dlclose().
  204. * We are not testing this on Windows, because it is done there in a
  205. * completely different way. Especially as a call to DSO_dsobyaddr()
  206. * will always return an error, because DSO_pathbyaddr() is not
  207. * implemented there.
  208. */
  209. if (!shlib_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym)
  210. || !shlib_sym(cryptolib, "DSO_free", &symbols[1].sym)) {
  211. fprintf(stderr, "Unable to load DSO symbols\n");
  212. goto end;
  213. }
  214. myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
  215. myDSO_free = (DSO_free_t)symbols[1].func;
  216. {
  217. DSO *hndl;
  218. /* use known symbol from crypto module */
  219. hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0);
  220. if (hndl == NULL) {
  221. fprintf(stderr, "DSO_dsobyaddr() failed\n");
  222. goto end;
  223. }
  224. myDSO_free(hndl);
  225. }
  226. # endif /* DSO_DLFCN */
  227. }
  228. if (!shlib_close(cryptolib)) {
  229. fprintf(stderr, "Failed to close libcrypto\n");
  230. goto end;
  231. }
  232. if (test_type == CRYPTO_FIRST || test_type == SSL_FIRST) {
  233. if (!shlib_close(ssllib)) {
  234. fprintf(stderr, "Failed to close libssl\n");
  235. goto end;
  236. }
  237. }
  238. # if defined(OPENSSL_NO_PINSHARED) \
  239. && defined(__GLIBC__) \
  240. && defined(__GLIBC_PREREQ) \
  241. && defined(OPENSSL_SYS_LINUX)
  242. # if __GLIBC_PREREQ(2, 3)
  243. /*
  244. * If we didn't pin the so then we are hopefully on a platform that supports
  245. * running atexit() on so unload. If not we might crash. We know this is
  246. * true on linux since glibc 2.2.3
  247. */
  248. if (test_type != NO_ATEXIT && atexit_handler_done != 1) {
  249. fprintf(stderr, "atexit() handler did not run\n");
  250. goto end;
  251. }
  252. # endif
  253. # endif
  254. result = 1;
  255. end:
  256. return result;
  257. }
  258. #endif
  259. /*
  260. * shlibloadtest should not use the normal test framework because we don't want
  261. * it to link against libcrypto (which the framework uses). The point of the
  262. * test is to check dynamic loading and unloading of libcrypto/libssl.
  263. */
  264. int main(int argc, char *argv[])
  265. {
  266. const char *p;
  267. if (argc != 5) {
  268. fprintf(stderr, "Incorrect number of arguments\n");
  269. return 1;
  270. }
  271. p = argv[1];
  272. if (strcmp(p, "-crypto_first") == 0) {
  273. test_type = CRYPTO_FIRST;
  274. } else if (strcmp(p, "-ssl_first") == 0) {
  275. test_type = SSL_FIRST;
  276. } else if (strcmp(p, "-just_crypto") == 0) {
  277. test_type = JUST_CRYPTO;
  278. } else if (strcmp(p, "-dso_ref") == 0) {
  279. test_type = DSO_REFTEST;
  280. } else if (strcmp(p, "-no_atexit") == 0) {
  281. test_type = NO_ATEXIT;
  282. } else {
  283. fprintf(stderr, "Unrecognised argument\n");
  284. return 1;
  285. }
  286. path_crypto = argv[2];
  287. path_ssl = argv[3];
  288. path_atexit = argv[4];
  289. if (path_crypto == NULL || path_ssl == NULL) {
  290. fprintf(stderr, "Invalid libcrypto/libssl path\n");
  291. return 1;
  292. }
  293. #if defined(DSO_DLFCN) || defined(DSO_WIN32)
  294. if (!test_lib())
  295. return 1;
  296. #endif
  297. return 0;
  298. }