Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2933483iog; Mon, 20 Jun 2022 07:46:55 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vSvhMX0DNi2VgpP1Apk5bv8ncWzLJcHBorZtzB/oXH3d1MDmZcG2hXLtsE65K0m61V0nAS X-Received: by 2002:a17:90a:1c02:b0:1e0:df7:31f2 with SMTP id s2-20020a17090a1c0200b001e00df731f2mr38941291pjs.222.1655736414909; Mon, 20 Jun 2022 07:46:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655736414; cv=none; d=google.com; s=arc-20160816; b=vWfccNQPkutiLgoBB/k+ZtwyUxJZjTp+JPbTAQDNHaP1TLVNSZKKy1Nge1ShK1QkIi tXqWSYzHJWLKjilir7sRFjirl2b8c+12Sfss25w5chYYKX5bpIz0iZD9PiEzsZ4VTF5o Z5HHEK8FB/vssmdrfFTgwr9WAunx17MLSr/Ec9yLWTmFYmEk2QUssAGi1Jsjugmus+6c W/0vX5Mw0JiQYOMD1e8o+ACvQCunTgc1b5lOj+ICXokCKHk3/0mOAJB2EEveYgYDPK46 kaSA1Ou9eDjGQ8UtC8rNKQblqgPXLYUuR+X5mgIShIE5H7dYdmW3GYwll87K4evhx465 BqQw== 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=QBdyJz+Yz3p4nNere3QRyCpr1R8YLe7LNeH6K/lIXXw=; b=VYnUoYazIXTu4y/oX7jfKmpVA91e34f33TqDw+X5ZE0Uv9cbSHd6Z1VsG4x9keke44 bsp3+Sj+kEh3amsEderv+FLU+Wu7ICZ8JJPS/GP0jTBdEbqqiwHa8Ig7BW/JAog50L97 4K2mKucHUmuTFmkDw+JX/sCVLdmvMU9BdeaM2o9OKdST6Cu6SFBMhMkkSpDl5L4mN6lA WB11eIC78s8b3f9MxFLUaddHPjk7TITv45ZfznEuPU+jRj/rXOLjTmy6fB+5TxsqPC/3 Jhm9gmctfYz9LdiGRhIdRbY5FqjPb7RFpt8eCpvQqKc7Q8vB16OdNKkFYTNBT73iVPI1 YBjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="KZJ/gKHW"; 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 w184-20020a6382c1000000b00403fdb3af20si16747812pgd.722.2022.06.20.07.46.42; Mon, 20 Jun 2022 07:46:54 -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="KZJ/gKHW"; 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 S1348359AbiFTNl4 (ORCPT + 99 others); Mon, 20 Jun 2022 09:41:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349276AbiFTNji (ORCPT ); Mon, 20 Jun 2022 09:39:38 -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 C410C2A25E; Mon, 20 Jun 2022 06:14:59 -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 2161160ABE; Mon, 20 Jun 2022 13:14:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0ECC8C3411C; Mon, 20 Jun 2022 13:14:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655730898; bh=Fhh0+KeC4hJGdWsxqip2sdxqu4dNb3LZg3P2An9/C3o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KZJ/gKHWvU3ko9h55ObgGAzePw7wy4+hf0YbsESwns0/oIcJJRKVvJVqXlwi66+Qf nHKw1pLPRdSlg1KhwaMreRw7WsSXq4Xd/W1wruFsnJr0lgHB8WD5z9mh7e5eMiYQZs qWvagWycTuQY1O0JYqPSGTWkXPMpmRpQXMWXDnZQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Theodore Tso , Dominik Brodowski , Eric Biggers , "Jason A. Donenfeld" Subject: [PATCH 5.4 090/240] random: introduce drain_entropy() helper to declutter crng_reseed() Date: Mon, 20 Jun 2022 14:49:51 +0200 Message-Id: <20220620124741.621354299@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: "Jason A. Donenfeld" commit 246c03dd899164d0186b6d685d6387f228c28d93 upstream. In preparation for separating responsibilities, break out the entropy count management part of crng_reseed() into its own function. No functional changes. Cc: Theodore Ts'o Reviewed-by: Dominik Brodowski Reviewed-by: Eric Biggers Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -260,6 +260,7 @@ static struct { }; static void extract_entropy(void *buf, size_t nbytes); +static bool drain_entropy(void *buf, size_t nbytes); static void crng_reseed(void); @@ -454,23 +455,13 @@ static void crng_slow_load(const void *c static void crng_reseed(void) { unsigned long flags; - int entropy_count; unsigned long next_gen; u8 key[CHACHA_KEY_SIZE]; bool finalize_init = false; - /* - * First we make sure we have POOL_MIN_BITS of entropy in the pool, - * and then we drain all of it. Only then can we extract a new key. - */ - do { - entropy_count = READ_ONCE(input_pool.entropy_count); - if (entropy_count < POOL_MIN_BITS) - return; - } while (cmpxchg(&input_pool.entropy_count, entropy_count, 0) != entropy_count); - extract_entropy(key, sizeof(key)); - wake_up_interruptible(&random_write_wait); - kill_fasync(&fasync, SIGIO, POLL_OUT); + /* Only reseed if we can, to prevent brute forcing a small amount of new bits. */ + if (!drain_entropy(key, sizeof(key))) + return; /* * We copy the new key into the base_crng, overwriting the old one, @@ -898,6 +889,25 @@ static void extract_entropy(void *buf, s memzero_explicit(&block, sizeof(block)); } +/* + * First we make sure we have POOL_MIN_BITS of entropy in the pool, and then we + * set the entropy count to zero (but don't actually touch any data). Only then + * can we extract a new key with extract_entropy(). + */ +static bool drain_entropy(void *buf, size_t nbytes) +{ + unsigned int entropy_count; + do { + entropy_count = READ_ONCE(input_pool.entropy_count); + if (entropy_count < POOL_MIN_BITS) + return false; + } while (cmpxchg(&input_pool.entropy_count, entropy_count, 0) != entropy_count); + extract_entropy(buf, nbytes); + wake_up_interruptible(&random_write_wait); + kill_fasync(&fasync, SIGIO, POLL_OUT); + return true; +} + #define warn_unseeded_randomness(previous) \ _warn_unseeded_randomness(__func__, (void *)_RET_IP_, (previous))