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 <keys/asymmetric-type.h>
11 #include <keys/system_keyring.h>
12 #include "../integrity.h"
13 #include "keyring_handler.h"
16 * On T2 Macs reading the db and dbx efi variables to load UEFI Secure Boot
17 * certificates causes occurrence of a page fault in Apple's firmware and
18 * a crash disabling EFI runtime services. The following quirk skips reading
21 static const struct dmi_system_id uefi_skip_cert[] = {
22 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,1") },
23 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,2") },
24 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,3") },
25 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,4") },
26 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,1") },
27 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,2") },
28 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,3") },
29 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,4") },
30 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookAir8,1") },
31 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookAir8,2") },
32 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookAir9,1") },
33 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "Macmini8,1") },
34 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacPro7,1") },
35 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "iMac20,1") },
36 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "iMac20,2") },
41 * Look to see if a UEFI variable called MokIgnoreDB exists and return true if
44 * This UEFI variable is set by the shim if a user tells the shim to not use
45 * the certs/hashes in the UEFI db variable for verification purposes. If it
46 * is set, we should ignore the db variable also and the true return indicates
49 static __init bool uefi_check_ignore_db(void)
53 unsigned long size = sizeof(db);
54 efi_guid_t guid = EFI_SHIM_LOCK_GUID;
56 status = efi.get_variable(L"MokIgnoreDB", &guid, NULL, &size, &db);
57 return status == EFI_SUCCESS;
61 * Get a certificate list blob from the named EFI variable.
63 static __init void *get_cert_list(efi_char16_t *name, efi_guid_t *guid,
64 unsigned long *size, efi_status_t *status)
66 unsigned long lsize = 4;
67 unsigned long tmpdb[4];
70 *status = efi.get_variable(name, guid, NULL, &lsize, &tmpdb);
71 if (*status == EFI_NOT_FOUND)
74 if (*status != EFI_BUFFER_TOO_SMALL) {
75 pr_err("Couldn't get size: 0x%lx\n", *status);
79 db = kmalloc(lsize, GFP_KERNEL);
83 *status = efi.get_variable(name, guid, NULL, &lsize, db);
84 if (*status != EFI_SUCCESS) {
86 pr_err("Error reading db var: 0x%lx\n", *status);
95 * load_moklist_certs() - Load MokList certs
97 * Load the certs contained in the UEFI MokListRT database into the
98 * platform trusted keyring.
100 * This routine checks the EFI MOK config table first. If and only if
101 * that fails, this routine uses the MokListRT ordinary UEFI variable.
105 static int __init load_moklist_certs(void)
107 struct efi_mokvar_table_entry *mokvar_entry;
108 efi_guid_t mok_var = EFI_SHIM_LOCK_GUID;
110 unsigned long moksize;
114 /* First try to load certs from the EFI MOKvar config table.
115 * It's not an error if the MOKvar config table doesn't exist
116 * or the MokListRT entry is not found in it.
118 mokvar_entry = efi_mokvar_entry_find("MokListRT");
120 rc = parse_efi_signature_list("UEFI:MokListRT (MOKvar table)",
122 mokvar_entry->data_size,
124 /* All done if that worked. */
128 pr_err("Couldn't parse MokListRT signatures from EFI MOKvar config table: %d\n",
132 /* Get MokListRT. It might not exist, so it isn't an error
133 * if we can't get it.
135 mok = get_cert_list(L"MokListRT", &mok_var, &moksize, &status);
137 rc = parse_efi_signature_list("UEFI:MokListRT",
138 mok, moksize, get_handler_for_db);
141 pr_err("Couldn't parse MokListRT signatures: %d\n", rc);
144 if (status == EFI_NOT_FOUND)
145 pr_debug("MokListRT variable wasn't found\n");
147 pr_info("Couldn't get UEFI MokListRT\n");
152 * load_uefi_certs() - Load certs from UEFI sources
154 * Load the certs contained in the UEFI databases into the platform trusted
155 * keyring and the UEFI blacklisted X.509 cert SHA256 hashes into the blacklist
158 static int __init load_uefi_certs(void)
160 efi_guid_t secure_var = EFI_IMAGE_SECURITY_DATABASE_GUID;
161 efi_guid_t mok_var = EFI_SHIM_LOCK_GUID;
162 void *db = NULL, *dbx = NULL, *mokx = NULL;
163 unsigned long dbsize = 0, dbxsize = 0, mokxsize = 0;
166 const struct dmi_system_id *dmi_id;
168 dmi_id = dmi_first_match(uefi_skip_cert);
170 pr_err("Reading UEFI Secure Boot Certs is not supported on T2 Macs.\n");
174 if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
177 /* Get db and dbx. They might not exist, so it isn't an error
178 * if we can't get them.
180 if (!uefi_check_ignore_db()) {
181 db = get_cert_list(L"db", &secure_var, &dbsize, &status);
183 if (status == EFI_NOT_FOUND)
184 pr_debug("MODSIGN: db variable wasn't found\n");
186 pr_err("MODSIGN: Couldn't get UEFI db list\n");
188 rc = parse_efi_signature_list("UEFI:db",
189 db, dbsize, get_handler_for_db);
191 pr_err("Couldn't parse db signatures: %d\n",
197 dbx = get_cert_list(L"dbx", &secure_var, &dbxsize, &status);
199 if (status == EFI_NOT_FOUND)
200 pr_debug("dbx variable wasn't found\n");
202 pr_info("Couldn't get UEFI dbx list\n");
204 rc = parse_efi_signature_list("UEFI:dbx",
206 get_handler_for_dbx);
208 pr_err("Couldn't parse dbx signatures: %d\n", rc);
212 mokx = get_cert_list(L"MokListXRT", &mok_var, &mokxsize, &status);
214 if (status == EFI_NOT_FOUND)
215 pr_debug("mokx variable wasn't found\n");
217 pr_info("Couldn't get mokx list\n");
219 rc = parse_efi_signature_list("UEFI:MokListXRT",
221 get_handler_for_dbx);
223 pr_err("Couldn't parse mokx signatures %d\n", rc);
227 /* Load the MokListRT certs */
228 rc = load_moklist_certs();
232 late_initcall(load_uefi_certs);