fuzz coverage

Coverage Report

Created: 2026-05-08 05:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/randomenv.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present 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
#include <bitcoin-build-config.h> // IWYU pragma: keep
7
8
#include <randomenv.h>
9
10
#include <clientversion.h>
11
#include <compat/compat.h>
12
#include <compat/cpuid.h>
13
#include <crypto/sha512.h>
14
#include <span.h>
15
#include <support/cleanse.h>
16
#include <util/byte_units.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
#else
33
#include <fcntl.h>
34
#include <netinet/in.h>
35
#include <sys/resource.h>
36
#include <sys/socket.h>
37
#include <sys/stat.h>
38
#include <sys/time.h>
39
#include <sys/utsname.h>
40
#include <unistd.h>
41
#endif
42
#ifdef HAVE_IFADDRS
43
#include <ifaddrs.h>
44
#endif
45
#ifdef HAVE_SYSCTL
46
#include <sys/sysctl.h>
47
#if __has_include(<vm/vm_param.h>)
48
#include <vm/vm_param.h>
49
#endif
50
#if __has_include(<sys/resources.h>)
51
#include <sys/resources.h>
52
#endif
53
#if __has_include(<sys/vmmeter.h>)
54
#include <sys/vmmeter.h>
55
#endif
56
#endif
57
#if defined(HAVE_STRONG_GETAUXVAL)
58
#include <sys/auxv.h>
59
#endif
60
61
#if defined(__APPLE__) || \
62
    defined(__FreeBSD__) || \
63
    defined(__NetBSD__) || \
64
    defined(__OpenBSD__) || \
65
    defined(__illumos__)
