Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: gregkh@linuxfoundation.org, stable@vger.kernel.org
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>
Subject: [PATCH stable 4.9.y] random: restore O_NONBLOCK support
Date: Thu, 13 Oct 2022 10:32:31 -0600	[thread overview]
Message-ID: <20221013163231.1410141-1-Jason@zx2c4.com> (raw)
In-Reply-To: <Y0g7N95gKNFMJZ72@kroah.com>

commit cd4f24ae9404fd31fc461066e57889be3b68641b upstream.

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: 30c08efec888 ("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>
---
 drivers/char/random.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 1cbc33ee5a5f..838f66723ccd 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1295,6 +1295,10 @@ static ssize_t random_read_iter(struct kiocb *kiocb, struct iov_iter *iter)
 {
 	int ret;
 
+	if (!crng_ready() &&
+	    (kiocb->ki_filp->f_flags & O_NONBLOCK))
+		return -EAGAIN;
+
 	ret = wait_for_random_bytes();
 	if (ret != 0)
 		return ret;
-- 
2.37.3


  reply	other threads:[~2022-10-13 16:32 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-13 15:36 [PATCH stable 0/3] recent failed backports for the rng Jason A. Donenfeld
2022-10-13 15:36 ` [PATCH stable 1/3] random: restore O_NONBLOCK support Jason A. Donenfeld
2022-10-13 16:20   ` Greg KH
2022-10-13 16:22     ` Greg KH
2022-10-13 16:32       ` Jason A. Donenfeld [this message]
2022-10-13 17:02         ` [PATCH stable 4.9.y] " Greg KH
2022-10-13 15:36 ` [PATCH stable 2/3] random: avoid reading two cache lines on irq randomness Jason A. Donenfeld
2022-10-13 15:36 ` [PATCH stable 3/3] random: use expired timer rather than wq for mixing fast pool Jason A. Donenfeld
2022-10-13 16:18 ` [PATCH stable 0/3] recent failed backports for the rng Greg KH
2022-10-13 16:29   ` Jason A. Donenfeld
2022-10-13 16:54     ` Greg KH
2022-10-13 17:01       ` Jason A. Donenfeld
2022-10-13 17:07         ` [PATCH stable 4.9.y 4.14.y] random: use expired timer rather than wq for mixing fast pool Jason A. Donenfeld
2022-10-13 17:17           ` Greg KH
2022-10-13 16:30   ` [PATCH stable 0/3] recent failed backports for the rng Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221013163231.1410141-1-Jason@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox