From patchwork Fri Apr 1 12:36:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 64848 Delivered-To: patch@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp710123lbc; Fri, 1 Apr 2016 05:38:02 -0700 (PDT) X-Received: by 10.98.44.73 with SMTP id s70mr30694318pfs.2.1459514281875; Fri, 01 Apr 2016 05:38:01 -0700 (PDT) Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id x80si21182069pfi.97.2016.04.01.05.38.01 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Apr 2016 05:38:01 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) client-ip=2001:1868:205::9; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) smtp.mailfrom=linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1alyJv-0000YY-Q0; Fri, 01 Apr 2016 12:36:59 +0000 Received: from mail-ig0-x22c.google.com ([2607:f8b0:4001:c05::22c]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1alyJr-0000Qp-M5 for linux-arm-kernel@lists.infradead.org; Fri, 01 Apr 2016 12:36:57 +0000 Received: by mail-ig0-x22c.google.com with SMTP id ui10so11991022igc.1 for ; Fri, 01 Apr 2016 05:36:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=VY+TKQ6QpYRMLXAxDGzclm2XZCl4XuJcnvUIsOcv/rE=; b=Lthif41StXzklamy/sQBPf1Rf1ytQOYC85o3JiNQvvaN2mrlIGM+GC+7lN1wTqpD/0 r1tU6XL1BQZfYP6mc6uH1NPqZYESPp9bt19T6CNXnw9qZm8PT+kDqS2ICVj0MDS/jdai wq4QcKFmMlvQa/eUSQUTEHIoB0N8i36we2i1U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=VY+TKQ6QpYRMLXAxDGzclm2XZCl4XuJcnvUIsOcv/rE=; b=B+sNX/OOfCn9DvSh9LGCrMYxc4c6ELTHVHnww2e1EzSjoF6hBE5skkAesWyMok9rA5 0B2oOk18nUQRHz2Xnck/cW5xn1nuCzcIzrviORYGhGYtIGjeVEcn3Lhou6ZJn5X/0Hzb xNzgeeFwFCJi3dxwgB4EjnKEKivHXrrv58vz74/GylUnZg32xBknjIs+q+P/ix+qy3Zn PTGUnsbC8WG5BQ72lmOctvmxEXy6XArsH/4nENpLU484eqbqxZvjf+2EWTxhtpDwlszk S3c0K1C0AkUFCLGcvJwQhlckyZK9HIYcPLn2iTG4vuMfLZOeWTLLPNWOYhvLwCkVS2eI eb1g== X-Gm-Message-State: AD7BkJLUItRCj8yMFAnd0ZFMCXJnk6mrC6CAOjZvwzaWvGD4Ps7oPALh8LQe4IxglwpHRpi8CL2d1yJL78EJ8FwH MIME-Version: 1.0 X-Received: by 10.50.78.130 with SMTP id b2mr2863549igx.71.1459514193694; Fri, 01 Apr 2016 05:36:33 -0700 (PDT) Received: by 10.36.29.6 with HTTP; Fri, 1 Apr 2016 05:36:33 -0700 (PDT) In-Reply-To: <1459355933-13529-5-git-send-email-ard.biesheuvel@linaro.org> References: <1459355933-13529-1-git-send-email-ard.biesheuvel@linaro.org> <1459355933-13529-5-git-send-email-ard.biesheuvel@linaro.org> Date: Fri, 1 Apr 2016 14:36:33 +0200 Message-ID: Subject: Re: [PATCH v2 4/5] efi: implement generic support for the Memory Attributes table From: Ard Biesheuvel To: "linux-efi@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , Matt Fleming , Mark Rutland , Russell King - ARM Linux X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160401_053655_988820_0C4EA675 X-CRM114-Status: GOOD ( 33.93 ) X-Spam-Score: -2.7 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2607:f8b0:4001:c05:0:0:0:22c listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ard Biesheuvel , Catalin Marinas , Will Deacon , Leif Lindholm , "Prakhya, Sai Praneeth" , Peter Jones Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org On 30 March 2016 at 18:38, Ard Biesheuvel wrote: > This implements shared support for discovering the presence of the > Memory Attributes table, and for parsing and validating its contents. > > The table is validated against the construction rules in the UEFI spec. > Since this is a new table, it makes sense to complain if we encounter > a table that does not follow those rules. > > The parsing and validation routine takes a callback that can be specified > per architecture, that gets passed each unique validated region, with the > virtual address retrieved from the ordinary memory map. > > Signed-off-by: Ard Biesheuvel > --- > drivers/firmware/efi/Makefile | 2 +- > drivers/firmware/efi/memattr.c | 170 ++++++++++++++++++++ > include/linux/efi.h | 13 ++ > 3 files changed, 184 insertions(+), 1 deletion(-) > > diff --git a/drivers/firmware/efi/Makefile b/drivers/firmware/efi/Makefile > index 62e654f255f4..d5be62399130 100644 > --- a/drivers/firmware/efi/Makefile > +++ b/drivers/firmware/efi/Makefile > @@ -9,7 +9,7 @@ > # > KASAN_SANITIZE_runtime-wrappers.o := n > > -obj-$(CONFIG_EFI) += efi.o vars.o reboot.o > +obj-$(CONFIG_EFI) += efi.o vars.o reboot.o memattr.o > obj-$(CONFIG_EFI_VARS) += efivars.o > obj-$(CONFIG_EFI_ESRT) += esrt.o > obj-$(CONFIG_EFI_VARS_PSTORE) += efi-pstore.o > diff --git a/drivers/firmware/efi/memattr.c b/drivers/firmware/efi/memattr.c > new file mode 100644 > index 000000000000..c55683937f4a > --- /dev/null > +++ b/drivers/firmware/efi/memattr.c > @@ -0,0 +1,170 @@ > +/* > + * Copyright (C) 2016 Linaro Ltd. > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU General Public License version 2 as > + * published by the Free Software Foundation. > + */ > + > +#define pr_fmt(fmt) "efi: " fmt > + > +#include > +#include > +#include > +#include > + > +#include > + > +static int __initdata tbl_size; > + > +/* > + * Reserve the memory associated with the Memory Attributes configuration > + * table, if it exists. > + */ > +int __init efi_memattr_init(void) > +{ > + efi_memory_attributes_table_t *tbl; > + > + if (efi.mem_attr_table == EFI_INVALID_TABLE_ADDR) > + return 0; > + > + tbl = early_memremap(efi.mem_attr_table, sizeof(*tbl)); > + if (!tbl) { > + pr_err("Failed to map EFI Memory Attribute table @ 0x%lx\n", > + efi.mem_attr_table); > + return -ENOMEM; > + } > + > + if (tbl->version > 1) { > + pr_warn("Unexpected EFI Memory Attribute table version %d\n", > + tbl->version); > + tbl_size = 0; > + goto unmap; > + } > + > + tbl_size = sizeof(*tbl) + tbl->num_entries * tbl->desc_size; > + memblock_reserve(efi.mem_attr_table, tbl_size); > + > +unmap: > + early_memunmap(tbl, sizeof(*tbl)); > + return 0; > +} > + > +/* > + * Returns a copy @out of the UEFI memory descriptor @in if it is covered > + * entirely by a UEFI memory map entry with matching attributes. The virtual > + * address of @out is set according to the matching entry that was found. > + */ > +static bool entry_is_valid(const efi_memory_desc_t *in, efi_memory_desc_t *out) > +{ > + u64 in_paddr = in->phys_addr; > + u64 in_size = in->num_pages << EFI_PAGE_SHIFT; > + efi_memory_desc_t *md; > + > + if (in->type != EFI_RUNTIME_SERVICES_CODE && > + in->type != EFI_RUNTIME_SERVICES_DATA) { > + pr_warn("MEMATTR table entry type should be RuntimeServiceCode/Data\n"); > + return false; > + } > + > + if (!(in->attribute & (EFI_MEMORY_RO | EFI_MEMORY_XP))) { > + pr_warn("MEMATTR table entry attributes invalid: RO and XP bits both cleared\n"); > + return false; > + } > + > + if (PAGE_SIZE > EFI_PAGE_SIZE && > + (!PAGE_ALIGNED(in->phys_addr) || > + !PAGE_ALIGNED(in->num_pages << EFI_PAGE_SHIFT))) { > + /* > + * Since arm64 may execute with page sizes of up to 64 KB, the > + * UEFI spec mandates that RuntimeServices memory regions must > + * be 64 KB aligned. We need to validate this here since we will > + * not be able to tighten permissions on such regions without > + * affecting adjacent regions. > + */ > + pr_warn("MEMATTR table entry misaligned\n"); > + return false; > + } > + > + for_each_efi_memory_desc(&memmap, md) { > + u64 md_paddr = md->phys_addr; > + u64 md_size = md->num_pages << EFI_PAGE_SHIFT; > + > + if (!(md->attribute & EFI_MEMORY_RUNTIME)) > + continue; > + if (md->virt_addr == 0) { > + /* no virtual mapping has been installed by the stub */ > + break; > + } > + > + if (md_paddr > in_paddr || (in_paddr - md_paddr) >= md_size) > + continue; > + > + /* > + * This entry covers the start of @in, check whether > + * it covers the end as well. > + */ > + if (md_paddr + md_size < in_paddr + in_size) { > + pr_warn("MEMATTR table entry covers multiple UEFI memory map regions\n"); > + return false; > + } > + > + if (md->type != in->type) { > + pr_warn("MEMATTR table entry type deviates from UEFI memory map region type\n"); > + return false; > + } > + > + *out = *in; > + out->virt_addr = in_paddr + > + (md->virt_addr - md->phys_addr); > + return true; > + } > + return false; > +} > + > +/* > + * To be called after the EFI page tables have been populated. If a memory > + * attributes table is available, its contents will be used to update the > + * mappings with tightened permissions as described by the table. > + * This requires the UEFI memory map to have already been populated with > + * virtual addresses. > + */ > +int __init efi_memattr_apply_permissions(struct mm_struct *mm, > + efi_memattr_perm_setter fn) > +{ > + efi_memory_attributes_table_t *tbl; > + int i, ret; > + > + if (tbl_size <= sizeof(*tbl)) > + return 0; > + > + tbl = memremap(efi.mem_attr_table, tbl_size, MEMREMAP_WB); > + if (!tbl) { > + pr_err("Failed to map EFI Memory Attribute table @ 0x%lx\n", > + efi.mem_attr_table); > + return -ENOMEM; > + } > + > + if (efi_enabled(EFI_DBG)) > + pr_info("Processing UEFI Memory Attributes table:\n"); > + > + for (i = ret = 0; ret == 0 && i < tbl->num_entries; i++) { > + efi_memory_desc_t md; > + unsigned long size; > + bool valid; > + char buf[64]; > + > + valid = entry_is_valid((void *)tbl->entry + i * tbl->desc_size, > + &md); > + size = md.num_pages << EFI_PAGE_SHIFT; > + if (efi_enabled(EFI_DBG) || !valid) > + pr_info(" 0x%012llx-0x%012llx %s\n", md.phys_addr, > + md.phys_addr + size - 1, > + efi_md_typeattr_format(buf, sizeof(buf), &md)); > + > + if (valid) > + ret = fn(mm, &md); > + } > + memunmap(tbl); > + return ret; > +} > diff --git a/include/linux/efi.h b/include/linux/efi.h > index 346d01ad7cca..277a9bb4e587 100644 > --- a/include/linux/efi.h > +++ b/include/linux/efi.h > @@ -970,6 +970,19 @@ extern void __init efi_fake_memmap(void); > static inline void efi_fake_memmap(void) { } > #endif > > +/* > + * efi_memattr_perm_setter - arch specific callback function passed into > + * efi_memattr_apply_permissions() that updates the > + * mapping permissions described by the second > + * argument in the page tables referrred to by the > + * first argument > + */ > +typedef int (*efi_memattr_perm_setter)(struct mm_struct *, efi_memory_desc_t *); > + > +extern int efi_memattr_init(void); > +extern int efi_memattr_apply_permissions(struct mm_struct *mm, > + efi_memattr_perm_setter fn); > + > /* Iterate through an efi_memory_map */ > #define for_each_efi_memory_desc(m, md) \ > for ((md) = (m)->map; \ > -- > 2.5.0 > This patch needs the following folded on top to deal with tables where the memory attribute table entry has not counterpart in the memory map pr_warn("MEMATTR table entry type should be RuntimeServiceCode/Data\n"); @@ -114,11 +116,11 @@ static bool entry_is_valid(const efi_memory_desc_t *in, efi_memory_desc_t *out) return false; } - *out = *in; out->virt_addr = in_paddr + (md->virt_addr - md->phys_addr); return true; } + pr_warn("MEMATTR table entry does not having a matching entry in the memory map\n"); return false; } _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel diff --git a/drivers/firmware/efi/memattr.c b/drivers/firmware/efi/memattr.c index c55683937f4a..4dfef78e64d8 100644 --- a/drivers/firmware/efi/memattr.c +++ b/drivers/firmware/efi/memattr.c @@ -61,6 +61,8 @@ static bool entry_is_valid(const efi_memory_desc_t *in, efi_memory_desc_t *out) u64 in_size = in->num_pages << EFI_PAGE_SHIFT; efi_memory_desc_t *md; + *out = *in; + if (in->type != EFI_RUNTIME_SERVICES_CODE && in->type != EFI_RUNTIME_SERVICES_DATA) {