Skip to content

Commit 30c08ef

Browse files
amlutotytso
authored andcommitted
random: make /dev/random be almost like /dev/urandom
This patch changes the read semantics of /dev/random to be the same as /dev/urandom except that reads will block until the CRNG is ready. None of the cleanups that this enables have been done yet. As a result, this gives a warning about an unused function. Signed-off-by: Andy Lutomirski <luto@kernel.org> Link: https://lore.kernel.org/r/5e6ac8831c6cf2e56a7a4b39616d1732b2bdd06c.1577088521.git.luto@kernel.org Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 48446f1 commit 30c08ef

1 file changed

Lines changed: 14 additions & 41 deletions

File tree

drivers/char/random.c

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@
354354
#define INPUT_POOL_WORDS (1 << (INPUT_POOL_SHIFT-5))
355355
#define OUTPUT_POOL_SHIFT 10
356356
#define OUTPUT_POOL_WORDS (1 << (OUTPUT_POOL_SHIFT-5))
357-
#define SEC_XFER_SIZE 512
358357
#define EXTRACT_SIZE 10
359358

360359

@@ -803,7 +802,6 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
803802
if (entropy_bits >= random_read_wakeup_bits &&
804803
wq_has_sleeper(&random_read_wait)) {
805804
wake_up_interruptible(&random_read_wait);
806-
kill_fasync(&fasync, SIGIO, POLL_IN);
807805
}
808806
/* If the input pool is getting full, and the blocking
809807
* pool has room, send some entropy to the blocking
@@ -1031,6 +1029,7 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r)
10311029
crng_init = 2;
10321030
process_random_ready_list();
10331031
wake_up_interruptible(&crng_init_wait);
1032+
kill_fasync(&fasync, SIGIO, POLL_IN);
10341033
pr_notice("random: crng init done\n");
10351034
if (unseeded_warning.missed) {
10361035
pr_notice("random: %d get_random_xx warning(s) missed "
@@ -1982,43 +1981,6 @@ void rand_initialize_disk(struct gendisk *disk)
19821981
}
19831982
#endif
19841983

1985-
static ssize_t
1986-
_random_read(int nonblock, char __user *buf, size_t nbytes)
1987-
{
1988-
ssize_t n;
1989-
1990-
if (nbytes == 0)
1991-
return 0;
1992-
1993-
nbytes = min_t(size_t, nbytes, SEC_XFER_SIZE);
1994-
while (1) {
1995-
n = extract_entropy_user(&blocking_pool, buf, nbytes);
1996-
if (n < 0)
1997-
return n;
1998-
trace_random_read(n*8, (nbytes-n)*8,
1999-
ENTROPY_BITS(&blocking_pool),
2000-
ENTROPY_BITS(&input_pool));
2001-
if (n > 0)
2002-
return n;
2003-
2004-
/* Pool is (near) empty. Maybe wait and retry. */
2005-
if (nonblock)
2006-
return -EAGAIN;
2007-
2008-
wait_event_interruptible(random_read_wait,
2009-
blocking_pool.initialized &&
2010-
(ENTROPY_BITS(&input_pool) >= random_read_wakeup_bits));
2011-
if (signal_pending(current))
2012-
return -ERESTARTSYS;
2013-
}
2014-
}
2015-
2016-
static ssize_t
2017-
random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
2018-
{
2019-
return _random_read(file->f_flags & O_NONBLOCK, buf, nbytes);
2020-
}
2021-
20221984
static ssize_t
20231985
urandom_read_nowarn(struct file *file, char __user *buf, size_t nbytes,
20241986
loff_t *ppos)
@@ -2051,15 +2013,26 @@ urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
20512013
return urandom_read_nowarn(file, buf, nbytes, ppos);
20522014
}
20532015

2016+
static ssize_t
2017+
random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
2018+
{
2019+
int ret;
2020+
2021+
ret = wait_for_random_bytes();
2022+
if (ret != 0)
2023+
return ret;
2024+
return urandom_read_nowarn(file, buf, nbytes, ppos);
2025+
}
2026+
20542027
static __poll_t
20552028
random_poll(struct file *file, poll_table * wait)
20562029
{
20572030
__poll_t mask;
20582031

2059-
poll_wait(file, &random_read_wait, wait);
2032+
poll_wait(file, &crng_init_wait, wait);
20602033
poll_wait(file, &random_write_wait, wait);
20612034
mask = 0;
2062-
if (ENTROPY_BITS(&input_pool) >= random_read_wakeup_bits)
2035+
if (crng_ready())
20632036
mask |= EPOLLIN | EPOLLRDNORM;
20642037
if (ENTROPY_BITS(&input_pool) < random_write_wakeup_bits)
20652038
mask |= EPOLLOUT | EPOLLWRNORM;

0 commit comments

Comments
 (0)