Skip to content

Commit 9a47249

Browse files
zx2c4tytso
authored andcommitted
random: Make crng state queryable
It is very 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. The particular use case is for 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. This patch, therefore, makes that information directly accessible. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent b34fbaa commit 9a47249

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

drivers/char/random.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,21 @@ int wait_for_random_bytes(void)
16631663
}
16641664
EXPORT_SYMBOL(wait_for_random_bytes);
16651665

1666+
/*
1667+
* Returns whether or not the urandom pool has been seeded and thus guaranteed
1668+
* to supply cryptographically secure random numbers. This applies to: the
1669+
* /dev/urandom device, the get_random_bytes function, and the get_random_{u32,
1670+
* ,u64,int,long} family of functions.
1671+
*
1672+
* Returns: true if the urandom pool has been seeded.
1673+
* false if the urandom pool has not been seeded.
1674+
*/
1675+
bool rng_is_initialized(void)
1676+
{
1677+
return crng_ready();
1678+
}
1679+
EXPORT_SYMBOL(rng_is_initialized);
1680+
16661681
/*
16671682
* Add a callback function that will be invoked when the nonblocking
16681683
* pool is initialised.

include/linux/random.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy;
3636

3737
extern void get_random_bytes(void *buf, int nbytes);
3838
extern int wait_for_random_bytes(void);
39+
extern bool rng_is_initialized(void);
3940
extern int add_random_ready_callback(struct random_ready_callback *rdy);
4041
extern void del_random_ready_callback(struct random_ready_callback *rdy);
4142
extern int __must_check get_random_bytes_arch(void *buf, int nbytes);

0 commit comments

Comments
 (0)