Skip to content

Commit cd4f24a

Browse files
committed
random: restore O_NONBLOCK support
Prior to 5.6, when /dev/random was opened with O_NONBLOCK, it would return -EAGAIN if there was no entropy. When the pools were unified in 5.6, this was lost. The post 5.6 behavior of blocking until the pool is initialized, and ignoring O_NONBLOCK in the process, went unnoticed, with no reports about the regression received for two and a half years. However, eventually this indeed did break somebody's userspace. So we restore the old behavior, by returning -EAGAIN if the pool is not initialized. Unlike the old /dev/random, this can only occur during early boot, after which it never blocks again. In order to make this O_NONBLOCK behavior consistent with other expectations, also respect users reading with preadv2(RWF_NOWAIT) and similar. Fixes: 30c08ef ("random: make /dev/random be almost like /dev/urandom") Reported-by: Guozihua <guozihua@huawei.com> Reported-by: Zhongguohua <zhongguohua1@huawei.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Theodore Ts'o <tytso@mit.edu> Cc: Andrew Lutomirski <luto@kernel.org> Cc: stable@vger.kernel.org Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
1 parent 504c25c commit cd4f24a

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

drivers/char/mem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,8 @@ static const struct memdev {
712712
#endif
713713
[5] = { "zero", 0666, &zero_fops, FMODE_NOWAIT },
714714
[7] = { "full", 0666, &full_fops, 0 },
715-
[8] = { "random", 0666, &random_fops, 0 },
716-
[9] = { "urandom", 0666, &urandom_fops, 0 },
715+
[8] = { "random", 0666, &random_fops, FMODE_NOWAIT },
716+
[9] = { "urandom", 0666, &urandom_fops, FMODE_NOWAIT },
717717
#ifdef CONFIG_PRINTK
718718
[11] = { "kmsg", 0644, &kmsg_fops, 0 },
719719
#endif

drivers/char/random.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,11 @@ static ssize_t random_read_iter(struct kiocb *kiocb, struct iov_iter *iter)
13471347
{
13481348
int ret;
13491349

1350+
if (!crng_ready() &&
1351+
((kiocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO)) ||
1352+
(kiocb->ki_filp->f_flags & O_NONBLOCK)))
1353+
return -EAGAIN;
1354+
13501355
ret = wait_for_random_bytes();
13511356
if (ret != 0)
13521357
return ret;

0 commit comments

Comments
 (0)