shlibloadtest.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/types.h>
  15. #include "simpledynamic.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 SD_INIT
  40. static int atexit_handler_done = 0;
  41. static void atexit_handler(void)
  42. {
  43. FILE *atexit_file = fopen(path_atexit, "w");
  44. if (atexit_file == NULL)
  45. return;
  46. fprintf(atexit_file, "atexit() run\n");
  47. fclose(atexit_file);
  48. atexit_handler_done++;
  49. }
  50. static int test_lib(void)
  51. {
  52. SD ssllib = SD_INIT;
  53. SD cryptolib = SD_INIT;
  54. SSL_CTX *ctx;
  55. union {
  56. void (*func)(void);
  57. SD_SYM sym;
  58. } symbols[5];
  59. TLS_method_t myTLS_method;
  60. SSL_CTX_new_t mySSL_CTX_new;
  61. SSL_CTX_free_t mySSL_CTX_free;
  62. ERR_get_error_t myERR_get_error;
  63. OPENSSL_version_major_t myOPENSSL_version_major;
  64. OPENSSL_version_minor_t myOPENSSL_version_minor;
  65. OPENSSL_version_patch_t myOPENSSL_version_patch;
  66. OPENSSL_atexit_t myOPENSSL_atexit;
  67. int result = 0;
  68. switch (test_type) {
  69. case JUST_CRYPTO:
  70. case DSO_REFTEST:
  71. case NO_ATEXIT:
  72. case CRYPTO_FIRST:
  73. if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) {
  74. fprintf(stderr, "Failed to load libcrypto\n");
  75. goto end;
  76. }
  77. if (test_type != CRYPTO_FIRST)
  78. break;
  79. /* Fall through */
  80. case SSL_FIRST:
  81. if (!sd_load(path_ssl, &ssllib, SD_SHLIB)) {
  82. fprintf(stderr, "Failed to load libssl\n");
  83. goto end;
  84. }
  85. if (test_type != SSL_FIRST)
  86. break;
  87. if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) {
  88. fprintf(stderr, "Failed to load libcrypto\n");
  89. goto end;
  90. }
  91. break;
  92. }
  93. if (test_type == NO_ATEXIT) {
  94. OPENSSL_init_crypto_t myOPENSSL_init_crypto;
  95. if (!sd_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) {
  96. fprintf(stderr, "Failed to load OPENSSL_init_crypto symbol\n");
  97. goto end;
  98. }
  99. myOPENSSL_init_crypto = (OPENSSL_init_crypto_t)symbols[0].func;
  100. if (!myOPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, NULL)) {
  101. fprintf(stderr, "Failed to initialise libcrypto\n");
  102. goto end;
  103. }
  104. }
  105. if (test_type != JUST_CRYPTO
  106. && test_type != DSO_REFTEST
  107. && test_type != NO_ATEXIT) {
  108. if (!sd_sym(ssllib, "TLS_method", &symbols[0].sym)
  109. || !sd_sym(ssllib, "SSL_CTX_new", &symbols[1].sym)
  110. || !sd_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)) {
  111. fprintf(stderr, "Failed to load libssl symbols\n");
  112. goto end;
  113. }
  114. myTLS_method = (TLS_method_t)symbols[0].func;
  115. mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
  116. mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
  117. ctx = mySSL_CTX_new(myTLS_method());
  118. if (ctx == NULL) {
  119. fprintf(stderr, "Failed to create SSL_CTX\n");
  120. goto end;
  121. }
  122. mySSL_CTX_free(ctx);
  123. }
  124. if (!sd_sym(cryptolib, "ERR_get_error", &symbols[0].sym)
  125. || !sd_sym(cryptolib, "OPENSSL_version_major", &symbols[1].sym)
  126. || !sd_sym(cryptolib, "OPENSSL_version_minor", &symbols[2].sym)
  127. || !sd_sym(cryptolib, "OPENSSL_version_patch", &symbols[3].sym)
  128. || !sd_sym(cryptolib, "OPENSSL_atexit", &symbols[4].sym)) {
  129. fprintf(stderr, "Failed to load libcrypto symbols\n");
  130. goto end;
  131. }
  132. myERR_get_error = (ERR_get_error_t)symbols[0].func;
  133. if (myERR_get_error() != 0) {
  134. fprintf(stderr, "Unexpected ERR_get_error() response\n");
  135. goto end;
  136. }
  137. /* Library and header version should be identical in this test */
  138. myOPENSSL_version_major = (OPENSSL_version_major_t)symbols[1].func;
  139. myOPENSSL_version_minor = (OPENSSL_version_minor_t)symbols[2].func;
  140. myOPENSSL_version_patch = (OPENSSL_version_patch_t)symbols[3].func;
  141. if (myOPENSSL_version_major() != OPENSSL_VERSION_MAJOR
  142. || myOPENSSL_version_minor() != OPENSSL_VERSION_MINOR
  143. || myOPENSSL_version_patch() != OPENSSL_VERSION_PATCH) {
  144. fprintf(stderr, "Invalid library version number\n");
  145. goto end;
  146. }
  147. myOPENSSL_atexit = (OPENSSL_atexit_t)symbols[4].func;
  148. if (!myOPENSSL_atexit(atexit_handler)) {
  149. fprintf(stderr, "Failed to register atexit handler\n");
  150. goto end;
  151. }
  152. if (test_type == DSO_REFTEST) {
  153. # ifdef DSO_DLFCN
  154. DSO_dsobyaddr_t myDSO_dsobyaddr;
  155. DSO_free_t myDSO_free;
  156. /*
  157. * This is resembling the code used in ossl_init_base() and
  158. * OPENSSL_atexit() to block unloading the library after dlclose().
  159. * We are not testing this on Windows, because it is done there in a
  160. * completely different way. Especially as a call to DSO_dsobyaddr()
  161. * will always return an error, because DSO_pathbyaddr() is not
  162. * implemented there.
  163. */
  164. if (!sd_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym)
  165. || !sd_sym(cryptolib, "DSO_free", &symbols[1].sym)) {
  166. fprintf(stderr, "Unable to load DSO symbols\n");
  167. goto end;
  168. }
  169. myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
  170. myDSO_free = (DSO_free_t)symbols[1].func;
  171. {
  172. DSO *hndl;
  173. /* use known symbol from crypto module */
  174. hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0);
  175. if (hndl == NULL) {
  176. fprintf(stderr, "DSO_dsobyaddr() failed\n");
  177. goto end;
  178. }
  179. myDSO_free(hndl);
  180. }
  181. # endif /* DSO_DLFCN */
  182. }
  183. if (!sd_close(cryptolib)) {
  184. fprintf(stderr, "Failed to close libcrypto\n");
  185. goto end;
  186. }
  187. if (test_type == CRYPTO_FIRST || test_type == SSL_FIRST) {
  188. if (!sd_close(ssllib)) {
  189. fprintf(stderr, "Failed to close libssl\n");
  190. goto end;
  191. }
  192. }
  193. # if defined(OPENSSL_NO_PINSHARED) \
  194. && defined(__GLIBC__) \
  195. && defined(__GLIBC_PREREQ) \
  196. && defined(OPENSSL_SYS_LINUX)
  197. # if __GLIBC_PREREQ(2, 3)
  198. /*
  199. * If we didn't pin the so then we are hopefully on a platform that supports
  200. * running atexit() on so unload. If not we might crash. We know this is
  201. * true on linux since glibc 2.2.3
  202. */
  203. if (test_type != NO_ATEXIT && atexit_handler_done != 1) {
  204. fprintf(stderr, "atexit() handler did not run\n");
  205. goto end;
  206. }
  207. # endif
  208. # endif
  209. result = 1;
  210. end:
  211. return result;
  212. }
  213. #endif
  214. /*
  215. * shlibloadtest should not use the normal test framework because we don't want
  216. * it to link against libcrypto (which the framework uses). The point of the
  217. * test is to check dynamic loading and unloading of libcrypto/libssl.
  218. */
  219. int main(int argc, char *argv[])
  220. {
  221. const char *p;
  222. if (argc != 5) {
  223. fprintf(stderr, "Incorrect number of arguments\n");
  224. return 1;
  225. }
  226. p = argv[1];
  227. if (strcmp(p, "-crypto_first") == 0) {
  228. test_type = CRYPTO_FIRST;
  229. } else if (strcmp(p, "-ssl_first") == 0) {
  230. test_type = SSL_FIRST;
  231. } else if (strcmp(p, "-just_crypto") == 0) {
  232. test_type = JUST_CRYPTO;
  233. } else if (strcmp(p, "-dso_ref") == 0) {
  234. test_type = DSO_REFTEST;
  235. } else if (strcmp(p, "-no_atexit") == 0) {
  236. test_type = NO_ATEXIT;
  237. } else {
  238. fprintf(stderr, "Unrecognised argument\n");
  239. return 1;
  240. }
  241. path_crypto = argv[2];
  242. path_ssl = argv[3];
  243. path_atexit = argv[4];
  244. if (path_crypto == NULL || path_ssl == NULL) {
  245. fprintf(stderr, "Invalid libcrypto/libssl path\n");
  246. return 1;
  247. }
  248. #ifdef SD_INIT
  249. if (!test_lib())
  250. return 1;
  251. #endif
  252. return 0;
  253. }