1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2020 Red Hat
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.
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
19 * This module includes code that validates and maps the EFI MOK table,
20 * if it's presence was detected very early in boot.
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
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:
29 * /sys/firmware/efi/mok-variables/
32 #define pr_fmt(fmt) "mokvar: " fmt
34 #include <linux/capability.h>
35 #include <linux/efi.h>
36 #include <linux/init.h>
38 #include <linux/kernel.h>
39 #include <linux/kobject.h>
40 #include <linux/list.h>
41 #include <linux/slab.h>
43 #include <asm/early_ioremap.h>
46 * The LINUX_EFI_MOK_VARIABLE_TABLE_GUID config table is a packed
47 * sequence of struct efi_mokvar_table_entry, one for each named
48 * MOK variable. The sequence is terminated by an entry with a
49 * completely NULL name and 0 data size.
51 * efi_mokvar_table_size is set to the computed size of the
52 * MOK config table by efi_mokvar_table_init(). This will be
53 * non-zero if and only if the table if present and has been
54 * validated by efi_mokvar_table_init().
56 static size_t efi_mokvar_table_size;
59 * efi_mokvar_table_va is the kernel virtual address at which the
60 * EFI MOK config table has been mapped by efi_mokvar_sysfs_init().
62 static struct efi_mokvar_table_entry *efi_mokvar_table_va;
65 * Each /sys/firmware/efi/mok-variables/ sysfs file is represented by
66 * an instance of struct efi_mokvar_sysfs_attr on efi_mokvar_sysfs_list.
67 * bin_attr.private points to the associated EFI MOK config table entry.
69 * This list is created during boot and then remains unchanged.
70 * So no synchronization is currently required to walk the list.
72 struct efi_mokvar_sysfs_attr {
73 struct bin_attribute bin_attr;
74 struct list_head node;
77 static LIST_HEAD(efi_mokvar_sysfs_list);
78 static struct kobject *mokvar_kobj;
81 * efi_mokvar_table_init() - Early boot validation of EFI MOK config table
83 * If present, validate and compute the size of the EFI MOK variable
84 * configuration table. This table may be provided by an EFI boot loader
85 * as an alternative to ordinary EFI variables, due to platform-dependent
86 * limitations. The memory occupied by this table is marked as reserved.
88 * This routine must be called before efi_free_boot_services() in order
89 * to guarantee that it can mark the table as reserved.
92 * efi.mokvar_table: Physical address of EFI MOK variable config table
93 * or special value that indicates no such table.
96 * efi_mokvar_table_size: Computed size of EFI MOK variable config table.
97 * The table is considered present and valid if this
100 void __init efi_mokvar_table_init(void)
102 efi_memory_desc_t md;
104 unsigned long cur_offset = 0;
105 unsigned long offset_limit;
106 unsigned long map_size = 0;
107 unsigned long map_size_needed = 0;
109 struct efi_mokvar_table_entry *mokvar_entry;
112 if (!efi_enabled(EFI_MEMMAP))
115 if (efi.mokvar_table == EFI_INVALID_TABLE_ADDR)
118 * The EFI MOK config table must fit within a single EFI memory
121 err = efi_mem_desc_lookup(efi.mokvar_table, &md);
123 pr_warn("EFI MOKvar config table is not within the EFI memory map\n");
127 offset_limit = efi_mem_desc_end(&md) - efi.mokvar_table;
130 * Validate the MOK config table. Since there is no table header
131 * from which we could get the total size of the MOK config table,
132 * we compute the total size as we validate each variably sized
133 * entry, remapping as necessary.
136 while (cur_offset + sizeof(*mokvar_entry) <= offset_limit) {
137 mokvar_entry = va + cur_offset;
138 map_size_needed = cur_offset + sizeof(*mokvar_entry);
139 if (map_size_needed > map_size) {
141 early_memunmap(va, map_size);
143 * Map a little more than the fixed size entry
144 * header, anticipating some data. It's safe to
145 * do so as long as we stay within current memory
148 map_size = min(map_size_needed + 2*EFI_PAGE_SIZE,
150 va = early_memremap(efi.mokvar_table, map_size);
152 pr_err("Failed to map EFI MOKvar config table pa=0x%lx, size=%lu.\n",
153 efi.mokvar_table, map_size);
156 mokvar_entry = va + cur_offset;
159 /* Check for last sentinel entry */
160 if (mokvar_entry->name[0] == '\0') {
161 if (mokvar_entry->data_size != 0)
167 /* Sanity check that the name is null terminated */
168 size = strnlen(mokvar_entry->name,
169 sizeof(mokvar_entry->name));
170 if (size >= sizeof(mokvar_entry->name))
173 /* Advance to the next entry */
174 cur_offset = map_size_needed + mokvar_entry->data_size;
178 early_memunmap(va, map_size);
180 pr_err("EFI MOKvar config table is not valid\n");
184 if (md.type == EFI_BOOT_SERVICES_DATA)
185 efi_mem_reserve(efi.mokvar_table, map_size_needed);
187 efi_mokvar_table_size = map_size_needed;
191 * efi_mokvar_entry_next() - Get next entry in the EFI MOK config table
193 * mokvar_entry: Pointer to current EFI MOK config table entry
194 * or null. Null indicates get first entry.
195 * Passed by reference. This is updated to the
196 * same value as the return value.
198 * Returns: Pointer to next EFI MOK config table entry
199 * or null, if there are no more entries.
200 * Same value is returned in the mokvar_entry
203 * This routine depends on the EFI MOK config table being entirely
204 * mapped with it's starting virtual address in efi_mokvar_table_va.
206 struct efi_mokvar_table_entry *efi_mokvar_entry_next(
207 struct efi_mokvar_table_entry **mokvar_entry)
209 struct efi_mokvar_table_entry *mokvar_cur;
210 struct efi_mokvar_table_entry *mokvar_next;
213 mokvar_cur = *mokvar_entry;
214 *mokvar_entry = NULL;
216 if (efi_mokvar_table_va == NULL)
219 if (mokvar_cur == NULL) {
220 mokvar_next = efi_mokvar_table_va;
222 if (mokvar_cur->name[0] == '\0')
224 size_cur = sizeof(*mokvar_cur) + mokvar_cur->data_size;
225 mokvar_next = (void *)mokvar_cur + size_cur;
228 if (mokvar_next->name[0] == '\0')
231 *mokvar_entry = mokvar_next;
236 * efi_mokvar_entry_find() - Find EFI MOK config entry by name
238 * name: Name of the entry to look for.
240 * Returns: Pointer to EFI MOK config table entry if found;
243 * This routine depends on the EFI MOK config table being entirely
244 * mapped with it's starting virtual address in efi_mokvar_table_va.
246 struct efi_mokvar_table_entry *efi_mokvar_entry_find(const char *name)
248 struct efi_mokvar_table_entry *mokvar_entry = NULL;
250 while (efi_mokvar_entry_next(&mokvar_entry)) {
251 if (!strncmp(name, mokvar_entry->name,
252 sizeof(mokvar_entry->name)))
259 * efi_mokvar_sysfs_read() - sysfs binary file read routine
261 * Returns: Count of bytes read.
263 * Copy EFI MOK config table entry data for this mokvar sysfs binary file
264 * to the supplied buffer, starting at the specified offset into mokvar table
265 * entry data, for the specified count bytes. The copy is limited by the
266 * amount of data in this mokvar config table entry.
268 static ssize_t efi_mokvar_sysfs_read(struct file *file, struct kobject *kobj,
269 struct bin_attribute *bin_attr, char *buf,
270 loff_t off, size_t count)
272 struct efi_mokvar_table_entry *mokvar_entry = bin_attr->private;
274 if (!capable(CAP_SYS_ADMIN))
277 if (off >= mokvar_entry->data_size)
279 if (count > mokvar_entry->data_size - off)
280 count = mokvar_entry->data_size - off;
282 memcpy(buf, mokvar_entry->data + off, count);
287 * efi_mokvar_sysfs_init() - Map EFI MOK config table and create sysfs
289 * Map the EFI MOK variable config table for run-time use by the kernel
290 * and create the sysfs entries in /sys/firmware/efi/mok-variables/
292 * This routine just returns if a valid EFI MOK variable config table
293 * was not found earlier during boot.
295 * This routine must be called during a "middle" initcall phase, i.e.
296 * after efi_mokvar_table_init() but before UEFI certs are loaded
300 * efi.mokvar_table: Physical address of EFI MOK variable config table
301 * or special value that indicates no such table.
303 * efi_mokvar_table_size: Computed size of EFI MOK variable config table.
304 * The table is considered present and valid if this
308 * efi_mokvar_table_va: Start virtual address of the EFI MOK config table.
310 static int __init efi_mokvar_sysfs_init(void)
313 struct efi_mokvar_table_entry *mokvar_entry = NULL;
314 struct efi_mokvar_sysfs_attr *mokvar_sysfs = NULL;
317 if (efi_mokvar_table_size == 0)
320 config_va = memremap(efi.mokvar_table, efi_mokvar_table_size,
323 pr_err("Failed to map EFI MOKvar config table\n");
326 efi_mokvar_table_va = config_va;
328 mokvar_kobj = kobject_create_and_add("mok-variables", efi_kobj);
330 pr_err("Failed to create EFI mok-variables sysfs entry\n");
334 while (efi_mokvar_entry_next(&mokvar_entry)) {
335 mokvar_sysfs = kzalloc(sizeof(*mokvar_sysfs), GFP_KERNEL);
341 sysfs_bin_attr_init(&mokvar_sysfs->bin_attr);
342 mokvar_sysfs->bin_attr.private = mokvar_entry;
343 mokvar_sysfs->bin_attr.attr.name = mokvar_entry->name;
344 mokvar_sysfs->bin_attr.attr.mode = 0400;
345 mokvar_sysfs->bin_attr.size = mokvar_entry->data_size;
346 mokvar_sysfs->bin_attr.read = efi_mokvar_sysfs_read;
348 err = sysfs_create_bin_file(mokvar_kobj,
349 &mokvar_sysfs->bin_attr);
353 list_add_tail(&mokvar_sysfs->node, &efi_mokvar_sysfs_list);
357 pr_err("Failed to create some EFI mok-variables sysfs entries\n");
362 device_initcall(efi_mokvar_sysfs_init);