change sys/random.h to random.h in AC_MSG_CHECK
remove definitions that are applicable to macos which include unistd.h
additionally change comparator argument from NULL to nullptr when evaluating &getentropy
Inspired by: c13c97dbf846cf0e6a5581ac414ef96a215b0dc6
If the code was compiled with newer (>=3.17) kernel headers but executed
on a system without the system call, every use of random would crash the
program. Add a fallback for that case.
Cherry-picked from: 7e6dcd9995b99e894b8017f09016c405b066ca36
Move the OS random test to a sanity check function that is called every
time bitcoind is initialized.
Keep `src/test/random_tests.cpp` for the case that later random tests
are added, and keep a rudimentary test that just calls the sanity check.
Cherry-picked from: 7cad84929907c4294f07377453aa77887911b486
These are available in sandboxes without access to files or
devices. Also [they are safer and more straightforward](https://en.wikipedia.org/wiki/Entropy-supplying_system_calls)
to use than `/dev/urandom` as reading from a file has quite a few edge
cases:
- Linux: `getrandom(buf, buflen, 0)`. [getrandom(2)](http://man7.org/linux/man-pages/man2/getrandom.2.html)
was introduced in version 3.17 of the Linux kernel.
- OpenBSD: `getentropy(buf, buflen)`. The [getentropy(2)](http://man.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man2/getentropy.2)
function appeared in OpenBSD 5.6.
- FreeBSD and NetBSD: `sysctl(KERN_ARND)`. Not sure when this was added
but it has existed for quite a while.
Alternatives:
- Linux has sysctl `CTL_KERN` / `KERN_RANDOM` / `RANDOM_UUID`
which gives 16 bytes of randomness. This may be available
on older kernels, however [sysctl is deprecated on Linux](https://lwn.net/Articles/605392/)
and even removed in some distros so we shouldn't use it.
Add tests for `GetOSRand()`:
- Test that no error happens (otherwise `RandFailure()` which aborts)
- Test that all 32 bytes are overwritten (initialize with zeros, try multiple times)
Discussion:
- When to use these? Currently they are always used when available.
Another option would be to use them only when `/dev/urandom` is not
available. But this would mean these code paths receive less testing,
and I'm not sure there is any reason to prefer `/dev/urandom`.
Closes: #9676
Cherry-picked from: 224e6eb089a0f4977d22f3803fc27e44b5e7eea5
Contains squashed commit of aa09ccbb74ea9febd83ce3362238ac5339069909 squashme: comment that NUM_OS_RANDOM_BYTES should not be changed lightly
Removes undefined behavior by refactoring the way we destruct
objects that track mutexes when debugging.
1. Prevent UB from LockData custom destructor by making it implicit
2. Replace the thread_specific_ptr holding the lock stack with a
static map that stores it per thread.
Also cleans up the aliasing to make the code better readable.
Backported from: 458992b0
8d8921ab
f511f61d
58e6881b
26c093a9
Co-authored-by: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com>
Co-authored-by: Wladimir J. van der Laan <laanwj@protonmail.com>
Abstracts away how a path is opened to a `FILE*`.
Reduces the number of places where path is converted to a string
for anything else but printing.
Cherry-picked from: 2a5f5747
Conflicts:
- additional instances in bitcoin-tx.cpp and test/script_tests.cpp
adds:
- maxoutamount - highest value output
- minoutamount - lowest value output
- dustouts - number of outputs under our soft dust limit
Each of these ignores OP_RETURN and coinbase outputs