1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
4 #include <linux/sched.h>
5 #include <linux/cred.h>
9 #include <linux/slab.h>
10 #include <linux/ima.h>
11 #include <keys/asymmetric-type.h>
12 #include <keys/system_keyring.h>
13 #include "../integrity.h"
14 #include "keyring_handler.h"
17 * On T2 Macs reading the db and dbx efi variables to load UEFI Secure Boot
18 * certificates causes occurrence of a page fault in Apple's firmware and
19 * a crash disabling EFI runtime services. The following quirk skips reading
22 static const struct dmi_system_id uefi_skip_cert[] = {
23 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,1") },
24 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,2") },
25 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,3") },
26 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,4") },
27 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,1") },
28 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,2") },
29 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,3") },
30 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,4") },
31 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookAir8,1") },
32 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookAir8,2") },
33 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookAir9,1") },
34 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "Macmini8,1") },
35 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacPro7,1") },
36 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "iMac20,1") },
37 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "iMac20,2") },
38 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "iMacPro1,1") },
43 * Look to see if a UEFI variable called MokIgnoreDB exists and return true if
46 * This UEFI variable is set by the shim if a user tells the shim to not use
47 * the certs/hashes in the UEFI db variable for verification purposes. If it
48 * is set, we should ignore the db variable also and the true return indicates
51 static __init bool uefi_check_ignore_db(void)
55 unsigned long size = sizeof(db);
56 efi_guid_t guid = EFI_SHIM_LOCK_GUID;
58 status = efi.get_variable(L"MokIgnoreDB", &guid, NULL, &size, &db);
59 return status == EFI_SUCCESS;
63 * Get a certificate list blob from the named EFI variable.
65 static __init void *get_cert_list(efi_char16_t *name, efi_guid_t *guid,
66 unsigned long *size, efi_status_t *status)
68 unsigned long lsize = 4;
69 unsigned long tmpdb[4];
72 *status = efi.get_variable(name, guid, NULL, &lsize, &tmpdb);
73 if (*status == EFI_NOT_FOUND)
76 if (*status != EFI_BUFFER_TOO_SMALL) {
77 pr_err("Couldn't get size: 0x%lx\n", *status);
81 db = kmalloc(lsize, GFP_KERNEL);
85 *status = efi.get_variable(name, guid, NULL, &lsize, db);
86 if (*status != EFI_SUCCESS) {
88 pr_err("Error reading db var: 0x%lx\n", *status);
97 * load_moklist_certs() - Load MokList certs
99 * Load the certs contained in the UEFI MokListRT database into the
100 * platform trusted keyring.
102 * This routine checks the EFI MOK config table first. If and only if
103 * that fails, this routine uses the MokListRT ordinary UEFI variable.
107 static int __init load_moklist_certs(void)
109 struct efi_mokvar_table_entry *mokvar_entry;
110 efi_guid_t mok_var = EFI_SHIM_LOCK_GUID;
112 unsigned long moksize;
116 /* First try to load certs from the EFI MOKvar config table.
117 * It's not an error if the MOKvar config table doesn't exist
118 * or the MokListRT entry is not found in it.
120 mokvar_entry = efi_mokvar_entry_find("MokListRT");
122 rc = parse_efi_signature_list("UEFI:MokListRT (MOKvar table)",
124 mokvar_entry->data_size,
125 get_handler_for_mok);
126 /* All done if that worked. */
130 pr_err("Couldn't parse MokListRT signatures from EFI MOKvar config table: %d\n",
134 /* Get MokListRT. It might not exist, so it isn't an error
135 * if we can't get it.
137 mok = get_cert_list(L"MokListRT", &mok_var, &moksize, &status);
139 rc = parse_efi_signature_list("UEFI:MokListRT",
140 mok, moksize, get_handler_for_mok);
143 pr_err("Couldn't parse MokListRT signatures: %d\n", rc);
146 if (status == EFI_NOT_FOUND)
147 pr_debug("MokListRT variable wasn't found\n");
149 pr_info("Couldn't get UEFI MokListRT\n");
154 * load_uefi_certs() - Load certs from UEFI sources
156 * Load the certs contained in the UEFI databases into the platform trusted
157 * keyring and the UEFI blacklisted X.509 cert SHA256 hashes into the blacklist
160 static int __init load_uefi_certs(void)
162 efi_guid_t secure_var = EFI_IMAGE_SECURITY_DATABASE_GUID;
163 efi_guid_t mok_var = EFI_SHIM_LOCK_GUID;
164 void *db = NULL, *dbx = NULL, *mokx = NULL;
165 unsigned long dbsize = 0, dbxsize = 0, mokxsize = 0;
168 const struct dmi_system_id *dmi_id;
170 dmi_id = dmi_first_match(uefi_skip_cert);
172 pr_err("Reading UEFI Secure Boot Certs is not supported on T2 Macs.\n");
176 if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
179 /* Get db and dbx. They might not exist, so it isn't an error
180 * if we can't get them.
182 if (!uefi_check_ignore_db()) {
183 db = get_cert_list(L"db", &secure_var, &dbsize, &status);
185 if (status == EFI_NOT_FOUND)
186 pr_debug("MODSIGN: db variable wasn't found\n");
188 pr_err("MODSIGN: Couldn't get UEFI db list\n");
190 rc = parse_efi_signature_list("UEFI:db",
191 db, dbsize, get_handler_for_db);
193 pr_err("Couldn't parse db signatures: %d\n",
199 dbx = get_cert_list(L"dbx", &secure_var, &dbxsize, &status);
201 if (status == EFI_NOT_FOUND)
202 pr_debug("dbx variable wasn't found\n");
204 pr_info("Couldn't get UEFI dbx list\n");
206 rc = parse_efi_signature_list("UEFI:dbx",
208 get_handler_for_dbx);
210 pr_err("Couldn't parse dbx signatures: %d\n", rc);
214 /* the MOK/MOKx can not be trusted when secure boot is disabled */
215 if (!arch_ima_get_secureboot())
218 mokx = get_cert_list(L"MokListXRT", &mok_var, &mokxsize, &status);
220 if (status == EFI_NOT_FOUND)
221 pr_debug("mokx variable wasn't found\n");
223 pr_info("Couldn't get mokx list\n");
225 rc = parse_efi_signature_list("UEFI:MokListXRT",
227 get_handler_for_dbx);
229 pr_err("Couldn't parse mokx signatures %d\n", rc);
233 /* Load the MokListRT certs */
234 rc = load_moklist_certs();
238 late_initcall(load_uefi_certs);