lkml.org 
[lkml]   [2018]   [Jun]   [18]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH] random: make crng state queryable
Date
It is extremely useful to be able to know whether or not get_random_
bytes_wait / wait_for_random_bytes is going to block or not, or whether
plain get_random_bytes is going to return good randomness or bad
randomness.

My particular use case is mitigating certain attacks in WireGuard. A
handshake packet arrives and is queued up. Elsewhere a worker thread
takes items from the queue and processes them. In replying to these
items, it needs to use some random data, and it has to be good random
data. If we simply block until we can have good randomness, then it's
possible for an attacker to fill the queue up with packets waiting to be
processed. Upon realizing the queue is full, WireGuard will detect that
it's under a denial of service attack, and behave accordingly. A better
approach is just to drop incoming handshake packets if the crng is not
yet initialized. Currently the below awful code is necessary to do such
a thing.

This patch, therefore, makes that information directly accessible.

~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

The wrong way to do things:

struct rng_is_initialized_callback {
struct random_ready_callback cb;
atomic_t *rng_state;
};
static void rng_is_initialized_callback(struct random_ready_callback *cb)
{
struct rng_is_initialized_callback *rdy = container_of(cb, struct rng_is_initialized_callback, cb);
atomic_set(rdy->rng_state, 2);
kfree(rdy);
}
static bool rng_is_initialized(void)
{
static atomic_t rng_state = ATOMIC_INIT(0);

if (atomic_read(&rng_state) == 2)
return true;

if (atomic_cmpxchg(&rng_state, 0, 1) == 0) {
int ret;
struct rng_is_initialized_callback *rdy = kmalloc(sizeof(*rdy), GFP_ATOMIC);
if (!rdy) {
atomic_set(&rng_state, 0);
return false;
}
rdy->cb.owner = THIS_MODULE;
rdy->cb.func = rng_is_initialized_callback;
rdy->rng_state = &rng_state;
ret = add_random_ready_callback(&rdy->cb);
if (ret)
kfree(rdy);
if (ret == -EALREADY) {
atomic_set(&rng_state, 2);
return true;
} else if (ret)
atomic_set(&rng_state, 0);
return false;
}
return false;
}

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
drivers/char/random.c | 15 +++++++++++++++
include/linux/random.h | 1 +
2 files changed, 16 insertions(+)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index a8fb0020ba5c..871724f7b810 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1657,6 +1657,21 @@ int wait_for_random_bytes(void)
}
EXPORT_SYMBOL(wait_for_random_bytes);

+/*
+ * Returns whether or not the urandom pool has been seeded and thus guaranteed
+ * to supply cryptographically secure random numbers. This applies to: the
+ * /dev/urandom device, the get_random_bytes function, and the get_random_{u32,
+ * ,u64,int,long} family of functions.
+ *
+ * Returns: true if the urandom pool has been seeded.
+ * false if the urandom pool has not been seeded.
+ */
+bool rng_is_initialized(void)
+{
+ return crng_ready();
+}
+EXPORT_SYMBOL(rng_is_initialized);
+
/*
* Add a callback function that will be invoked when the nonblocking
* pool is initialised.
diff --git a/include/linux/random.h b/include/linux/random.h
index 2ddf13b4281e..c8208e0ff227 100644
--- a/include/linux/random.h
+++ b/include/linux/random.h
@@ -36,6 +36,7 @@ extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy;

extern void get_random_bytes(void *buf, int nbytes);
extern int wait_for_random_bytes(void);
+extern bool rng_is_initialized(void);
extern int add_random_ready_callback(struct random_ready_callback *rdy);
extern void del_random_ready_callback(struct random_ready_callback *rdy);
extern void get_random_bytes_arch(void *buf, int nbytes);
--
2.17.1
\
 
 \ /
  Last update: 2018-06-19 01:44    [W:0.036 / U:0.480 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site