1 #include <linux/kernel.h>
2 #include <linux/sched.h>
3 #include <linux/cred.h>
6 #include <linux/slab.h>
7 #include <keys/asymmetric-type.h>
8 #include <keys/system_keyring.h>
11 static __initdata efi_guid_t efi_cert_x509_guid
= EFI_CERT_X509_GUID
;
12 static __initdata efi_guid_t efi_cert_x509_sha256_guid
= EFI_CERT_X509_SHA256_GUID
;
13 static __initdata efi_guid_t efi_cert_sha256_guid
= EFI_CERT_SHA256_GUID
;
16 * Get a certificate list blob from the named EFI variable.
18 static __init
void *get_cert_list(efi_char16_t
*name
, efi_guid_t
*guid
,
22 unsigned long lsize
= 4;
23 unsigned long tmpdb
[4];
26 status
= efi
.get_variable(name
, guid
, NULL
, &lsize
, &tmpdb
);
27 if (status
!= EFI_BUFFER_TOO_SMALL
) {
28 pr_err("Couldn't get size: 0x%lx\n", status
);
32 db
= kmalloc(lsize
, GFP_KERNEL
);
34 pr_err("Couldn't allocate memory for uefi cert list\n");
38 status
= efi
.get_variable(name
, guid
, NULL
, &lsize
, db
);
39 if (status
!= EFI_SUCCESS
) {
41 pr_err("Error reading db var: 0x%lx\n", status
);
50 * Blacklist an X509 TBS hash.
52 static __init
void uefi_blacklist_x509_tbs(const char *source
,
53 const void *data
, size_t len
)
57 hash
= kmalloc(4 + len
* 2 + 1, GFP_KERNEL
);
60 p
= memcpy(hash
, "tbs:", 4);
62 bin2hex(p
, data
, len
);
66 mark_hash_blacklisted(hash
);
71 * Blacklist the hash of an executable.
73 static __init
void uefi_blacklist_binary(const char *source
,
74 const void *data
, size_t len
)
78 hash
= kmalloc(4 + len
* 2 + 1, GFP_KERNEL
);
81 p
= memcpy(hash
, "bin:", 4);
83 bin2hex(p
, data
, len
);
87 mark_hash_blacklisted(hash
);
92 * Return the appropriate handler for particular signature list types found in
93 * the UEFI db and MokListRT tables.
95 static __init efi_element_handler_t
get_handler_for_db(const efi_guid_t
*sig_type
)
97 if (efi_guidcmp(*sig_type
, efi_cert_x509_guid
) == 0)
98 return add_trusted_secondary_key
;
103 * Return the appropriate handler for particular signature list types found in
104 * the UEFI dbx and MokListXRT tables.
106 static __init efi_element_handler_t
get_handler_for_dbx(const efi_guid_t
*sig_type
)
108 if (efi_guidcmp(*sig_type
, efi_cert_x509_sha256_guid
) == 0)
109 return uefi_blacklist_x509_tbs
;
110 if (efi_guidcmp(*sig_type
, efi_cert_sha256_guid
) == 0)
111 return uefi_blacklist_binary
;
116 * Load the certs contained in the UEFI databases
118 static int __init
load_uefi_certs(void)
120 efi_guid_t secure_var
= EFI_IMAGE_SECURITY_DATABASE_GUID
;
121 efi_guid_t mok_var
= EFI_SHIM_LOCK_GUID
;
122 void *db
= NULL
, *dbx
= NULL
, *mok
= NULL
;
123 unsigned long dbsize
= 0, dbxsize
= 0, moksize
= 0;
126 if (!efi
.get_variable
)
129 /* Get db, MokListRT, and dbx. They might not exist, so it isn't
130 * an error if we can't get them.
132 db
= get_cert_list(L
"db", &secure_var
, &dbsize
);
134 pr_err("MODSIGN: Couldn't get UEFI db list\n");
136 rc
= parse_efi_signature_list("UEFI:db",
137 db
, dbsize
, get_handler_for_db
);
139 pr_err("Couldn't parse db signatures: %d\n", rc
);
143 mok
= get_cert_list(L
"MokListRT", &mok_var
, &moksize
);
145 pr_info("MODSIGN: Couldn't get UEFI MokListRT\n");
147 rc
= parse_efi_signature_list("UEFI:MokListRT",
148 mok
, moksize
, get_handler_for_db
);
150 pr_err("Couldn't parse MokListRT signatures: %d\n", rc
);
154 dbx
= get_cert_list(L
"dbx", &secure_var
, &dbxsize
);
156 pr_info("MODSIGN: Couldn't get UEFI dbx list\n");
158 rc
= parse_efi_signature_list("UEFI:dbx",
160 get_handler_for_dbx
);
162 pr_err("Couldn't parse dbx signatures: %d\n", rc
);
168 late_initcall(load_uefi_certs
);