66
extern char** environ; // Necessary on the above platforms
67
#endif
68
69
namespace {
70
71
/** Helper to easily feed data into a CSHA512.
72
 *
73
 * Note that this does not serialize the passed object (like stream.h's << operators do).
74
 * Its raw memory representation is used directly.
75
 */
76
template<typename T>
77
0
CSHA512& operator<<(CSHA512& hasher, const T& data) {
78
0
    static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
79
0
    static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
80
0
    static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
81
0
    static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want");
82
0
    hasher.Write((const unsigned char*)&data, sizeof(data));
83
0
    return hasher;
84
0
}
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<stat>(CSHA512&, stat const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<unsigned int>(CSHA512&, unsigned int const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<timespec>(CSHA512&, timespec const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<timeval>(CSHA512&, timeval const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<long>(CSHA512&, long const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<rusage>(CSHA512&, rusage const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<void**>(CSHA512&, void** const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<void*>(CSHA512&, void* const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<bool>(CSHA512&, bool const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<unsigned long>(CSHA512&, unsigned long const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<int>(CSHA512&, int const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<CSHA512*>(CSHA512&, CSHA512* const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<void (*)(CSHA512&)>(CSHA512&, void (* const&)(CSHA512&))
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<void* (*)(unsigned long) noexcept>(CSHA512&, void* (* const&)(unsigned long) noexcept)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<int*>(CSHA512&, int* const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<char***>(CSHA512&, char*** const&)
Unexecuted instantiation: randomenv.cpp:CSHA512& (anonymous namespace)::operator<<<std::thread::id>(CSHA512&, std::thread::id const&)
85
86
#ifndef WIN32
87
void AddSockaddr(CSHA512& hasher, const struct sockaddr *addr)
88
0
{
89
0
    if (addr == nullptr) return;
90
0
    switch (addr->sa_family) {
91
0
    case AF_INET:
92
0
        hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in));
93
0
        break;
94
0
    case AF_INET6:
95
0
        hasher.Write((const unsigned char*)addr, sizeof(sockaddr_in6));
96
0
        break;
97
0
    default:
98
0
        hasher.Write((const unsigned char*)&addr->sa_family, sizeof(addr->sa_family));
99
0
    }
100
0
}
101
102
void AddFile(CSHA512& hasher, const char *path)
103
0
{
104
0
    struct stat sb = {};
105
0
    int f = open(path, O_RDONLY);
106
0
    size_t total = 0;
107
0
    if (f != -1) {
108
0
        unsigned char fbuf[4096];
109
0
        int n;
110
0
        hasher.Write((const unsigned char*)&f, sizeof(f));
111
0
        if (fstat(f, &sb) == 0) hasher << sb;
112
0
        do {
113
0
            n = read(f, fbuf, sizeof(fbuf));
114
0
            if (n > 0) hasher.Write(fbuf, n);
115
0
            total += n;
116
            /* not bothering with EINTR handling. */
117
0
        } while (n == sizeof(fbuf) && total < 1_MiB); // Read only the first 1 Mbyte
118
0
        close(f);
119
0
    }
120
0
}
121
122
void AddPath(CSHA512& hasher, const char *path)
123
0
{
124
0
    struct stat sb = {};
125
0
    if (stat(path, &sb) == 0) {
126
0
        hasher.Write((const unsigned char*)path, strlen(path) + 1);
127
0
        hasher << sb;
128
0
    }
129
0
}
130
#endif
131
132
#ifdef HAVE_SYSCTL
133
template<int... S>
134
void AddSysctl(CSHA512& hasher)
135
{
136
    int CTL[sizeof...(S)] = {S...};
137
    unsigned char buffer[65536];
138
    size_t siz = 65536;
139
    int ret = sysctl(CTL, sizeof...(S), buffer, &siz, nullptr, 0);
140
    if (ret == 0 || (ret == -1 && errno == ENOMEM)) {
141
        hasher << sizeof(CTL);
142
        hasher.Write((const unsigned char*)CTL, sizeof(CTL));
143
        if (siz > sizeof(buffer)) siz = sizeof(buffer);
144
        hasher << siz;
145
        hasher.Write(buffer, siz);
146
    }
147
}
148
#endif
149
150
#ifdef HAVE_GETCPUID
151
void inline AddCPUID(CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx)
152
0
{
153
0
    GetCPUID(leaf, subleaf, ax, bx, cx, dx);
154
0
    hasher << leaf << subleaf << ax << bx << cx << dx;
155
0
}
156
157
void AddAllCPUID(CSHA512& hasher)
158
0
{
159
0
    uint32_t ax, bx, cx, dx;
160
    // Iterate over all standard leaves
161
0
    AddCPUID(hasher, 0, 0, ax, bx, cx, dx); // Returns max leaf in ax
162
0
    uint32_t max = ax;
163
0
    for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
164
0
        uint32_t maxsub = 0;
165
0
        for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
166
0
            AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
167
            // Iterate subleafs for leaf values 4, 7, 11, 13
168
0
            if (leaf == 4) {
169
0
                if ((ax & 0x1f) == 0) break;
170
0
            } else if (leaf == 7) {
171
0
                if (subleaf == 0) maxsub = ax;
172
0
                if (subleaf == maxsub) break;
173
0
            } else if (leaf == 11) {
174
0
                if ((cx & 0xff00) == 0) break;
175
0
            } else if (leaf == 13) {
176
0
                if (ax == 0 && bx == 0 && cx == 0 && dx == 0) break;
177
0
            } else {
178
                // For any other leaf, stop after subleaf 0.
179
0
                break;
180
0
            }
181
0
        }
182
0
    }
183
    // Iterate over all extended leaves
184
0
    AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx); // Returns max extended leaf in ax
185
0
    uint32_t ext_max = ax;
186
0
    for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
187
0
        AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
188
0
    }
189
0
}
190
#endif
191
} // namespace
192
193
void RandAddDynamicEnv(CSHA512& hasher)
194
0
{
195
    // Various clocks
196
#ifdef WIN32
197
    FILETIME ftime;
198
    GetSystemTimeAsFileTime(&ftime);
199
    hasher << ftime;
200
#else
201
0
    struct timespec ts = {};
202
0
#    ifdef CLOCK_MONOTONIC
203
0
    clock_gettime(CLOCK_MONOTONIC, &ts);
204
0
    hasher << ts;
205
0
#    endif
206
0
#    ifdef CLOCK_REALTIME
207
0
    clock_gettime(CLOCK_REALTIME, &ts);
208
0
    hasher << ts;
209
0
#    endif
210
0
#    ifdef CLOCK_BOOTTIME
211
0
    clock_gettime(CLOCK_BOOTTIME, &ts);
212
0
    hasher << ts;
213
0
#    endif
214
    // gettimeofday is available on all UNIX systems, but only has microsecond precision.
215
0
    struct timeval tv = {};
216
0
    gettimeofday(&tv, nullptr);
217
0
    hasher << tv;
218
0
#endif
219
    // Probably redundant, but also use all the standard library clocks:
220
0
    hasher << std::chrono::system_clock::now().time_since_epoch().count();
221
0
    hasher << std::chrono::steady_clock::now().time_since_epoch().count();
222
0
    hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
223
224
0
#ifndef WIN32
225
    // Current resource usage.
226
0
    struct rusage usage = {};
227
0
    if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
228
0
#endif
229
230
0
#ifdef __linux__
231
0
    AddFile(hasher, "/proc/diskstats");
232
0
    AddFile(hasher, "/proc/vmstat");
233
0
    AddFile(hasher, "/proc/schedstat");
234
0
    AddFile(hasher, "/proc/zoneinfo");
235
0
    AddFile(hasher, "/proc/meminfo");
236
0
    AddFile(hasher, "/proc/softirqs");
237
0
    AddFile(hasher, "/proc/stat");
238
0
    AddFile(hasher, "/proc/self/schedstat");
239
0
    AddFile(hasher, "/proc/self/status");
240
0
#endif
241
242
#ifdef HAVE_SYSCTL
243
#  ifdef CTL_KERN
244
#    if defined(KERN_PROC) && defined(KERN_PROC_ALL)
245
    AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
246
#    endif
247
#  endif
248
#  ifdef CTL_HW
249
#    ifdef HW_DISKSTATS
250
    AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
251
#    endif
252
#  endif
253
#  ifdef CTL_VM
254
#    ifdef VM_LOADAVG
255
    AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
256
#    endif
257
#    ifdef VM_TOTAL
258
    AddSysctl<CTL_VM, VM_TOTAL>(hasher);
259
#    endif
260
#    ifdef VM_METER
261
    AddSysctl<CTL_VM, VM_METER>(hasher);
262
#    endif
263
#  endif
264
#endif
265
266
    // Stack and heap location
267
0
    void* addr = malloc(4097);
268
0
    hasher << &addr << addr;
269
0
    free(addr);
270
0
}
271
272
void RandAddStaticEnv(CSHA512& hasher)
273
0
{
274
    // Some compile-time static properties
275
0
    hasher << (CHAR_MIN < 0) << sizeof(void*) << sizeof(long) << sizeof(int);
276
0
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
277
0
    hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
278
0
#endif
279
#ifdef _MSC_VER
280
    hasher << _MSC_VER;
281
#endif
282
0
    hasher << __cplusplus;
283
0
#ifdef _XOPEN_VERSION
284
0
    hasher << _XOPEN_VERSION;
285
0
#endif
286
0
#ifdef __VERSION__
287
0
    const char* COMPILER_VERSION = __VERSION__;
288
0
    hasher.Write((const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
289
0
#endif
290
291
    // Bitcoin client version
292
0
    hasher << CLIENT_VERSION;
293
294
0
#if defined(HAVE_STRONG_GETAUXVAL)
295
    // Information available through getauxval()
296
0
#  ifdef AT_HWCAP
297
0
    hasher << getauxval(AT_HWCAP);
298
0
#  endif
299
0
#  ifdef AT_HWCAP2
300
0
    hasher << getauxval(AT_HWCAP2);
301
0
#  endif
302
0
#  ifdef AT_RANDOM
303
0
    const unsigned char* random_aux = (const unsigned char*)getauxval(AT_RANDOM);
304
0
    if (random_aux) hasher.Write(random_aux, 16);
305
0
#  endif
306
0
#  ifdef AT_PLATFORM
307
0
    const char* platform_str = (const char*)getauxval(AT_PLATFORM);
308
0
    if (platform_str) hasher.Write((const unsigned char*)platform_str, strlen(platform_str) + 1);
309
0
#  endif
310
0
#  ifdef AT_EXECFN
311
0
    const char* exec_str = (const char*)getauxval(AT_EXECFN);
312
0
    if (exec_str) hasher.Write((const unsigned char*)exec_str, strlen(exec_str) + 1);
313
0
#  endif
314
0
#endif // HAVE_STRONG_GETAUXVAL
315
316
0
#ifdef HAVE_GETCPUID
317
0
    AddAllCPUID(hasher);
318
0
#endif
319
320
    // Memory locations
321
0
    hasher << &hasher << &RandAddStaticEnv << &malloc << &errno << &environ;
322
323
    // Hostname
324
#ifdef WIN32
325
    constexpr DWORD max_size = MAX_COMPUTERNAME_LENGTH + 1;
326
    char hname[max_size];
327
    DWORD size = max_size;
328
    if (GetComputerNameA(hname, &size) != 0) {
329
        hasher.Write(UCharCast(hname), size);
330
    }
331
#else
332
0
    char hname[256];
333
0
    if (gethostname(hname, 256) == 0) {
334
0
        hasher.Write((const unsigned char*)hname, strnlen(hname, 256));
335
0
    }
336
0
#endif
337
338
0
#ifdef HAVE_IFADDRS
339
    // Network interfaces
340
0
    struct ifaddrs *ifad = nullptr;
341
0
    getifaddrs(&ifad);
342
0
    struct ifaddrs *ifit = ifad;
343
0
    while (ifit != nullptr) {
344
0
        hasher.Write((const unsigned char*)&ifit, sizeof(ifit));
345
0
        hasher.Write((const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
346
0
        hasher.Write((const unsigned char*)&ifit->ifa_flags, sizeof(ifit->ifa_flags));
347
0
        AddSockaddr(hasher, ifit->ifa_addr);
348
0
        AddSockaddr(hasher, ifit->ifa_netmask);
349
0
        AddSockaddr(hasher, ifit->ifa_dstaddr);
350
0
        ifit = ifit->ifa_next;
351
0
    }
352
0
    freeifaddrs(ifad);
353
0
#endif
354
355
0
#ifndef WIN32
356
    // UNIX kernel information
357
0
    struct utsname name;
358
0
    if (uname(&name) != -1) {
359
0
        hasher.Write((const unsigned char*)&name.sysname, strlen(name.sysname) + 1);
360
0
        hasher.Write((const unsigned char*)&name.nodename, strlen(name.nodename) + 1);
361
0
        hasher.Write((const unsigned char*)&name.release, strlen(name.release) + 1);
362
0
        hasher.Write((const unsigned char*)&name.version, strlen(name.version) + 1);
363
0
        hasher.Write((const unsigned char*)&name.machine, strlen(name.machine) + 1);
364
0
    }
365
366
    /* Path and filesystem provided data */
367
0
    AddPath(hasher, "/");
368
0
    AddPath(hasher, ".");
369
0
    AddPath(hasher, "/tmp");
370
0
    AddPath(hasher, "/home");
371
0
    AddPath(hasher, "/proc");
372
0
#ifdef __linux__
373
0
    AddFile(hasher, "/proc/cmdline");
374
0
    AddFile(hasher, "/proc/cpuinfo");
375
0
    AddFile(hasher, "/proc/version");
376
0
#endif
377
0
    AddFile(hasher, "/etc/passwd");
378
0
    AddFile(hasher, "/etc/group");
379
0
    AddFile(hasher, "/etc/hosts");
380
0
    AddFile(hasher, "/etc/resolv.conf");
381
0
    AddFile(hasher, "/etc/timezone");
382
0
    AddFile(hasher, "/etc/localtime");
383
0
#endif
384
385
    // For MacOS/BSDs, gather data through sysctl instead of /proc. Not all of these
386
    // will exist on every system.
387
#ifdef HAVE_SYSCTL
388
#  ifdef CTL_HW
389
#    ifdef HW_MACHINE
390
    AddSysctl<CTL_HW, HW_MACHINE>(hasher);
391
#    endif
392
#    ifdef HW_MODEL
393
    AddSysctl<CTL_HW, HW_MODEL>(hasher);
394
#    endif
395
#    ifdef HW_NCPU
396
    AddSysctl<CTL_HW, HW_NCPU>(hasher);
397
#    endif
398
#    ifdef HW_PHYSMEM
399
    AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
400
#    endif
401
#    ifdef HW_USERMEM
402
    AddSysctl<CTL_HW, HW_USERMEM>(hasher);
403
#    endif
404
#    ifdef HW_MACHINE_ARCH
405
    AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
406
#    endif
407
#    ifdef HW_REALMEM
408
    AddSysctl<CTL_HW, HW_REALMEM>(hasher);
409
#    endif
410
#    ifdef HW_CPU_FREQ
411
    AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
412
#    endif
413
#    ifdef HW_BUS_FREQ
414
    AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
415
#    endif
416
#    ifdef HW_CACHELINE
417
    AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
418
#    endif
419
#  endif
420
#  ifdef CTL_KERN
421
#    ifdef KERN_BOOTFILE
422
     AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
423
#    endif
424
#    ifdef KERN_BOOTTIME
425
     AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
426
#    endif
427
#    ifdef KERN_CLOCKRATE
428
     AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
429
#    endif
430
#    ifdef KERN_HOSTID
431
     AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
432
#    endif
433
#    ifdef KERN_HOSTUUID
434
     AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
435
#    endif
436
#    ifdef KERN_HOSTNAME
437
     AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
438
#    endif
439
#    ifdef KERN_OSRELDATE
440
     AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
441
#    endif
442
#    ifdef KERN_OSRELEASE
443
     AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
444
#    endif
445
#    ifdef KERN_OSREV
446
     AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
447
#    endif
448
#    ifdef KERN_OSTYPE
449
     AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
450
#    endif
451
#    ifdef KERN_POSIX1
452
     AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
453
#    endif
454
#    ifdef KERN_VERSION
455
     AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
456
#    endif
457
#  endif
458
#endif
459
460
    // Env variables
461
0
    if (environ) {
462
0
        for (size_t i = 0; environ[i]; ++i) {
463
0
            hasher.Write((const unsigned char*)environ[i], strlen(environ[i]));
464
0
        }
465
0
    }
466
467
    // Process, thread, user, session, group, ... ids.
468
#ifdef WIN32
469
    hasher << GetCurrentProcessId() << GetCurrentThreadId();
470
#else
471
0
    hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
472
0
#endif
473
0
    hasher << std::this_thread::get_id();
474
0
}