mokvar-table.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mokvar-table.c
  4. *
  5. * Copyright (c) 2020 Red Hat
  6. * Author: Lenny Szubowicz <[email protected]>
  7. *
  8. * This module contains the kernel support for the Linux EFI Machine
  9. * Owner Key (MOK) variable configuration table, which is identified by
  10. * the LINUX_EFI_MOK_VARIABLE_TABLE_GUID.
  11. *
  12. * This EFI configuration table provides a more robust alternative to
  13. * EFI volatile variables by which an EFI boot loader can pass the
  14. * contents of the Machine Owner Key (MOK) certificate stores to the
  15. * kernel during boot. If both the EFI MOK config table and corresponding
  16. * EFI MOK variables are present, the table should be considered as
  17. * more authoritative.
  18. *
  19. * This module includes code that validates and maps the EFI MOK table,
  20. * if it's presence was detected very early in boot.
  21. *
  22. * Kernel interface routines are provided to walk through all the
  23. * entries in the MOK config table or to search for a specific named
  24. * entry.
  25. *
  26. * The contents of the individual named MOK config table entries are
  27. * made available to user space via read-only sysfs binary files under:
  28. *
  29. * /sys/firmware/efi/mok-variables/
  30. *
  31. */
  32. #define pr_fmt(fmt) "mokvar: " fmt
  33. #include <linux/capability.h>
  34. #include <linux/efi.h>
  35. #include <linux/init.h>
  36. #include <linux/io.h>
  37. #include <linux/kernel.h>
  38. #include <linux/kobject.h>
  39. #include <linux/list.h>
  40. #include <linux/slab.h>
  41. #include <asm/early_ioremap.h>
  42. /*
  43. * The LINUX_EFI_MOK_VARIABLE_TABLE_GUID config table is a packed
  44. * sequence of struct efi_mokvar_table_entry, one for each named
  45. * MOK variable. The sequence is terminated by an entry with a
  46. * completely NULL name and 0 data size.
  47. *
  48. * efi_mokvar_table_size is set to the computed size of the
  49. * MOK config table by efi_mokvar_table_init(). This will be
  50. * non-zero if and only if the table if present and has been
  51. * validated by efi_mokvar_table_init().
  52. */
  53. static size_t efi_mokvar_table_size;
  54. /*
  55. * efi_mokvar_table_va is the kernel virtual address at which the
  56. * EFI MOK config table has been mapped by efi_mokvar_sysfs_init().
  57. */
  58. static struct efi_mokvar_table_entry *efi_mokvar_table_va;
  59. /*
  60. * Each /sys/firmware/efi/mok-variables/ sysfs file is represented by
  61. * an instance of struct efi_mokvar_sysfs_attr on efi_mokvar_sysfs_list.
  62. * bin_attr.private points to the associated EFI MOK config table entry.
  63. *
  64. * This list is created during boot and then remains unchanged.
  65. * So no synchronization is currently required to walk the list.
  66. */
  67. struct efi_mokvar_sysfs_attr {
  68. struct bin_attribute bin_attr;
  69. struct list_head node;
  70. };
  71. static LIST_HEAD(efi_mokvar_sysfs_list);
  72. static struct kobject *mokvar_kobj;
  73. /*
  74. * efi_mokvar_table_init() - Early boot validation of EFI MOK config table
  75. *
  76. * If present, validate and compute the size of the EFI MOK variable
  77. * configuration table. This table may be provided by an EFI boot loader
  78. * as an alternative to ordinary EFI variables, due to platform-dependent
  79. * limitations. The memory occupied by this table is marked as reserved.
  80. *
  81. * This routine must be called before efi_free_boot_services() in order
  82. * to guarantee that it can mark the table as reserved.
  83. *
  84. * Implicit inputs:
  85. * efi.mokvar_table: Physical address of EFI MOK variable config table
  86. * or special value that indicates no such table.
  87. *
  88. * Implicit outputs:
  89. * efi_mokvar_table_size: Computed size of EFI MOK variable config table.
  90. * The table is considered present and valid if this
  91. * is non-zero.
  92. */
  93. void __init efi_mokvar_table_init(void)
  94. {
  95. efi_memory_desc_t md;
  96. void *va = NULL;
  97. unsigned long cur_offset = 0;
  98. unsigned long offset_limit;
  99. unsigned long map_size = 0;
  100. unsigned long map_size_needed = 0;
  101. unsigned long size;
  102. struct efi_mokvar_table_entry *mokvar_entry;
  103. int err;
  104. if (!efi_enabled(EFI_MEMMAP))
  105. return;
  106. if (efi.mokvar_table == EFI_INVALID_TABLE_ADDR)
  107. return;
  108. /*
  109. * The EFI MOK config table must fit within a single EFI memory
  110. * descriptor range.
  111. */
  112. err = efi_mem_desc_lookup(efi.mokvar_table, &md);
  113. if (err) {
  114. pr_warn("EFI MOKvar config table is not within the EFI memory map\n");
  115. return;
  116. }
  117. offset_limit = efi_mem_desc_end(&md) - efi.mokvar_table;
  118. /*
  119. * Validate the MOK config table. Since there is no table header
  120. * from which we could get the total size of the MOK config table,
  121. * we compute the total size as we validate each variably sized
  122. * entry, remapping as necessary.
  123. */
  124. err = -EINVAL;
  125. while (cur_offset + sizeof(*mokvar_entry) <= offset_limit) {
  126. mokvar_entry = va + cur_offset;
  127. map_size_needed = cur_offset + sizeof(*mokvar_entry);
  128. if (map_size_needed > map_size) {
  129. if (va)
  130. early_memunmap(va, map_size);
  131. /*
  132. * Map a little more than the fixed size entry
  133. * header, anticipating some data. It's safe to
  134. * do so as long as we stay within current memory
  135. * descriptor.
  136. */
  137. map_size = min(map_size_needed + 2*EFI_PAGE_SIZE,
  138. offset_limit);
  139. va = early_memremap(efi.mokvar_table, map_size);
  140. if (!va) {
  141. pr_err("Failed to map EFI MOKvar config table pa=0x%lx, size=%lu.\n",
  142. efi.mokvar_table, map_size);
  143. return;
  144. }
  145. mokvar_entry = va + cur_offset;
  146. }
  147. /* Check for last sentinel entry */
  148. if (mokvar_entry->name[0] == '\0') {
  149. if (mokvar_entry->data_size != 0)
  150. break;
  151. err = 0;
  152. break;
  153. }
  154. /* Sanity check that the name is null terminated */
  155. size = strnlen(mokvar_entry->name,
  156. sizeof(mokvar_entry->name));
  157. if (size >= sizeof(mokvar_entry->name))
  158. break;
  159. /* Advance to the next entry */
  160. cur_offset = map_size_needed + mokvar_entry->data_size;
  161. }
  162. if (va)
  163. early_memunmap(va, map_size);
  164. if (err) {
  165. pr_err("EFI MOKvar config table is not valid\n");
  166. return;
  167. }
  168. if (md.type == EFI_BOOT_SERVICES_DATA)
  169. efi_mem_reserve(efi.mokvar_table, map_size_needed);
  170. efi_mokvar_table_size = map_size_needed;
  171. }
  172. /*
  173. * efi_mokvar_entry_next() - Get next entry in the EFI MOK config table
  174. *
  175. * mokvar_entry: Pointer to current EFI MOK config table entry
  176. * or null. Null indicates get first entry.
  177. * Passed by reference. This is updated to the
  178. * same value as the return value.
  179. *
  180. * Returns: Pointer to next EFI MOK config table entry
  181. * or null, if there are no more entries.
  182. * Same value is returned in the mokvar_entry
  183. * parameter.
  184. *
  185. * This routine depends on the EFI MOK config table being entirely
  186. * mapped with it's starting virtual address in efi_mokvar_table_va.
  187. */
  188. struct efi_mokvar_table_entry *efi_mokvar_entry_next(
  189. struct efi_mokvar_table_entry **mokvar_entry)
  190. {
  191. struct efi_mokvar_table_entry *mokvar_cur;
  192. struct efi_mokvar_table_entry *mokvar_next;
  193. size_t size_cur;
  194. mokvar_cur = *mokvar_entry;
  195. *mokvar_entry = NULL;
  196. if (efi_mokvar_table_va == NULL)
  197. return NULL;
  198. if (mokvar_cur == NULL) {
  199. mokvar_next = efi_mokvar_table_va;
  200. } else {
  201. if (mokvar_cur->name[0] == '\0')
  202. return NULL;
  203. size_cur = sizeof(*mokvar_cur) + mokvar_cur->data_size;
  204. mokvar_next = (void *)mokvar_cur + size_cur;
  205. }
  206. if (mokvar_next->name[0] == '\0')
  207. return NULL;
  208. *mokvar_entry = mokvar_next;
  209. return mokvar_next;
  210. }
  211. /*
  212. * efi_mokvar_entry_find() - Find EFI MOK config entry by name
  213. *
  214. * name: Name of the entry to look for.
  215. *
  216. * Returns: Pointer to EFI MOK config table entry if found;
  217. * null otherwise.
  218. *
  219. * This routine depends on the EFI MOK config table being entirely
  220. * mapped with it's starting virtual address in efi_mokvar_table_va.
  221. */
  222. struct efi_mokvar_table_entry *efi_mokvar_entry_find(const char *name)
  223. {
  224. struct efi_mokvar_table_entry *mokvar_entry = NULL;
  225. while (efi_mokvar_entry_next(&mokvar_entry)) {
  226. if (!strncmp(name, mokvar_entry->name,
  227. sizeof(mokvar_entry->name)))
  228. return mokvar_entry;
  229. }
  230. return NULL;
  231. }
  232. /*
  233. * efi_mokvar_sysfs_read() - sysfs binary file read routine
  234. *
  235. * Returns: Count of bytes read.
  236. *
  237. * Copy EFI MOK config table entry data for this mokvar sysfs binary file
  238. * to the supplied buffer, starting at the specified offset into mokvar table
  239. * entry data, for the specified count bytes. The copy is limited by the
  240. * amount of data in this mokvar config table entry.
  241. */
  242. static ssize_t efi_mokvar_sysfs_read(struct file *file, struct kobject *kobj,
  243. struct bin_attribute *bin_attr, char *buf,
  244. loff_t off, size_t count)
  245. {
  246. struct efi_mokvar_table_entry *mokvar_entry = bin_attr->private;
  247. if (!capable(CAP_SYS_ADMIN))
  248. return 0;
  249. if (off >= mokvar_entry->data_size)
  250. return 0;
  251. if (count > mokvar_entry->data_size - off)
  252. count = mokvar_entry->data_size - off;
  253. memcpy(buf, mokvar_entry->data + off, count);
  254. return count;
  255. }
  256. /*
  257. * efi_mokvar_sysfs_init() - Map EFI MOK config table and create sysfs
  258. *
  259. * Map the EFI MOK variable config table for run-time use by the kernel
  260. * and create the sysfs entries in /sys/firmware/efi/mok-variables/
  261. *
  262. * This routine just returns if a valid EFI MOK variable config table
  263. * was not found earlier during boot.
  264. *
  265. * This routine must be called during a "middle" initcall phase, i.e.
  266. * after efi_mokvar_table_init() but before UEFI certs are loaded
  267. * during late init.
  268. *
  269. * Implicit inputs:
  270. * efi.mokvar_table: Physical address of EFI MOK variable config table
  271. * or special value that indicates no such table.
  272. *
  273. * efi_mokvar_table_size: Computed size of EFI MOK variable config table.
  274. * The table is considered present and valid if this
  275. * is non-zero.
  276. *
  277. * Implicit outputs:
  278. * efi_mokvar_table_va: Start virtual address of the EFI MOK config table.
  279. */
  280. static int __init efi_mokvar_sysfs_init(void)
  281. {
  282. void *config_va;
  283. struct efi_mokvar_table_entry *mokvar_entry = NULL;
  284. struct efi_mokvar_sysfs_attr *mokvar_sysfs = NULL;
  285. int err = 0;
  286. if (efi_mokvar_table_size == 0)
  287. return -ENOENT;
  288. config_va = memremap(efi.mokvar_table, efi_mokvar_table_size,
  289. MEMREMAP_WB);
  290. if (!config_va) {
  291. pr_err("Failed to map EFI MOKvar config table\n");
  292. return -ENOMEM;
  293. }
  294. efi_mokvar_table_va = config_va;
  295. mokvar_kobj = kobject_create_and_add("mok-variables", efi_kobj);
  296. if (!mokvar_kobj) {
  297. pr_err("Failed to create EFI mok-variables sysfs entry\n");
  298. return -ENOMEM;
  299. }
  300. while (efi_mokvar_entry_next(&mokvar_entry)) {
  301. mokvar_sysfs = kzalloc(sizeof(*mokvar_sysfs), GFP_KERNEL);
  302. if (!mokvar_sysfs) {
  303. err = -ENOMEM;
  304. break;
  305. }
  306. sysfs_bin_attr_init(&mokvar_sysfs->bin_attr);
  307. mokvar_sysfs->bin_attr.private = mokvar_entry;
  308. mokvar_sysfs->bin_attr.attr.name = mokvar_entry->name;
  309. mokvar_sysfs->bin_attr.attr.mode = 0400;
  310. mokvar_sysfs->bin_attr.size = mokvar_entry->data_size;
  311. mokvar_sysfs->bin_attr.read = efi_mokvar_sysfs_read;
  312. err = sysfs_create_bin_file(mokvar_kobj,
  313. &mokvar_sysfs->bin_attr);
  314. if (err)
  315. break;
  316. list_add_tail(&mokvar_sysfs->node, &efi_mokvar_sysfs_list);
  317. }
  318. if (err) {
  319. pr_err("Failed to create some EFI mok-variables sysfs entries\n");
  320. kfree(mokvar_sysfs);
  321. }
  322. return err;
  323. }
  324. fs_initcall(efi_mokvar_sysfs_init);