Stop init loops
[openssl.git] / crypto / init.c
1 /*
2  * Copyright 2016 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
10 #include <internal/cryptlib_int.h>
11 #include <openssl/err.h>
12 #include <internal/rand.h>
13 #include <internal/bio.h>
14 #include <openssl/evp.h>
15 #include <internal/evp_int.h>
16 #include <internal/conf.h>
17 #include <internal/async.h>
18 #include <internal/engine.h>
19 #include <internal/comp.h>
20 #include <internal/err.h>
21 #include <internal/err_int.h>
22 #include <internal/objects.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <internal/thread_once.h>
26 #include <internal/dso.h>
27
28 static int stopped = 0;
29
30 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
31
32 static CRYPTO_THREAD_LOCAL threadstopkey;
33
34 static void ossl_init_thread_stop_wrap(void *local)
35 {
36     ossl_init_thread_stop((struct thread_local_inits_st *)local);
37 }
38
39 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
40 {
41     struct thread_local_inits_st *local =
42         CRYPTO_THREAD_get_local(&threadstopkey);
43
44     if (local == NULL && alloc) {
45         local = OPENSSL_zalloc(sizeof *local);
46         CRYPTO_THREAD_set_local(&threadstopkey, local);
47     }
48     if (!alloc) {
49         CRYPTO_THREAD_set_local(&threadstopkey, NULL);
50     }
51
52     return local;
53 }
54
55 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
56 struct ossl_init_stop_st {
57     void (*handler)(void);
58     OPENSSL_INIT_STOP *next;
59 };
60
61 static OPENSSL_INIT_STOP *stop_handlers = NULL;
62 static CRYPTO_RWLOCK *init_lock = NULL;
63
64 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
65 static int base_inited = 0;
66 DEFINE_RUN_ONCE_STATIC(ossl_init_base)
67 {
68 #ifdef OPENSSL_INIT_DEBUG
69     fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
70 #endif
71     /*
72      * We use a dummy thread local key here. We use the destructor to detect
73      * when the thread is going to stop (where that feature is available)
74      */
75     CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
76 #ifndef OPENSSL_SYS_UEFI
77     atexit(OPENSSL_cleanup);
78 #endif
79     if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
80         return 0;
81     OPENSSL_cpuid_setup();
82     base_inited = 1;
83
84 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
85 # ifdef DSO_WIN32
86     {
87         HMODULE handle = NULL;
88         BOOL ret;
89
90         /* We don't use the DSO route for WIN32 because there is a better way */
91         ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
92                                 | GET_MODULE_HANDLE_EX_FLAG_PIN,
93                                 (void *)&base_inited, &handle);
94
95         return (ret == TRUE) ? 1 : 0;
96     }
97 # else
98     /*
99      * Deliberately leak a reference to ourselves. This will force the library
100      * to remain loaded until the atexit() handler is run a process exit.
101      */
102     {
103         DSO *dso = NULL;
104
105         dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
106         DSO_free(dso);
107     }
108 # endif
109 #endif
110
111     return 1;
112 }
113
114 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
115 static int load_crypto_strings_inited = 0;
116 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
117 {
118     /* Do nothing in this case */
119     return 1;
120 }
121
122 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
123 {
124     int ret = 1;
125     /*
126      * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
127      * pulling in all the error strings during static linking
128      */
129 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
130 # ifdef OPENSSL_INIT_DEBUG
131     fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
132                     "err_load_crypto_strings_int()\n");
133 # endif
134     ret = err_load_crypto_strings_int();
135     load_crypto_strings_inited = 1;
136 #endif    
137     return ret;
138 }
139
140 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
141 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
142 {
143     /*
144      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
145      * pulling in all the ciphers during static linking
146      */
147 #ifndef OPENSSL_NO_AUTOALGINIT
148 # ifdef OPENSSL_INIT_DEBUG
149     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
150                     "openssl_add_all_ciphers_int()\n");
151 # endif
152     openssl_add_all_ciphers_int();
153 #endif
154     return 1;
155 }
156
157 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
158 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
159 {
160     /*
161      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
162      * pulling in all the ciphers during static linking
163      */
164 #ifndef OPENSSL_NO_AUTOALGINIT
165 # ifdef OPENSSL_INIT_DEBUG
166     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
167                     "openssl_add_all_digests()\n");
168 # endif
169     openssl_add_all_digests_int();
170 #endif
171     return 1;
172 }
173
174 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
175 {
176     /* Do nothing */
177     return 1;
178 }
179
180 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
181 static int config_inited = 0;
182 static const char *appname;
183 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
184 {
185 #ifdef OPENSSL_INIT_DEBUG
186     fprintf(stderr,
187             "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
188             appname == NULL ? "NULL" : appname);
189 #endif
190     openssl_config_int(appname);
191     config_inited = 1;
192     return 1;
193 }
194 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
195 {
196 #ifdef OPENSSL_INIT_DEBUG
197     fprintf(stderr,
198             "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
199 #endif
200     openssl_no_config_int();
201     config_inited = 1;
202     return 1;
203 }
204
205 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
206 static int async_inited = 0;
207 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
208 {
209 #ifdef OPENSSL_INIT_DEBUG
210     fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
211 #endif
212     if (!async_init())
213         return 0;
214     async_inited = 1;
215     return 1;
216 }
217
218 #ifndef OPENSSL_NO_ENGINE
219 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
220 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
221 {
222 # ifdef OPENSSL_INIT_DEBUG
223     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
224                     "engine_load_openssl_int()\n");
225 # endif
226     engine_load_openssl_int();
227     return 1;
228 }
229 # if !defined(OPENSSL_NO_HW) && \
230     (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(HAVE_CRYPTODEV))
231 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
232 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_cryptodev)
233 {
234 #  ifdef OPENSSL_INIT_DEBUG
235     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
236                     "engine_load_cryptodev_int()\n");
237 #  endif
238     engine_load_cryptodev_int();
239     return 1;
240 }
241 # endif
242
243 # ifndef OPENSSL_NO_RDRAND
244 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
245 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
246 {
247 #  ifdef OPENSSL_INIT_DEBUG
248     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
249                     "engine_load_rdrand_int()\n");
250 #  endif
251     engine_load_rdrand_int();
252     return 1;
253 }
254 # endif
255 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
256 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
257 {
258 # ifdef OPENSSL_INIT_DEBUG
259     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
260                     "engine_load_dynamic_int()\n");
261 # endif
262     engine_load_dynamic_int();
263     return 1;
264 }
265 # ifndef OPENSSL_NO_STATIC_ENGINE
266 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
267 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
268 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
269 {
270 #   ifdef OPENSSL_INIT_DEBUG
271     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
272                     "engine_load_padlock_int()\n");
273 #   endif
274     engine_load_padlock_int();
275     return 1;
276 }
277 #  endif
278 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
279 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
280 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
281 {
282 #   ifdef OPENSSL_INIT_DEBUG
283     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
284                     "engine_load_capi_int()\n");
285 #   endif
286     engine_load_capi_int();
287     return 1;
288 }
289 #  endif
290 #  if !defined(OPENSSL_NO_AFALGENG)
291 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
292 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
293 {
294 #   ifdef OPENSSL_INIT_DEBUG
295     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
296                     "engine_load_afalg_int()\n");
297 #   endif
298     engine_load_afalg_int();
299     return 1;
300 }
301 #  endif
302 # endif
303 #endif
304
305 #ifndef OPENSSL_NO_COMP
306 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
307
308 static int zlib_inited = 0;
309 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
310 {
311     /* Do nothing - we need to know about this for the later cleanup */
312     zlib_inited = 1;
313     return 1;
314 }
315 #endif
316
317 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
318 {
319     /* Can't do much about this */
320     if (locals == NULL)
321         return;
322
323     if (locals->async) {
324 #ifdef OPENSSL_INIT_DEBUG
325         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
326                         "ASYNC_cleanup_thread()\n");
327 #endif
328         ASYNC_cleanup_thread();
329     }
330
331     if (locals->err_state) {
332 #ifdef OPENSSL_INIT_DEBUG
333         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
334                         "err_delete_thread_state()\n");
335 #endif
336         err_delete_thread_state();
337     }
338
339     OPENSSL_free(locals);
340 }
341
342 void OPENSSL_thread_stop(void)
343 {
344     ossl_init_thread_stop(
345         (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
346 }
347
348 int ossl_init_thread_start(uint64_t opts)
349 {
350     struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
351
352     if (locals == NULL)
353         return 0;
354
355     if (opts & OPENSSL_INIT_THREAD_ASYNC) {
356 #ifdef OPENSSL_INIT_DEBUG
357         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
358                         "marking thread for async\n");
359 #endif
360         locals->async = 1;
361     }
362
363     if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
364 #ifdef OPENSSL_INIT_DEBUG
365         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
366                         "marking thread for err_state\n");
367 #endif
368         locals->err_state = 1;
369     }
370
371     return 1;
372 }
373
374 void OPENSSL_cleanup(void)
375 {
376     OPENSSL_INIT_STOP *currhandler, *lasthandler;
377
378     /* If we've not been inited then no need to deinit */
379     if (!base_inited)
380         return;
381
382     /* Might be explicitly called and also by atexit */
383     if (stopped)
384         return;
385     stopped = 1;
386
387     /*
388      * Thread stop may not get automatically called by the thread library for
389      * the very last thread in some situations, so call it directly.
390      */
391     ossl_init_thread_stop(ossl_init_get_thread_local(0));
392
393     currhandler = stop_handlers;
394     while (currhandler != NULL) {
395         currhandler->handler();
396         lasthandler = currhandler;
397         currhandler = currhandler->next;
398         OPENSSL_free(lasthandler);
399     }
400     stop_handlers = NULL;
401
402     CRYPTO_THREAD_lock_free(init_lock);
403
404     /*
405      * We assume we are single-threaded for this function, i.e. no race
406      * conditions for the various "*_inited" vars below.
407      */
408
409 #ifndef OPENSSL_NO_COMP
410     if (zlib_inited) {
411 #ifdef OPENSSL_INIT_DEBUG
412         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
413                         "comp_zlib_cleanup_int()\n");
414 #endif
415         comp_zlib_cleanup_int();
416     }
417 #endif
418
419     if (async_inited) {
420 # ifdef OPENSSL_INIT_DEBUG
421         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
422                         "async_deinit()\n");
423 # endif
424         async_deinit();
425     }
426
427     if (load_crypto_strings_inited) {
428 #ifdef OPENSSL_INIT_DEBUG
429         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
430                         "err_free_strings_int()\n");
431 #endif
432         err_free_strings_int();
433     }
434
435     CRYPTO_THREAD_cleanup_local(&threadstopkey);
436
437 #ifdef OPENSSL_INIT_DEBUG
438     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
439                     "rand_cleanup_int()\n");
440     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
441                     "conf_modules_free_int()\n");
442 #ifndef OPENSSL_NO_ENGINE
443     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
444                     "engine_cleanup_int()\n");
445 #endif
446     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
447                     "crypto_cleanup_all_ex_data_int()\n");
448     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
449                     "bio_sock_cleanup_int()\n");
450     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
451                     "bio_cleanup()\n");
452     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
453                     "evp_cleanup_int()\n");
454     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
455                     "obj_cleanup_int()\n");
456     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
457                     "err_cleanup()\n");
458 #endif
459     /*
460      * Note that cleanup order is important:
461      * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
462      * must be called before engine_cleanup_int()
463      * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
464      * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
465      * - conf_modules_free_int() can end up in ENGINE code so must be called
466      * before engine_cleanup_int()
467      * - ENGINEs and additional EVP algorithms might use added OIDs names so
468      * obj_cleanup_int() must be called last
469      */
470     rand_cleanup_int();
471     conf_modules_free_int();
472 #ifndef OPENSSL_NO_ENGINE
473     engine_cleanup_int();
474 #endif
475     crypto_cleanup_all_ex_data_int();
476     bio_cleanup();
477     evp_cleanup_int();
478     obj_cleanup_int();
479     err_cleanup();
480
481     base_inited = 0;
482 }
483
484 /*
485  * If this function is called with a non NULL settings value then it must be
486  * called prior to any threads making calls to any OpenSSL functions,
487  * i.e. passing a non-null settings value is assumed to be single-threaded.
488  */
489 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
490 {
491     static int stoperrset = 0;
492
493     if (stopped) {
494         if (!stoperrset) {
495             /*
496              * We only ever set this once to avoid getting into an infinite
497              * loop where the error system keeps trying to init and fails so
498              * sets an error etc
499              */
500             stoperrset = 1;
501             CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
502         }
503         return 0;
504     }
505
506     if (!base_inited && !RUN_ONCE(&base, ossl_init_base))
507         return 0;
508
509     if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
510             && !RUN_ONCE(&load_crypto_strings,
511                          ossl_init_no_load_crypto_strings))
512         return 0;
513
514     if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
515             && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
516         return 0;
517
518     if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
519             && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
520         return 0;
521
522     if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
523             && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
524         return 0;
525
526     if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
527             && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
528         return 0;
529
530     if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
531             && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
532         return 0;
533
534     if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
535             && !RUN_ONCE(&config, ossl_init_no_config))
536         return 0;
537
538     if (opts & OPENSSL_INIT_LOAD_CONFIG) {
539         int ret;
540         CRYPTO_THREAD_write_lock(init_lock);
541         appname = (settings == NULL) ? NULL : settings->appname;
542         ret = RUN_ONCE(&config, ossl_init_config);
543         CRYPTO_THREAD_unlock(init_lock);
544         if (!ret)
545             return 0;
546     }
547
548     if ((opts & OPENSSL_INIT_ASYNC)
549             && !RUN_ONCE(&async, ossl_init_async))
550         return 0;
551
552 #ifndef OPENSSL_NO_ENGINE
553     if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
554             && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
555         return 0;
556 # if !defined(OPENSSL_NO_HW) && \
557     (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(HAVE_CRYPTODEV))
558     if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
559             && !RUN_ONCE(&engine_cryptodev, ossl_init_engine_cryptodev))
560         return 0;
561 # endif
562 # ifndef OPENSSL_NO_RDRAND
563     if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
564             && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
565         return 0;
566 # endif
567     if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
568             && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
569         return 0;
570 # ifndef OPENSSL_NO_STATIC_ENGINE
571 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
572     if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
573             && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
574         return 0;
575 #  endif
576 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
577     if ((opts & OPENSSL_INIT_ENGINE_CAPI)
578             && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
579         return 0;
580 #  endif
581 #  if !defined(OPENSSL_NO_AFALGENG)
582     if ((opts & OPENSSL_INIT_ENGINE_AFALG)
583             && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
584         return 0;
585 #  endif
586 # endif
587     if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
588                 | OPENSSL_INIT_ENGINE_OPENSSL
589                 | OPENSSL_INIT_ENGINE_AFALG)) {
590         ENGINE_register_all_complete();
591     }
592 #endif
593
594 #ifndef OPENSSL_NO_COMP
595     if ((opts & OPENSSL_INIT_ZLIB)
596             && !RUN_ONCE(&zlib, ossl_init_zlib))
597         return 0;
598 #endif
599
600     return 1;
601 }
602
603 int OPENSSL_atexit(void (*handler)(void))
604 {
605     OPENSSL_INIT_STOP *newhand;
606
607 #if !defined(OPENSSL_NO_DSO) && !defined(OPENSSL_USE_NODELETE)
608     {
609         union {
610             void *sym;
611             void (*func)(void);
612         } handlersym;
613
614         handlersym.func = handler;
615 # ifdef DSO_WIN32
616         {
617             HMODULE handle = NULL;
618             BOOL ret;
619
620             /*
621              * We don't use the DSO route for WIN32 because there is a better
622              * way
623              */
624             ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
625                                     | GET_MODULE_HANDLE_EX_FLAG_PIN,
626                                     handlersym.sym, &handle);
627
628             if (!ret)
629                 return 0;
630         }
631 # else
632         /*
633          * Deliberately leak a reference to the handler. This will force the
634          * library/code containing the handler to remain loaded until we run the
635          * atexit handler. If -znodelete has been used then this is
636          * unneccessary.
637          */
638         {
639             DSO *dso = NULL;
640
641             dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
642             DSO_free(dso);
643         }
644 # endif
645     }
646 #endif
647
648     newhand = OPENSSL_malloc(sizeof(*newhand));
649     if (newhand == NULL)
650         return 0;
651
652     newhand->handler = handler;
653     newhand->next = stop_handlers;
654     stop_handlers = newhand;
655
656     return 1;
657 }