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