Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6138366iog; Thu, 23 Jun 2022 12:08:47 -0700 (PDT) X-Google-Smtp-Source: AGRyM1snpUa37L5tPefN1M3/iiPNDfDMbxUkLLENjr7XRkYU1wTvRkGpSywY63hr9dQM6UkStdqi X-Received: by 2002:a17:902:d48a:b0:16a:3e7f:db21 with SMTP id c10-20020a170902d48a00b0016a3e7fdb21mr13319853plg.50.1656011326897; Thu, 23 Jun 2022 12:08:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011326; cv=none; d=google.com; s=arc-20160816; b=1KJXZGbvKoJdSCZlCpaaTL0UnInOMdM6226aC7WcOBEhpS6qNU/VjgxloBWcOR231l xEImxsNrVrUfGT/qCJaeGXS6ODvvilpHeCcju3aT+r+iq6MK77WBALa2n9av/yOHaTXv Wphe10WLXel2WLnXe28TRlPV3pqd1qSHdb0Wm2Dhcf3g0JIoRklCLsQ9Fphwws3cA3DM 0i94RtXSIrl56F3INwcUiBWy0QSml8MaZRDnTtFA+EWUiJXkC1UfawFl69iO3BQrdQTY mG9FdbV5ULxgZh3HQL0iQTMbeUOdtohX5uZSkWgoBn6tUilXUnEqIdtaij5av3IU6kmd YIjQ== 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=P+NLy47W9pTohL5OiHDBk0XucN58yxvBRm8otnvYcVY=; b=0IibWfDnEsZNOBUVA42LVJPfk+a0pA326nU/D+GW4b7TCSrXRnbaAJU84XDh05g297 PFEYAl6UQcNawSdediaSi5z31N1hRhAv1miAz6qT05z91XF/a08COgBrvzihe9idWEUh Tp+fJbB0VRDuljOtPFi4SP/adSA4Z9TJpFK+947lUTVS4k5NeY/l/HK8ya4Piyk+v0Vd GhtAA3GdKqXfG7NvtedrEUgXCO+f6TTgriJaQWD2I/isHFZVuhsh3qcLBh4MfoV7luO+ kqIvpRCBVIyOdNw76VRkKBZs1Ye9xsTv+TM7HtVXVy9iW9TF3WiUiNhWzYxoFS+5ST6K yyqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QFSqdJRV; 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 c11-20020a170903234b00b00161739962ffsi468740plh.163.2022.06.23.12.08.34; Thu, 23 Jun 2022 12:08:46 -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=QFSqdJRV; 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 S235765AbiFWSEN (ORCPT + 99 others); Thu, 23 Jun 2022 14:04:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235966AbiFWSCt (ORCPT ); Thu, 23 Jun 2022 14:02:49 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74A82B4AB1; Thu, 23 Jun 2022 10:16: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 ams.source.kernel.org (Postfix) with ESMTPS id 3998FB82490; Thu, 23 Jun 2022 17:16:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9B11BC3411B; Thu, 23 Jun 2022 17:16:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004617; bh=NWvMlAI05IB/mQN1UuiQajR7DeI2LADwiDVtC9A8hu8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QFSqdJRV8z2Tl94bzyj3a8TABW3ur5PiVMnKAgiZV4GblvAoCjeCn6besBmDaW1+E hJ8vj1NJ/RylM62uQKNqMX+CHN8v5Z8/bUNtH2YNPcsFQ+vKw5rq17jwhWd9+sjSKc 2Q/uh0PhJx3J4SV5XuFIkJse0Sb1EsuPcSqIZpyM= 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 4.19 096/234] random: introduce drain_entropy() helper to declutter crng_reseed() Date: Thu, 23 Jun 2022 18:42:43 +0200 Message-Id: <20220623164345.774737183@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: "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[CHACHA20_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))