Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6142619iog; Thu, 23 Jun 2022 12:13:45 -0700 (PDT) X-Google-Smtp-Source: AGRyM1suMWZu1ROnPx3ldR7PmYer6xzpcack2KDX5Q79RbVnIPSewGwyNiEISjukfufuhrTDwaca X-Received: by 2002:a17:902:f813:b0:169:8f5d:c343 with SMTP id ix19-20020a170902f81300b001698f5dc343mr37073073plb.98.1656011625774; Thu, 23 Jun 2022 12:13:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011625; cv=none; d=google.com; s=arc-20160816; b=hDVVWYIZ/ZuSCgKYkwnsBpyDIyyTcKR7baSovtVi3AzTxHqCYrXYfogRSNOCfcusjD BgB08o+rnNE2k1VACbBKKNanKvcbwl1HLCTGJYGPC6WqJb6ttgr8m21nLlHJ+x1Sa7b2 9HpBUpipOG91ohsLynG79WXih9338ruvT6uy30gq2xM8yzbS6HZ2SSM6Hn2oJzbtKYpq gyHCWokRo8WYzC9rSs1mmexMrbp2yJqtjEysCbCE0DU5GlkD7k25WO2MYNUVLLLYQFOx qpErcPlgGzAkChpCR1N6q2yynFyb4/j+yze+xHzAWEqPyB43LmCkQaGcxg4/243HPbZn gaSQ== 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=2sgK9JuM06SybrXFH0ueAamlXakrO0ZM0IANTwN12Ps=; b=GqqwKBoU2Z3xsONm/iZcypbXKvlSRhSfzxbaHrgKwy9xzgI/vsc++km8sQTwqUFZsm 8sgomJtCn0ArmEmJjlfkSEeqUbFNBvC1mG6knkQJfJIvHTYIWYesbrRnIQjnqWxnZQ2U oCtzpTkASw2zx4kXm1Xz2xsThm6/uoDoBqt+1KjceDMB5E0okw/OBZxtTH/CGj/8oWqS 5oQl/HxDoEmWztfhFnTcN+rgmUAeipBxBKvqAanflIe8uS+GCk88J77RHFT1K8Rmz5Oo +1CX+eiLOTt7ouj4j+7JgM8ZjcBdXYMM7K6C0IT9X313Sln4Bq3rvu0b9OtPiTvp8+Sr IM9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jm4R11S6; 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 x11-20020a170902ec8b00b0015eafbef962si548681plg.31.2022.06.23.12.13.34; Thu, 23 Jun 2022 12:13: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=jm4R11S6; 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 S236129AbiFWSG6 (ORCPT + 99 others); Thu, 23 Jun 2022 14:06:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235877AbiFWSGK (ORCPT ); Thu, 23 Jun 2022 14:06:10 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F006960C74; Thu, 23 Jun 2022 10:18: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 dfw.source.kernel.org (Postfix) with ESMTPS id 8577461E07; Thu, 23 Jun 2022 17:18:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4ED8DC3411B; Thu, 23 Jun 2022 17:18:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004704; bh=598Hl7kIDIGJ5LdbwkPEKiM7nzzTHD9/w2CWnbQ9xv0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jm4R11S6ieZEOfuBtbwQnRdTwgA6al5zlkkh4bect3V03oLeq3PNhiPq8BpsltGWS clWIcApFAx4DOG27mZnrXIUiKo2AMYD4nZUlM2+XuhMFQ0bf9Z7U+qwURkbTRnJT4q 8jyD8GBVDJJJtNwahcGwSKNbHJXyA0JyGWzGQPxo= 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.19 121/234] random: reseed more often immediately after booting Date: Thu, 23 Jun 2022 18:43:08 +0200 Message-Id: <20220623164346.481418713@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 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 @@ -336,6 +336,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. @@ -368,10 +390,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);