Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2022 The Bitcoin Core developers
3 : // Distributed under the MIT software license, see the accompanying
4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 :
6 : #if defined(HAVE_CONFIG_H)
7 : #include <config/bitcoin-config.h>
8 : #endif
9 :
10 : #include <randomenv.h>
11 :
12 : #include <clientversion.h>
13 : #include <compat/compat.h>
14 : #include <compat/cpuid.h>
15 : #include <crypto/sha512.h>
16 : #include <support/cleanse.h>
17 : #include <util/time.h>
18 :
19 : #include <algorithm>
20 : #include <atomic>
21 : #include <cstdint>
22 : #include <cstring>
23 : #include <chrono>
24 : #include <climits>
25 : #include <thread>
26 : #include <vector>
27 :
28 : #include <sys/types.h> // must go before a number of other headers
29 :
30 : #ifdef WIN32
31 : #include <windows.h>
32 : #include <winreg.h>
33 : #else
34 : #include <fcntl.h>
35 : #include <netinet/in.h>
36 : #include <sys/resource.h>
37 : #include <sys/socket.h>
38 : #include <sys/stat.h>
39 : #include <sys/time.h>
40 : #include <sys/utsname.h>
41 : #include <unistd.h>
42 : #endif
43 : #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
44 : #include <ifaddrs.h>
45 : #endif
46 : #if HAVE_SYSCTL
47 : #include <sys/sysctl.h>
48 : #if HAVE_VM_VM_PARAM_H
49 : #include <vm/vm_param.h>
50 : #endif
51 : #if HAVE_SYS_RESOURCES_H
52 : #include <sys/resources.h>
53 : #endif
54 : #if HAVE_SYS_VMMETER_H
55 : #include <sys/vmmeter.h>
56 : #endif
57 : #endif
58 : #if defined(HAVE_STRONG_GETAUXVAL)
59 : #include <sys/auxv.h>
60 : #endif
61 :
62 : extern char** environ; // NOLINT(readability-redundant-declaration): Necessary on some platforms
63 :
64 : namespace {
65 :
66 2 : void RandAddSeedPerfmon(CSHA512& hasher)
67 : {
68 : #ifdef WIN32
69 : // Seed with the entire set of perfmon data
70 :
71 : // This can take up to 2 seconds, so only do it every 10 minutes.
72 : // Initialize last_perfmon to 0 seconds, we don't skip the first call.
73 : static std::atomic<std::chrono::seconds> last_perfmon{0s};
74 : auto last_time = last_perfmon.load();
75 : auto current_time = GetTime<std::chrono::seconds>();
76 : if (current_time < last_time + std::chrono::minutes{10}) return;
77 : last_perfmon = current_time;
78 :
79 : std::vector<unsigned char> vData(250000, 0);
80 : long ret = 0;
81 : unsigned long nSize = 0;
82 : const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
83 : while (true) {
84 : nSize = vData.size();
85 : ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize);
86 : if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
87 : break;
88 : vData.resize(std::min((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
89 : }
90 : RegCloseKey(HKEY_PERFORMANCE_DATA);
91 : if (ret == ERROR_SUCCESS) {
92 : hasher.Write(vData.data(), nSize);
93 : memory_cleanse(vData.data(), nSize);
94 : } else {
95 : // Performance data is only a best-effort attempt at improving the
96 : // situation when the OS randomness (and other sources) aren't
97 : // adequate. As a result, failure to read it is isn't considered critical,
98 : // so we don't call RandFailure().
99 : // TODO: Add logging when the logger is made functional before global
100 : // constructors have been invoked.
101 : }
102 : #endif
103 2 : }
104 :
105 : /** Helper to easily feed data into a CSHA512.
106 : *
107 : * Note that this does not serialize the passed object (like stream.h's << operators do).
108 : * Its raw memory representation is used directly.
109 : */
110 : template<typename T>
111 778 : CSHA512& operator<<(CSHA512& hasher, const T& data) {
112 : static_assert(!std::is_same<typename std::decay<T>::type, char*>::value, "Calling operator<<(CSHA512, char*) is probably not what you want");
113 : static_assert(!std::is_same<typename std::decay<T>::type, unsigned char*>::value, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
114 : static_assert(!std::is_same<typename std::decay<T>::type, const char*>::value, "Calling operator<<(CSHA512, const char*) is probably not what you want");
115 : static_assert(!std::is_same<typename std::decay<T>::type, const unsigned char*>::value, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want");
116 778 : hasher.Write((const unsigned char*)&data, sizeof(data));
117 778 : return hasher;
118 : }
119 :
120 : #ifndef WIN32
121 78 : void AddSockaddr(CSHA512& hasher, const struct sockaddr *addr)
122 : {
123 78 : if (addr == nullptr) return;
124 58 : switch (addr->sa_family) {
125 : case AF_INET:
126 18 : hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in));
127 18 : break;
128 : case AF_INET6:
129 20 : hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in6));
130 20 : break;
131 : default:
132 20 : hasher.Write((const unsigned char*)&addr->sa_family, sizeof(addr->sa_family));
133 20 : }
134 78 : }
135 :
136 36 : void AddFile(CSHA512& hasher, const char *path)
137 : {
138 36 : struct stat sb = {};
139 36 : int f = open(path, O_RDONLY);
140 36 : size_t total = 0;
141 36 : if (f != -1) {
142 : unsigned char fbuf[4096];
143 : int n;
144 36 : hasher.Write((const unsigned char*)&f, sizeof(f));
145 36 : if (fstat(f, &sb) == 0) hasher << sb;
146 36 : do {
147 54 : n = read(f, fbuf, sizeof(fbuf));
148 54 : if (n > 0) hasher.Write(fbuf, n);
149 54 : total += n;
150 : /* not bothering with EINTR handling. */
151 54 : } while (n == sizeof(fbuf) && total < 1048576); // Read only the first 1 Mbyte
152 36 : close(f);
153 36 : }
154 36 : }
155 :
156 10 : void AddPath(CSHA512& hasher, const char *path)
157 : {
158 10 : struct stat sb = {};
159 10 : if (stat(path, &sb) == 0) {
160 10 : hasher.Write((const unsigned char*)path, strlen(path) + 1);
161 10 : hasher << sb;
162 10 : }
163 10 : }
164 : #endif
165 :
166 : #if HAVE_SYSCTL
167 : template<int... S>
168 : void AddSysctl(CSHA512& hasher)
169 : {
170 : int CTL[sizeof...(S)] = {S...};
171 : unsigned char buffer[65536];
172 : size_t siz = 65536;
173 : int ret = sysctl(CTL, sizeof...(S), buffer, &siz, nullptr, 0);
174 : if (ret == 0 || (ret == -1 && errno == ENOMEM)) {
175 : hasher << sizeof(CTL);
176 : hasher.Write((const unsigned char*)CTL, sizeof(CTL));
177 : if (siz > sizeof(buffer)) siz = sizeof(buffer);
178 : hasher << siz;
179 : hasher.Write(buffer, siz);
180 : }
181 : }
182 : #endif
183 :
184 : #ifdef HAVE_GETCPUID
185 110 : void inline AddCPUID(CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx)
186 : {
187 110 : GetCPUID(leaf, subleaf, ax, bx, cx, dx);
188 110 : hasher << leaf << subleaf << ax << bx << cx << dx;
189 110 : }
190 :
191 2 : void AddAllCPUID(CSHA512& hasher)
192 : {
193 : uint32_t ax, bx, cx, dx;
194 : // Iterate over all standard leaves
195 2 : AddCPUID(hasher, 0, 0, ax, bx, cx, dx); // Returns max leaf in ax
196 2 : uint32_t max = ax;
197 34 : for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
198 32 : uint32_t maxsub = 0;
199 42 : for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
200 42 : AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
201 : // Iterate subleafs for leaf values 4, 7, 11, 13
202 42 : if (leaf == 4) {
203 2 : if ((ax & 0x1f) == 0) break;
204 40 : } else if (leaf == 7) {
205 2 : if (subleaf == 0) maxsub = ax;
206 2 : if (subleaf == maxsub) break;
207 38 : } else if (leaf == 11) {
208 6 : if ((cx & 0xff00) == 0) break;
209 36 : } else if (leaf == 13) {
210 8 : if (ax == 0 && bx == 0 && cx == 0 && dx == 0) break;
211 6 : } else {
212 : // For any other leaf, stop after subleaf 0.
213 24 : break;
214 : }
215 10 : }
216 32 : }
217 : // Iterate over all extended leaves
218 2 : AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx); // Returns max extended leaf in ax
219 2 : uint32_t ext_max = ax;
220 66 : for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
221 64 : AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
222 64 : }
223 2 : }
224 : #endif
225 : } // namespace
226 :
227 2 : void RandAddDynamicEnv(CSHA512& hasher)
228 : {
229 2 : RandAddSeedPerfmon(hasher);
230 :
231 : // Various clocks
232 : #ifdef WIN32
233 : FILETIME ftime;
234 : GetSystemTimeAsFileTime(&ftime);
235 : hasher << ftime;
236 : #else
237 2 : struct timespec ts = {};
238 : # ifdef CLOCK_MONOTONIC
239 2 : clock_gettime(CLOCK_MONOTONIC, &ts);
240 2 : hasher << ts;
241 : # endif
242 : # ifdef CLOCK_REALTIME
243 2 : clock_gettime(CLOCK_REALTIME, &ts);
244 2 : hasher << ts;
245 : # endif
246 : # ifdef CLOCK_BOOTTIME
247 2 : clock_gettime(CLOCK_BOOTTIME, &ts);
248 2 : hasher << ts;
249 : # endif
250 : // gettimeofday is available on all UNIX systems, but only has microsecond precision.
251 2 : struct timeval tv = {};
252 2 : gettimeofday(&tv, nullptr);
253 2 : hasher << tv;
254 : #endif
255 : // Probably redundant, but also use all the standard library clocks:
256 2 : hasher << std::chrono::system_clock::now().time_since_epoch().count();
257 2 : hasher << std::chrono::steady_clock::now().time_since_epoch().count();
258 2 : hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
259 :
260 : #ifndef WIN32
261 : // Current resource usage.
262 2 : struct rusage usage = {};
263 2 : if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
264 : #endif
265 :
266 : #ifdef __linux__
267 2 : AddFile(hasher, "/proc/diskstats");
268 2 : AddFile(hasher, "/proc/vmstat");
269 2 : AddFile(hasher, "/proc/schedstat");
270 2 : AddFile(hasher, "/proc/zoneinfo");
271 2 : AddFile(hasher, "/proc/meminfo");
272 2 : AddFile(hasher, "/proc/softirqs");
273 2 : AddFile(hasher, "/proc/stat");
274 2 : AddFile(hasher, "/proc/self/schedstat");
275 2 : AddFile(hasher, "/proc/self/status");
276 : #endif
277 :
278 : #if HAVE_SYSCTL
279 : # ifdef CTL_KERN
280 : # if defined(KERN_PROC) && defined(KERN_PROC_ALL)
281 : AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
282 : # endif
283 : # endif
284 : # ifdef CTL_HW
285 : # ifdef HW_DISKSTATS
286 : AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
287 : # endif
288 : # endif
289 : # ifdef CTL_VM
290 : # ifdef VM_LOADAVG
291 : AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
292 : # endif
293 : # ifdef VM_TOTAL
294 : AddSysctl<CTL_VM, VM_TOTAL>(hasher);
295 : # endif
296 : # ifdef VM_METER
297 : AddSysctl<CTL_VM, VM_METER>(hasher);
298 : # endif
299 : # endif
300 : #endif
301 :
302 : // Stack and heap location
303 2 : void* addr = malloc(4097);
304 2 : hasher << &addr << addr;
305 2 : free(addr);
306 2 : }
307 :
308 2 : void RandAddStaticEnv(CSHA512& hasher)
309 : {
310 : // Some compile-time static properties
311 2 : hasher << (CHAR_MIN < 0) << sizeof(void*) << sizeof(long) << sizeof(int);
312 : #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
313 2 : hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
314 : #endif
315 : #ifdef _MSC_VER
316 : hasher << _MSC_VER;
317 : #endif
318 2 : hasher << __cplusplus;
319 : #ifdef _XOPEN_VERSION
320 2 : hasher << _XOPEN_VERSION;
321 : #endif
322 : #ifdef __VERSION__
323 2 : const char* COMPILER_VERSION = __VERSION__;
324 2 : hasher.Write((const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
325 : #endif
326 :
327 : // Bitcoin client version
328 2 : hasher << CLIENT_VERSION;
329 :
330 : #if defined(HAVE_STRONG_GETAUXVAL)
331 : // Information available through getauxval()
332 : # ifdef AT_HWCAP
333 2 : hasher << getauxval(AT_HWCAP);
334 : # endif
335 : # ifdef AT_HWCAP2
336 2 : hasher << getauxval(AT_HWCAP2);
337 : # endif
338 : # ifdef AT_RANDOM
339 2 : const unsigned char* random_aux = (const unsigned char*)getauxval(AT_RANDOM);
340 2 : if (random_aux) hasher.Write(random_aux, 16);
341 : # endif
342 : # ifdef AT_PLATFORM
343 2 : const char* platform_str = (const char*)getauxval(AT_PLATFORM);
344 2 : if (platform_str) hasher.Write((const unsigned char*)platform_str, strlen(platform_str) + 1);
345 : # endif
346 : # ifdef AT_EXECFN
347 2 : const char* exec_str = (const char*)getauxval(AT_EXECFN);
348 2 : if (exec_str) hasher.Write((const unsigned char*)exec_str, strlen(exec_str) + 1);
349 : # endif
350 : #endif // HAVE_STRONG_GETAUXVAL
351 :
352 : #ifdef HAVE_GETCPUID
353 2 : AddAllCPUID(hasher);
354 : #endif
355 :
356 : // Memory locations
357 2 : hasher << &hasher << &RandAddStaticEnv << &malloc << &errno << &environ;
358 :
359 : // Hostname
360 : char hname[256];
361 2 : if (gethostname(hname, 256) == 0) {
362 2 : hasher.Write((const unsigned char*)hname, strnlen(hname, 256));
363 2 : }
364 :
365 : #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
366 : // Network interfaces
367 2 : struct ifaddrs *ifad = nullptr;
368 2 : getifaddrs(&ifad);
369 2 : struct ifaddrs *ifit = ifad;
370 28 : while (ifit != nullptr) {
371 26 : hasher.Write((const unsigned char*)&ifit, sizeof(ifit));
372 26 : hasher.Write((const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
373 26 : hasher.Write((const unsigned char*)&ifit->ifa_flags, sizeof(ifit->ifa_flags));
374 26 : AddSockaddr(hasher, ifit->ifa_addr);
375 26 : AddSockaddr(hasher, ifit->ifa_netmask);
376 26 : AddSockaddr(hasher, ifit->ifa_dstaddr);
377 26 : ifit = ifit->ifa_next;
378 : }
379 2 : freeifaddrs(ifad);
380 : #endif
381 :
382 : #ifndef WIN32
383 : // UNIX kernel information
384 : struct utsname name;
385 2 : if (uname(&name) != -1) {
386 2 : hasher.Write((const unsigned char*)&name.sysname, strlen(name.sysname) + 1);
387 2 : hasher.Write((const unsigned char*)&name.nodename, strlen(name.nodename) + 1);
388 2 : hasher.Write((const unsigned char*)&name.release, strlen(name.release) + 1);
389 2 : hasher.Write((const unsigned char*)&name.version, strlen(name.version) + 1);
390 2 : hasher.Write((const unsigned char*)&name.machine, strlen(name.machine) + 1);
391 2 : }
392 :
393 : /* Path and filesystem provided data */
394 2 : AddPath(hasher, "/");
395 2 : AddPath(hasher, ".");
396 2 : AddPath(hasher, "/tmp");
397 2 : AddPath(hasher, "/home");
398 2 : AddPath(hasher, "/proc");
399 : #ifdef __linux__
400 2 : AddFile(hasher, "/proc/cmdline");
401 2 : AddFile(hasher, "/proc/cpuinfo");
402 2 : AddFile(hasher, "/proc/version");
403 : #endif
404 2 : AddFile(hasher, "/etc/passwd");
405 2 : AddFile(hasher, "/etc/group");
406 2 : AddFile(hasher, "/etc/hosts");
407 2 : AddFile(hasher, "/etc/resolv.conf");
408 2 : AddFile(hasher, "/etc/timezone");
409 2 : AddFile(hasher, "/etc/localtime");
410 : #endif
411 :
412 : // For MacOS/BSDs, gather data through sysctl instead of /proc. Not all of these
413 : // will exist on every system.
414 : #if HAVE_SYSCTL
415 : # ifdef CTL_HW
416 : # ifdef HW_MACHINE
417 : AddSysctl<CTL_HW, HW_MACHINE>(hasher);
418 : # endif
419 : # ifdef HW_MODEL
420 : AddSysctl<CTL_HW, HW_MODEL>(hasher);
421 : # endif
422 : # ifdef HW_NCPU
423 : AddSysctl<CTL_HW, HW_NCPU>(hasher);
424 : # endif
425 : # ifdef HW_PHYSMEM
426 : AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
427 : # endif
428 : # ifdef HW_USERMEM
429 : AddSysctl<CTL_HW, HW_USERMEM>(hasher);
430 : # endif
431 : # ifdef HW_MACHINE_ARCH
432 : AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
433 : # endif
434 : # ifdef HW_REALMEM
435 : AddSysctl<CTL_HW, HW_REALMEM>(hasher);
436 : # endif
437 : # ifdef HW_CPU_FREQ
438 : AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
439 : # endif
440 : # ifdef HW_BUS_FREQ
441 : AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
442 : # endif
443 : # ifdef HW_CACHELINE
444 : AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
445 : # endif
446 : # endif
447 : # ifdef CTL_KERN
448 : # ifdef KERN_BOOTFILE
449 : AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
450 : # endif
451 : # ifdef KERN_BOOTTIME
452 : AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
453 : # endif
454 : # ifdef KERN_CLOCKRATE
455 : AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
456 : # endif
457 : # ifdef KERN_HOSTID
458 : AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
459 : # endif
460 : # ifdef KERN_HOSTUUID
461 : AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
462 : # endif
463 : # ifdef KERN_HOSTNAME
464 : AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
465 : # endif
466 : # ifdef KERN_OSRELDATE
467 : AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
468 : # endif
469 : # ifdef KERN_OSRELEASE
470 : AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
471 : # endif
472 : # ifdef KERN_OSREV
473 : AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
474 : # endif
475 : # ifdef KERN_OSTYPE
476 : AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
477 : # endif
478 : # ifdef KERN_POSIX1
479 : AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
480 : # endif
481 : # ifdef KERN_VERSION
482 : AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
483 : # endif
484 : # endif
485 : #endif
486 :
487 : // Env variables
488 2 : if (environ) {
489 6 : for (size_t i = 0; environ[i]; ++i) {
490 4 : hasher.Write((const unsigned char*)environ[i], strlen(environ[i]));
491 4 : }
492 2 : }
493 :
494 : // Process, thread, user, session, group, ... ids.
495 : #ifdef WIN32
496 : hasher << GetCurrentProcessId() << GetCurrentThreadId();
497 : #else
498 2 : hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
499 : #endif
500 2 : hasher << std::this_thread::get_id();
501 2 : }
|