1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/kernel.h>
4 #include <linux/sched.h>
5 #include <linux/cred.h>
6 #include <linux/err.h>
7 #include <linux/efi.h>
8 #include <linux/slab.h>
9 #include <keys/asymmetric-type.h>
10 #include <keys/system_keyring.h>
11 #include "../integrity.h"
12 #include "keyring_handler.h"
13 
14 static efi_guid_t efi_cert_x509_guid __initdata = EFI_CERT_X509_GUID;
15 static efi_guid_t efi_cert_x509_sha256_guid __initdata =
16 	EFI_CERT_X509_SHA256_GUID;
17 static efi_guid_t efi_cert_sha256_guid __initdata = EFI_CERT_SHA256_GUID;
18 
19 /*
20  * Blacklist a hash.
21  */
22 static __init void uefi_blacklist_hash(const char *source, const void *data,
23 				       size_t len, const char *type,
24 				       size_t type_len)
25 {
26 	char *hash, *p;
27 
28 	hash = kmalloc(type_len + len * 2 + 1, GFP_KERNEL);
29 	if (!hash)
30 		return;
31 	p = memcpy(hash, type, type_len);
32 	p += type_len;
33 	bin2hex(p, data, len);
34 	p += len * 2;
35 	*p = 0;
36 
37 	mark_hash_blacklisted(hash);
38 	kfree(hash);
39 }
40 
41 /*
42  * Blacklist an X509 TBS hash.
43  */
44 static __init void uefi_blacklist_x509_tbs(const char *source,
45 					   const void *data, size_t len)
46 {
47 	uefi_blacklist_hash(source, data, len, "tbs:", 4);
48 }
49 
50 /*
51  * Blacklist the hash of an executable.
52  */
53 static __init void uefi_blacklist_binary(const char *source,
54 					 const void *data, size_t len)
55 {
56 	uefi_blacklist_hash(source, data, len, "bin:", 4);
57 }
58 
59 /*
60  * Add an X509 cert to the revocation list.
61  */
62 static __init void uefi_revocation_list_x509(const char *source,
63 					     const void *data, size_t len)
64 {
65 	add_key_to_revocation_list(data, len);
66 }
67 
68 /*
69  * Return the appropriate handler for particular signature list types found in
70  * the UEFI db tables.
71  */
72 __init efi_element_handler_t get_handler_for_db(const efi_guid_t *sig_type)
73 {
74 	if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0)
75 		return add_to_platform_keyring;
76 	return 0;
77 }
78 
79 /*
80  * Return the appropriate handler for particular signature list types found in
81  * the MokListRT tables.
82  */
83 __init efi_element_handler_t get_handler_for_mok(const efi_guid_t *sig_type)
84 {
85 	if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0) {
86 		if (IS_ENABLED(CONFIG_INTEGRITY_MACHINE_KEYRING) && trust_moklist())
87 			return add_to_machine_keyring;
88 		else
89 			return add_to_platform_keyring;
90 	}
91 	return 0;
92 }
93 
94 /*
95  * Return the appropriate handler for particular signature list types found in
96  * the UEFI dbx and MokListXRT tables.
97  */
98 __init efi_element_handler_t get_handler_for_dbx(const efi_guid_t *sig_type)
99 {
100 	if (efi_guidcmp(*sig_type, efi_cert_x509_sha256_guid) == 0)
101 		return uefi_blacklist_x509_tbs;
102 	if (efi_guidcmp(*sig_type, efi_cert_sha256_guid) == 0)
103 		return uefi_blacklist_binary;
104 	if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0)
105 		return uefi_revocation_list_x509;
106 	return 0;
107 }
108