1 | /* |
2 | Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file |
3 | |
4 | This file is part of libzmq, the ZeroMQ core engine in C++. |
5 | |
6 | libzmq is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU Lesser General Public License (LGPL) as published |
8 | by the Free Software Foundation; either version 3 of the License, or |
9 | (at your option) any later version. |
10 | |
11 | As a special exception, the Contributors give you permission to link |
12 | this library with independent modules to produce an executable, |
13 | regardless of the license terms of these independent modules, and to |
14 | copy and distribute the resulting executable under terms of your choice, |
15 | provided that you also meet, for each linked independent module, the |
16 | terms and conditions of the license of that module. An independent |
17 | module is a module which is not derived from or based on this library. |
18 | If you modify this library, you must extend this exception to your |
19 | version of the library. |
20 | |
21 | libzmq is distributed in the hope that it will be useful, but WITHOUT |
22 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
24 | License for more details. |
25 | |
26 | You should have received a copy of the GNU Lesser General Public License |
27 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
28 | */ |
29 | |
30 | #include "precompiled.hpp" |
31 | #include <stdlib.h> |
32 | |
33 | #if !defined ZMQ_HAVE_WINDOWS |
34 | #include <unistd.h> |
35 | #endif |
36 | |
37 | #include "random.hpp" |
38 | #include "stdint.hpp" |
39 | #include "clock.hpp" |
40 | #include "mutex.hpp" |
41 | #include "macros.hpp" |
42 | |
43 | #if defined(ZMQ_USE_TWEETNACL) |
44 | #include "tweetnacl.h" |
45 | #elif defined(ZMQ_USE_LIBSODIUM) |
46 | #include "sodium.h" |
47 | #endif |
48 | |
49 | void zmq::seed_random () |
50 | { |
51 | #if defined ZMQ_HAVE_WINDOWS |
52 | int pid = static_cast<int> (GetCurrentProcessId ()); |
53 | #else |
54 | int pid = static_cast<int> (getpid ()); |
55 | #endif |
56 | srand (static_cast<unsigned int> (clock_t::now_us () + pid)); |
57 | } |
58 | |
59 | uint32_t zmq::generate_random () |
60 | { |
61 | // Compensate for the fact that rand() returns signed integer. |
62 | uint32_t low = static_cast<uint32_t> (rand ()); |
63 | uint32_t high = static_cast<uint32_t> (rand ()); |
64 | high <<= (sizeof (int) * 8 - 1); |
65 | return high | low; |
66 | } |
67 | |
68 | // When different threads have their own context the file descriptor |
69 | // variable is shared and is subject to race conditions in tweetnacl, |
70 | // that lead to file descriptors leaks. In long-running programs with |
71 | // ephemeral threads this is a problem as it accumulates. |
72 | // thread-local storage cannot be used to initialise the file descriptor |
73 | // as it is perfectly legal to share a context among many threads, each |
74 | // of which might call curve APIs. |
75 | // Also libsodium documentation specifically states that sodium_init |
76 | // must not be called concurrently from multiple threads, for the |
77 | // same reason. Inspecting the code also reveals that the close API is |
78 | // not thread safe. |
79 | // The context class cannot be used with static variables as the curve |
80 | // utility APIs like zmq_curve_keypair also call into the crypto |
81 | // library. |
82 | // The safest solution for all use cases therefore is to have a |
83 | // static lock to serialize calls into an initialiser and a finaliser, |
84 | // using refcounts to make sure that a thread does not close the library |
85 | // while another is still using it. To avoid the static initialization |
86 | // order fiasco, this is done using function-local statics, if the |
87 | // compiler implementation supports thread-safe initialization of those. |
88 | // Otherwise, we fall back to global statics. |
89 | // HOWEVER, this initialisation code imposes ordering constraints, which |
90 | // are not obvious to users of libzmq, and may lead to problems if atexit |
91 | // or similar methods are used for cleanup. |
92 | // In that case, a strict ordering is imposed whereas the contexts MUST |
93 | // be initialised BEFORE registering the cleanup with atexit. CZMQ is an |
94 | // example. Hence we make the choice to restrict this global transition |
95 | // mechanism ONLY to Tweenacl + *NIX (when using /dev/urandom) as it is |
96 | // the less risky option. |
97 | |
98 | // TODO if there is some other user of libsodium besides libzmq, this must |
99 | // be synchronized by the application. This should probably also be |
100 | // configurable via config.h |
101 | |
102 | // TODO this should probably be done via config.h |
103 | #if __cplusplus >= 201103L \ |
104 | || (defined(__cpp_threadsafe_static_init) \ |
105 | && __cpp_threadsafe_static_init >= 200806) \ |
106 | || (defined(_MSC_VER) && _MSC_VER >= 1900) |
107 | #define ZMQ_HAVE_THREADSAFE_STATIC_LOCAL_INIT 1 |
108 | // TODO this might probably also be set if a sufficiently recent gcc is used |
109 | // without -fno-threadsafe-statics, but this cannot be determined at |
110 | // compile-time, so it must be set via config.h |
111 | #else |
112 | #define ZMQ_HAVE_THREADSAFE_STATIC_LOCAL_INIT 0 |
113 | #endif |
114 | |
115 | #if !ZMQ_HAVE_THREADSAFE_STATIC_LOCAL_INIT \ |
116 | && (defined(ZMQ_USE_TWEETNACL) && !defined(ZMQ_HAVE_WINDOWS) \ |
117 | && !defined(ZMQ_HAVE_GETRANDOM)) |
118 | static unsigned int random_refcount = 0; |
119 | static zmq::mutex_t random_sync; |
120 | #endif |
121 | |
122 | static void manage_random (bool init_) |
123 | { |
124 | #if defined(ZMQ_USE_TWEETNACL) && !defined(ZMQ_HAVE_WINDOWS) \ |
125 | && !defined(ZMQ_HAVE_GETRANDOM) |
126 | |
127 | #if ZMQ_HAVE_THREADSAFE_STATIC_LOCAL_INIT |
128 | static int random_refcount = 0; |
129 | static zmq::mutex_t random_sync; |
130 | #endif |
131 | |
132 | if (init_) { |
133 | zmq::scoped_lock_t locker (random_sync); |
134 | |
135 | if (random_refcount == 0) { |
136 | int rc = sodium_init (); |
137 | zmq_assert (rc != -1); |
138 | } |
139 | |
140 | ++random_refcount; |
141 | } else { |
142 | zmq::scoped_lock_t locker (random_sync); |
143 | --random_refcount; |
144 | |
145 | if (random_refcount == 0) { |
146 | randombytes_close (); |
147 | } |
148 | } |
149 | |
150 | #elif defined(ZMQ_USE_LIBSODIUM) |
151 | if (init_) { |
152 | int rc = sodium_init (); |
153 | zmq_assert (rc != -1); |
154 | } else { |
155 | randombytes_close (); |
156 | } |
157 | #else |
158 | LIBZMQ_UNUSED (init_); |
159 | #endif |
160 | } |
161 | |
162 | void zmq::random_open () |
163 | { |
164 | manage_random (true); |
165 | } |
166 | |
167 | void zmq::random_close () |
168 | { |
169 | manage_random (false); |
170 | } |
171 | |