Skip to content

Commit 9aefae4

Browse files
amlutogregkh
authored andcommitted
random: make /dev/random be almost like /dev/urandom
commit 30c08efec8884fb106b8e57094baa51bb4c44e32 upstream. 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> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent aa38e37 commit 9aefae4

1 file changed

Lines changed: 13 additions & 41 deletions

File tree

drivers/char/random.c

Lines changed: 13 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

@@ -804,7 +803,6 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
804803
if (entropy_bits >= random_read_wakeup_bits &&
805804
wq_has_sleeper(&random_read_wait)) {
806805
wake_up_interruptible(&random_read_wait);
807-
kill_fasync(&fasync, SIGIO, POLL_IN);
808806
}
809807
/* If the input pool is getting full, and the blocking
810808
* pool has room, send some entropy to the blocking
@@ -1924,43 +1922,6 @@ void rand_initialize_disk(struct gendisk *disk)
19241922
}
19251923
#endif
19261924

1927-
static ssize_t
1928-
_random_read(int nonblock, char __user *buf, size_t nbytes)
1929-
{
1930-
ssize_t n;
1931-
1932-
if (nbytes == 0)
1933-
return 0;
1934-
1935-
nbytes = min_t(size_t, nbytes, SEC_XFER_SIZE);
1936-
while (1) {
1937-
n = extract_entropy_user(&blocking_pool, buf, nbytes);
1938-
if (n < 0)
1939-
return n;
1940-
trace_random_read(n*8, (nbytes-n)*8,
1941-
ENTROPY_BITS(&blocking_pool),
1942-
ENTROPY_BITS(&input_pool));
1943-
if (n > 0)
1944-
return n;
1945-
1946-
/* Pool is (near) empty. Maybe wait and retry. */
1947-
if (nonblock)
1948-
return -EAGAIN;
1949-
1950-
wait_event_interruptible(random_read_wait,
1951-
blocking_pool.initialized &&
1952-
(ENTROPY_BITS(&input_pool) >= random_read_wakeup_bits));
1953-
if (signal_pending(current))
1954-
return -ERESTARTSYS;
1955-
}
1956-
}
1957-
1958-
static ssize_t
1959-
random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
1960-
{
1961-
return _random_read(file->f_flags & O_NONBLOCK, buf, nbytes);
1962-
}
1963-
19641925
static ssize_t
19651926
urandom_read_nowarn(struct file *file, char __user *buf, size_t nbytes,
19661927
loff_t *ppos)
@@ -1993,15 +1954,26 @@ urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
19931954
return urandom_read_nowarn(file, buf, nbytes, ppos);
19941955
}
19951956

1957+
static ssize_t
1958+
random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
1959+
{
1960+
int ret;
1961+
1962+
ret = wait_for_random_bytes();
1963+
if (ret != 0)
1964+
return ret;
1965+
return urandom_read_nowarn(file, buf, nbytes, ppos);
1966+
}
1967+
19961968
static unsigned int
19971969
random_poll(struct file *file, poll_table * wait)
19981970
{
19991971
unsigned int mask;
20001972

2001-
poll_wait(file, &random_read_wait, wait);
1973+
poll_wait(file, &crng_init_wait, wait);
20021974
poll_wait(file, &random_write_wait, wait);
20031975
mask = 0;
2004-
if (ENTROPY_BITS(&input_pool) >= random_read_wakeup_bits)
1976+
if (crng_ready())
20051977
mask |= POLLIN | POLLRDNORM;
20061978
if (ENTROPY_BITS(&input_pool) < random_write_wakeup_bits)
20071979
mask |= POLLOUT | POLLWRNORM;

0 commit comments

Comments
 (0)