shlibloadtest.c 9.1 KB

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