From patchwork Thu May 16 10:28:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hsin-Yi Wang X-Patchwork-Id: 10946303 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 02AF9924 for ; Thu, 16 May 2019 10:29:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DF658288F8 for ; Thu, 16 May 2019 10:29:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D0DDD28978; Thu, 16 May 2019 10:29:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 00140288F8 for ; Thu, 16 May 2019 10:29:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Xlhe2qwtLykQ5F9uoP1wsF3GciRt+qUurLp0AClKlds=; b=uqdSss5EsdhamN iMdPb1NxO2PxvxM8ITp1B09z+4UtVnIlGO3tXvrFwlbcRT/VrtTxDrhJeqTb4rZAuJ/i61t+LvEs6 WHB9nq5UHNb2QU8B7h2AcCsRy14fYDZHX9TZGpm+fpi+wydAQqL0AKmOdNAgrKOf7nGQbbu6LYX4q 8PoZzxzkcwgUdVw79biBsWfsqOqfnfKfAnw+L7Qxc6pUyThfSbw0O/93pMjD3zoOIdkDeFgiyx8+Y CNrc5FRGfZ2XEoHxfMXvMUkm9uINnE6k7QAZsg2rBSrVxDXvako3Nixz5Z5aSkmttg5NVQ1NiQXK2 0dWaFfaDLZ3dPzRJ5gFw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hRDdK-00029n-8s; Thu, 16 May 2019 10:29:06 +0000 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hRDdC-0001zC-Uw for linux-arm-kernel@lists.infradead.org; Thu, 16 May 2019 10:29:03 +0000 Received: by mail-pf1-x441.google.com with SMTP id z26so1624967pfg.6 for ; Thu, 16 May 2019 03:28:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MnHxDH7/gQC7S9Ex4Yrjn2JKssZnvEJdzim34WmQ+q8=; b=Q7r9C3C+1xp3K+F79f6HS9pMPjdOXBJlhNlNH1sNo8Gf28sq+UxJPIbw2WS+1yYLw6 yZqgEDWASIraQfHYA0liC/dDovipJoQ9hzXfSk4dtix+XDB4gYK6M8Y3wfvVbl2uYTjW uf+2pUVkJmyMR0yFOwkMZUe0h831zzwbPUIRs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=MnHxDH7/gQC7S9Ex4Yrjn2JKssZnvEJdzim34WmQ+q8=; b=e7GeVzG7VGLVPEf/kPVwzQinJKN1aVyFju7u/U+ARF7qFXWRUsLFfUggIfXwPdWn7r M08mtvoNJTGV+kga6BPg7kGwOGfwo5ykZN4UC5G95E5zIAN/KKez19EY1LJ43+tN6o+c 66cdYbAZiiD8VbiQ4ye0CdXE18aPCZTdZ10DOo0rAEi15E94E89FLErIA5v0BhMjrYZE Tv1xODvrGXDBe2cncgl7q34OMAF599De7MFKFsXRDrX3/lWBlKcYX0BwgkFDfH73Dmos nGnk4GCIC1AT2M/nOD2fOYp9Dts9K3+RI+yNegEIvB4GdW1Tgj3ceG8wF3fli5712Tm8 8ijw== X-Gm-Message-State: APjAAAWIIj7ZWE9hCfgkxCxTyEJiCl4krWu/nGC0CzIHYs8Fu/N5VECW Xy8V+BQgglZKjn4lO+C/rMa+p/4tzXc= X-Google-Smtp-Source: APXvYqyyjv4fLjo+oyGYCsVCI5nasI3GONHx4lFzfytCasfenGK3gbTutnpws7AafXrv+jwtMbgFYw== X-Received: by 2002:a65:5c82:: with SMTP id a2mr50043330pgt.378.1558002537858; Thu, 16 May 2019 03:28:57 -0700 (PDT) Received: from hsinyi-z840.tpe.corp.google.com ([2401:fa00:1:10:b852:bd51:9305:4261]) by smtp.gmail.com with ESMTPSA id h123sm9338048pfe.80.2019.05.16.03.28.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 16 May 2019 03:28:57 -0700 (PDT) From: Hsin-Yi Wang To: linux-arm-kernel@lists.infradead.org Subject: [PATCH v3 3/3] fdt: add support for rng-seed Date: Thu, 16 May 2019 18:28:17 +0800 Message-Id: <20190516102817.188519-3-hsinyi@chromium.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190516102817.188519-1-hsinyi@chromium.org> References: <20190516102817.188519-1-hsinyi@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190516_032859_323458_2BD4D699 X-CRM114-Status: GOOD ( 17.36 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , devicetree@vger.kernel.org, Yu Zhao , Kees Cook , Ard Biesheuvel , Catalin Marinas , Stephen Boyd , Chintan Pandya , Will Deacon , linux-kernel@vger.kernel.org, Mike Rapoport , Jun Yao , Miles Chen , Rob Herring , James Morse , Hsin-Yi Wang , Andrew Murray , Andrew Morton , Laura Abbott , Frank Rowand , Robin Murphy Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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. Signed-off-by: Hsin-Yi Wang --- change v2->v3: 1. use arch hook for fdt pgprot change 2. handle CONFIG_KEXEC --- Documentation/devicetree/bindings/chosen.txt | 14 +++++ drivers/of/fdt.c | 55 ++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/Documentation/devicetree/bindings/chosen.txt b/Documentation/devicetree/bindings/chosen.txt index 45e79172a646..fef5c82672dc 100644 --- a/Documentation/devicetree/bindings/chosen.txt +++ b/Documentation/devicetree/bindings/chosen.txt @@ -28,6 +28,20 @@ mode) when EFI_RNG_PROTOCOL is supported, it will be overwritten by the Linux EFI stub (which will populate the property itself, using EFI_RNG_PROTOCOL). +rng-seed +----------- + +This property served as an entropy to add device randomness. It is parsed +as a byte array, e.g. + +/ { + chosen { + rng-seed = <0x31 0x95 0x1b 0x3c 0xc9 0xfa 0xb3 ...>; + }; +}; + +This random value should be provided by bootloader. + stdout-path ----------- diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index e84971d1e9ea..e9862657268d 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -24,6 +25,8 @@ #include #include #include +#include +#include #include /* for COMMAND_LINE_SIZE */ #include @@ -1087,11 +1090,14 @@ int __init early_init_dt_scan_memory(unsigned long node, const char *uname, return 0; } +int rng_seed_size; + int __init early_init_dt_scan_chosen(unsigned long node, const char *uname, int depth, void *data) { int l; const char *p; + const void *rng_seed; pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname); @@ -1126,6 +1132,16 @@ int __init early_init_dt_scan_chosen(unsigned long node, const char *uname, pr_debug("Command line is: %s\n", (char*)data); + rng_seed = of_get_flat_dt_prop(node, "rng-seed", &rng_seed_size); + if (rng_seed && rng_seed_size > 0) { + add_device_randomness(rng_seed, rng_seed_size); + + /* try to clear seed so it won't be found. */ + update_fdt_pgprot(PAGE_KERNEL); + fdt_delprop(initial_boot_params, node, "rng-seed"); + update_fdt_pgprot(PAGE_KERNEL_RO); + } + /* break now */ return 1; } @@ -1327,4 +1343,43 @@ static int __init of_fdt_raw_init(void) late_initcall(of_fdt_raw_init); #endif +#ifdef CONFIG_KEXEC +static int update_fdt_random_seed(struct notifier_block *nb, + unsigned long code, void *unused) +{ + int node; + void *rng_seed; + + if (!kexec_in_progress || !rng_seed_size) + return NOTIFY_DONE; + + node = fdt_path_offset(initial_boot_params, "/chosen"); + if (node < 0) + node = fdt_path_offset(initial_boot_params, "/chosen@0"); + if (node < 0) + return NOTIFY_DONE; + + rng_seed = kmalloc(rng_seed_size, GFP_ATOMIC); + get_random_bytes(rng_seed, rng_seed_size); + + update_fdt_pgprot(PAGE_KERNEL); + fdt_setprop(initial_boot_params, node, "rng-seed", rng_seed, + rng_seed_size); + + kfree(rng_seed); + + return NOTIFY_DONE; +} + +static struct notifier_block fdt_random_seed_nb = { + .notifier_call = update_fdt_random_seed, +}; + +static int register_update_fdt_random_seed(void) +{ + return register_reboot_notifier(&fdt_random_seed_nb); +} +late_initcall(register_update_fdt_random_seed); +#endif + #endif /* CONFIG_OF_EARLY_FLATTREE */