2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2015-04-10 22:27:10 +02:00
|
|
|
|
2018-11-08 15:47:16 +01:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
2018-07-26 10:42:01 +02:00
|
|
|
#include <cpuid.h>
|
|
|
|
#endif
|
|
|
|
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <elf.h>
|
2015-04-10 22:27:10 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2018-01-11 00:39:12 +01:00
|
|
|
#include <linux/random.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <stdbool.h>
|
2018-01-11 00:39:12 +01:00
|
|
|
#include <stdint.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <stdlib.h>
|
2018-01-11 00:39:12 +01:00
|
|
|
#include <string.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_SYS_AUXV_H
|
2017-04-19 22:13:27 +02:00
|
|
|
# include <sys/auxv.h>
|
|
|
|
#endif
|
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if USE_SYS_RANDOM_H
|
2017-04-19 22:13:27 +02:00
|
|
|
# include <sys/random.h>
|
|
|
|
#else
|
|
|
|
# include <linux/random.h>
|
2015-06-02 11:07:39 +02:00
|
|
|
#endif
|
2015-04-10 22:27:10 +02:00
|
|
|
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-10-25 14:08:25 +01:00
|
|
|
#include "io-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "missing.h"
|
2015-04-10 22:27:10 +02:00
|
|
|
#include "random-util.h"
|
|
|
|
#include "time-util.h"
|
|
|
|
|
2018-11-10 09:10:49 +01:00
|
|
|
#if HAS_FEATURE_MEMORY_SANITIZER
|
|
|
|
#include <sanitizer/msan_interface.h>
|
|
|
|
#endif
|
2018-07-26 10:42:01 +02:00
|
|
|
|
2018-11-08 15:47:16 +01:00
|
|
|
int rdrand(unsigned long *ret) {
|
2018-07-26 10:42:01 +02:00
|
|
|
|
2018-11-08 15:47:16 +01:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
2018-07-26 10:42:01 +02:00
|
|
|
static int have_rdrand = -1;
|
|
|
|
unsigned char err;
|
|
|
|
|
|
|
|
if (have_rdrand < 0) {
|
|
|
|
uint32_t eax, ebx, ecx, edx;
|
|
|
|
|
|
|
|
/* Check if RDRAND is supported by the CPU */
|
|
|
|
if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) == 0) {
|
|
|
|
have_rdrand = false;
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
have_rdrand = !!(ecx & (1U << 30));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_rdrand == 0)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
asm volatile("rdrand %0;"
|
|
|
|
"setc %1"
|
|
|
|
: "=r" (*ret),
|
|
|
|
"=qm" (err));
|
2018-11-10 09:10:49 +01:00
|
|
|
|
|
|
|
#if HAS_FEATURE_MEMORY_SANITIZER
|
|
|
|
__msan_unpoison(&err, sizeof(err));
|
|
|
|
#endif
|
|
|
|
|
2018-07-26 10:42:01 +02:00
|
|
|
if (!err)
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-07 18:40:26 +01:00
|
|
|
int genuine_random_bytes(void *p, size_t n, RandomFlags flags) {
|
2015-04-10 22:27:10 +02:00
|
|
|
static int have_syscall = -1;
|
|
|
|
_cleanup_close_ int fd = -1;
|
2018-11-07 19:46:18 +01:00
|
|
|
bool got_some = false;
|
2017-07-11 10:35:47 +02:00
|
|
|
int r;
|
2015-04-10 22:27:10 +02:00
|
|
|
|
2018-11-07 19:46:18 +01:00
|
|
|
/* Gathers some randomness from the kernel (or the CPU if the RANDOM_ALLOW_RDRAND flag is set). This call won't
|
|
|
|
* block, unless the RANDOM_BLOCK flag is set. If RANDOM_DONT_DRAIN is set, an error is returned if the random
|
|
|
|
* pool is not initialized. Otherwise it will always return some data from the kernel, regardless of whether
|
|
|
|
* the random pool is fully initialized or not. */
|
2015-04-10 22:27:10 +02:00
|
|
|
|
2018-11-07 19:14:47 +01:00
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
|
|
|
|
2018-11-07 19:46:18 +01:00
|
|
|
if (FLAGS_SET(flags, RANDOM_ALLOW_RDRAND))
|
|
|
|
/* Try x86-64' RDRAND intrinsic if we have it. We only use it if high quality randomness is not
|
|
|
|
* required, as we don't trust it (who does?). Note that we only do a single iteration of RDRAND here,
|
|
|
|
* even though the Intel docs suggest calling this in a tight loop of 10 invocations or so. That's
|
|
|
|
* because we don't really care about the quality here. We generally prefer using RDRAND if the caller
|
|
|
|
* allows us too, since this way we won't drain the kernel randomness pool if we don't need it, as the
|
|
|
|
* pool's entropy is scarce. */
|
|
|
|
for (;;) {
|
2018-11-08 15:47:16 +01:00
|
|
|
unsigned long u;
|
2018-11-07 19:46:18 +01:00
|
|
|
size_t m;
|
|
|
|
|
2018-11-08 15:47:16 +01:00
|
|
|
if (rdrand(&u) < 0) {
|
2018-11-07 19:46:18 +01:00
|
|
|
if (got_some && FLAGS_SET(flags, RANDOM_EXTEND_WITH_PSEUDO)) {
|
|
|
|
/* Fill in the remaining bytes using pseudo-random values */
|
|
|
|
pseudo_random_bytes(p, n);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OK, this didn't work, let's go to getrandom() + /dev/urandom instead */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m = MIN(sizeof(u), n);
|
|
|
|
memcpy(p, &u, m);
|
|
|
|
|
|
|
|
p = (uint8_t*) p + m;
|
|
|
|
n -= m;
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return 0; /* Yay, success! */
|
|
|
|
|
|
|
|
got_some = true;
|
|
|
|
}
|
|
|
|
|
basic/random-util: do not fall back to /dev/urandom if getrandom() returns short
During early boot, we'd call getrandom(), and immediately fall back to
reading from /dev/urandom unless we got the full requested number of bytes.
Those two sources are the same, so the most likely result is /dev/urandom
producing some pseudorandom numbers for us, complaining widely on the way.
Let's change our behaviour to be more conservative:
- if the numbers are only used to initialize a hash table, a short read is OK,
we don't really care if we get the first part of the seed truly random and
then some pseudorandom bytes. So just do that and return "success".
- if getrandom() returns -EAGAIN, fall back to rand() instead of querying
/dev/urandom again.
The idea with those two changes is to avoid generating a warning about
reading from an /dev/urandom when the kernel doesn't have enough entropy.
- only in the cases where we really need to make the best effort possible
(sd_id128_randomize and firstboot password hashing), fall back to
/dev/urandom.
When calling getrandom(), drop the checks whether the argument fits in an int —
getrandom() should do that for us already, and we call it with small arguments
only anyway.
Note that this does not really change the (relatively high) number of random
bytes we request from the kernel. On my laptop, during boot, PID 1 and all
other processes using this code through libsystemd request:
74780 bytes with high_quality_required == false
464 bytes with high_quality_required == true
and it does not eliminate reads from /dev/urandom completely. If the kernel was
short on entropy and getrandom() would fail, we would fall back to /dev/urandom
for those 464 bytes.
When falling back to /dev/urandom, don't lose the short read we already got,
and just read the remaining bytes.
If getrandom() syscall is not available, we fall back to /dev/urandom same
as before.
Fixes #4167 (possibly partially, let's see).
2017-06-25 23:09:05 +02:00
|
|
|
/* Use the getrandom() syscall unless we know we don't have it. */
|
basic/random-util: do not use getrandom() under msan
`fuzz-journal-remote` seems to be failing under `msan` as soon as it starts:
$ sudo infra/helper.py run_fuzzer systemd fuzz-journal-remote
Running: docker run --rm -i --privileged -e FUZZING_ENGINE=libfuzzer -v /home/vagrant/oss-fuzz/build/out/systemd:/out -t gcr.io/oss-fuzz-base/base-runner run_fuzzer fuzz-journal-remote
Using seed corpus: fuzz-journal-remote_seed_corpus.zip
/out/fuzz-journal-remote -rss_limit_mb=2048 -timeout=25 /tmp/fuzz-journal-remote_corpus -max_len=65536 < /dev/null
INFO: Seed: 3380449479
INFO: Loaded 2 modules (36336 inline 8-bit counters): 36139 [0x7ff36ea31d39, 0x7ff36ea3aa64), 197 [0x9998c8, 0x99998d),
INFO: Loaded 2 PC tables (36336 PCs): 36139 [0x7ff36ea3aa68,0x7ff36eac7d18), 197 [0x999990,0x99a5e0),
INFO: 2 files found in /tmp/fuzz-journal-remote_corpus
INFO: seed corpus: files: 2 min: 4657b max: 7790b total: 12447b rss: 97Mb
Uninitialized bytes in __interceptor_pwrite64 at offset 24 inside [0x7fffdd4d7230, 240)
==15==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x7ff36e685e8a in journal_file_init_header /work/build/../../src/systemd/src/journal/journal-file.c:436:13
#1 0x7ff36e683a9d in journal_file_open /work/build/../../src/systemd/src/journal/journal-file.c:3333:21
#2 0x7ff36e68b8f6 in journal_file_open_reliably /work/build/../../src/systemd/src/journal/journal-file.c:3520:13
#3 0x4a3f35 in open_output /work/build/../../src/systemd/src/journal-remote/journal-remote.c:70:13
#4 0x4a34d0 in journal_remote_get_writer /work/build/../../src/systemd/src/journal-remote/journal-remote.c:136:21
#5 0x4a550f in get_source_for_fd /work/build/../../src/systemd/src/journal-remote/journal-remote.c:183:13
#6 0x4a46bd in journal_remote_add_source /work/build/../../src/systemd/src/journal-remote/journal-remote.c:235:13
#7 0x4a271c in LLVMFuzzerTestOneInput /work/build/../../src/systemd/src/fuzz/fuzz-journal-remote.c:36:9
#8 0x4f27cc in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) /src/libfuzzer/FuzzerLoop.cpp:524:13
#9 0x4efa0b in fuzzer::Fuzzer::RunOne(unsigned char const*, unsigned long, bool, fuzzer::InputInfo*, bool*) /src/libfuzzer/FuzzerLoop.cpp:448:3
#10 0x4f8e96 in fuzzer::Fuzzer::ReadAndExecuteSeedCorpora(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, fuzzer::fuzzer_allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) /src/libfuzzer/FuzzerLoop.cpp:732:7
#11 0x4f9f73 in fuzzer::Fuzzer::Loop(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, fuzzer::fuzzer_allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) /src/libfuzzer/FuzzerLoop.cpp:752:3
#12 0x4bf329 in fuzzer::FuzzerDriver(int*, char***, int (*)(unsigned char const*, unsigned long)) /src/libfuzzer/FuzzerDriver.cpp:756:6
#13 0x4ac391 in main /src/libfuzzer/FuzzerMain.cpp:20:10
#14 0x7ff36d14982f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#15 0x41f9d8 in _start (/out/fuzz-journal-remote+0x41f9d8)
Uninitialized value was stored to memory at
#0 0x7ff36e61cd41 in sd_id128_randomize /work/build/../../src/systemd/src/libsystemd/sd-id128/sd-id128.c:288:16
#1 0x7ff36e685cec in journal_file_init_header /work/build/../../src/systemd/src/journal/journal-file.c:426:13
#2 0x7ff36e683a9d in journal_file_open /work/build/../../src/systemd/src/journal/journal-file.c:3333:21
#3 0x7ff36e68b8f6 in journal_file_open_reliably /work/build/../../src/systemd/src/journal/journal-file.c:3520:13
#4 0x4a3f35 in open_output /work/build/../../src/systemd/src/journal-remote/journal-remote.c:70:13
#5 0x4a34d0 in journal_remote_get_writer /work/build/../../src/systemd/src/journal-remote/journal-remote.c:136:21
#6 0x4a550f in get_source_for_fd /work/build/../../src/systemd/src/journal-remote/journal-remote.c:183:13
#7 0x4a46bd in journal_remote_add_source /work/build/../../src/systemd/src/journal-remote/journal-remote.c:235:13
#8 0x4a271c in LLVMFuzzerTestOneInput /work/build/../../src/systemd/src/fuzz/fuzz-journal-remote.c:36:9
#9 0x4f27cc in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) /src/libfuzzer/FuzzerLoop.cpp:524:13
#10 0x4efa0b in fuzzer::Fuzzer::RunOne(unsigned char const*, unsigned long, bool, fuzzer::InputInfo*, bool*) /src/libfuzzer/FuzzerLoop.cpp:448:3
#11 0x4f8e96 in fuzzer::Fuzzer::ReadAndExecuteSeedCorpora(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, fuzzer::fuzzer_allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) /src/libfuzzer/FuzzerLoop.cpp:732:7
#12 0x4f9f73 in fuzzer::Fuzzer::Loop(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, fuzzer::fuzzer_allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) /src/libfuzzer/FuzzerLoop.cpp:752:3
#13 0x4bf329 in fuzzer::FuzzerDriver(int*, char***, int (*)(unsigned char const*, unsigned long)) /src/libfuzzer/FuzzerDriver.cpp:756:6
#14 0x4ac391 in main /src/libfuzzer/FuzzerMain.cpp:20:10
#15 0x7ff36d14982f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
Uninitialized value was created by an allocation of 't' in the stack frame of function 'sd_id128_randomize'
#0 0x7ff36e61cb00 in sd_id128_randomize /work/build/../../src/systemd/src/libsystemd/sd-id128/sd-id128.c:274
SUMMARY: MemorySanitizer: use-of-uninitialized-value /work/build/../../src/systemd/src/journal/journal-file.c:436:13 in journal_file_init_header
Exiting
MS: 0 ; base unit: 0000000000000000000000000000000000000000
artifact_prefix='./'; Test unit written to ./crash-847911777b3096783f4ee70a69ab6d28380c810b
[vagrant@localhost oss-fuzz]$ sudo infra/helper.py check_build --sanitizer=memory systemd
Running: docker run --rm -i --privileged -e FUZZING_ENGINE=libfuzzer -e SANITIZER=memory -v /home/vagrant/oss-fuzz/build/out/systemd:/out -t gcr.io/oss-fuzz-base/base-runner test_all
INFO: performing bad build checks for /out/fuzz-dhcp-server.
INFO: performing bad build checks for /out/fuzz-journal-remote.
INFO: performing bad build checks for /out/fuzz-unit-file.
INFO: performing bad build checks for /out/fuzz-dns-packet.
4 fuzzers total, 0 seem to be broken (0%).
Check build passed.
It's a false positive which is most likely caused by
https://github.com/google/sanitizers/issues/852. I think it could be got around
by avoiding `getrandom` when the code is compiled with `msan`
2018-05-20 15:07:07 +02:00
|
|
|
if (have_syscall != 0 && !HAS_FEATURE_MEMORY_SANITIZER) {
|
2018-11-07 19:04:04 +01:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
r = getrandom(p, n, FLAGS_SET(flags, RANDOM_BLOCK) ? 0 : GRND_NONBLOCK);
|
|
|
|
if (r > 0) {
|
|
|
|
have_syscall = true;
|
|
|
|
|
|
|
|
if ((size_t) r == n)
|
|
|
|
return 0; /* Yay, success! */
|
|
|
|
|
|
|
|
assert((size_t) r < n);
|
|
|
|
p = (uint8_t*) p + r;
|
|
|
|
n -= r;
|
|
|
|
|
|
|
|
if (FLAGS_SET(flags, RANDOM_EXTEND_WITH_PSEUDO)) {
|
|
|
|
/* Fill in the remaining bytes using pseudo-random values */
|
|
|
|
pseudo_random_bytes(p, n);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-07 19:46:18 +01:00
|
|
|
got_some = true;
|
|
|
|
|
2018-11-07 19:04:04 +01:00
|
|
|
/* Hmm, we didn't get enough good data but the caller insists on good data? Then try again */
|
|
|
|
if (FLAGS_SET(flags, RANDOM_BLOCK))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Fill in the rest with /dev/urandom */
|
|
|
|
break;
|
|
|
|
|
|
|
|
} else if (r == 0) {
|
|
|
|
have_syscall = true;
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
} else if (errno == ENOSYS) {
|
|
|
|
/* We lack the syscall, continue with reading from /dev/urandom. */
|
|
|
|
have_syscall = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
} else if (errno == EAGAIN) {
|
2018-11-07 19:31:39 +01:00
|
|
|
/* The kernel has no entropy whatsoever. Let's remember to use the syscall the next
|
|
|
|
* time again though.
|
2018-11-07 19:04:04 +01:00
|
|
|
*
|
2018-11-07 19:31:39 +01:00
|
|
|
* If RANDOM_DONT_DRAIN is set, return an error so that random_bytes() can produce some
|
|
|
|
* pseudo-random bytes instead. Otherwise, fall back to /dev/urandom, which we know is empty,
|
|
|
|
* but the kernel will produce some bytes for us on a best-effort basis. */
|
2018-11-07 19:04:04 +01:00
|
|
|
have_syscall = true;
|
|
|
|
|
2018-11-07 19:46:18 +01:00
|
|
|
if (got_some && FLAGS_SET(flags, RANDOM_EXTEND_WITH_PSEUDO)) {
|
|
|
|
/* Fill in the remaining bytes using pseudorandom values */
|
|
|
|
pseudo_random_bytes(p, n);
|
2018-11-07 19:04:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-07 19:46:18 +01:00
|
|
|
if (FLAGS_SET(flags, RANDOM_DONT_DRAIN))
|
|
|
|
return -ENODATA;
|
|
|
|
|
2018-11-07 19:04:04 +01:00
|
|
|
/* Use /dev/urandom instead */
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
return -errno;
|
|
|
|
}
|
2015-04-10 22:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
|
|
|
|
if (fd < 0)
|
|
|
|
return errno == ENOENT ? -ENOSYS : -errno;
|
|
|
|
|
2018-11-07 19:04:04 +01:00
|
|
|
return loop_read_exact(fd, p, n, true);
|
2015-04-10 22:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void initialize_srand(void) {
|
|
|
|
static bool srand_called = false;
|
|
|
|
unsigned x;
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_SYS_AUXV_H
|
2018-11-06 12:08:07 +01:00
|
|
|
const void *auxv;
|
2015-04-10 22:27:10 +02:00
|
|
|
#endif
|
2018-11-08 15:47:16 +01:00
|
|
|
unsigned long k;
|
2015-04-10 22:27:10 +02:00
|
|
|
|
|
|
|
if (srand_called)
|
|
|
|
return;
|
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_SYS_AUXV_H
|
basic/random-util: do not fall back to /dev/urandom if getrandom() returns short
During early boot, we'd call getrandom(), and immediately fall back to
reading from /dev/urandom unless we got the full requested number of bytes.
Those two sources are the same, so the most likely result is /dev/urandom
producing some pseudorandom numbers for us, complaining widely on the way.
Let's change our behaviour to be more conservative:
- if the numbers are only used to initialize a hash table, a short read is OK,
we don't really care if we get the first part of the seed truly random and
then some pseudorandom bytes. So just do that and return "success".
- if getrandom() returns -EAGAIN, fall back to rand() instead of querying
/dev/urandom again.
The idea with those two changes is to avoid generating a warning about
reading from an /dev/urandom when the kernel doesn't have enough entropy.
- only in the cases where we really need to make the best effort possible
(sd_id128_randomize and firstboot password hashing), fall back to
/dev/urandom.
When calling getrandom(), drop the checks whether the argument fits in an int —
getrandom() should do that for us already, and we call it with small arguments
only anyway.
Note that this does not really change the (relatively high) number of random
bytes we request from the kernel. On my laptop, during boot, PID 1 and all
other processes using this code through libsystemd request:
74780 bytes with high_quality_required == false
464 bytes with high_quality_required == true
and it does not eliminate reads from /dev/urandom completely. If the kernel was
short on entropy and getrandom() would fail, we would fall back to /dev/urandom
for those 464 bytes.
When falling back to /dev/urandom, don't lose the short read we already got,
and just read the remaining bytes.
If getrandom() syscall is not available, we fall back to /dev/urandom same
as before.
Fixes #4167 (possibly partially, let's see).
2017-06-25 23:09:05 +02:00
|
|
|
/* The kernel provides us with 16 bytes of entropy in auxv, so let's
|
|
|
|
* try to make use of that to seed the pseudo-random generator. It's
|
|
|
|
* better than nothing... */
|
2015-04-10 22:27:10 +02:00
|
|
|
|
2018-11-06 12:08:07 +01:00
|
|
|
auxv = (const void*) getauxval(AT_RANDOM);
|
2016-01-28 17:00:38 +01:00
|
|
|
if (auxv) {
|
2017-07-11 10:36:53 +02:00
|
|
|
assert_cc(sizeof(x) <= 16);
|
2016-01-28 17:00:38 +01:00
|
|
|
memcpy(&x, auxv, sizeof(x));
|
|
|
|
} else
|
2015-04-10 22:27:10 +02:00
|
|
|
#endif
|
2016-01-28 17:00:38 +01:00
|
|
|
x = 0;
|
|
|
|
|
2015-04-10 22:27:10 +02:00
|
|
|
x ^= (unsigned) now(CLOCK_REALTIME);
|
|
|
|
x ^= (unsigned) gettid();
|
|
|
|
|
2018-11-08 15:47:16 +01:00
|
|
|
if (rdrand(&k) >= 0)
|
2018-11-06 12:08:26 +01:00
|
|
|
x ^= (unsigned) k;
|
|
|
|
|
2015-04-10 22:27:10 +02:00
|
|
|
srand(x);
|
|
|
|
srand_called = true;
|
|
|
|
}
|
|
|
|
|
2017-06-25 23:32:53 +02:00
|
|
|
/* INT_MAX gives us only 31 bits, so use 24 out of that. */
|
|
|
|
#if RAND_MAX >= INT_MAX
|
|
|
|
# define RAND_STEP 3
|
|
|
|
#else
|
|
|
|
/* SHORT_INT_MAX or lower gives at most 15 bits, we just just 8 out of that. */
|
|
|
|
# define RAND_STEP 1
|
|
|
|
#endif
|
|
|
|
|
2018-11-07 18:27:57 +01:00
|
|
|
void pseudo_random_bytes(void *p, size_t n) {
|
2015-04-10 22:27:10 +02:00
|
|
|
uint8_t *q;
|
2017-06-25 23:32:53 +02:00
|
|
|
|
|
|
|
initialize_srand();
|
|
|
|
|
|
|
|
for (q = p; q < (uint8_t*) p + n; q += RAND_STEP) {
|
|
|
|
unsigned rr;
|
|
|
|
|
|
|
|
rr = (unsigned) rand();
|
|
|
|
|
|
|
|
#if RAND_STEP >= 3
|
2017-07-11 10:35:47 +02:00
|
|
|
if ((size_t) (q - (uint8_t*) p + 2) < n)
|
2017-06-25 23:32:53 +02:00
|
|
|
q[2] = rr >> 16;
|
|
|
|
#endif
|
|
|
|
#if RAND_STEP >= 2
|
2017-07-11 10:35:47 +02:00
|
|
|
if ((size_t) (q - (uint8_t*) p + 1) < n)
|
2017-06-25 23:32:53 +02:00
|
|
|
q[1] = rr >> 8;
|
|
|
|
#endif
|
|
|
|
q[0] = rr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void random_bytes(void *p, size_t n) {
|
2015-04-10 22:27:10 +02:00
|
|
|
|
2018-11-07 19:46:18 +01:00
|
|
|
if (genuine_random_bytes(p, n, RANDOM_EXTEND_WITH_PSEUDO|RANDOM_DONT_DRAIN|RANDOM_ALLOW_RDRAND) >= 0)
|
2015-04-10 22:27:10 +02:00
|
|
|
return;
|
|
|
|
|
2018-11-07 18:27:57 +01:00
|
|
|
/* If for some reason some user made /dev/urandom unavailable to us, or the kernel has no entropy, use a PRNG instead. */
|
|
|
|
pseudo_random_bytes(p, n);
|
2015-04-10 22:27:10 +02:00
|
|
|
}
|