Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6112407iog; Thu, 23 Jun 2022 11:34:49 -0700 (PDT) X-Google-Smtp-Source: AGRyM1se77FHbXc2za/CP/XXIAcEIY9zJfs5Q3z4XZJiN23J5aBhDPnw6eCm8sC3Zu9/KjZ39rQQ X-Received: by 2002:a17:907:6e8b:b0:726:2b16:d1b4 with SMTP id sh11-20020a1709076e8b00b007262b16d1b4mr2342008ejc.27.1656009289513; Thu, 23 Jun 2022 11:34:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656009289; cv=none; d=google.com; s=arc-20160816; b=bdgQ45H3P1zb4zaLf13rawzAcWKFhoDpflVIhYYQR8Ku+E4nhwYBPGcMyUbhMfj7QI YQLLk2EjF+xJ7wCrRXi43ENy+JCq8K/wZueIeQi3yBEVdpV1h3gQ3VxI0xbCy71hrbMF UFhKPqhQkss1qGtMqXPklyzqPqfeQAsF4C3MriCt5UCN9umrybFXEb2sBAaG552IZfJ+ 6jr911nmyY8nKc4XdJ8sDmbpVLBzFEKH+3Cm4jpC6xaxpjHofuoUfB05/ecXPTTTcRab JeKq9eceKEyG3PSnqpOuQl1xH79yL+7V2kCSX88OG+zFVLl2BY4iv1ANPe/f2C3IGXJL pIIg== 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=LfG1SkOM+2JMfVdMNpXDm7xbjacldMA2+yuHwkvZUvk=; b=Va+SNS1lmawkr0ozuzuwSEEA2+f/bPRg02oQpMOkhUL4fmvZWXHcYIVT94brhHMrow kIo1WPhh9URe834C+yUXirs2ldGHJVzYUsOWDp0BE9M2RyieZlef2HNlGDgMC98SJrjw RCI1drAhdUfwbM7wY5PgTlk/IIUz/X7Rr/byMPRkbhE9uxlo2Y0XDxdStSCu04Mej21C l9DZTWZv6/N9Gmo4oZkBcWkDR/XMCsvz+lNRQl1BKlTjoMqgfV26+EDODMjiTJWM7M60 cyB8nndNvouHxN4a1FTAlyGYqFXDEjVlrncSlUynx62eK+Ry1M/PJYeBoL1VMgWw96bU ApAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KoQleTTD; 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 gb9-20020a170907960900b007262d20ea72si324165ejc.179.2022.06.23.11.34.24; Thu, 23 Jun 2022 11:34:49 -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=KoQleTTD; 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 S232964AbiFWRAa (ORCPT + 99 others); Thu, 23 Jun 2022 13:00:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232927AbiFWQt6 (ORCPT ); Thu, 23 Jun 2022 12:49:58 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 181F917E22; Thu, 23 Jun 2022 09:48:00 -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 1DD0C61F8B; Thu, 23 Jun 2022 16:48:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE28FC3411B; Thu, 23 Jun 2022 16:47:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656002879; bh=tXADHCElv9FFRCLP12ReD/eJFv4M5z5yG2zHjKV+we8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KoQleTTDcGYtJn0R/wE2brvDiyv3x5xckK7kI5GhaefIr4OgeZXSjM/yrvIv+ykzo 7mtiQxE53Hq7dPAXZvifEj9WxRSVX9wtryWw2JTxaU6o6HDPlRsyeeC/aE8y/KIzjc GlCXmvz7EnRvngptWuTiaHnS8e74pApAFrtEW0fg= 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 4.9 057/264] random: make /dev/random be almost like /dev/urandom Date: Thu, 23 Jun 2022 18:40:50 +0200 Message-Id: <20220623164345.686555681@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164344.053938039@linuxfoundation.org> References: <20220623164344.053938039@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 @@ -355,7 +355,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 @@ -805,7 +804,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 @@ -1978,43 +1976,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) { @@ -2046,15 +2007,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 unsigned int random_poll(struct file *file, poll_table * wait) { unsigned int 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 |= POLLIN | POLLRDNORM; if (ENTROPY_BITS(&input_pool) < random_write_wakeup_bits) mask |= POLLOUT | POLLWRNORM;