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 <keys/system_keyring.h>
19 #include "blacklist.h"
22 static struct key *blacklist_keyring;
24 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
25 extern __initconst const u8 revocation_certificate_list[];
26 extern __initconst const unsigned long revocation_certificate_list_size;
30 * The description must be a type prefix, a colon and then an even number of
31 * hex digits. The hash is kept in the description.
33 static int blacklist_vet_description(const char *desc)
46 for (; *desc; desc++) {
47 if (!isxdigit(*desc) || isupper(*desc))
58 * The hash to be blacklisted is expected to be in the description. There will
61 static int blacklist_preparse(struct key_preparsed_payload *prep)
63 if (prep->datalen > 0)
68 static void blacklist_free_preparse(struct key_preparsed_payload *prep)
72 static void blacklist_describe(const struct key *key, struct seq_file *m)
74 seq_puts(m, key->description);
77 static struct key_type key_type_blacklist = {
79 .vet_description = blacklist_vet_description,
80 .preparse = blacklist_preparse,
81 .free_preparse = blacklist_free_preparse,
82 .instantiate = generic_key_instantiate,
83 .describe = blacklist_describe,
87 * mark_hash_blacklisted - Add a hash to the system blacklist
88 * @hash: The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
90 int mark_hash_blacklisted(const char *hash)
94 key = key_create_or_update(make_key_ref(blacklist_keyring, true),
99 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
101 KEY_ALLOC_NOT_IN_QUOTA |
104 pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key));
111 * is_hash_blacklisted - Determine if a hash is blacklisted
112 * @hash: The hash to be checked as a binary blob
113 * @hash_len: The length of the binary hash
114 * @type: Type of hash
116 int is_hash_blacklisted(const u8 *hash, size_t hash_len, const char *type)
119 size_t type_len = strlen(type);
123 buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL);
126 p = memcpy(buffer, type, type_len);
129 bin2hex(p, hash, hash_len);
133 kref = keyring_search(make_key_ref(blacklist_keyring, true),
134 &key_type_blacklist, buffer, false);
143 EXPORT_SYMBOL_GPL(is_hash_blacklisted);
145 int is_binary_blacklisted(const u8 *hash, size_t hash_len)
147 if (is_hash_blacklisted(hash, hash_len, "bin") == -EKEYREJECTED)
152 EXPORT_SYMBOL_GPL(is_binary_blacklisted);
154 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
156 * add_key_to_revocation_list - Add a revocation certificate to the blacklist
157 * @data: The data blob containing the certificate
158 * @size: The size of data blob
160 int add_key_to_revocation_list(const char *data, size_t size)
164 key = key_create_or_update(make_key_ref(blacklist_keyring, true),
169 ((KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW),
170 KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN);
173 pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key));
181 * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked
182 * @pkcs7: The PKCS#7 message to check
184 int is_key_on_revocation_list(struct pkcs7_message *pkcs7)
188 ret = pkcs7_validate_trust(pkcs7, blacklist_keyring);
191 return -EKEYREJECTED;
198 * Initialise the blacklist
200 static int __init blacklist_init(void)
202 const char *const *bl;
204 if (register_key_type(&key_type_blacklist) < 0)
205 panic("Can't allocate system blacklist key type\n");
208 keyring_alloc(".blacklist",
209 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
210 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
211 KEY_USR_VIEW | KEY_USR_READ |
213 KEY_ALLOC_NOT_IN_QUOTA |
216 if (IS_ERR(blacklist_keyring))
217 panic("Can't allocate system blacklist keyring\n");
219 for (bl = blacklist_hashes; *bl; bl++)
220 if (mark_hash_blacklisted(*bl) < 0)
221 pr_err("- blacklisting failed\n");
226 * Must be initialised before we try and load the keys into the keyring.
228 device_initcall(blacklist_init);
230 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
232 * Load the compiled-in list of revocation X.509 certificates.
234 static __init int load_revocation_certificate_list(void)
236 if (revocation_certificate_list_size)
237 pr_notice("Loading compiled-in revocation X.509 certificates\n");
239 return load_certificate_list(revocation_certificate_list, revocation_certificate_list_size,
242 late_initcall(load_revocation_certificate_list);