xref: /openbmc/linux/certs/blacklist.c (revision bf21dc59)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* System hash blacklist.
3  *
4  * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #define pr_fmt(fmt) "blacklist: "fmt
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/key.h>
12 #include <linux/key-type.h>
13 #include <linux/sched.h>
14 #include <linux/ctype.h>
15 #include <linux/err.h>
16 #include <linux/seq_file.h>
17 #include <linux/uidgid.h>
18 #include <keys/system_keyring.h>
19 #include "blacklist.h"
20 #include "common.h"
21 
22 /*
23  * According to crypto/asymmetric_keys/x509_cert_parser.c:x509_note_pkey_algo(),
24  * the size of the currently longest supported hash algorithm is 512 bits,
25  * which translates into 128 hex characters.
26  */
27 #define MAX_HASH_LEN	128
28 
29 static const char tbs_prefix[] = "tbs";
30 static const char bin_prefix[] = "bin";
31 
32 static struct key *blacklist_keyring;
33 
34 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
35 extern __initconst const u8 revocation_certificate_list[];
36 extern __initconst const unsigned long revocation_certificate_list_size;
37 #endif
38 
39 /*
40  * The description must be a type prefix, a colon and then an even number of
41  * hex digits.  The hash is kept in the description.
42  */
43 static int blacklist_vet_description(const char *desc)
44 {
45 	int i, prefix_len, tbs_step = 0, bin_step = 0;
46 
47 	/* The following algorithm only works if prefix lengths match. */
48 	BUILD_BUG_ON(sizeof(tbs_prefix) != sizeof(bin_prefix));
49 	prefix_len = sizeof(tbs_prefix) - 1;
50 	for (i = 0; *desc; desc++, i++) {
51 		if (*desc == ':') {
52 			if (tbs_step == prefix_len)
53 				goto found_colon;
54 			if (bin_step == prefix_len)
55 				goto found_colon;
56 			return -EINVAL;
57 		}
58 		if (i >= prefix_len)
59 			return -EINVAL;
60 		if (*desc == tbs_prefix[i])
61 			tbs_step++;
62 		if (*desc == bin_prefix[i])
63 			bin_step++;
64 	}
65 	return -EINVAL;
66 
67 found_colon:
68 	desc++;
69 	for (i = 0; *desc && i < MAX_HASH_LEN; desc++, i++) {
70 		if (!isxdigit(*desc) || isupper(*desc))
71 			return -EINVAL;
72 	}
73 	if (*desc)
74 		/* The hash is greater than MAX_HASH_LEN. */
75 		return -ENOPKG;
76 
77 	/* Checks for an even number of hexadecimal characters. */
78 	if (i == 0 || i & 1)
79 		return -EINVAL;
80 	return 0;
81 }
82 
83 /*
84  * The hash to be blacklisted is expected to be in the description.  There will
85  * be no payload.
86  */
87 static int blacklist_preparse(struct key_preparsed_payload *prep)
88 {
89 	if (prep->datalen > 0)
90 		return -EINVAL;
91 	return 0;
92 }
93 
94 static void blacklist_free_preparse(struct key_preparsed_payload *prep)
95 {
96 }
97 
98 static void blacklist_describe(const struct key *key, struct seq_file *m)
99 {
100 	seq_puts(m, key->description);
101 }
102 
103 static struct key_type key_type_blacklist = {
104 	.name			= "blacklist",
105 	.vet_description	= blacklist_vet_description,
106 	.preparse		= blacklist_preparse,
107 	.free_preparse		= blacklist_free_preparse,
108 	.instantiate		= generic_key_instantiate,
109 	.describe		= blacklist_describe,
110 };
111 
112 static char *get_raw_hash(const u8 *hash, size_t hash_len,
113 		enum blacklist_hash_type hash_type)
114 {
115 	size_t type_len;
116 	const char *type_prefix;
117 	char *buffer, *p;
118 
119 	switch (hash_type) {
120 	case BLACKLIST_HASH_X509_TBS:
121 		type_len = sizeof(tbs_prefix) - 1;
122 		type_prefix = tbs_prefix;
123 		break;
124 	case BLACKLIST_HASH_BINARY:
125 		type_len = sizeof(bin_prefix) - 1;
126 		type_prefix = bin_prefix;
127 		break;
128 	default:
129 		WARN_ON_ONCE(1);
130 		return ERR_PTR(-EINVAL);
131 	}
132 	buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL);
133 	if (!buffer)
134 		return ERR_PTR(-ENOMEM);
135 	p = memcpy(buffer, type_prefix, type_len);
136 	p += type_len;
137 	*p++ = ':';
138 	bin2hex(p, hash, hash_len);
139 	p += hash_len * 2;
140 	*p = '\0';
141 	return buffer;
142 }
143 
144 /**
145  * mark_raw_hash_blacklisted - Add a hash to the system blacklist
146  * @hash: The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
147  */
148 static int mark_raw_hash_blacklisted(const char *hash)
149 {
150 	key_ref_t key;
151 
152 	key = key_create_or_update(make_key_ref(blacklist_keyring, true),
153 				   "blacklist",
154 				   hash,
155 				   NULL,
156 				   0,
157 				   ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
158 				    KEY_USR_VIEW),
159 				   KEY_ALLOC_NOT_IN_QUOTA |
160 				   KEY_ALLOC_BUILT_IN);
161 	if (IS_ERR(key)) {
162 		pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key));
163 		return PTR_ERR(key);
164 	}
165 	return 0;
166 }
167 
168 int mark_hash_blacklisted(const u8 *hash, size_t hash_len,
169 		enum blacklist_hash_type hash_type)
170 {
171 	const char *buffer;
172 	int err;
173 
174 	buffer = get_raw_hash(hash, hash_len, hash_type);
175 	if (IS_ERR(buffer))
176 		return PTR_ERR(buffer);
177 	err = mark_raw_hash_blacklisted(buffer);
178 	kfree(buffer);
179 	return err;
180 }
181 
182 /**
183  * is_hash_blacklisted - Determine if a hash is blacklisted
184  * @hash: The hash to be checked as a binary blob
185  * @hash_len: The length of the binary hash
186  * @hash_type: Type of hash
187  */
188 int is_hash_blacklisted(const u8 *hash, size_t hash_len,
189 		enum blacklist_hash_type hash_type)
190 {
191 	key_ref_t kref;
192 	const char *buffer;
193 	int ret = 0;
194 
195 	buffer = get_raw_hash(hash, hash_len, hash_type);
196 	if (IS_ERR(buffer))
197 		return PTR_ERR(buffer);
198 	kref = keyring_search(make_key_ref(blacklist_keyring, true),
199 			      &key_type_blacklist, buffer, false);
200 	if (!IS_ERR(kref)) {
201 		key_ref_put(kref);
202 		ret = -EKEYREJECTED;
203 	}
204 
205 	kfree(buffer);
206 	return ret;
207 }
208 EXPORT_SYMBOL_GPL(is_hash_blacklisted);
209 
210 int is_binary_blacklisted(const u8 *hash, size_t hash_len)
211 {
212 	if (is_hash_blacklisted(hash, hash_len, BLACKLIST_HASH_BINARY) ==
213 			-EKEYREJECTED)
214 		return -EPERM;
215 
216 	return 0;
217 }
218 EXPORT_SYMBOL_GPL(is_binary_blacklisted);
219 
220 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
221 /**
222  * add_key_to_revocation_list - Add a revocation certificate to the blacklist
223  * @data: The data blob containing the certificate
224  * @size: The size of data blob
225  */
226 int add_key_to_revocation_list(const char *data, size_t size)
227 {
228 	key_ref_t key;
229 
230 	key = key_create_or_update(make_key_ref(blacklist_keyring, true),
231 				   "asymmetric",
232 				   NULL,
233 				   data,
234 				   size,
235 				   ((KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW),
236 				   KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN);
237 
238 	if (IS_ERR(key)) {
239 		pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key));
240 		return PTR_ERR(key);
241 	}
242 
243 	return 0;
244 }
245 
246 /**
247  * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked
248  * @pkcs7: The PKCS#7 message to check
249  */
250 int is_key_on_revocation_list(struct pkcs7_message *pkcs7)
251 {
252 	int ret;
253 
254 	ret = pkcs7_validate_trust(pkcs7, blacklist_keyring);
255 
256 	if (ret == 0)
257 		return -EKEYREJECTED;
258 
259 	return -ENOKEY;
260 }
261 #endif
262 
263 /*
264  * Initialise the blacklist
265  */
266 static int __init blacklist_init(void)
267 {
268 	const char *const *bl;
269 
270 	if (register_key_type(&key_type_blacklist) < 0)
271 		panic("Can't allocate system blacklist key type\n");
272 
273 	blacklist_keyring =
274 		keyring_alloc(".blacklist",
275 			      GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
276 			      (KEY_POS_ALL & ~KEY_POS_SETATTR) |
277 			      KEY_USR_VIEW | KEY_USR_READ |
278 			      KEY_USR_SEARCH,
279 			      KEY_ALLOC_NOT_IN_QUOTA |
280 			      KEY_ALLOC_SET_KEEP,
281 			      NULL, NULL);
282 	if (IS_ERR(blacklist_keyring))
283 		panic("Can't allocate system blacklist keyring\n");
284 
285 	for (bl = blacklist_hashes; *bl; bl++)
286 		if (mark_raw_hash_blacklisted(*bl) < 0)
287 			pr_err("- blacklisting failed\n");
288 	return 0;
289 }
290 
291 /*
292  * Must be initialised before we try and load the keys into the keyring.
293  */
294 device_initcall(blacklist_init);
295 
296 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
297 /*
298  * Load the compiled-in list of revocation X.509 certificates.
299  */
300 static __init int load_revocation_certificate_list(void)
301 {
302 	if (revocation_certificate_list_size)
303 		pr_notice("Loading compiled-in revocation X.509 certificates\n");
304 
305 	return load_certificate_list(revocation_certificate_list, revocation_certificate_list_size,
306 				     blacklist_keyring);
307 }
308 late_initcall(load_revocation_certificate_list);
309 #endif
310