]> git.ipfire.org Git - people/ms/linux.git/blame - certs/blacklist.c
KVM: x86: Always enable legacy FP/SSE in allowed user XFEATURES
[people/ms/linux.git] / certs / blacklist.c
CommitLineData
b4d0d230 1// SPDX-License-Identifier: GPL-2.0-or-later
734114f8
DH
2/* System hash blacklist.
3 *
4 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
734114f8
DH
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>
a6cb0ab7 17#include <linux/uidgid.h>
60050ffe 18#include <keys/asymmetric-type.h>
734114f8
DH
19#include <keys/system_keyring.h>
20#include "blacklist.h"
21
bf21dc59
MS
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
6364d106
MS
29#define BLACKLIST_KEY_PERM (KEY_POS_SEARCH | KEY_POS_VIEW | \
30 KEY_USR_SEARCH | KEY_USR_VIEW)
31
bf21dc59
MS
32static const char tbs_prefix[] = "tbs";
33static const char bin_prefix[] = "bin";
34
734114f8
DH
35static struct key *blacklist_keyring;
36
d1f04410
ES
37#ifdef CONFIG_SYSTEM_REVOCATION_LIST
38extern __initconst const u8 revocation_certificate_list[];
39extern __initconst const unsigned long revocation_certificate_list_size;
40#endif
41
734114f8
DH
42/*
43 * The description must be a type prefix, a colon and then an even number of
44 * hex digits. The hash is kept in the description.
45 */
46static int blacklist_vet_description(const char *desc)
47{
bf21dc59 48 int i, prefix_len, tbs_step = 0, bin_step = 0;
734114f8 49
bf21dc59
MS
50 /* The following algorithm only works if prefix lengths match. */
51 BUILD_BUG_ON(sizeof(tbs_prefix) != sizeof(bin_prefix));
52 prefix_len = sizeof(tbs_prefix) - 1;
53 for (i = 0; *desc; desc++, i++) {
54 if (*desc == ':') {
55 if (tbs_step == prefix_len)
56 goto found_colon;
57 if (bin_step == prefix_len)
58 goto found_colon;
59 return -EINVAL;
60 }
61 if (i >= prefix_len)
62 return -EINVAL;
63 if (*desc == tbs_prefix[i])
64 tbs_step++;
65 if (*desc == bin_prefix[i])
66 bin_step++;
67 }
734114f8
DH
68 return -EINVAL;
69
70found_colon:
71 desc++;
bf21dc59 72 for (i = 0; *desc && i < MAX_HASH_LEN; desc++, i++) {
84ffbefd 73 if (!isxdigit(*desc) || isupper(*desc))
734114f8 74 return -EINVAL;
734114f8 75 }
bf21dc59
MS
76 if (*desc)
77 /* The hash is greater than MAX_HASH_LEN. */
78 return -ENOPKG;
734114f8 79
bf21dc59
MS
80 /* Checks for an even number of hexadecimal characters. */
81 if (i == 0 || i & 1)
734114f8
DH
82 return -EINVAL;
83 return 0;
84}
85
6364d106
MS
86static int blacklist_key_instantiate(struct key *key,
87 struct key_preparsed_payload *prep)
734114f8 88{
6364d106
MS
89#ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
90 int err;
91#endif
92
93 /* Sets safe default permissions for keys loaded by user space. */
94 key->perm = BLACKLIST_KEY_PERM;
95
96 /*
97 * Skips the authentication step for builtin hashes, they are not
98 * signed but still trusted.
99 */
100 if (key->flags & (1 << KEY_FLAG_BUILTIN))
101 goto out;
102
103#ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
104 /*
105 * Verifies the description's PKCS#7 signature against the builtin
106 * trusted keyring.
107 */
108 err = verify_pkcs7_signature(key->description,
109 strlen(key->description), prep->data, prep->datalen,
110 NULL, VERIFYING_UNSPECIFIED_SIGNATURE, NULL, NULL);
111 if (err)
112 return err;
113#else
114 /*
115 * It should not be possible to come here because the keyring doesn't
116 * have KEY_USR_WRITE and the only other way to call this function is
117 * for builtin hashes.
118 */
119 WARN_ON_ONCE(1);
120 return -EPERM;
121#endif
122
123out:
124 return generic_key_instantiate(key, prep);
734114f8
DH
125}
126
6364d106
MS
127static int blacklist_key_update(struct key *key,
128 struct key_preparsed_payload *prep)
734114f8 129{
6364d106 130 return -EPERM;
734114f8
DH
131}
132
133static void blacklist_describe(const struct key *key, struct seq_file *m)
134{
135 seq_puts(m, key->description);
136}
137
138static struct key_type key_type_blacklist = {
139 .name = "blacklist",
140 .vet_description = blacklist_vet_description,
6364d106
MS
141 .instantiate = blacklist_key_instantiate,
142 .update = blacklist_key_update,
734114f8
DH
143 .describe = blacklist_describe,
144};
145
141e5239
MS
146static char *get_raw_hash(const u8 *hash, size_t hash_len,
147 enum blacklist_hash_type hash_type)
148{
149 size_t type_len;
150 const char *type_prefix;
151 char *buffer, *p;
152
153 switch (hash_type) {
154 case BLACKLIST_HASH_X509_TBS:
155 type_len = sizeof(tbs_prefix) - 1;
156 type_prefix = tbs_prefix;
157 break;
158 case BLACKLIST_HASH_BINARY:
159 type_len = sizeof(bin_prefix) - 1;
160 type_prefix = bin_prefix;
161 break;
162 default:
163 WARN_ON_ONCE(1);
164 return ERR_PTR(-EINVAL);
165 }
166 buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL);
167 if (!buffer)
168 return ERR_PTR(-ENOMEM);
169 p = memcpy(buffer, type_prefix, type_len);
170 p += type_len;
171 *p++ = ':';
172 bin2hex(p, hash, hash_len);
173 p += hash_len * 2;
174 *p = '\0';
175 return buffer;
176}
177
734114f8 178/**
141e5239 179 * mark_raw_hash_blacklisted - Add a hash to the system blacklist
0b2d443b 180 * @hash: The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
734114f8 181 */
141e5239 182static int mark_raw_hash_blacklisted(const char *hash)
734114f8
DH
183{
184 key_ref_t key;
185
186 key = key_create_or_update(make_key_ref(blacklist_keyring, true),
187 "blacklist",
188 hash,
189 NULL,
190 0,
6364d106 191 BLACKLIST_KEY_PERM,
734114f8
DH
192 KEY_ALLOC_NOT_IN_QUOTA |
193 KEY_ALLOC_BUILT_IN);
194 if (IS_ERR(key)) {
195 pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key));
196 return PTR_ERR(key);
197 }
198 return 0;
199}
200
141e5239
MS
201int mark_hash_blacklisted(const u8 *hash, size_t hash_len,
202 enum blacklist_hash_type hash_type)
203{
204 const char *buffer;
205 int err;
206
207 buffer = get_raw_hash(hash, hash_len, hash_type);
208 if (IS_ERR(buffer))
209 return PTR_ERR(buffer);
210 err = mark_raw_hash_blacklisted(buffer);
211 kfree(buffer);
212 return err;
213}
214
734114f8
DH
215/**
216 * is_hash_blacklisted - Determine if a hash is blacklisted
217 * @hash: The hash to be checked as a binary blob
218 * @hash_len: The length of the binary hash
141e5239 219 * @hash_type: Type of hash
734114f8 220 */
141e5239
MS
221int is_hash_blacklisted(const u8 *hash, size_t hash_len,
222 enum blacklist_hash_type hash_type)
734114f8
DH
223{
224 key_ref_t kref;
141e5239 225 const char *buffer;
734114f8
DH
226 int ret = 0;
227
141e5239
MS
228 buffer = get_raw_hash(hash, hash_len, hash_type);
229 if (IS_ERR(buffer))
230 return PTR_ERR(buffer);
734114f8 231 kref = keyring_search(make_key_ref(blacklist_keyring, true),
dcf49dbc 232 &key_type_blacklist, buffer, false);
734114f8
DH
233 if (!IS_ERR(kref)) {
234 key_ref_put(kref);
235 ret = -EKEYREJECTED;
236 }
237
238 kfree(buffer);
239 return ret;
240}
241EXPORT_SYMBOL_GPL(is_hash_blacklisted);
242
2434f7d2
NJ
243int is_binary_blacklisted(const u8 *hash, size_t hash_len)
244{
141e5239
MS
245 if (is_hash_blacklisted(hash, hash_len, BLACKLIST_HASH_BINARY) ==
246 -EKEYREJECTED)
2434f7d2
NJ
247 return -EPERM;
248
249 return 0;
250}
251EXPORT_SYMBOL_GPL(is_binary_blacklisted);
252
56c58126
ES
253#ifdef CONFIG_SYSTEM_REVOCATION_LIST
254/**
255 * add_key_to_revocation_list - Add a revocation certificate to the blacklist
256 * @data: The data blob containing the certificate
257 * @size: The size of data blob
258 */
259int add_key_to_revocation_list(const char *data, size_t size)
260{
261 key_ref_t key;
262
263 key = key_create_or_update(make_key_ref(blacklist_keyring, true),
264 "asymmetric",
265 NULL,
266 data,
267 size,
6364d106
MS
268 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH
269 | KEY_USR_VIEW,
270 KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN
271 | KEY_ALLOC_BYPASS_RESTRICTION);
56c58126
ES
272
273 if (IS_ERR(key)) {
274 pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key));
275 return PTR_ERR(key);
276 }
277
278 return 0;
279}
280
281/**
282 * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked
283 * @pkcs7: The PKCS#7 message to check
284 */
285int is_key_on_revocation_list(struct pkcs7_message *pkcs7)
286{
287 int ret;
288
289 ret = pkcs7_validate_trust(pkcs7, blacklist_keyring);
290
291 if (ret == 0)
292 return -EKEYREJECTED;
293
294 return -ENOKEY;
295}
296#endif
297
6364d106
MS
298static int restrict_link_for_blacklist(struct key *dest_keyring,
299 const struct key_type *type, const union key_payload *payload,
300 struct key *restrict_key)
301{
302 if (type == &key_type_blacklist)
303 return 0;
304 return -EOPNOTSUPP;
305}
306
734114f8 307/*
6e7c2b4d 308 * Initialise the blacklist
4d997501
MS
309 *
310 * The blacklist_init() function is registered as an initcall via
311 * device_initcall(). As a result if the blacklist_init() function fails for
312 * any reason the kernel continues to execute. While cleanly returning -ENODEV
313 * could be acceptable for some non-critical kernel parts, if the blacklist
314 * keyring fails to load it defeats the certificate/key based deny list for
315 * signed modules. If a critical piece of security functionality that users
316 * expect to be present fails to initialize, panic()ing is likely the right
317 * thing to do.
734114f8
DH
318 */
319static int __init blacklist_init(void)
320{
321 const char *const *bl;
6364d106 322 struct key_restriction *restriction;
734114f8
DH
323
324 if (register_key_type(&key_type_blacklist) < 0)
325 panic("Can't allocate system blacklist key type\n");
326
6364d106
MS
327 restriction = kzalloc(sizeof(*restriction), GFP_KERNEL);
328 if (!restriction)
329 panic("Can't allocate blacklist keyring restriction\n");
330 restriction->check = restrict_link_for_blacklist;
331
734114f8
DH
332 blacklist_keyring =
333 keyring_alloc(".blacklist",
a6cb0ab7 334 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
6364d106
MS
335 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
336 KEY_POS_WRITE |
337 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH
338#ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
339 | KEY_USR_WRITE
340#endif
341 , KEY_ALLOC_NOT_IN_QUOTA |
4993e1f9 342 KEY_ALLOC_SET_KEEP,
6364d106 343 restriction, NULL);
734114f8
DH
344 if (IS_ERR(blacklist_keyring))
345 panic("Can't allocate system blacklist keyring\n");
346
347 for (bl = blacklist_hashes; *bl; bl++)
141e5239 348 if (mark_raw_hash_blacklisted(*bl) < 0)
734114f8
DH
349 pr_err("- blacklisting failed\n");
350 return 0;
351}
352
353/*
354 * Must be initialised before we try and load the keys into the keyring.
355 */
356device_initcall(blacklist_init);
d1f04410
ES
357
358#ifdef CONFIG_SYSTEM_REVOCATION_LIST
359/*
360 * Load the compiled-in list of revocation X.509 certificates.
361 */
362static __init int load_revocation_certificate_list(void)
363{
364 if (revocation_certificate_list_size)
365 pr_notice("Loading compiled-in revocation X.509 certificates\n");
366
60050ffe
DH
367 return x509_load_certificate_list(revocation_certificate_list,
368 revocation_certificate_list_size,
369 blacklist_keyring);
d1f04410
ES
370}
371late_initcall(load_revocation_certificate_list);
372#endif