From nobody Fri Mar 11 02:12:14 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 2C9D01A17D3B; Fri, 11 Mar 2022 02:12:15 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KF8Zk5LfMz4bRp; Fri, 11 Mar 2022 02:12:14 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646964734; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bJ7d+61OJdQ/0AVtXcrJ9E72QFZNvmP13t0JYrsPjYM=; b=H/jZyNorxaDUuZTIfPAdZy+T8fvRm9P3fZw1qGs9uDEFLlgLVsZM4RQZo8hDtjgNJCDt4a EGgUEz/oZLtjIjOW7mQI4Cs7IIFrajkHIx+Lh6E13jbxvVbRlP8GeaeUv1SE40vK3e/fVG YYvmEWvSH29OfjANuXUUKycIW+kbVYndiuvKvJaUJOiaqr5Rc8uy3V7wPe32DJNSECsqJ6 I3ZnTNSmgDfNsupS/elwDdxAr6OakSorr49Qyf6hl2cIYtXKVQvRoUVVxOmjMq64dxxN/U +Xn8b1YrZuMdB+E7FVCIpRRHWWCosDVNcvMRbuvNsjXpMcpJseVDwstqS8+KXw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 2B53410F8D; Fri, 11 Mar 2022 02:12:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 22B2CEt2028173; Fri, 11 Mar 2022 02:12:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 22B2CE0N028172; Fri, 11 Mar 2022 02:12:14 GMT (envelope-from git) Date: Fri, 11 Mar 2022 02:12:14 GMT Message-Id: <202203110212.22B2CE0N028172@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Colin Percival Subject: git: dd6c1475a63a - stable/13 - Add support for getting early entropy from UEFI List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: cperciva X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: dd6c1475a63a749a34098392a457f1a6f9c48fce Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646964734; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bJ7d+61OJdQ/0AVtXcrJ9E72QFZNvmP13t0JYrsPjYM=; b=Jd10wG3UHuJ4NEP8e2wJAAxm1OXqoc1ywkBG937obgrUD53Tzc/kE665pfr/zyWF2NvkV8 H0CxWma2hIsRxVq2qqNIu4N1c/QJQbrbfFeeVPlTdD+qaXU48koP4yTBgJ/BU8MTVKyZFn hU0nbOvPUs2NnS788hggyVJwyOhSU9D4vQ96t1LfjNS2FJjkAj7H5X6lQ2hVuI4CEBxSF0 8jIFECwDTxsS6SOUd55KnB2wFcuGC7tz9SNY8DVOQ2u96jimAWk6lpTQ1RPufm7SV5StIe Zj/BblFbpvST39Dr/sAU5kM64Bqs1Yu+9rPvlB4ZhDLnIs5/gr6M8VjoxzrQNQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1646964734; a=rsa-sha256; cv=none; b=jjfUT7L8dWKS5vwzoQ52UAHZD0hceOdVB/a4WGVT7JvtlBhHPci8kJ39/KqjnuiWvsRcJx K5QPuufgdNJYZjYoNYuVH1uBJ6DBj2aGXl+wwcaiJNbSpDu79apzaBdhy4DO7AlHdVr8XX /NklxCXQWNxvVoa+sKBxcNFEl2JcRz3705EqscHeFtUxxDqBYGiq1XOkYtXiY0oZDVQHX2 AoCdRIvsmwSw2PasraoGatQ6T7y9lDoFPj78V6s+f0O3Wmsqold9zaRzaYw6gSGjSeIO8Z eiYd5rPIhQBRD6IIPElY/1A4Qxv0nZUsM0XhzXtTkopOl84cbLu4cDZrh4uI6w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by cperciva: URL: https://cgit.FreeBSD.org/src/commit/?id=dd6c1475a63a749a34098392a457f1a6f9c48fce commit dd6c1475a63a749a34098392a457f1a6f9c48fce Author: Colin Percival AuthorDate: 2022-02-17 21:01:11 +0000 Commit: Colin Percival CommitDate: 2022-03-11 02:11:41 +0000 Add support for getting early entropy from UEFI UEFI provides a protocol for accessing randomness. This is a good way to gather early entropy, especially when there's no driver for the RNG on the platform (as is the case on the Marvell Armada8k (MACCHIATObin) for now). If the entropy_efi_seed option is enabled in loader.conf (default: YES) obtain 2048 bytes of entropy from UEFI and pass is to the kernel as a "module" of name "efi_rng_seed" and type "boot_entropy_platform"; if present, ingest it into the kernel RNG. Submitted by: Greg V Reviewed by: markm, kevans Approved by: csprng (markm) Differential Revision: https://reviews.freebsd.org/D20780 --- stand/defaults/loader.conf | 4 +++- stand/efi/include/efirng.h | 50 ++++++++++++++++++++++++++++++++++++++++ stand/efi/loader/main.c | 42 +++++++++++++++++++++++++++++++++ stand/lua/core.lua | 10 ++++++++ sys/dev/random/random_harvestq.c | 8 +++++++ sys/sys/random.h | 1 + 6 files changed, 114 insertions(+), 1 deletion(-) diff --git a/stand/defaults/loader.conf b/stand/defaults/loader.conf index 6feb909d708a..24a47e75c39a 100644 --- a/stand/defaults/loader.conf +++ b/stand/defaults/loader.conf @@ -42,12 +42,14 @@ hostuuid_type="hostuuid" # See rc.conf(5). The entropy_boot_file config variable must agree with the # settings below. entropy_cache_load="YES" # Set this to NO to disable loading - # entropy at boot time + # cached entropy at boot time entropy_cache_name="/boot/entropy" # Set this to the name of the file entropy_cache_type="boot_entropy_cache" # Required for the kernel to find # the boot-time entropy cache. This # must not change value even if the # _name above does change! +entropy_efi_seed="YES" # Set this to NO to disable loading + # entropy from the UEFI hardware random number generator API ### RAM Blacklist configuration ############################ ram_blacklist_load="NO" # Set this to YES to load a file diff --git a/stand/efi/include/efirng.h b/stand/efi/include/efirng.h new file mode 100644 index 000000000000..3d6c77677981 --- /dev/null +++ b/stand/efi/include/efirng.h @@ -0,0 +1,50 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright 2006 - 2016 Unified EFI, Inc.
+ * Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ * This program and the accompanying materials + * are licensed and made available under the terms and conditions of the BSD License + * which accompanies this distribution. The full text of the license may be found at + * http://opensource.org/licenses/bsd-license.php + * + * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + * + */ + +#ifndef _EFIRNG_H +#define _EFIRNG_H + +#define EFI_RNG_PROTOCOL_GUID \ + { 0x3152bca5, 0xeade, 0x433d, {0x86, 0x2e, 0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44} } + +INTERFACE_DECL(_EFI_RNG_PROTOCOL); + +typedef EFI_GUID EFI_RNG_ALGORITHM; + +typedef +EFI_STATUS +(EFIAPI *EFI_RNG_GET_INFO) ( + IN struct _EFI_RNG_PROTOCOL *This, + IN OUT UINTN *RNGAlgorithmListSize, + OUT EFI_RNG_ALGORITHM *RNGAlgorithmList + ); + +typedef +EFI_STATUS +(EFIAPI *EFI_RNG_GET_RNG) ( + IN struct _EFI_RNG_PROTOCOL *This, + IN EFI_RNG_ALGORITHM *RNGAlgorithm, OPTIONAL + IN UINTN RNGValueLength, + OUT UINT8 *RNGValue + ); + +typedef struct _EFI_RNG_PROTOCOL { + EFI_RNG_GET_INFO GetInfo; + EFI_RNG_GET_RNG GetRNG; +} EFI_RNG_PROTOCOL; + +static EFI_GUID rng_guid = EFI_RNG_PROTOCOL_GUID; + +#endif /* _EFIRNG_H */ diff --git a/stand/efi/loader/main.c b/stand/efi/loader/main.c index ee55366e0c44..7752751efd0b 100644 --- a/stand/efi/loader/main.c +++ b/stand/efi/loader/main.c @@ -52,6 +52,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include @@ -1180,6 +1181,47 @@ main(int argc, CHAR16 *argv[]) return (EFI_SUCCESS); /* keep compiler happy */ } +COMMAND_SET(efi_seed_entropy, "efi-seed-entropy", "try to get entropy from the EFI RNG", command_seed_entropy); + +static int +command_seed_entropy(int argc, char *argv[]) +{ + EFI_STATUS status; + EFI_RNG_PROTOCOL *rng; + unsigned int size = 2048; + void *buf; + + if (argc > 1) { + size = strtol(argv[1], NULL, 0); + } + + status = BS->LocateProtocol(&rng_guid, NULL, (VOID **)&rng); + if (status != EFI_SUCCESS) { + command_errmsg = "RNG protocol not found"; + return (CMD_ERROR); + } + + if ((buf = malloc(size)) == NULL) { + command_errmsg = "out of memory"; + return (CMD_ERROR); + } + + status = rng->GetRNG(rng, NULL, size, (UINT8 *)buf); + if (status != EFI_SUCCESS) { + free(buf); + command_errmsg = "GetRNG failed"; + return (CMD_ERROR); + } + + if (file_addbuf("efi_rng_seed", "boot_entropy_platform", size, buf) != 0) { + free(buf); + return (CMD_ERROR); + } + + free(buf); + return (CMD_OK); +} + COMMAND_SET(poweroff, "poweroff", "power off the system", command_poweroff); static int diff --git a/stand/lua/core.lua b/stand/lua/core.lua index 67d51b992858..27e3c14de9ff 100644 --- a/stand/lua/core.lua +++ b/stand/lua/core.lua @@ -351,6 +351,14 @@ function core.changeRewindCheckpoint() end end +function core.loadEntropy() + if core.isUEFIBoot() then + if (loader.getenv("entropy_efi_seed") or "no"):lower() == "yes" then + loader.perform("efi-seed-entropy") + end + end +end + function core.setDefaults() core.setACPI(core.getACPIPresent(true)) core.setSafeMode(default_safe_mode) @@ -363,6 +371,7 @@ function core.autoboot(argstr) if loader.getenv("kernelname") == nil then config.loadelf() end + core.loadEntropy() loader.perform(composeLoaderCmd("autoboot", argstr)) end @@ -371,6 +380,7 @@ function core.boot(argstr) if loader.getenv("kernelname") == nil then config.loadelf() end + core.loadEntropy() loader.perform(composeLoaderCmd("boot", argstr)) end diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c index 563ff73a3d38..ff5d6f70f8a0 100644 --- a/sys/dev/random/random_harvestq.c +++ b/sys/dev/random/random_harvestq.c @@ -494,6 +494,14 @@ random_harvestq_prime(void *unused __unused) else printf("random: no preloaded entropy cache\n"); } + size = random_prime_loader_file(RANDOM_PLATFORM_BOOT_ENTROPY_MODULE); + if (bootverbose) { + if (size > 0) + printf("random: read %zu bytes from platform bootloader\n", + size); + else + printf("random: no platform bootloader entropy\n"); + } } SYSINIT(random_device_prime, SI_SUB_RANDOM, SI_ORDER_MIDDLE, random_harvestq_prime, NULL); diff --git a/sys/sys/random.h b/sys/sys/random.h index e2c4ab144d2a..2786cfad4b8f 100644 --- a/sys/sys/random.h +++ b/sys/sys/random.h @@ -108,6 +108,7 @@ _Static_assert(ENTROPYSOURCE <= 32, "hardcoded assumption that values fit in a typical word-sized bitset"); #define RANDOM_CACHED_BOOT_ENTROPY_MODULE "boot_entropy_cache" +#define RANDOM_PLATFORM_BOOT_ENTROPY_MODULE "boot_entropy_platform" extern u_int hc_source_mask; void random_harvest_queue_(const void *, u_int, enum random_entropy_source);