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 <keys/system_keyring.h>
18 #include "blacklist.h"
20 static struct key *blacklist_keyring;
23 * The description must be a type prefix, a colon and then an even number of
24 * hex digits. The hash is kept in the description.
26 static int blacklist_vet_description(const char *desc)
39 for (; *desc; desc++) {
51 * The hash to be blacklisted is expected to be in the description. There will
54 static int blacklist_preparse(struct key_preparsed_payload *prep)
56 if (prep->datalen > 0)
61 static void blacklist_free_preparse(struct key_preparsed_payload *prep)
65 static void blacklist_describe(const struct key *key, struct seq_file *m)
67 seq_puts(m, key->description);
70 static struct key_type key_type_blacklist = {
72 .vet_description = blacklist_vet_description,
73 .preparse = blacklist_preparse,
74 .free_preparse = blacklist_free_preparse,
75 .instantiate = generic_key_instantiate,
76 .describe = blacklist_describe,
80 * mark_hash_blacklisted - Add a hash to the system blacklist
81 * @hash - The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
83 int mark_hash_blacklisted(const char *hash)
87 key = key_create_or_update(make_key_ref(blacklist_keyring, true),
92 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
94 KEY_ALLOC_NOT_IN_QUOTA |
97 pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key));
104 * is_hash_blacklisted - Determine if a hash is blacklisted
105 * @hash: The hash to be checked as a binary blob
106 * @hash_len: The length of the binary hash
107 * @type: Type of hash
109 int is_hash_blacklisted(const u8 *hash, size_t hash_len, const char *type)
112 size_t type_len = strlen(type);
116 buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL);
119 p = memcpy(buffer, type, type_len);
122 bin2hex(p, hash, hash_len);
126 kref = keyring_search(make_key_ref(blacklist_keyring, true),
127 &key_type_blacklist, buffer, false);
136 EXPORT_SYMBOL_GPL(is_hash_blacklisted);
139 * Initialise the blacklist
141 static int __init blacklist_init(void)
143 const char *const *bl;
145 if (register_key_type(&key_type_blacklist) < 0)
146 panic("Can't allocate system blacklist key type\n");
149 keyring_alloc(".blacklist",
150 KUIDT_INIT(0), KGIDT_INIT(0),
152 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
153 KEY_USR_VIEW | KEY_USR_READ |
155 KEY_ALLOC_NOT_IN_QUOTA |
158 if (IS_ERR(blacklist_keyring))
159 panic("Can't allocate system blacklist keyring\n");
161 for (bl = blacklist_hashes; *bl; bl++)
162 if (mark_hash_blacklisted(*bl) < 0)
163 pr_err("- blacklisting failed\n");
168 * Must be initialised before we try and load the keys into the keyring.
170 device_initcall(blacklist_init);