// SPDX-License-Identifier: GPL-2.01/*2* mokvar-table.c3*4* Copyright (c) 2020 Red Hat5* Author: Lenny Szubowicz <[email protected]>6*7* This module contains the kernel support for the Linux EFI Machine8* Owner Key (MOK) variable configuration table, which is identified by9* the LINUX_EFI_MOK_VARIABLE_TABLE_GUID.10*11* This EFI configuration table provides a more robust alternative to12* EFI volatile variables by which an EFI boot loader can pass the13* contents of the Machine Owner Key (MOK) certificate stores to the14* kernel during boot. If both the EFI MOK config table and corresponding15* EFI MOK variables are present, the table should be considered as16* more authoritative.17*18* This module includes code that validates and maps the EFI MOK table,19* if it's presence was detected very early in boot.20*21* Kernel interface routines are provided to walk through all the22* entries in the MOK config table or to search for a specific named23* entry.24*25* The contents of the individual named MOK config table entries are26* made available to user space via read-only sysfs binary files under:27*28* /sys/firmware/efi/mok-variables/29*30*/31#define pr_fmt(fmt) "mokvar: " fmt3233#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>4142#include <asm/early_ioremap.h>4344/*45* The LINUX_EFI_MOK_VARIABLE_TABLE_GUID config table is a packed46* sequence of struct efi_mokvar_table_entry, one for each named47* MOK variable. The sequence is terminated by an entry with a48* completely NULL name and 0 data size.49*50* efi_mokvar_table_size is set to the computed size of the51* MOK config table by efi_mokvar_table_init(). This will be52* non-zero if and only if the table if present and has been53* validated by efi_mokvar_table_init().54*/55static size_t efi_mokvar_table_size;5657/*58* efi_mokvar_table_va is the kernel virtual address at which the59* EFI MOK config table has been mapped by efi_mokvar_sysfs_init().60*/61static struct efi_mokvar_table_entry *efi_mokvar_table_va;6263/*64* Each /sys/firmware/efi/mok-variables/ sysfs file is represented by65* an instance of struct efi_mokvar_sysfs_attr on efi_mokvar_sysfs_list.66* bin_attr.private points to the associated EFI MOK config table entry.67*68* This list is created during boot and then remains unchanged.69* So no synchronization is currently required to walk the list.70*/71struct efi_mokvar_sysfs_attr {72struct bin_attribute bin_attr;73struct list_head node;74};7576static LIST_HEAD(efi_mokvar_sysfs_list);77static struct kobject *mokvar_kobj;7879/*80* efi_mokvar_table_init() - Early boot validation of EFI MOK config table81*82* If present, validate and compute the size of the EFI MOK variable83* configuration table. This table may be provided by an EFI boot loader84* as an alternative to ordinary EFI variables, due to platform-dependent85* limitations. The memory occupied by this table is marked as reserved.86*87* This routine must be called before efi_free_boot_services() in order88* to guarantee that it can mark the table as reserved.89*90* Implicit inputs:91* efi.mokvar_table: Physical address of EFI MOK variable config table92* or special value that indicates no such table.93*94* Implicit outputs:95* efi_mokvar_table_size: Computed size of EFI MOK variable config table.96* The table is considered present and valid if this97* is non-zero.98*/99void __init efi_mokvar_table_init(void)100{101struct efi_mokvar_table_entry __aligned(1) *mokvar_entry, *next_entry;102efi_memory_desc_t md;103void *va = NULL;104unsigned long cur_offset = 0;105unsigned long offset_limit;106unsigned long map_size_needed = 0;107unsigned long size;108int err;109110if (!efi_enabled(EFI_MEMMAP))111return;112113if (efi.mokvar_table == EFI_INVALID_TABLE_ADDR)114return;115/*116* The EFI MOK config table must fit within a single EFI memory117* descriptor range.118*/119err = efi_mem_desc_lookup(efi.mokvar_table, &md);120if (err) {121pr_warn("EFI MOKvar config table is not within the EFI memory map\n");122return;123}124125offset_limit = efi_mem_desc_end(&md) - efi.mokvar_table;126127/*128* Validate the MOK config table. Since there is no table header129* from which we could get the total size of the MOK config table,130* we compute the total size as we validate each variably sized131* entry, remapping as necessary.132*/133err = -EINVAL;134while (cur_offset + sizeof(*mokvar_entry) <= offset_limit) {135if (va)136early_memunmap(va, sizeof(*mokvar_entry));137va = early_memremap(efi.mokvar_table + cur_offset, sizeof(*mokvar_entry));138if (!va) {139pr_err("Failed to map EFI MOKvar config table pa=0x%lx, size=%zu.\n",140efi.mokvar_table + cur_offset, sizeof(*mokvar_entry));141return;142}143mokvar_entry = va;144next:145/* Check for last sentinel entry */146if (mokvar_entry->name[0] == '\0') {147if (mokvar_entry->data_size != 0)148break;149err = 0;150map_size_needed = cur_offset + sizeof(*mokvar_entry);151break;152}153154/* Enforce that the name is NUL terminated */155mokvar_entry->name[sizeof(mokvar_entry->name) - 1] = '\0';156157/* Advance to the next entry */158size = sizeof(*mokvar_entry) + mokvar_entry->data_size;159cur_offset += size;160161/*162* Don't bother remapping if the current entry header and the163* next one end on the same page.164*/165next_entry = (void *)((unsigned long)mokvar_entry + size);166if (((((unsigned long)(mokvar_entry + 1) - 1) ^167((unsigned long)(next_entry + 1) - 1)) & PAGE_MASK) == 0) {168mokvar_entry = next_entry;169goto next;170}171}172173if (va)174early_memunmap(va, sizeof(*mokvar_entry));175if (err) {176pr_err("EFI MOKvar config table is not valid\n");177return;178}179180if (md.type == EFI_BOOT_SERVICES_DATA)181efi_mem_reserve(efi.mokvar_table, map_size_needed);182183efi_mokvar_table_size = map_size_needed;184}185186/*187* efi_mokvar_entry_next() - Get next entry in the EFI MOK config table188*189* mokvar_entry: Pointer to current EFI MOK config table entry190* or null. Null indicates get first entry.191* Passed by reference. This is updated to the192* same value as the return value.193*194* Returns: Pointer to next EFI MOK config table entry195* or null, if there are no more entries.196* Same value is returned in the mokvar_entry197* parameter.198*199* This routine depends on the EFI MOK config table being entirely200* mapped with it's starting virtual address in efi_mokvar_table_va.201*/202struct efi_mokvar_table_entry *efi_mokvar_entry_next(203struct efi_mokvar_table_entry **mokvar_entry)204{205struct efi_mokvar_table_entry *mokvar_cur;206struct efi_mokvar_table_entry *mokvar_next;207size_t size_cur;208209mokvar_cur = *mokvar_entry;210*mokvar_entry = NULL;211212if (efi_mokvar_table_va == NULL)213return NULL;214215if (mokvar_cur == NULL) {216mokvar_next = efi_mokvar_table_va;217} else {218if (mokvar_cur->name[0] == '\0')219return NULL;220size_cur = sizeof(*mokvar_cur) + mokvar_cur->data_size;221mokvar_next = (void *)mokvar_cur + size_cur;222}223224if (mokvar_next->name[0] == '\0')225return NULL;226227*mokvar_entry = mokvar_next;228return mokvar_next;229}230231/*232* efi_mokvar_entry_find() - Find EFI MOK config entry by name233*234* name: Name of the entry to look for.235*236* Returns: Pointer to EFI MOK config table entry if found;237* null otherwise.238*239* This routine depends on the EFI MOK config table being entirely240* mapped with it's starting virtual address in efi_mokvar_table_va.241*/242struct efi_mokvar_table_entry *efi_mokvar_entry_find(const char *name)243{244struct efi_mokvar_table_entry *mokvar_entry = NULL;245246while (efi_mokvar_entry_next(&mokvar_entry)) {247if (!strncmp(name, mokvar_entry->name,248sizeof(mokvar_entry->name)))249return mokvar_entry;250}251return NULL;252}253254/*255* efi_mokvar_sysfs_read() - sysfs binary file read routine256*257* Returns: Count of bytes read.258*259* Copy EFI MOK config table entry data for this mokvar sysfs binary file260* to the supplied buffer, starting at the specified offset into mokvar table261* entry data, for the specified count bytes. The copy is limited by the262* amount of data in this mokvar config table entry.263*/264static ssize_t efi_mokvar_sysfs_read(struct file *file, struct kobject *kobj,265const struct bin_attribute *bin_attr, char *buf,266loff_t off, size_t count)267{268struct efi_mokvar_table_entry *mokvar_entry = bin_attr->private;269270if (!capable(CAP_SYS_ADMIN))271return 0;272273if (off >= mokvar_entry->data_size)274return 0;275if (count > mokvar_entry->data_size - off)276count = mokvar_entry->data_size - off;277278memcpy(buf, mokvar_entry->data + off, count);279return count;280}281282/*283* efi_mokvar_sysfs_init() - Map EFI MOK config table and create sysfs284*285* Map the EFI MOK variable config table for run-time use by the kernel286* and create the sysfs entries in /sys/firmware/efi/mok-variables/287*288* This routine just returns if a valid EFI MOK variable config table289* was not found earlier during boot.290*291* This routine must be called during a "middle" initcall phase, i.e.292* after efi_mokvar_table_init() but before UEFI certs are loaded293* during late init.294*295* Implicit inputs:296* efi.mokvar_table: Physical address of EFI MOK variable config table297* or special value that indicates no such table.298*299* efi_mokvar_table_size: Computed size of EFI MOK variable config table.300* The table is considered present and valid if this301* is non-zero.302*303* Implicit outputs:304* efi_mokvar_table_va: Start virtual address of the EFI MOK config table.305*/306static int __init efi_mokvar_sysfs_init(void)307{308void *config_va;309struct efi_mokvar_table_entry *mokvar_entry = NULL;310struct efi_mokvar_sysfs_attr *mokvar_sysfs = NULL;311int err = 0;312313if (efi_mokvar_table_size == 0)314return -ENOENT;315316config_va = memremap(efi.mokvar_table, efi_mokvar_table_size,317MEMREMAP_WB);318if (!config_va) {319pr_err("Failed to map EFI MOKvar config table\n");320return -ENOMEM;321}322efi_mokvar_table_va = config_va;323324mokvar_kobj = kobject_create_and_add("mok-variables", efi_kobj);325if (!mokvar_kobj) {326pr_err("Failed to create EFI mok-variables sysfs entry\n");327return -ENOMEM;328}329330while (efi_mokvar_entry_next(&mokvar_entry)) {331mokvar_sysfs = kzalloc(sizeof(*mokvar_sysfs), GFP_KERNEL);332if (!mokvar_sysfs) {333err = -ENOMEM;334break;335}336337sysfs_bin_attr_init(&mokvar_sysfs->bin_attr);338mokvar_sysfs->bin_attr.private = mokvar_entry;339mokvar_sysfs->bin_attr.attr.name = mokvar_entry->name;340mokvar_sysfs->bin_attr.attr.mode = 0400;341mokvar_sysfs->bin_attr.size = mokvar_entry->data_size;342mokvar_sysfs->bin_attr.read = efi_mokvar_sysfs_read;343344err = sysfs_create_bin_file(mokvar_kobj,345&mokvar_sysfs->bin_attr);346if (err)347break;348349list_add_tail(&mokvar_sysfs->node, &efi_mokvar_sysfs_list);350}351352if (err) {353pr_err("Failed to create some EFI mok-variables sysfs entries\n");354kfree(mokvar_sysfs);355}356return err;357}358fs_initcall(efi_mokvar_sysfs_init);359360361