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