summaryrefslogtreecommitdiff
path: root/src/basic/random-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2019-05-07 16:18:13 -0400
committerLennart Poettering <lennart@poettering.net>2019-05-07 17:30:40 -0400
commit1a0ffa1e737e65312abac63dcf4b44e1ac0e1642 (patch)
treeeb979b4869a52cf47a9e24c0702e6974f7eb2fac /src/basic/random-util.c
parent717e8eda77b93ac396dc7ed325d5bcc61dfe2fa8 (diff)
downloadsystemd-1a0ffa1e737e65312abac63dcf4b44e1ac0e1642.tar.gz
random-util: rename RANDOM_DONT_DRAIN → RANDOM_MAY_FAIL
The old flag name was a bit of a misnomer, as /dev/urandom cannot be "drained". Once it's initialized it's initialized and then is good forever. (Only /dev/random has a concept of 'draining', but we never use that, as it's an obsolete interface). The flag is still useful though, since it allows us to suppress accesses to the random pool while it is not initialized, as that trips up the kernel and it logs about any such attempts, which we really don't want.
Diffstat (limited to 'src/basic/random-util.c')
-rw-r--r--src/basic/random-util.c36
1 files changed, 19 insertions, 17 deletions
diff --git a/src/basic/random-util.c b/src/basic/random-util.c
index ca25fd2420..de29e07549 100644
--- a/src/basic/random-util.c
+++ b/src/basic/random-util.c
@@ -71,21 +71,22 @@ int genuine_random_bytes(void *p, size_t n, RandomFlags flags) {
bool got_some = false;
int r;
- /* 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. */
+ /* 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_MAY_FAIL 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. */
if (n == 0)
return 0;
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. */
+ /* 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 to, since this way we won't upset
+ * the kernel's random subsystem by accessing it before the pool is initialized (after all it
+ * will kmsg log about every attempt to do so)..*/
for (;;) {
unsigned long u;
size_t m;
@@ -153,12 +154,13 @@ int genuine_random_bytes(void *p, size_t n, RandomFlags flags) {
break;
} else if (errno == EAGAIN) {
- /* The kernel has no entropy whatsoever. Let's remember to use the syscall the next
- * time again though.
+ /* The kernel has no entropy whatsoever. Let's remember to use the syscall
+ * the next time again though.
*
- * 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. */
+ * If RANDOM_MAY_FAIL 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. */
have_syscall = true;
if (got_some && FLAGS_SET(flags, RANDOM_EXTEND_WITH_PSEUDO)) {
@@ -167,7 +169,7 @@ int genuine_random_bytes(void *p, size_t n, RandomFlags flags) {
return 0;
}
- if (FLAGS_SET(flags, RANDOM_DONT_DRAIN))
+ if (FLAGS_SET(flags, RANDOM_MAY_FAIL))
return -ENODATA;
/* Use /dev/urandom instead */
@@ -250,7 +252,7 @@ void pseudo_random_bytes(void *p, size_t n) {
void random_bytes(void *p, size_t n) {
- if (genuine_random_bytes(p, n, RANDOM_EXTEND_WITH_PSEUDO|RANDOM_DONT_DRAIN|RANDOM_ALLOW_RDRAND) >= 0)
+ if (genuine_random_bytes(p, n, RANDOM_EXTEND_WITH_PSEUDO|RANDOM_MAY_FAIL|RANDOM_ALLOW_RDRAND) >= 0)
return;
/* If for some reason some user made /dev/urandom unavailable to us, or the kernel has no entropy, use a PRNG instead. */