Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6145783iog; Thu, 23 Jun 2022 12:17:21 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tdHkYvhyn9dwJ0TT2gNnlBFRTUZjB8MyhHTEz4AMANAFdzhj/DJn64n9MDzeVptCwRcd0m X-Received: by 2002:aa7:81c6:0:b0:525:21a0:e57 with SMTP id c6-20020aa781c6000000b0052521a00e57mr23447561pfn.48.1656011840766; Thu, 23 Jun 2022 12:17:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011840; cv=none; d=google.com; s=arc-20160816; b=upswE9erZKdj9vLmCqqneKXUUrQ+vDuXQqBAVB+LrTxm1NbHK7r+ci5IjElWrXXdP4 EmSelsfj3rJ/c67J+xwguqpzu8hZTMpHLRduYyjC/GaYI73FddR2pDaaws+mgqIkKReb bgZ7NWo0ov5BuswFItOi6ucD0BXBC28P869/iXU/zcCqT67WADAJFBwFBZu7rCBsDDcd fEFoa68CXgMiHNOpQY5T4Lzk8WuyMXi8gyhEUFpVSHRhTiwGMt25JErY0iNAqvVDq+ci DplzyCBy03HxQmg7z7B3HbefAHgV2htUqlvnZ9eT4uiYzCnAzHH62LBpq/z5J7GefvqW TtzA== 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=PzeQxKgpadzT6foAUV+0//L/mdDdVN4xcwP0dPhpmeY=; b=tbp1gI8wrFpUnRBFcGf583ckcAf3kBLvCO316PQt806i//wc++9OVpUr+ecWbFNxyB ZmUVakfrZVI12Ev/+6sXWk7Qa/58WNOlE8CdDW0dQUceZsJ8lNRr872+CpPW+QPlDx7j xBywuSk7U7ZDVH4pT5j70yeIcGU0sz5pHvHljuXuRGKXeStwuB01aFF5iKyRfznAxcz1 CZvWDmEzkXD7Dss+wtVjYmo4AonTGL2c+zxfxjFt/6GIhMUK2rAUQuIjlAxiutNkG9uy hKQSzOzz8lz7hsbZkAl2SakLlCc1HNpb2FmGqLKIzi8AlGqtyJ90jb8MEXLOAEP2tFwf fhNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=JwFYcGz7; 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 s34-20020a056a0017a200b00525659add83si1687842pfg.294.2022.06.23.12.17.09; Thu, 23 Jun 2022 12:17:20 -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=JwFYcGz7; 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 S232677AbiFWRCH (ORCPT + 99 others); Thu, 23 Jun 2022 13:02:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233520AbiFWQ5w (ORCPT ); Thu, 23 Jun 2022 12:57:52 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35E394EDD1; Thu, 23 Jun 2022 09:53:39 -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 D82EC61FBF; Thu, 23 Jun 2022 16:53:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9A2FCC341CF; Thu, 23 Jun 2022 16:53:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003208; bh=Bg9ECFE1CKePOK7lsrlIrQAb49dhB0JOAbwxVSGLmxg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JwFYcGz7Z0u+OJPS8IptX8REzN8++m10CH52zw730N0dot/7caektlaPrRVMaQa4b 49JKlFuep99Zq8WjFedHZ4P7ZAmijXXUU/PM1lIK84O9wXgMlSvc5z5jiMZtJXiz4b atMeNnAP2yEKJVfACb4DNM3vsC392NULWyJy2O9I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Theodore Tso , Eric Biggers , "Jason A. Donenfeld" Subject: [PATCH 4.9 164/264] random: reseed more often immediately after booting Date: Thu, 23 Jun 2022 18:42:37 +0200 Message-Id: <20220623164348.704828555@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: "Jason A. Donenfeld" commit 7a7ff644aeaf071d433caffb3b8ea57354b55bd3 upstream. In order to chip away at the "premature first" problem, we augment our existing entropy accounting with more frequent reseedings at boot. The idea is that at boot, we're getting entropy from various places, and we're not very sure which of early boot entropy is good and which isn't. Even when we're crediting the entropy, we're still not totally certain that it's any good. Since boot is the one time (aside from a compromise) that we have zero entropy, it's important that we shepherd entropy into the crng fairly often. At the same time, we don't want a "premature next" problem, whereby an attacker can brute force individual bits of added entropy. In lieu of going full-on Fortuna (for now), we can pick a simpler strategy of just reseeding more often during the first 5 minutes after boot. This is still bounded by the 256-bit entropy credit requirement, so we'll skip a reseeding if we haven't reached that, but in case entropy /is/ coming in, this ensures that it makes its way into the crng rather rapidly during these early stages. Ordinarily we reseed if the previous reseeding is 300 seconds old. This commit changes things so that for the first 600 seconds of boot time, we reseed if the previous reseeding is uptime / 2 seconds old. That means that we'll reseed at the very least double the uptime of the previous reseeding. Cc: Theodore Ts'o Reviewed-by: Eric Biggers Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -337,6 +337,28 @@ static void crng_fast_key_erasure(u8 key } /* + * Return whether the crng seed is considered to be sufficiently + * old that a reseeding might be attempted. This happens if the last + * reseeding was CRNG_RESEED_INTERVAL ago, or during early boot, at + * an interval proportional to the uptime. + */ +static bool crng_has_old_seed(void) +{ + static bool early_boot = true; + unsigned long interval = CRNG_RESEED_INTERVAL; + + if (unlikely(READ_ONCE(early_boot))) { + time64_t uptime = ktime_get_seconds(); + if (uptime >= CRNG_RESEED_INTERVAL / HZ * 2) + WRITE_ONCE(early_boot, false); + else + interval = max_t(unsigned int, 5 * HZ, + (unsigned int)uptime / 2 * HZ); + } + return time_after(jiffies, READ_ONCE(base_crng.birth) + interval); +} + +/* * This function returns a ChaCha state that you may use for generating * random data. It also returns up to 32 bytes on its own of random data * that may be used; random_data_len may not be greater than 32. @@ -369,10 +391,10 @@ static void crng_make_state(u32 chacha_s } /* - * If the base_crng is more than 5 minutes old, we reseed, which - * in turn bumps the generation counter that we check below. + * If the base_crng is old enough, we try to reseed, which in turn + * bumps the generation counter that we check below. */ - if (unlikely(time_after(jiffies, READ_ONCE(base_crng.birth) + CRNG_RESEED_INTERVAL))) + if (unlikely(crng_has_old_seed())) crng_reseed(); local_irq_save(flags);