From patchwork Wed Apr 19 17:13:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 97669 Delivered-To: patch@linaro.org Received: by 10.140.109.52 with SMTP id k49csp420169qgf; Wed, 19 Apr 2017 10:15:26 -0700 (PDT) X-Received: by 10.157.48.120 with SMTP id w53mr282533otd.55.1492622125966; Wed, 19 Apr 2017 10:15:25 -0700 (PDT) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org. [192.237.175.120]) by mx.google.com with ESMTPS id t6si1703193oie.179.2017.04.19.10.15.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 19 Apr 2017 10:15:25 -0700 (PDT) Received-SPF: neutral (google.com: 192.237.175.120 is neither permitted nor denied by best guess record for domain of xen-devel-bounces@lists.xen.org) client-ip=192.237.175.120; Authentication-Results: mx.google.com; spf=neutral (google.com: 192.237.175.120 is neither permitted nor denied by best guess record for domain of xen-devel-bounces@lists.xen.org) smtp.mailfrom=xen-devel-bounces@lists.xen.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d0tAQ-0001KE-QG; Wed, 19 Apr 2017 17:13:22 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d0tAP-0001JH-Vv for xen-devel@lists.xen.org; Wed, 19 Apr 2017 17:13:22 +0000 Received: from [85.158.143.35] by server-4.bemta-6.messagelabs.com id 37/9B-02956-1BA97F85; Wed, 19 Apr 2017 17:13:21 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrGLMWRWlGSWpSXmKPExsVysyfVTXfDrO8 RBi9fG1ss+biYxYHR4+ju30wBjFGsmXlJ+RUJrBm3prsWrJWt+NfzgrmBcbV4FyMXh5DAJkaJ i6desUI4pxkl1mw7CORwcrAJaErc+fyJCcQWEZCWuPb5MiOIzSzgIPHm4z0WEFtYIELi8p+ZY PUsAqoSj1rOsoPYvAIWEjseTASzJQTkJXa1XQSr4RSwlDi5+TjYHCGgmlundrNMYORewMiwil GjOLWoLLVI18hAL6koMz2jJDcxM0fX0MBMLze1uDgxPTUnMalYLzk/dxMj0L8MQLCD8deygEO MkhxMSqK8ZfXfI4T4kvJTKjMSizPii0pzUosPMcpwcChJ8IbOBMoJFqWmp1akZeYAAw0mLcHB oyTCqwiS5i0uSMwtzkyHSJ1iVJQS5+0CSQiAJDJK8+DaYMF9iVFWSpiXEegQIZ6C1KLczBJU+ VeM4hyMSsK8r2YATeHJzCuBm/4KaDET0OKIgC8gi0sSEVJSDYw2nlOeO23ofu7CM4s750HQyU V7jLkvftJ0PclqVZfFkhNqvcNTZ/3HTpX6pk9/tnzInqe4YFmQ65L96uK2nPamRy3Tq578/zK /q9yr8/9+13Cx/eESxwS5GJQc53118s66cr6i7AoT///S60u2Pk+53Bp1YU3WUxftCQYXD65+ Z/g86tBus+NKLMUZiYZazEXFiQBUV6ZlaQIAAA== X-Env-Sender: julien.grall@arm.com X-Msg-Ref: server-4.tower-21.messagelabs.com!1492622000!58208060!1 X-Originating-IP: [217.140.101.70] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.12; banners=-,-,- X-VirusChecked: Checked Received: (qmail 51783 invoked from network); 19 Apr 2017 17:13:20 -0000 Received: from foss.arm.com (HELO foss.arm.com) (217.140.101.70) by server-4.tower-21.messagelabs.com with SMTP; 19 Apr 2017 17:13:20 -0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id EE0C1CFC; Wed, 19 Apr 2017 10:13:19 -0700 (PDT) Received: from e108454-lin.cambridge.arm.com (e108454-lin.cambridge.arm.com [10.1.206.53]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 595253F4FF; Wed, 19 Apr 2017 10:13:19 -0700 (PDT) From: Julien Grall To: xen-devel@lists.xen.org Date: Wed, 19 Apr 2017 18:13:10 +0100 Message-Id: <20170419171311.3243-4-julien.grall@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170419171311.3243-1-julien.grall@arm.com> References: <20170419171311.3243-1-julien.grall@arm.com> Cc: Julien Grall , sstabellini@kernel.org Subject: [Xen-devel] [PATCH for-4.9 3/4] xen/arm: Check if the FDT passed by the bootloader is valid X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" There is currently no sanity check on the FDT passed by the bootloader. Whilst they are stricly not necessary, it will avoid us to spend hours to try to find out why it does not work. >From the booting documentation for AArch32 [1] and AArch64 [2] must : - be placed on 8-byte boundary - not exceed 2MB (only on AArch64) Even if AArch32 does not seem to limit the size, Xen is not currently able to support more the 2MB FDT. It is better to crash rather with a nice error message than claiming we are supporting any size of FDT. The checks are mostly borrowed from the Linux code (see fixmap_remap_fdt in arch/arm64/mm/mmu.c). [1] Section 2 in linux/Documentation/arm64/booting.txt [2] Section 4b in linux/Documentation/arm/Booting Signed-off-by: Julien Grall Reviewed-by: Stefano Stabellini --- xen/arch/arm/mm.c | 29 ++++++++++++++++++++++++++++- xen/arch/arm/setup.c | 6 ++++++ xen/include/asm-arm/setup.h | 3 +++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/xen/arch/arm/mm.c b/xen/arch/arm/mm.c index 0d076489c6..53d36e2ce2 100644 --- a/xen/arch/arm/mm.c +++ b/xen/arch/arm/mm.c @@ -39,6 +39,8 @@ #include #include #include +#include +#include static void __init create_mappings(lpae_t *second, unsigned long virt_offset, @@ -447,11 +449,36 @@ void * __init early_fdt_map(paddr_t fdt_paddr) { /* We are using 2MB superpage for mapping the FDT */ paddr_t base_paddr = fdt_paddr & SECOND_MASK; + paddr_t offset; + void *fdt_virt; + + /* + * Check whether the physical FDT address is set and meets the minimum + * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be at + * least 8 bytes so that we always access the magic and size fields + * of the FDT header after mapping the first chunk, double check if + * that is indeed the case. + */ + BUILD_BUG_ON(MIN_FDT_ALIGN < 8); + if ( !fdt_paddr || fdt_paddr % MIN_FDT_ALIGN ) + return NULL; + + /* The FDT is mapped using 2MB superpage */ + BUILD_BUG_ON(BOOT_FDT_VIRT_START % SZ_2M); create_mappings(boot_second, BOOT_FDT_VIRT_START, paddr_to_pfn(base_paddr), SZ_2M >> PAGE_SHIFT, SZ_2M); - return (void *)BOOT_FDT_VIRT_START + (fdt_paddr % SECOND_SIZE); + offset = fdt_paddr % SECOND_SIZE; + fdt_virt = (void *)BOOT_FDT_VIRT_START + offset; + + if ( fdt_magic(fdt_virt) != FDT_MAGIC ) + return NULL; + + if ( fdt_totalsize(fdt_virt) > MAX_FDT_SIZE ) + return NULL; + + return fdt_virt; } void __init remove_early_mappings(void) diff --git a/xen/arch/arm/setup.c b/xen/arch/arm/setup.c index 986398970f..8f72f31fb5 100644 --- a/xen/arch/arm/setup.c +++ b/xen/arch/arm/setup.c @@ -725,6 +725,12 @@ void __init start_xen(unsigned long boot_phys_offset, smp_clear_cpu_maps(); device_tree_flattened = early_fdt_map(fdt_paddr); + if ( !device_tree_flattened ) + panic("Invalid device tree blob at physical address %#lx\n" + "The DTB must be 8-byte aligned and must not exceed 2 MB in size\n" + "\nPlease check your bootloader.", + fdt_paddr); + fdt_size = boot_fdt_info(device_tree_flattened, fdt_paddr); cmdline = boot_fdt_cmdline(device_tree_flattened); diff --git a/xen/include/asm-arm/setup.h b/xen/include/asm-arm/setup.h index 7c761851d2..7ff2c34dab 100644 --- a/xen/include/asm-arm/setup.h +++ b/xen/include/asm-arm/setup.h @@ -3,6 +3,9 @@ #include +#define MIN_FDT_ALIGN 8 +#define MAX_FDT_SIZE SZ_2M + #define NR_MEM_BANKS 64 #define MAX_MODULES 5 /* Current maximum useful modules */