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") },
37 { UEFI_QUIRK_SKIP_CERT("Apple Inc.", "iMacPro1,1") },
42 * Look to see if a UEFI variable called MokIgnoreDB exists and return true if
45 * This UEFI variable is set by the shim if a user tells the shim to not use
46 * the certs/hashes in the UEFI db variable for verification purposes. If it
47 * is set, we should ignore the db variable also and the true return indicates
50 static __init bool uefi_check_ignore_db(void)
54 unsigned long size = sizeof(db);
55 efi_guid_t guid = EFI_SHIM_LOCK_GUID;
57 status = efi.get_variable(L"MokIgnoreDB", &guid, NULL, &size, &db);
58 return status == EFI_SUCCESS;
62 * Get a certificate list blob from the named EFI variable.
64 static __init void *get_cert_list(efi_char16_t *name, efi_guid_t *guid,
65 unsigned long *size, efi_status_t *status)
67 unsigned long lsize = 4;
68 unsigned long tmpdb[4];
71 *status = efi.get_variable(name, guid, NULL, &lsize, &tmpdb);
72 if (*status == EFI_NOT_FOUND)
75 if (*status != EFI_BUFFER_TOO_SMALL) {
76 pr_err("Couldn't get size: 0x%lx\n", *status);
80 db = kmalloc(lsize, GFP_KERNEL);
84 *status = efi.get_variable(name, guid, NULL, &lsize, db);
85 if (*status != EFI_SUCCESS) {
87 pr_err("Error reading db var: 0x%lx\n", *status);
96 * load_moklist_certs() - Load MokList certs
98 * Load the certs contained in the UEFI MokListRT database into the
99 * platform trusted keyring.
101 * This routine checks the EFI MOK config table first. If and only if
102 * that fails, this routine uses the MokListRT ordinary UEFI variable.
106 static int __init load_moklist_certs(void)
108 struct efi_mokvar_table_entry *mokvar_entry;
109 efi_guid_t mok_var = EFI_SHIM_LOCK_GUID;
111 unsigned long moksize;
115 /* First try to load certs from the EFI MOKvar config table.
116 * It's not an error if the MOKvar config table doesn't exist
117 * or the MokListRT entry is not found in it.
119 mokvar_entry = efi_mokvar_entry_find("MokListRT");
121 rc = parse_efi_signature_list("UEFI:MokListRT (MOKvar table)",
123 mokvar_entry->data_size,
125 /* All done if that worked. */
129 pr_err("Couldn't parse MokListRT signatures from EFI MOKvar config table: %d\n",
133 /* Get MokListRT. It might not exist, so it isn't an error
134 * if we can't get it.
136 mok = get_cert_list(L"MokListRT", &mok_var, &moksize, &status);
138 rc = parse_efi_signature_list("UEFI:MokListRT",
139 mok, moksize, get_handler_for_db);
142 pr_err("Couldn't parse MokListRT signatures: %d\n", rc);
145 if (status == EFI_NOT_FOUND)
146 pr_debug("MokListRT variable wasn't found\n");
148 pr_info("Couldn't get UEFI MokListRT\n");
153 * load_uefi_certs() - Load certs from UEFI sources
155 * Load the certs contained in the UEFI databases into the platform trusted
156 * keyring and the UEFI blacklisted X.509 cert SHA256 hashes into the blacklist
159 static int __init load_uefi_certs(void)
161 efi_guid_t secure_var = EFI_IMAGE_SECURITY_DATABASE_GUID;
162 efi_guid_t mok_var = EFI_SHIM_LOCK_GUID;
163 void *db = NULL, *dbx = NULL, *mokx = NULL;
164 unsigned long dbsize = 0, dbxsize = 0, mokxsize = 0;
167 const struct dmi_system_id *dmi_id;
169 dmi_id = dmi_first_match(uefi_skip_cert);
171 pr_err("Reading UEFI Secure Boot Certs is not supported on T2 Macs.\n");
175 if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
178 /* Get db and dbx. They might not exist, so it isn't an error
179 * if we can't get them.
181 if (!uefi_check_ignore_db()) {
182 db = get_cert_list(L"db", &secure_var, &dbsize, &status);
184 if (status == EFI_NOT_FOUND)
185 pr_debug("MODSIGN: db variable wasn't found\n");
187 pr_err("MODSIGN: Couldn't get UEFI db list\n");
189 rc = parse_efi_signature_list("UEFI:db",
190 db, dbsize, get_handler_for_db);
192 pr_err("Couldn't parse db signatures: %d\n",
198 dbx = get_cert_list(L"dbx", &secure_var, &dbxsize, &status);
200 if (status == EFI_NOT_FOUND)
201 pr_debug("dbx variable wasn't found\n");
203 pr_info("Couldn't get UEFI dbx list\n");
205 rc = parse_efi_signature_list("UEFI:dbx",
207 get_handler_for_dbx);
209 pr_err("Couldn't parse dbx signatures: %d\n", rc);
213 mokx = get_cert_list(L"MokListXRT", &mok_var, &mokxsize, &status);
215 if (status == EFI_NOT_FOUND)
216 pr_debug("mokx variable wasn't found\n");
218 pr_info("Couldn't get mokx list\n");
220 rc = parse_efi_signature_list("UEFI:MokListXRT",
222 get_handler_for_dbx);
224 pr_err("Couldn't parse mokx signatures %d\n", rc);
228 /* Load the MokListRT certs */
229 rc = load_moklist_certs();
233 late_initcall(load_uefi_certs);