Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6145399iog; Thu, 23 Jun 2022 12:16:55 -0700 (PDT) X-Google-Smtp-Source: AGRyM1urWhvigh+A8HeGi/dVI3Gk59pWzCB8c3L2j5sZmgg6HjL5KHlyNP8ormnfC0doD5Vcxb5J X-Received: by 2002:a17:906:7292:b0:712:3880:3e7b with SMTP id b18-20020a170906729200b0071238803e7bmr9431229ejl.118.1656011815172; Thu, 23 Jun 2022 12:16:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011815; cv=none; d=google.com; s=arc-20160816; b=ADmrqsktbKn72LlbzYnKXQh4o4p6JFXjnh5fU/L3M8FCu+rmx72DnOnlr7iOur2+n7 N0hBg25jguXqCc29KA7vSXZGSPIblbSScmHVU3PCTKCigCegfySaAgbIaHn5eQ9xU//k YoUdVdYWpcDccxcJxFPzkoZ6kX63Ez8vjASKHXtf2QAfytqUeQR8NViyD/rvtrrtWdDL FZUfEK0d29VLa3N8F4I/MEynw/NO0UGU6P/nYOFBkel8McUHEhmId2rZh6SBFoT0WmJX 3Q8cy2KTwY8vrSkqykf2EXzfNlCbZGs55m3Zs3oaD1Dy6r/Yo1+NA9jcjMdKztSZoZuD yqsA== 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=DxswS0qhZhaJ2hIIdPudM8OLaUoCzM3v9VRqDBQPKlM=; b=e/AT/k51TOnklio4yRXic86dMbnGJMmJt1pHRDzrf02Br6O4Gc01yZ70Za8olEciSm OGfGzGXM18x1LnA41RoQZW3b4zgzhlnTH2CmbPX1G7vhnpTVGGM72AzKuZWPpVMR8DOT v54EuB30ALKtmER+yrQVjpOu5o3DyjmZ+smo5F76zNHtrXXIOOSupCioxF8NbJASfcB8 vhgHNrz70DoDKNtRPfTJDWz+F2OAgDvZwHnF1J+tPNXqEq4GndlY+rVfElXogebEKyW0 F+3VAiemuOBhU1qFRwM/HbgcMuXz8WmuSfimKjOIxIY26nsuvJfeOgTohbES5wZEYN4c uKqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=wuxGNLzQ; 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 ga21-20020a1709070c1500b007121b9fc6f9si23876942ejc.954.2022.06.23.12.16.29; Thu, 23 Jun 2022 12:16:55 -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=wuxGNLzQ; 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 S235673AbiFWSBi (ORCPT + 99 others); Thu, 23 Jun 2022 14:01:38 -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 S236377AbiFWR7O (ORCPT ); Thu, 23 Jun 2022 13:59:14 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52846B2133; Thu, 23 Jun 2022 10:16:23 -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 02731B824BD; Thu, 23 Jun 2022 17:16:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4EAC1C3411B; Thu, 23 Jun 2022 17:16:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004580; bh=ItNpXmB9KremrNBDFMKBa3jxjg/gsnDbp4nq9HlkHlE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wuxGNLzQ6kAvGfF+ceul87Y1tgY+hCWfK0Q0laLn1EQr3yKug5AoOfcTlxP5j/SbN MuMOZr9Z434ZYYuKPTUSdNksOrbdjOhB48D+3H+wfdu/5fn+YwlfB/9Q6Ri5TT/lCV PTrDTSbbXBmu9cWbRuDb21frbUQ1Ysombc3KqQQg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mark Rutland , Mark Brown , Theodore Tso , "Jason A. Donenfeld" Subject: [PATCH 4.19 042/234] random: add arch_get_random_*long_early() Date: Thu, 23 Jun 2022 18:41:49 +0200 Message-Id: <20220623164344.256101334@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: Mark Rutland commit 253d3194c2b58152fe830fd27c2fd83ebc6fe5ee upstream. Some architectures (e.g. arm64) can have heterogeneous CPUs, and the boot CPU may be able to provide entropy while secondary CPUs cannot. On such systems, arch_get_random_long() and arch_get_random_seed_long() will fail unless support for RNG instructions has been detected on all CPUs. This prevents the boot CPU from being able to provide (potentially) trusted entropy when seeding the primary CRNG. To make it possible to seed the primary CRNG from the boot CPU without adversely affecting the runtime versions of arch_get_random_long() and arch_get_random_seed_long(), this patch adds new early versions of the functions used when initializing the primary CRNG. Default implementations are provided atop of the existing arch_get_random_long() and arch_get_random_seed_long() so that only architectures with such constraints need to provide the new helpers. There should be no functional change as a result of this patch. Signed-off-by: Mark Rutland Cc: Mark Brown Cc: Theodore Ts'o Link: https://lore.kernel.org/r/20200210130015.17664-3-mark.rutland@arm.com Signed-off-by: Theodore Ts'o Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 20 +++++++++++++++++++- include/linux/random.h | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -801,6 +801,24 @@ static bool crng_init_try_arch(struct cr return arch_init; } +static bool __init crng_init_try_arch_early(struct crng_state *crng) +{ + int i; + bool arch_init = true; + unsigned long rv; + + for (i = 4; i < 16; i++) { + if (!arch_get_random_seed_long_early(&rv) && + !arch_get_random_long_early(&rv)) { + rv = random_get_entropy(); + arch_init = false; + } + crng->state[i] ^= rv; + } + + return arch_init; +} + static void __maybe_unused crng_initialize_secondary(struct crng_state *crng) { memcpy(&crng->state[0], "expand 32-byte k", 16); @@ -813,7 +831,7 @@ static void __init crng_initialize_prima { memcpy(&crng->state[0], "expand 32-byte k", 16); _extract_entropy(&input_pool, &crng->state[4], sizeof(__u32) * 12, 0); - if (crng_init_try_arch(crng) && trust_cpu) { + if (crng_init_try_arch_early(crng) && trust_cpu) { invalidate_batched_entropy(); numa_crng_init(); crng_init = 2; --- a/include/linux/random.h +++ b/include/linux/random.h @@ -7,6 +7,8 @@ #ifndef _LINUX_RANDOM_H #define _LINUX_RANDOM_H +#include +#include #include #include @@ -136,4 +138,24 @@ static inline bool __must_check arch_get } #endif +/* + * Called from the boot CPU during startup; not valid to call once + * secondary CPUs are up and preemption is possible. + */ +#ifndef arch_get_random_seed_long_early +static inline bool __init arch_get_random_seed_long_early(unsigned long *v) +{ + WARN_ON(system_state != SYSTEM_BOOTING); + return arch_get_random_seed_long(v); +} +#endif + +#ifndef arch_get_random_long_early +static inline bool __init arch_get_random_long_early(unsigned long *v) +{ + WARN_ON(system_state != SYSTEM_BOOTING); + return arch_get_random_long(v); +} +#endif + #endif /* _LINUX_RANDOM_H */