Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6146528iog; Thu, 23 Jun 2022 12:18:17 -0700 (PDT) X-Google-Smtp-Source: AGRyM1volrPCSD4Cb/TGXMAJ35KpDtCLxfssINEXxTQ5asJln6dE5SxWmGpIB1X53QgkVaY3DIGO X-Received: by 2002:a05:6a00:1acd:b0:51c:795b:860c with SMTP id f13-20020a056a001acd00b0051c795b860cmr42911997pfv.16.1656011897544; Thu, 23 Jun 2022 12:18:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011897; cv=none; d=google.com; s=arc-20160816; b=AFYNf3ebFh0TplVxZ58Cyf3fs/Lsaxc47ZMZ/qBlo8xPL6jeAFpjZE1rMSf2KO5/SG zpCmw6+n2yUhpMabusu4f3gWvX+XJyjNEDQkpHeAb6ITwIr/jrno9M0a2atUSPaEKZyQ 4HfKWMHwX3wZJReHGKsibujw6vZ18UUNpOvii2APnrGYPRqV3N5AJzpLEnIOZo5/Y7gp DpwhuLV8Gx/XmEm1Lud9hxyFi2S3HypKMr229OQP8tLsKsU3U6cNry5FrXosbs43zU4X X3tMo0J8Rn+6rgKWXFgz6nk8ViPJk59ws8KRyAasrNs6FtmLsXmP2s/PLDkqh+zfRh3o 4iiQ== 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=uXZrbpNjj+dOvbJfTRJ/m5V0bel5Eg5Wme8GA7Q/yOg=; b=0g0mUVGsLymMWDJkPn0jLf7cG9x0c72FY7/rL+wP3gCOmp50wuxqzZmg2CjtY6fjin jyNIikC7NhF5oxgYxMCDOY/jbM15Nf2+kKv7Jo8ip2hijDKet7iYrbSfVm0ro6iJ6rDK Mo3zu1yfGTKQ92JLpyuB8J9cW6PxJtrqtZhVxnSfhm//AEVUbCi8IgdtH+G937AY5tL0 CFinCzwEtRVrgNcOBAstuKD8O01GzL18kw0uT8qLtHy3nF6iHcKcbHhJ0GbZnivunyDi R8Va6dv0AcoFHswEoPSsTMEFKidpa7t262A3hKfmOvwrMyUxE/NPLhGl2efHem8uU33O hf7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ZNE4duzv; 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 j64-20020a638043000000b0040c9f150eacsi15953793pgd.568.2022.06.23.12.18.05; Thu, 23 Jun 2022 12:18:17 -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=ZNE4duzv; 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 S233724AbiFWQ6Y (ORCPT + 99 others); Thu, 23 Jun 2022 12:58:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233483AbiFWQvD (ORCPT ); Thu, 23 Jun 2022 12:51:03 -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 063E24B42F; Thu, 23 Jun 2022 09:49:00 -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 8827461FC3; Thu, 23 Jun 2022 16:49:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6792FC3411B; Thu, 23 Jun 2022 16:48:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656002939; bh=x4A2+oNxm/tBREbzWPgS26fuDSRD1nOucw18xy2kmpE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZNE4duzvuvuLKLz/osiqGzqAFPfmrCa2q6BPhiEao/jslDFxF4mCLJsO74OQO0tA+ TLdDzIM5ZsHVmM4oGfqhksczVf/wZuF1A2ebzXrqMf+B6Ktp+hcz2wsFW/6uXQCoTh 2IEVwRhIjaxSRw6zIXry1jR3FsSAett3T/NuDr/o= 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.9 075/264] random: add arch_get_random_*long_early() Date: Thu, 23 Jun 2022 18:41:08 +0200 Message-Id: <20220623164346.192817801@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: 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 @@ -6,6 +6,8 @@ #ifndef _LINUX_RANDOM_H #define _LINUX_RANDOM_H +#include +#include #include #include @@ -114,4 +116,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 */