Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6133038iog; Thu, 23 Jun 2022 12:02:59 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tgdx5MOZflxlcTHWGxhda6eHMFkbKTO64VxlbOYVxCrcFUBGZZxWokOlyMY466AxjpBCNp X-Received: by 2002:a17:90a:4216:b0:1ec:a909:2b16 with SMTP id o22-20020a17090a421600b001eca9092b16mr5375351pjg.86.1656010978773; Thu, 23 Jun 2022 12:02:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656010978; cv=none; d=google.com; s=arc-20160816; b=pmMVxqC7fkII4it/pWpPjc72Th8LFj19iY5N2Fau+QUEHerOUo+HQW3KbRJqwD+3Zu nNbPPat/PBilkjnt7T1aZPQp2WyqQMB2cIEYdMYX50T8KFbSDmjNockMbf2z1uI1r1nF qs78OcZH7Lz0j/lVeHiG3Ik2oV0IvnYKiMTah532/GMda2AxiFUyjH2HpCS9/dY+cvUi K2eTe8f/nRUu2vuMjc7p1JxyP6d/bCj4vEmAUtM9qiS54EemxzVKozv+c5AGo9Ll7PGe 1e84K0drrSWLuuFYFAmf/ax0hVy/dzaayjJJDtNQyylQVyYVyIRPQHd4TUPN4YVYVJ5y WLfw== 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=6REYd4eTMXXNVvHMISFrOXa/NwGLiA99IXX6IDcH/6c=; b=M7W3mZ8P53CVFYncVJE7fsHNxgAX25XWIGK4WSxZ+qwcGr3Ni0VbOqLfHtgvN1ENum jIz33KPLMxpjaQlW1x0CCmOGv9IleWW1nSdQWtE86kqFiRe1NmBEfctglCa95N0wGwyC kAXNrtbpfEqXcAAIwI/5C9cQnnqQ+0KXG6lfDZvc4+/7C8nR3Cse6gcHUNWDNMHSQ1Wg OP9zvfp6W/y7HVBGJvEEZ8LkdhrkUKv9i1K1s3ylvRyf52y+g2SDl7dLKgY4y2LhZS2R zgwQgs3Q6Fdx4jWDrUXZxBn0DPQNAfhvqPNTuNYl1JVkHkiDiVqvCquU2nykOzHiEuvI TTIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=sxbGiPnZ; 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 y10-20020a17090a600a00b001e86a88ccbesi241506pji.81.2022.06.23.12.02.47; Thu, 23 Jun 2022 12:02:58 -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=sxbGiPnZ; 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 S236584AbiFWSOH (ORCPT + 99 others); Thu, 23 Jun 2022 14:14:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236807AbiFWSM4 (ORCPT ); Thu, 23 Jun 2022 14:12:56 -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 9764ABFA26; Thu, 23 Jun 2022 10:21:04 -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 A04E661EBA; Thu, 23 Jun 2022 17:21:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77BB3C341C4; Thu, 23 Jun 2022 17:21:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004862; bh=CSjXoZG+OyNxQvyHaxh0K2/0jQfSGUQbCDszMIbQA6M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sxbGiPnZvTCHwbDrLfeBPryafuTQBJKnOzZBqWBwhtDqS97by3Myq2L0Eyl8/ovhe TvOL3OC3/QH7bMkQgUrYeC9TTBYGt1+9hWxfR/oKWHD7+9xAiuVfm6HY498uj268vG R5BCfB7J0TQPImET0LTG7wsnwJp6aftrcpR8rVOY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nicolai Stange , =?UTF-8?q?Stephan=20M=C3=BCller?= , Herbert Xu , "Jason A. Donenfeld" Subject: [PATCH 4.19 175/234] crypto: drbg - move dynamic ->reseed_threshold adjustments to __drbg_seed() Date: Thu, 23 Jun 2022 18:44:02 +0200 Message-Id: <20220623164348.003444202@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: Nicolai Stange commit 262d83a4290c331cd4f617a457408bdb82fbb738 upstream. Since commit 42ea507fae1a ("crypto: drbg - reseed often if seedsource is degraded"), the maximum seed lifetime represented by ->reseed_threshold gets temporarily lowered if the get_random_bytes() source cannot provide sufficient entropy yet, as is common during boot, and restored back to the original value again once that has changed. More specifically, if the add_random_ready_callback() invoked from drbg_prepare_hrng() in the course of DRBG instantiation does not return -EALREADY, that is, if get_random_bytes() has not been fully initialized at this point yet, drbg_prepare_hrng() will lower ->reseed_threshold to a value of 50. The drbg_async_seed() scheduled from said random_ready_callback will eventually restore the original value. A future patch will replace the random_ready_callback based notification mechanism and thus, there will be no add_random_ready_callback() return value anymore which could get compared to -EALREADY. However, there's __drbg_seed() which gets invoked in the course of both, the DRBG instantiation as well as the eventual reseeding from get_random_bytes() in aforementioned drbg_async_seed(), if any. Moreover, it knows about the get_random_bytes() initialization state by the time the seed data had been obtained from it: the new_seed_state argument introduced with the previous patch would get set to DRBG_SEED_STATE_PARTIAL in case get_random_bytes() had not been fully initialized yet and to DRBG_SEED_STATE_FULL otherwise. Thus, __drbg_seed() provides a convenient alternative for managing that ->reseed_threshold lowering and restoring at a central place. Move all ->reseed_threshold adjustment code from drbg_prepare_hrng() and drbg_async_seed() respectively to __drbg_seed(). Make __drbg_seed() lower the ->reseed_threshold to 50 in case its new_seed_state argument equals DRBG_SEED_STATE_PARTIAL and let it restore the original value otherwise. There is no change in behaviour. Signed-off-by: Nicolai Stange Reviewed-by: Stephan Müller Signed-off-by: Herbert Xu Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman Signed-off-by: Greg Kroah-Hartman --- crypto/drbg.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) --- a/crypto/drbg.c +++ b/crypto/drbg.c @@ -1046,6 +1046,26 @@ static inline int __drbg_seed(struct drb /* 10.1.1.2 / 10.1.1.3 step 5 */ drbg->reseed_ctr = 1; + switch (drbg->seeded) { + case DRBG_SEED_STATE_UNSEEDED: + /* Impossible, but handle it to silence compiler warnings. */ + case DRBG_SEED_STATE_PARTIAL: + /* + * Require frequent reseeds until the seed source is + * fully initialized. + */ + drbg->reseed_threshold = 50; + break; + + case DRBG_SEED_STATE_FULL: + /* + * Seed source has become fully initialized, frequent + * reseeds no longer required. + */ + drbg->reseed_threshold = drbg_max_requests(drbg); + break; + } + return ret; } @@ -1094,9 +1114,6 @@ static void drbg_async_seed(struct work_ __drbg_seed(drbg, &seedlist, true, DRBG_SEED_STATE_FULL); - if (drbg->seeded == DRBG_SEED_STATE_FULL) - drbg->reseed_threshold = drbg_max_requests(drbg); - unlock: mutex_unlock(&drbg->drbg_mutex); @@ -1530,12 +1547,6 @@ static int drbg_prepare_hrng(struct drbg return err; } - /* - * Require frequent reseeds until the seed source is fully - * initialized. - */ - drbg->reseed_threshold = 50; - return err; }