Bitcoin Core  0.19.99
P2P Digital Currency
randomenv.cpp
Go to the documentation of this file.
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)
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 <util/time.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
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 
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 is 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 
113 template<typename T>
114 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  hasher.Write((const unsigned char*)&data, sizeof(data));
120  return hasher;
121 }
122 
123 #ifndef WIN32
124 void AddSockaddr(CSHA512& hasher, const struct sockaddr *addr)
125 {
126  if (addr == nullptr) return;
127  switch (addr->sa_family) {
128  case AF_INET:
129  hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in));
130  break;
131  case AF_INET6:
132  hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in6));
133  break;
134  default:
135  hasher.Write((const unsigned char*)&addr->sa_family, sizeof(addr->sa_family));
136  }
137 }
138 
139 void AddFile(CSHA512& hasher, const char *path)
140 {
141  struct stat sb = {};
142  int f = open(path, O_RDONLY);
143  size_t total = 0;
144  if (f != -1) {
145  unsigned char fbuf[4096];
146  int n;
147  hasher.Write((const unsigned char*)&f, sizeof(f));
148  if (fstat(f, &sb) == 0) hasher << sb;
149  do {
150  n = read(f, fbuf, sizeof(fbuf));
151  if (n > 0) hasher.Write(fbuf, n);
152  total += n;
153  /* not bothering with EINTR handling. */
154  } while (n == sizeof(fbuf) && total < 1048576); // Read only the first 1 Mbyte
155  close(f);
156  }
157 }
158 
159 void AddPath(CSHA512& hasher, const char *path)
160 {
161  struct stat sb = {};
162  if (stat(path, &sb) == 0) {
163  hasher.Write((const unsigned char*)path, strlen(path) + 1);
164  hasher << sb;
165  }
166 }
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 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  GetCPUID(leaf, subleaf, ax, bx, cx, dx);
191  hasher << leaf << subleaf << ax << bx << cx << dx;
192 }
193 
194 void AddAllCPUID(CSHA512& hasher)
195 {
196  uint32_t ax, bx, cx, dx;
197  // Iterate over all standard leaves
198  AddCPUID(hasher, 0, 0, ax, bx, cx, dx); // Returns max leaf in ax
199  uint32_t max = ax;
200  for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
201  uint32_t maxsub = 0;
202  for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
203  AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
204  // Iterate subleafs for leaf values 4, 7, 11, 13
205  if (leaf == 4) {
206  if ((ax & 0x1f) == 0) break;
207  } else if (leaf == 7) {
208  if (subleaf == 0) maxsub = ax;
209  if (subleaf == maxsub) break;
210  } else if (leaf == 11) {
211  if ((cx & 0xff00) == 0) break;
212  } else if (leaf == 13) {
213  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  AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx); // Returns max extended leaf in ax
222  uint32_t ext_max = ax;
223  for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
224  AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
225  }
226 }
227 #endif
228 } // namespace
229 
231 {
232  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  struct timespec ts = {};
243 # ifdef CLOCK_MONOTONIC
244  clock_gettime(CLOCK_MONOTONIC, &ts);
245  hasher << ts;
246 # endif
247 # ifdef CLOCK_REALTIME
248  clock_gettime(CLOCK_REALTIME, &ts);
249  hasher << ts;
250 # endif
251 # ifdef CLOCK_BOOTTIME
252  clock_gettime(CLOCK_BOOTTIME, &ts);
253  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  struct timeval tv = {};
269  gettimeofday(&tv, nullptr);
270  hasher << tv;
271 #endif
272  // Probably redundant, but also use all the clocks C++11 provides:
273  hasher << std::chrono::system_clock::now().time_since_epoch().count();
274  hasher << std::chrono::steady_clock::now().time_since_epoch().count();
275  hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
276 
277 #ifndef WIN32
278  // Current resource usage.
279  struct rusage usage = {};
280  if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
281 #endif
282 
283 #ifdef __linux__
284  AddFile(hasher, "/proc/diskstats");
285  AddFile(hasher, "/proc/vmstat");
286  AddFile(hasher, "/proc/schedstat");
287  AddFile(hasher, "/proc/zoneinfo");
288  AddFile(hasher, "/proc/meminfo");
289  AddFile(hasher, "/proc/softirqs");
290  AddFile(hasher, "/proc/stat");
291  AddFile(hasher, "/proc/self/schedstat");
292  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  void* addr = malloc(4097);
321  hasher << &addr << addr;
322  free(addr);
323 }
324 
326 {
327  // Some compile-time static properties
328  hasher << (CHAR_MIN < 0) << sizeof(void*) << sizeof(long) << sizeof(int);
329 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
330  hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
331 #endif
332 #ifdef _MSC_VER
333  hasher << _MSC_VER;
334 #endif
335  hasher << __cplusplus;
336 #ifdef _XOPEN_VERSION
337  hasher << _XOPEN_VERSION;
338 #endif
339 #ifdef __VERSION__
340  const char* COMPILER_VERSION = __VERSION__;
341  hasher.Write((const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
342 #endif
343 
344  // Bitcoin client version
345  hasher << CLIENT_VERSION;
346 
347 #ifdef __linux__
348  // Information available through getauxval()
349 # ifdef AT_HWCAP
350  hasher << getauxval(AT_HWCAP);
351 # endif
352 # ifdef AT_HWCAP2
353  hasher << getauxval(AT_HWCAP2);
354 # endif
355 # ifdef AT_RANDOM
356  const unsigned char* random_aux = (const unsigned char*)getauxval(AT_RANDOM);
357  if (random_aux) hasher.Write(random_aux, 16);
358 # endif
359 # ifdef AT_PLATFORM
360  const char* platform_str = (const char*)getauxval(AT_PLATFORM);
361  if (platform_str) hasher.Write((const unsigned char*)platform_str, strlen(platform_str) + 1);
362 # endif
363 # ifdef AT_EXECFN
364  const char* exec_str = (const char*)getauxval(AT_EXECFN);
365  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  AddAllCPUID(hasher);
371 #endif
372 
373  // Memory locations
374  hasher << &hasher << &RandAddStaticEnv << &malloc << &errno << &environ;
375 
376  // Hostname
377  char hname[256];
378  if (gethostname(hname, 256) == 0) {
379  hasher.Write((const unsigned char*)hname, strnlen(hname, 256));
380  }
381 
382 #if HAVE_DECL_GETIFADDRS
383  // Network interfaces
384  struct ifaddrs *ifad = NULL;
385  getifaddrs(&ifad);
386  struct ifaddrs *ifit = ifad;
387  while (ifit != NULL) {
388  hasher.Write((const unsigned char*)&ifit, sizeof(ifit));
389  hasher.Write((const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
390  hasher.Write((const unsigned char*)&ifit->ifa_flags, sizeof(ifit->ifa_flags));
391  AddSockaddr(hasher, ifit->ifa_addr);
392  AddSockaddr(hasher, ifit->ifa_netmask);
393  AddSockaddr(hasher, ifit->ifa_dstaddr);
394  ifit = ifit->ifa_next;
395  }
396  freeifaddrs(ifad);
397 #endif
398 
399 #ifndef WIN32
400  // UNIX kernel information
401  struct utsname name;
402  if (uname(&name) != -1) {
403  hasher.Write((const unsigned char*)&name.sysname, strlen(name.sysname) + 1);
404  hasher.Write((const unsigned char*)&name.nodename, strlen(name.nodename) + 1);
405  hasher.Write((const unsigned char*)&name.release, strlen(name.release) + 1);
406  hasher.Write((const unsigned char*)&name.version, strlen(name.version) + 1);
407  hasher.Write((const unsigned char*)&name.machine, strlen(name.machine) + 1);
408  }
409 
410  /* Path and filesystem provided data */
411  AddPath(hasher, "/");
412  AddPath(hasher, ".");
413  AddPath(hasher, "/tmp");
414  AddPath(hasher, "/home");
415  AddPath(hasher, "/proc");
416 #ifdef __linux__
417  AddFile(hasher, "/proc/cmdline");
418  AddFile(hasher, "/proc/cpuinfo");
419  AddFile(hasher, "/proc/version");
420 #endif
421  AddFile(hasher, "/etc/passwd");
422  AddFile(hasher, "/etc/group");
423  AddFile(hasher, "/etc/hosts");
424  AddFile(hasher, "/etc/resolv.conf");
425  AddFile(hasher, "/etc/timezone");
426  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  if (environ) {
506  for (size_t i = 0; environ[i]; ++i) {
507  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  hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
516 #endif
517  hasher << std::this_thread::get_id();
518 }
char ** environ
Necessary on some platforms.
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
Definition: cleanse.cpp:14
size_t strnlen(const char *start, size_t max_len)
Definition: strnlen.cpp:12
CSHA512 & Write(const unsigned char *data, size_t len)
Definition: sha512.cpp:159
A hasher class for SHA-512.
Definition: sha512.h:12
void RandAddStaticEnv(CSHA512 &hasher)
Gather non-cryptographic environment data that does not change over time.
Definition: randomenv.cpp:325
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:38
void RandAddDynamicEnv(CSHA512 &hasher)
Gather non-cryptographic environment data that changes over time.
Definition: randomenv.cpp:230