Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2931425iog; Mon, 20 Jun 2022 07:44:36 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vlkuOO0seh7hcihYZG/LpiFSPy34U/ztvybNHNUDhb9J85DQJ54toX7FkjQ3N+WXMTs3/A X-Received: by 2002:a63:d949:0:b0:408:870f:70d1 with SMTP id e9-20020a63d949000000b00408870f70d1mr21996751pgj.620.1655736276078; Mon, 20 Jun 2022 07:44:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655736276; cv=none; d=google.com; s=arc-20160816; b=lvZ8RAs+UCtKP703g/KtoFMP3wahY4xnbnov7jFegvCg5SWodZBzT0/8dwF0kgrZHX CFURanw+IP61DAixnz+JQGFW/03f49U+cR0s7cGlKi/B/IrbHMiZYf9sWTflQscccTFM mJTJ4ZX/t4jak8rbMoxnNzzO4bvlWDEBygYIk39pCJhJZ3XGQF2Q5LR5BBpKsKEjM8Ed 9ZgBJT3JkBrqJUDE2gZeatFhLu3+W2VWc6hexgUx0rQUrdWMeLnAdDk5xGYbd7qDD9ph aguLUCW7j0eo/uynaQ6d+4fLkuHCk00hN3PDtSpoPeSucAHLorvi1N/5IoAKAcNnv+QO 2p3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=u5IdYKjWlssESGl0lYKOWbAG8nlaEInTvGLloUzgqmY=; b=qBg2r1vAoQzGxf2AE2VzbQrZDN4EcgTU3a2nhCSLFcy7oxANhwc8XyjjtgZrcE3/H1 Fpas6C9HykDMnggBtxRw9YDIx1TXEj87lAX3mTPU/EjLVxMtJkfnHmIJx7OMLhGeB8Gi LAWdOheSObk23kbhCNAof9zXzoWsJ3zmSI734WeG8tYXEXRkKmzDhj8j8hjtrBzuSV/q T9Hc10naE2itWW78pzkNNY2zwMSYBsT0OpgSHAfDAquK9Ru9Q1oIQwZZvDHX/5RQXojD G4BqrSWe5Y6EMVMcrbMeOJSxyuTQB6HeAWyt8x2jrtPqY7qOLyHGhA8HE+X1OROQ+6nQ XpVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=joNeD6cd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u20-20020a056a00125400b00522ac3d89ddsi16918270pfi.216.2022.06.20.07.44.23; Mon, 20 Jun 2022 07:44:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=joNeD6cd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348684AbiFTNqC (ORCPT + 99 others); Mon, 20 Jun 2022 09:46:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350001AbiFTNom (ORCPT ); Mon, 20 Jun 2022 09:44:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94F132CE1C; Mon, 20 Jun 2022 06:16:31 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 266EC60C98; Mon, 20 Jun 2022 13:10:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1671EC3411B; Mon, 20 Jun 2022 13:10:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655730648; bh=mFwsKGduHehyAoFLCK8qRt2g0jAA6EtO8El7vftSjdw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=joNeD6cdeJFe7O0z8k/sdrBU815wMiAjIXgx+LKh434WFRUSONPb2QXNTw623F9YN C5FluG+I9aTF1zO0TYc8wI9dQGaNCeIRyMJte2CUJCn3rR2I0DRzGX/1wJOiCFWiXc hKiyp/eBZVAofmuzghUt77Chzp3dJxkpVB7VkpIw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andy Lutomirski , Theodore Tso , "Jason A. Donenfeld" Subject: [PATCH 5.4 013/240] random: make /dev/random be almost like /dev/urandom Date: Mon, 20 Jun 2022 14:48:34 +0200 Message-Id: <20220620124738.192627631@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220620124737.799371052@linuxfoundation.org> References: <20220620124737.799371052@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andy Lutomirski 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 Link: https://lore.kernel.org/r/5e6ac8831c6cf2e56a7a4b39616d1732b2bdd06c.1577088521.git.luto@kernel.org Signed-off-by: Theodore Ts'o Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 54 ++++++++++++-------------------------------------- 1 file changed, 13 insertions(+), 41 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -354,7 +354,6 @@ #define INPUT_POOL_WORDS (1 << (INPUT_POOL_SHIFT-5)) #define OUTPUT_POOL_SHIFT 10 #define OUTPUT_POOL_WORDS (1 << (OUTPUT_POOL_SHIFT-5)) -#define SEC_XFER_SIZE 512 #define EXTRACT_SIZE 10 @@ -804,7 +803,6 @@ retry: if (entropy_bits >= random_read_wakeup_bits && wq_has_sleeper(&random_read_wait)) { wake_up_interruptible(&random_read_wait); - kill_fasync(&fasync, SIGIO, POLL_IN); } /* If the input pool is getting full, and the blocking * pool has room, send some entropy to the blocking @@ -2006,43 +2004,6 @@ void rand_initialize_disk(struct gendisk #endif static ssize_t -_random_read(int nonblock, char __user *buf, size_t nbytes) -{ - ssize_t n; - - if (nbytes == 0) - return 0; - - nbytes = min_t(size_t, nbytes, SEC_XFER_SIZE); - while (1) { - n = extract_entropy_user(&blocking_pool, buf, nbytes); - if (n < 0) - return n; - trace_random_read(n*8, (nbytes-n)*8, - ENTROPY_BITS(&blocking_pool), - ENTROPY_BITS(&input_pool)); - if (n > 0) - return n; - - /* Pool is (near) empty. Maybe wait and retry. */ - if (nonblock) - return -EAGAIN; - - wait_event_interruptible(random_read_wait, - blocking_pool.initialized && - (ENTROPY_BITS(&input_pool) >= random_read_wakeup_bits)); - if (signal_pending(current)) - return -ERESTARTSYS; - } -} - -static ssize_t -random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) -{ - return _random_read(file->f_flags & O_NONBLOCK, buf, nbytes); -} - -static ssize_t urandom_read_nowarn(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) { @@ -2074,15 +2035,26 @@ urandom_read(struct file *file, char __u return urandom_read_nowarn(file, buf, nbytes, ppos); } +static ssize_t +random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) +{ + int ret; + + ret = wait_for_random_bytes(); + if (ret != 0) + return ret; + return urandom_read_nowarn(file, buf, nbytes, ppos); +} + static __poll_t random_poll(struct file *file, poll_table * wait) { __poll_t mask; - poll_wait(file, &random_read_wait, wait); + poll_wait(file, &crng_init_wait, wait); poll_wait(file, &random_write_wait, wait); mask = 0; - if (ENTROPY_BITS(&input_pool) >= random_read_wakeup_bits) + if (crng_ready()) mask |= EPOLLIN | EPOLLRDNORM; if (ENTROPY_BITS(&input_pool) < random_write_wakeup_bits) mask |= EPOLLOUT | EPOLLWRNORM;