Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6140069iog; Thu, 23 Jun 2022 12:10:45 -0700 (PDT) X-Google-Smtp-Source: AGRyM1toz4M+JDzRbNdA3/kn+zzyi3X//WQjTJuxSvWdav2WaEj49bDRtH9K8m9YDBRmVY3Sf4tR X-Received: by 2002:a65:6e04:0:b0:40d:26eb:8225 with SMTP id bd4-20020a656e04000000b0040d26eb8225mr8687129pgb.138.1656011445741; Thu, 23 Jun 2022 12:10:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011445; cv=none; d=google.com; s=arc-20160816; b=VKwy2qc/397yp7bW/ubY9huN9sp7qdD0wGdwb2CDA+HkG5HvY55scgcRLmWczcMAPA g7SHBEAnvc5RY4a4+DcEVpFuWCrLcCaKHyphIDfGIkQO5aRcm9LPi9S2gpN8GFUhyjXK +P3GzJBgXRM8OX68FDY9hJk0dcnlroZduJdYVT/IJqB6JvD68NenSpmEuS2bnigSBXJp VvYMF6lUBtEJkojbYglVJWsNoJWsWDiTLgMCwtP22+yNSFLghcKYHlL5F8WudtLnncz1 L4op7TP1eI/5+hqIH2idbKnKoaTQ7ogBR5j8y0JGQwwfRDdr6M4W9e7stGpf6vt26Vml YrSg== 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=5MHZQXjgC3MwTyMehKrskg7ojcKOr3KmxsHl8PnzAJk=; b=H9Yp55UCWDtoa73ZFYzIAwrIWuJDSvvoL4dlrMFkOaIclR3lqy+5ubIW200XCR51di 4//v/mU2jWMOPEq9rP6NZB0oaGxR/+XabGXQg/TN5RG3bhU6ku/iF8crTCOtaviw2Ye/ Yqne/Ap20/GOi0XeaNy4D6evb3KhH8wyCZRFMpsTEXHM5by72p9wLQ1Y0YfOPzWix5Xq XMA1LL7lo0hRcB+2xB27WxSZtZFJfRXQXGLT5NlbrBh4GiM8awE6gEj7ybnl8S1v2xEP aR7QJbbkMNH+HLP5+cbZkJM0QTgM3p8ZeWR6hJ6g8wgR11qNAO9LXVNqQTuYS42c8vO4 mjEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yM5ayYg8; 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 f5-20020a625105000000b0052527705946si37432pfb.47.2022.06.23.12.10.33; Thu, 23 Jun 2022 12:10:45 -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=yM5ayYg8; 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 S233228AbiFWRRd (ORCPT + 99 others); Thu, 23 Jun 2022 13:17:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233273AbiFWRMn (ORCPT ); Thu, 23 Jun 2022 13:12:43 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC0D52F3; Thu, 23 Jun 2022 09:58:25 -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 89444B8248A; Thu, 23 Jun 2022 16:58:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E9365C3411B; Thu, 23 Jun 2022 16:58:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003503; bh=9P2pvINs/sgb/fVH7QEjMuFU0EZR1PdrE2W4adZFrjo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yM5ayYg8mqfqhTeFAnC9LWv8l6NzAu2JivxQNIo+PtnNENf7H6i2EU6yDGD5BF5Pc vv1KOa4as4eqMyqL6dfp1yCifZ1FkydDs4iO8idtri6PxetqZhHdgQ6vq2CNAHQnvy DU9gOXeWbBQHcOyk7tgGt9X/n2aarx/LlfJdz41w= 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.9 219/264] crypto: drbg - move dynamic ->reseed_threshold adjustments to __drbg_seed() Date: Thu, 23 Jun 2022 18:43:32 +0200 Message-Id: <20220623164350.273618284@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: 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 @@ -1048,6 +1048,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; } @@ -1096,9 +1116,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); @@ -1532,12 +1549,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; }