1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* System hash blacklist.
4 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
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"
21 static struct key *blacklist_keyring;
24 * The description must be a type prefix, a colon and then an even number of
25 * hex digits. The hash is kept in the description.
27 static int blacklist_vet_description(const char *desc)
40 for (; *desc; desc++) {
41 if (!isxdigit(*desc) || isupper(*desc))
52 * The hash to be blacklisted is expected to be in the description. There will
55 static int blacklist_preparse(struct key_preparsed_payload *prep)
57 if (prep->datalen > 0)
62 static void blacklist_free_preparse(struct key_preparsed_payload *prep)
66 static void blacklist_describe(const struct key *key, struct seq_file *m)
68 seq_puts(m, key->description);
71 static struct key_type key_type_blacklist = {
73 .vet_description = blacklist_vet_description,
74 .preparse = blacklist_preparse,
75 .free_preparse = blacklist_free_preparse,
76 .instantiate = generic_key_instantiate,
77 .describe = blacklist_describe,
81 * mark_hash_blacklisted - Add a hash to the system blacklist
82 * @hash: The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
84 int mark_hash_blacklisted(const char *hash)
88 key = key_create_or_update(make_key_ref(blacklist_keyring, true),
93 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
95 KEY_ALLOC_NOT_IN_QUOTA |
98 pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key));
105 * is_hash_blacklisted - Determine if a hash is blacklisted
106 * @hash: The hash to be checked as a binary blob
107 * @hash_len: The length of the binary hash
108 * @type: Type of hash
110 int is_hash_blacklisted(const u8 *hash, size_t hash_len, const char *type)
113 size_t type_len = strlen(type);
117 buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL);
120 p = memcpy(buffer, type, type_len);
123 bin2hex(p, hash, hash_len);
127 kref = keyring_search(make_key_ref(blacklist_keyring, true),
128 &key_type_blacklist, buffer, false);
137 EXPORT_SYMBOL_GPL(is_hash_blacklisted);
139 int is_binary_blacklisted(const u8 *hash, size_t hash_len)
141 if (is_hash_blacklisted(hash, hash_len, "bin") == -EKEYREJECTED)
146 EXPORT_SYMBOL_GPL(is_binary_blacklisted);
149 * Initialise the blacklist
151 static int __init blacklist_init(void)
153 const char *const *bl;
155 if (register_key_type(&key_type_blacklist) < 0)
156 panic("Can't allocate system blacklist key type\n");
159 keyring_alloc(".blacklist",
160 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
161 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
162 KEY_USR_VIEW | KEY_USR_READ |
164 KEY_ALLOC_NOT_IN_QUOTA |
167 if (IS_ERR(blacklist_keyring))
168 panic("Can't allocate system blacklist keyring\n");
170 for (bl = blacklist_hashes; *bl; bl++)
171 if (mark_hash_blacklisted(*bl) < 0)
172 pr_err("- blacklisting failed\n");
177 * Must be initialised before we try and load the keys into the keyring.
179 device_initcall(blacklist_init);