memattr.c 5.4 KB

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