Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6034143iog; Thu, 23 Jun 2022 10:00:15 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vytlQXIPPPP4TC7ic5aW8t6eEnjk/PUt0aapQvSSpcgX/8d/0OwBje1OW1la7EXo937nkh X-Received: by 2002:a17:906:dc8c:b0:722:f40e:e653 with SMTP id cs12-20020a170906dc8c00b00722f40ee653mr8314600ejc.83.1656003615224; Thu, 23 Jun 2022 10:00:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656003615; cv=none; d=google.com; s=arc-20160816; b=xrElqZmPSzafoI0hpJAlD4CM9GGKqZkzRsGUTJx8vIHg9AruAvjQMMHEJuvqnvYuBa ZTkrnsf16+ITI8lyoczB1KZN38HAkAN74+e8W3yyIcgExIggAk9M4Qa5AzFkxFu4ojW2 C4RgZv50Z5cRxsy9VBQ6XyPl/fge/vCXjrRTAkEkUssUmQcea3BHa6qAvdJa2I1d8Jwo KGEqlFremkQs5Gh8mKj77gv+Tc4lGnfEcNhY/G84l5E6YG6eTe6KiSavZ3w3XGHk6MM1 yKCbw0rGaCuxgiLkSq/lVSwrCysgqxiPyeO8DY27HM2phYP8KZSuvv5EVUlj1p4Zjj5Y NQBg== 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=VyYHQHbWgx1pJtB/Tm739NhjjqldKdRAooCUPdP8/uY=; b=f2UXheztfx98Q6PPIvO6Aly375GDB/GXqpbQbTnK3ZwnC+9tyX8RFsFjRHSkoPk4nU ki+Kvrzwycu8+qHD8SLseBrHeCSaVYU3eeTQjty8fPnstF6vg4RWL7Amgb2Dm6u2I0LI GX4ct78ERsSqd39lbZUk7B0Gsi0cx5u3WahI7G47rSPFaBV3Bj1mET0LXToM7PUduP8k ffUER7UV9YOREJO62qxAwpQjN083Hzeg7Kku9YWQKj8psjNh6fK0Z9Bsu8JTm81SbNcE cvzNgT/khq1BsI7W2YVAl0t5LRSavYWTLIKTYivhbvgoepgtvd9MArYQ02+YYrQpafIc HOZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BHoU2prB; 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 dt13-20020a170907728d00b006fe992204c6si4814177ejc.684.2022.06.23.09.59.43; Thu, 23 Jun 2022 10:00:15 -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=BHoU2prB; 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 S233048AbiFWQ5X (ORCPT + 99 others); Thu, 23 Jun 2022 12:57:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233708AbiFWQvZ (ORCPT ); Thu, 23 Jun 2022 12:51:25 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AEF6506F5; Thu, 23 Jun 2022 09:49:32 -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 sin.source.kernel.org (Postfix) with ESMTPS id 0EEABCE24F9; Thu, 23 Jun 2022 16:49:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 07BEAC3411B; Thu, 23 Jun 2022 16:49:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656002969; bh=Y7iYCgSwsMda12QKXktsWuoDEuID8TlNrXx8exnzjOg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BHoU2prB3clgYn+oiJGgX1SWAab2gWcz7TFezqSymHr5GZdgdLuuNqUFcoDuf+eOw CorQIbxqx3HCZNLDHuWRN3wqo/cnefiA8JBRAULL5gdYln1w1xgJBYV6s4idiXpaS9 kriYeQ8H3+RWJusQjsbe6D1HGzurtuVGYcIWYxBI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hsin-Yi Wang , Stephen Boyd , Rob Herring , Will Deacon , "Jason A. Donenfeld" , Theodore Ts'o Subject: [PATCH 4.9 045/264] fdt: add support for rng-seed Date: Thu, 23 Jun 2022 18:40:38 +0200 Message-Id: <20220623164345.344162333@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: Hsin-Yi Wang commit 428826f5358c922dc378830a1717b682c0823160 upstream. Introducing a chosen node, rng-seed, which is an entropy that can be passed to kernel called very early to increase initial device randomness. Bootloader should provide this entropy and the value is read from /chosen/rng-seed in DT. Obtain of_fdt_crc32 for CRC check after early_init_dt_scan_nodes(), since early_init_dt_scan_chosen() would modify fdt to erase rng-seed. Add a new interface add_bootloader_randomness() for rng-seed use case. Depends on whether the seed is trustworthy, rng seed would be passed to add_hwgenerator_randomness(). Otherwise it would be passed to add_device_randomness(). Decision is controlled by kernel config RANDOM_TRUST_BOOTLOADER. Signed-off-by: Hsin-Yi Wang Reviewed-by: Stephen Boyd Reviewed-by: Rob Herring Reviewed-by: Theodore Ts'o # drivers/char/random.c Signed-off-by: Will Deacon Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/Kconfig | 9 +++++++++ drivers/char/random.c | 14 ++++++++++++++ drivers/of/fdt.c | 14 ++++++++++++-- include/linux/random.h | 1 + 4 files changed, 36 insertions(+), 2 deletions(-) --- a/drivers/char/Kconfig +++ b/drivers/char/Kconfig @@ -609,3 +609,12 @@ config RANDOM_TRUST_CPU has not installed a hidden back door to compromise the CPU's random number generation facilities. This can also be configured at boot with "random.trust_cpu=on/off". + +config RANDOM_TRUST_BOOTLOADER + bool "Trust the bootloader to initialize Linux's CRNG" + help + Some bootloaders can provide entropy to increase the kernel's initial + device randomness. Say Y here to assume the entropy provided by the + booloader is trustworthy so it will be added to the kernel's entropy + pool. Otherwise, say N here so it will be regarded as device input that + only mixes the entropy pool. \ No newline at end of file --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -2493,3 +2493,17 @@ void add_hwgenerator_randomness(const ch } } EXPORT_SYMBOL_GPL(add_hwgenerator_randomness); + +/* Handle random seed passed by bootloader. + * If the seed is trustworthy, it would be regarded as hardware RNGs. Otherwise + * it would be regarded as device data. + * The decision is controlled by CONFIG_RANDOM_TRUST_BOOTLOADER. + */ +void add_bootloader_randomness(const void *buf, unsigned int size) +{ + if (IS_ENABLED(CONFIG_RANDOM_TRUST_BOOTLOADER)) + add_hwgenerator_randomness(buf, size, size * 8); + else + add_device_randomness(buf, size); +} +EXPORT_SYMBOL_GPL(add_bootloader_randomness); \ No newline at end of file --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -27,6 +27,7 @@ #include #include #include +#include #include /* for COMMAND_LINE_SIZE */ #include @@ -1068,6 +1069,7 @@ int __init early_init_dt_scan_chosen(uns { int l; const char *p; + const void *rng_seed; pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname); @@ -1102,6 +1104,14 @@ int __init early_init_dt_scan_chosen(uns pr_debug("Command line is: %s\n", (char*)data); + rng_seed = of_get_flat_dt_prop(node, "rng-seed", &l); + if (rng_seed && l > 0) { + add_bootloader_randomness(rng_seed, l); + + /* try to clear seed so it won't be found. */ + fdt_nop_property(initial_boot_params, node, "rng-seed"); + } + /* break now */ return 1; } @@ -1203,8 +1213,6 @@ bool __init early_init_dt_verify(void *p /* Setup flat device-tree pointer */ initial_boot_params = params; - of_fdt_crc32 = crc32_be(~0, initial_boot_params, - fdt_totalsize(initial_boot_params)); return true; } @@ -1230,6 +1238,8 @@ bool __init early_init_dt_scan(void *par return false; early_init_dt_scan_nodes(); + of_fdt_crc32 = crc32_be(~0, initial_boot_params, + fdt_totalsize(initial_boot_params)); return true; } --- a/include/linux/random.h +++ b/include/linux/random.h @@ -18,6 +18,7 @@ struct random_ready_callback { }; extern void add_device_randomness(const void *, unsigned int); +extern void add_bootloader_randomness(const void *, unsigned int); #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__) static inline void add_latent_entropy(void)