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