Linux Audio

Check our new training course

Loading...
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2016 Linaro Ltd. <ard.biesheuvel@linaro.org>
  4 */
  5
  6#define pr_fmt(fmt)	"efi: memattr: " fmt
  7
  8#include <linux/efi.h>
  9#include <linux/init.h>
 10#include <linux/io.h>
 11#include <linux/memblock.h>
 12
 13#include <asm/early_ioremap.h>
 14
 15static int __initdata tbl_size;
 16unsigned long __ro_after_init efi_mem_attr_table = EFI_INVALID_TABLE_ADDR;
 17
 18/*
 19 * Reserve the memory associated with the Memory Attributes configuration
 20 * table, if it exists.
 21 */
 22int __init efi_memattr_init(void)
 23{
 24	efi_memory_attributes_table_t *tbl;
 
 25
 26	if (efi_mem_attr_table == EFI_INVALID_TABLE_ADDR)
 27		return 0;
 28
 29	tbl = early_memremap(efi_mem_attr_table, sizeof(*tbl));
 30	if (!tbl) {
 31		pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
 32		       efi_mem_attr_table);
 33		return -ENOMEM;
 34	}
 35
 36	if (tbl->version > 2) {
 37		pr_warn("Unexpected EFI Memory Attributes table version %d\n",
 38			tbl->version);
 39		goto unmap;
 40	}
 41
 42	tbl_size = sizeof(*tbl) + tbl->num_entries * tbl->desc_size;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 43	memblock_reserve(efi_mem_attr_table, tbl_size);
 44	set_bit(EFI_MEM_ATTR, &efi.flags);
 45
 46unmap:
 47	early_memunmap(tbl, sizeof(*tbl));
 48	return 0;
 49}
 50
 51/*
 52 * Returns a copy @out of the UEFI memory descriptor @in if it is covered
 53 * entirely by a UEFI memory map entry with matching attributes. The virtual
 54 * address of @out is set according to the matching entry that was found.
 55 */
 56static bool entry_is_valid(const efi_memory_desc_t *in, efi_memory_desc_t *out)
 57{
 58	u64 in_paddr = in->phys_addr;
 59	u64 in_size = in->num_pages << EFI_PAGE_SHIFT;
 60	efi_memory_desc_t *md;
 61
 62	*out = *in;
 63
 64	if (in->type != EFI_RUNTIME_SERVICES_CODE &&
 65	    in->type != EFI_RUNTIME_SERVICES_DATA) {
 66		pr_warn("Entry type should be RuntimeServiceCode/Data\n");
 67		return false;
 68	}
 69
 70	if (PAGE_SIZE > EFI_PAGE_SIZE &&
 71	    (!PAGE_ALIGNED(in->phys_addr) ||
 72	     !PAGE_ALIGNED(in->num_pages << EFI_PAGE_SHIFT))) {
 73		/*
 74		 * Since arm64 may execute with page sizes of up to 64 KB, the
 75		 * UEFI spec mandates that RuntimeServices memory regions must
 76		 * be 64 KB aligned. We need to validate this here since we will
 77		 * not be able to tighten permissions on such regions without
 78		 * affecting adjacent regions.
 79		 */
 80		pr_warn("Entry address region misaligned\n");
 81		return false;
 82	}
 83
 84	for_each_efi_memory_desc(md) {
 85		u64 md_paddr = md->phys_addr;
 86		u64 md_size = md->num_pages << EFI_PAGE_SHIFT;
 87
 88		if (!(md->attribute & EFI_MEMORY_RUNTIME))
 89			continue;
 90		if (md->virt_addr == 0 && md->phys_addr != 0) {
 91			/* no virtual mapping has been installed by the stub */
 92			break;
 93		}
 94
 95		if (md_paddr > in_paddr || (in_paddr - md_paddr) >= md_size)
 96			continue;
 97
 98		/*
 99		 * This entry covers the start of @in, check whether
100		 * it covers the end as well.
101		 */
102		if (md_paddr + md_size < in_paddr + in_size) {
103			pr_warn("Entry covers multiple EFI memory map regions\n");
104			return false;
105		}
106
107		if (md->type != in->type) {
108			pr_warn("Entry type deviates from EFI memory map region type\n");
109			return false;
110		}
111
112		out->virt_addr = in_paddr + (md->virt_addr - md_paddr);
113
114		return true;
115	}
116
117	pr_warn("No matching entry found in the EFI memory map\n");
118	return false;
119}
120
121/*
122 * To be called after the EFI page tables have been populated. If a memory
123 * attributes table is available, its contents will be used to update the
124 * mappings with tightened permissions as described by the table.
125 * This requires the UEFI memory map to have already been populated with
126 * virtual addresses.
127 */
128int __init efi_memattr_apply_permissions(struct mm_struct *mm,
129					 efi_memattr_perm_setter fn)
130{
131	efi_memory_attributes_table_t *tbl;
132	bool has_bti = false;
133	int i, ret;
134
135	if (tbl_size <= sizeof(*tbl))
136		return 0;
137
138	/*
139	 * We need the EFI memory map to be setup so we can use it to
140	 * lookup the virtual addresses of all entries in the  of EFI
141	 * Memory Attributes table. If it isn't available, this
142	 * function should not be called.
143	 */
144	if (WARN_ON(!efi_enabled(EFI_MEMMAP)))
145		return 0;
146
147	tbl = memremap(efi_mem_attr_table, tbl_size, MEMREMAP_WB);
148	if (!tbl) {
149		pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
150		       efi_mem_attr_table);
151		return -ENOMEM;
152	}
153
154	if (tbl->version > 1 &&
155	    (tbl->flags & EFI_MEMORY_ATTRIBUTES_FLAGS_RT_FORWARD_CONTROL_FLOW_GUARD))
156		has_bti = true;
157
158	if (efi_enabled(EFI_DBG))
159		pr_info("Processing EFI Memory Attributes table:\n");
160
161	for (i = ret = 0; ret == 0 && i < tbl->num_entries; i++) {
162		efi_memory_desc_t md;
163		unsigned long size;
164		bool valid;
165		char buf[64];
166
167		valid = entry_is_valid((void *)tbl->entry + i * tbl->desc_size,
168				       &md);
169		size = md.num_pages << EFI_PAGE_SHIFT;
170		if (efi_enabled(EFI_DBG) || !valid)
171			pr_info("%s 0x%012llx-0x%012llx %s\n",
172				valid ? "" : "!", md.phys_addr,
173				md.phys_addr + size - 1,
174				efi_md_typeattr_format(buf, sizeof(buf), &md));
175
176		if (valid) {
177			ret = fn(mm, &md, has_bti);
178			if (ret)
179				pr_err("Error updating mappings, skipping subsequent md's\n");
180		}
181	}
182	memunmap(tbl);
183	return ret;
184}
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2016 Linaro Ltd. <ard.biesheuvel@linaro.org>
  4 */
  5
  6#define pr_fmt(fmt)	"efi: memattr: " fmt
  7
  8#include <linux/efi.h>
  9#include <linux/init.h>
 10#include <linux/io.h>
 11#include <linux/memblock.h>
 12
 13#include <asm/early_ioremap.h>
 14
 15static int __initdata tbl_size;
 16unsigned long __ro_after_init efi_mem_attr_table = EFI_INVALID_TABLE_ADDR;
 17
 18/*
 19 * Reserve the memory associated with the Memory Attributes configuration
 20 * table, if it exists.
 21 */
 22int __init efi_memattr_init(void)
 23{
 24	efi_memory_attributes_table_t *tbl;
 25	unsigned long size;
 26
 27	if (efi_mem_attr_table == EFI_INVALID_TABLE_ADDR)
 28		return 0;
 29
 30	tbl = early_memremap(efi_mem_attr_table, sizeof(*tbl));
 31	if (!tbl) {
 32		pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
 33		       efi_mem_attr_table);
 34		return -ENOMEM;
 35	}
 36
 37	if (tbl->version > 2) {
 38		pr_warn("Unexpected EFI Memory Attributes table version %d\n",
 39			tbl->version);
 40		goto unmap;
 41	}
 42
 43
 44	/*
 45	 * Sanity check: the Memory Attributes Table contains up to 3 entries
 46	 * for each entry of type EfiRuntimeServicesCode in the EFI memory map.
 47	 * So if the size of the table exceeds 3x the size of the entire EFI
 48	 * memory map, there is clearly something wrong, and the table should
 49	 * just be ignored altogether.
 50	 */
 51	size = tbl->num_entries * tbl->desc_size;
 52	if (size > 3 * efi.memmap.nr_map * efi.memmap.desc_size) {
 53		pr_warn(FW_BUG "Corrupted EFI Memory Attributes Table detected! (version == %u, desc_size == %u, num_entries == %u)\n",
 54			tbl->version, tbl->desc_size, tbl->num_entries);
 55		goto unmap;
 56	}
 57
 58	tbl_size = sizeof(*tbl) + size;
 59	memblock_reserve(efi_mem_attr_table, tbl_size);
 60	set_bit(EFI_MEM_ATTR, &efi.flags);
 61
 62unmap:
 63	early_memunmap(tbl, sizeof(*tbl));
 64	return 0;
 65}
 66
 67/*
 68 * Returns a copy @out of the UEFI memory descriptor @in if it is covered
 69 * entirely by a UEFI memory map entry with matching attributes. The virtual
 70 * address of @out is set according to the matching entry that was found.
 71 */
 72static bool entry_is_valid(const efi_memory_desc_t *in, efi_memory_desc_t *out)
 73{
 74	u64 in_paddr = in->phys_addr;
 75	u64 in_size = in->num_pages << EFI_PAGE_SHIFT;
 76	efi_memory_desc_t *md;
 77
 78	*out = *in;
 79
 80	if (in->type != EFI_RUNTIME_SERVICES_CODE &&
 81	    in->type != EFI_RUNTIME_SERVICES_DATA) {
 82		pr_warn("Entry type should be RuntimeServiceCode/Data\n");
 83		return false;
 84	}
 85
 86	if (PAGE_SIZE > EFI_PAGE_SIZE &&
 87	    (!PAGE_ALIGNED(in->phys_addr) ||
 88	     !PAGE_ALIGNED(in->num_pages << EFI_PAGE_SHIFT))) {
 89		/*
 90		 * Since arm64 may execute with page sizes of up to 64 KB, the
 91		 * UEFI spec mandates that RuntimeServices memory regions must
 92		 * be 64 KB aligned. We need to validate this here since we will
 93		 * not be able to tighten permissions on such regions without
 94		 * affecting adjacent regions.
 95		 */
 96		pr_warn("Entry address region misaligned\n");
 97		return false;
 98	}
 99
100	for_each_efi_memory_desc(md) {
101		u64 md_paddr = md->phys_addr;
102		u64 md_size = md->num_pages << EFI_PAGE_SHIFT;
103
104		if (!(md->attribute & EFI_MEMORY_RUNTIME))
105			continue;
106		if (md->virt_addr == 0 && md->phys_addr != 0) {
107			/* no virtual mapping has been installed by the stub */
108			break;
109		}
110
111		if (md_paddr > in_paddr || (in_paddr - md_paddr) >= md_size)
112			continue;
113
114		/*
115		 * This entry covers the start of @in, check whether
116		 * it covers the end as well.
117		 */
118		if (md_paddr + md_size < in_paddr + in_size) {
119			pr_warn("Entry covers multiple EFI memory map regions\n");
120			return false;
121		}
122
123		if (md->type != in->type) {
124			pr_warn("Entry type deviates from EFI memory map region type\n");
125			return false;
126		}
127
128		out->virt_addr = in_paddr + (md->virt_addr - md_paddr);
129
130		return true;
131	}
132
133	pr_warn("No matching entry found in the EFI memory map\n");
134	return false;
135}
136
137/*
138 * To be called after the EFI page tables have been populated. If a memory
139 * attributes table is available, its contents will be used to update the
140 * mappings with tightened permissions as described by the table.
141 * This requires the UEFI memory map to have already been populated with
142 * virtual addresses.
143 */
144int __init efi_memattr_apply_permissions(struct mm_struct *mm,
145					 efi_memattr_perm_setter fn)
146{
147	efi_memory_attributes_table_t *tbl;
148	bool has_bti = false;
149	int i, ret;
150
151	if (tbl_size <= sizeof(*tbl))
152		return 0;
153
154	/*
155	 * We need the EFI memory map to be setup so we can use it to
156	 * lookup the virtual addresses of all entries in the  of EFI
157	 * Memory Attributes table. If it isn't available, this
158	 * function should not be called.
159	 */
160	if (WARN_ON(!efi_enabled(EFI_MEMMAP)))
161		return 0;
162
163	tbl = memremap(efi_mem_attr_table, tbl_size, MEMREMAP_WB);
164	if (!tbl) {
165		pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
166		       efi_mem_attr_table);
167		return -ENOMEM;
168	}
169
170	if (tbl->version > 1 &&
171	    (tbl->flags & EFI_MEMORY_ATTRIBUTES_FLAGS_RT_FORWARD_CONTROL_FLOW_GUARD))
172		has_bti = true;
173
174	if (efi_enabled(EFI_DBG))
175		pr_info("Processing EFI Memory Attributes table:\n");
176
177	for (i = ret = 0; ret == 0 && i < tbl->num_entries; i++) {
178		efi_memory_desc_t md;
179		unsigned long size;
180		bool valid;
181		char buf[64];
182
183		valid = entry_is_valid(efi_memdesc_ptr(tbl->entry, tbl->desc_size, i),
184				       &md);
185		size = md.num_pages << EFI_PAGE_SHIFT;
186		if (efi_enabled(EFI_DBG) || !valid)
187			pr_info("%s 0x%012llx-0x%012llx %s\n",
188				valid ? "" : "!", md.phys_addr,
189				md.phys_addr + size - 1,
190				efi_md_typeattr_format(buf, sizeof(buf), &md));
191
192		if (valid) {
193			ret = fn(mm, &md, has_bti);
194			if (ret)
195				pr_err("Error updating mappings, skipping subsequent md's\n");
196		}
197	}
198	memunmap(tbl);
199	return ret;
200}