Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6146381iog; Thu, 23 Jun 2022 12:18:07 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vkEMD5sJ4CTOXBEAsVON1xh5B9EgJF5L/RhfN9Q6R6oMtLnjjBjRsDTBqeA2PbIEstvFfk X-Received: by 2002:aa7:d88a:0:b0:435:6abb:1284 with SMTP id u10-20020aa7d88a000000b004356abb1284mr12767118edq.353.1656011886988; Thu, 23 Jun 2022 12:18:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011886; cv=none; d=google.com; s=arc-20160816; b=eS8eiwXjbVr3D3MK+suIUNQuR46szdOM9jcs3YIDZEKJ3QPewScyCNcZ04sh7ebLGN PkGrmIGqQLJgD0gIb7IAa7VAA+4ThDZoD4kxgGMQ5PLWDyCIWJUB1STluYlvww5aLiHV mCxtbM6/14JaFnNSvqIqTvFQpJ/jwmUm61qXFOX/q70pEUNKs7VjuaErxwJlzQ9kop2J YHQt10ao0sclS1ITHTwt/llXqbtd1YrioDOWRz8tun1xdphkx4U5BQup8DDMj++Cfp+8 ZcQ5JYtFq/bSLXIXw943hM/lwJmWixgPKZjX0odGwjUB1wxiHQ6DRuhA1OuBCywBhNFC pvdQ== 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=A2F8h2KKJHSQbdUzRG5N4j0KS3VhcteIQAjfW1Pi1Cg=; b=t0vm6HfRk3Rrn9kK6+B6aAO5+efgu1z0oyC/RxDvW8rZFkpSODcdi0e8OugFzOwrqJ ItW3p8ZoJbVtuDLgixKMJogQmWPzYQHyhCfe1wHzCkt39r4LgfS9POHMZSDFH17LblEj clHhOo+OUVPiOjyMMBVdB77ei+2PLbf9/7fj9CqfbyUrChG1sxTQk0e/ifdKS06mfmHw CTbkT9DzJj4GoZbysqEDsO5j/4I06Uou+G1Rp1I4oolvTTn/uhD3Yv1x5xg648/fp0eL KxdsJ3lUwkoGjsC6raRKxiD+4hgLAbzpFzFQQ4Sq0SVo5wfWiKQ2oT9MxhEWXWd8SNJf 2rvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OclBGaaH; 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 z7-20020a05640235c700b0042de30cf6e5si418061edc.365.2022.06.23.12.17.41; Thu, 23 Jun 2022 12:18:06 -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=OclBGaaH; 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 S235669AbiFWR5T (ORCPT + 99 others); Thu, 23 Jun 2022 13:57:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235407AbiFWRwb (ORCPT ); Thu, 23 Jun 2022 13:52:31 -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 429525001F; Thu, 23 Jun 2022 10:13:10 -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 7D21A61D18; Thu, 23 Jun 2022 17:13:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50B95C3411B; Thu, 23 Jun 2022 17:13:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004389; bh=7LoDRDsOjAOBXC925WysFwrZ1mHlsKEmoJU7RPNDbEU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OclBGaaHtYpIq/bL2PV/qY591NGubxNvYaDTvYmcw0eS53xsvs6IWwiPPLH+fqbU7 ZQ4ngpCIk6NcmrUoaKb7UX2uezke7Lwu3K7Hx+ojZmPff+k+WyFmBuzdAeJrN8gfLT SJHf99wdznS1nRHEyX0a3jGPTCy0ovGQGxsoEnKc= 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.19 022/234] random: make /dev/random be almost like /dev/urandom Date: Thu, 23 Jun 2022 18:41:29 +0200 Message-Id: <20220623164343.691152260@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164343.042598055@linuxfoundation.org> References: <20220623164343.042598055@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 @@ -1988,43 +1986,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) { @@ -2056,15 +2017,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;