1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* System hash blacklist.
4 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
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 <linux/verification.h>
19 #include <keys/system_keyring.h>
20 #include "blacklist.h"
24 * According to crypto/asymmetric_keys/x509_cert_parser.c:x509_note_pkey_algo(),
25 * the size of the currently longest supported hash algorithm is 512 bits,
26 * which translates into 128 hex characters.
28 #define MAX_HASH_LEN 128
30 #define BLACKLIST_KEY_PERM (KEY_POS_SEARCH | KEY_POS_VIEW | \
31 KEY_USR_SEARCH | KEY_USR_VIEW)
33 static const char tbs_prefix[] = "tbs";
34 static const char bin_prefix[] = "bin";
36 static struct key *blacklist_keyring;
38 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
39 extern __initconst const u8 revocation_certificate_list[];
40 extern __initconst const unsigned long revocation_certificate_list_size;
44 * The description must be a type prefix, a colon and then an even number of
45 * hex digits. The hash is kept in the description.
47 static int blacklist_vet_description(const char *desc)
49 int i, prefix_len, tbs_step = 0, bin_step = 0;
51 /* The following algorithm only works if prefix lengths match. */
52 BUILD_BUG_ON(sizeof(tbs_prefix) != sizeof(bin_prefix));
53 prefix_len = sizeof(tbs_prefix) - 1;
54 for (i = 0; *desc; desc++, i++) {
56 if (tbs_step == prefix_len)
58 if (bin_step == prefix_len)
64 if (*desc == tbs_prefix[i])
66 if (*desc == bin_prefix[i])
73 for (i = 0; *desc && i < MAX_HASH_LEN; desc++, i++) {
74 if (!isxdigit(*desc) || isupper(*desc))
78 /* The hash is greater than MAX_HASH_LEN. */
81 /* Checks for an even number of hexadecimal characters. */
87 static int blacklist_key_instantiate(struct key *key,
88 struct key_preparsed_payload *prep)
90 #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
94 /* Sets safe default permissions for keys loaded by user space. */
95 key->perm = BLACKLIST_KEY_PERM;
98 * Skips the authentication step for builtin hashes, they are not
99 * signed but still trusted.
101 if (key->flags & (1 << KEY_FLAG_BUILTIN))
104 #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
106 * Verifies the description's PKCS#7 signature against the builtin
109 err = verify_pkcs7_signature(key->description,
110 strlen(key->description), prep->data, prep->datalen,
111 NULL, VERIFYING_UNSPECIFIED_SIGNATURE, NULL, NULL);
116 * It should not be possible to come here because the keyring doesn't
117 * have KEY_USR_WRITE and the only other way to call this function is
118 * for builtin hashes.
125 return generic_key_instantiate(key, prep);
128 static int blacklist_key_update(struct key *key,
129 struct key_preparsed_payload *prep)
134 static void blacklist_describe(const struct key *key, struct seq_file *m)
136 seq_puts(m, key->description);
139 static struct key_type key_type_blacklist = {
141 .vet_description = blacklist_vet_description,
142 .instantiate = blacklist_key_instantiate,
143 .update = blacklist_key_update,
144 .describe = blacklist_describe,
147 static char *get_raw_hash(const u8 *hash, size_t hash_len,
148 enum blacklist_hash_type hash_type)
151 const char *type_prefix;
155 case BLACKLIST_HASH_X509_TBS:
156 type_len = sizeof(tbs_prefix) - 1;
157 type_prefix = tbs_prefix;
159 case BLACKLIST_HASH_BINARY:
160 type_len = sizeof(bin_prefix) - 1;
161 type_prefix = bin_prefix;
165 return ERR_PTR(-EINVAL);
167 buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL);
169 return ERR_PTR(-ENOMEM);
170 p = memcpy(buffer, type_prefix, type_len);
173 bin2hex(p, hash, hash_len);
180 * mark_raw_hash_blacklisted - Add a hash to the system blacklist
181 * @hash: The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
183 static int mark_raw_hash_blacklisted(const char *hash)
187 key = key_create_or_update(make_key_ref(blacklist_keyring, true),
193 KEY_ALLOC_NOT_IN_QUOTA |
196 pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key));
202 int mark_hash_blacklisted(const u8 *hash, size_t hash_len,
203 enum blacklist_hash_type hash_type)
208 buffer = get_raw_hash(hash, hash_len, hash_type);
210 return PTR_ERR(buffer);
211 err = mark_raw_hash_blacklisted(buffer);
217 * is_hash_blacklisted - Determine if a hash is blacklisted
218 * @hash: The hash to be checked as a binary blob
219 * @hash_len: The length of the binary hash
220 * @hash_type: Type of hash
222 int is_hash_blacklisted(const u8 *hash, size_t hash_len,
223 enum blacklist_hash_type hash_type)
229 buffer = get_raw_hash(hash, hash_len, hash_type);
231 return PTR_ERR(buffer);
232 kref = keyring_search(make_key_ref(blacklist_keyring, true),
233 &key_type_blacklist, buffer, false);
242 EXPORT_SYMBOL_GPL(is_hash_blacklisted);
244 int is_binary_blacklisted(const u8 *hash, size_t hash_len)
246 if (is_hash_blacklisted(hash, hash_len, BLACKLIST_HASH_BINARY) ==
252 EXPORT_SYMBOL_GPL(is_binary_blacklisted);
254 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
256 * add_key_to_revocation_list - Add a revocation certificate to the blacklist
257 * @data: The data blob containing the certificate
258 * @size: The size of data blob
260 int add_key_to_revocation_list(const char *data, size_t size)
264 key = key_create_or_update(make_key_ref(blacklist_keyring, true),
269 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH
271 KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN
272 | KEY_ALLOC_BYPASS_RESTRICTION);
275 pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key));
283 * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked
284 * @pkcs7: The PKCS#7 message to check
286 int is_key_on_revocation_list(struct pkcs7_message *pkcs7)
290 ret = pkcs7_validate_trust(pkcs7, blacklist_keyring);
293 return -EKEYREJECTED;
299 static int restrict_link_for_blacklist(struct key *dest_keyring,
300 const struct key_type *type, const union key_payload *payload,
301 struct key *restrict_key)
303 if (type == &key_type_blacklist)
309 * Initialise the blacklist
311 * The blacklist_init() function is registered as an initcall via
312 * device_initcall(). As a result if the blacklist_init() function fails for
313 * any reason the kernel continues to execute. While cleanly returning -ENODEV
314 * could be acceptable for some non-critical kernel parts, if the blacklist
315 * keyring fails to load it defeats the certificate/key based deny list for
316 * signed modules. If a critical piece of security functionality that users
317 * expect to be present fails to initialize, panic()ing is likely the right
320 static int __init blacklist_init(void)
322 const char *const *bl;
323 struct key_restriction *restriction;
325 if (register_key_type(&key_type_blacklist) < 0)
326 panic("Can't allocate system blacklist key type\n");
328 restriction = kzalloc(sizeof(*restriction), GFP_KERNEL);
330 panic("Can't allocate blacklist keyring restriction\n");
331 restriction->check = restrict_link_for_blacklist;
334 keyring_alloc(".blacklist",
335 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
336 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
338 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH
339 #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
342 , KEY_ALLOC_NOT_IN_QUOTA |
345 if (IS_ERR(blacklist_keyring))
346 panic("Can't allocate system blacklist keyring\n");
348 for (bl = blacklist_hashes; *bl; bl++)
349 if (mark_raw_hash_blacklisted(*bl) < 0)
350 pr_err("- blacklisting failed\n");
355 * Must be initialised before we try and load the keys into the keyring.
357 device_initcall(blacklist_init);
359 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
361 * Load the compiled-in list of revocation X.509 certificates.
363 static __init int load_revocation_certificate_list(void)
365 if (revocation_certificate_list_size)
366 pr_notice("Loading compiled-in revocation X.509 certificates\n");
368 return load_certificate_list(revocation_certificate_list, revocation_certificate_list_size,
371 late_initcall(load_revocation_certificate_list);