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