xref: /linux/certs/blacklist.c (revision e9a83bd2322035ed9d7dcf35753d3f984d76c6a5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* System hash blacklist.
3  *
4  * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
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>
17 #include <keys/system_keyring.h>
18 #include "blacklist.h"
19 
20 static struct key *blacklist_keyring;
21 
22 /*
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.
25  */
26 static int blacklist_vet_description(const char *desc)
27 {
28 	int n = 0;
29 
30 	if (*desc == ':')
31 		return -EINVAL;
32 	for (; *desc; desc++)
33 		if (*desc == ':')
34 			goto found_colon;
35 	return -EINVAL;
36 
37 found_colon:
38 	desc++;
39 	for (; *desc; desc++) {
40 		if (!isxdigit(*desc))
41 			return -EINVAL;
42 		n++;
43 	}
44 
45 	if (n == 0 || n & 1)
46 		return -EINVAL;
47 	return 0;
48 }
49 
50 /*
51  * The hash to be blacklisted is expected to be in the description.  There will
52  * be no payload.
53  */
54 static int blacklist_preparse(struct key_preparsed_payload *prep)
55 {
56 	if (prep->datalen > 0)
57 		return -EINVAL;
58 	return 0;
59 }
60 
61 static void blacklist_free_preparse(struct key_preparsed_payload *prep)
62 {
63 }
64 
65 static void blacklist_describe(const struct key *key, struct seq_file *m)
66 {
67 	seq_puts(m, key->description);
68 }
69 
70 static struct key_type key_type_blacklist = {
71 	.name			= "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,
77 };
78 
79 /**
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")
82  */
83 int mark_hash_blacklisted(const char *hash)
84 {
85 	key_ref_t key;
86 
87 	key = key_create_or_update(make_key_ref(blacklist_keyring, true),
88 				   "blacklist",
89 				   hash,
90 				   NULL,
91 				   0,
92 				   &internal_key_acl,
93 				   KEY_ALLOC_NOT_IN_QUOTA |
94 				   KEY_ALLOC_BUILT_IN);
95 	if (IS_ERR(key)) {
96 		pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key));
97 		return PTR_ERR(key);
98 	}
99 	return 0;
100 }
101 
102 /**
103  * is_hash_blacklisted - Determine if a hash is blacklisted
104  * @hash: The hash to be checked as a binary blob
105  * @hash_len: The length of the binary hash
106  * @type: Type of hash
107  */
108 int is_hash_blacklisted(const u8 *hash, size_t hash_len, const char *type)
109 {
110 	key_ref_t kref;
111 	size_t type_len = strlen(type);
112 	char *buffer, *p;
113 	int ret = 0;
114 
115 	buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL);
116 	if (!buffer)
117 		return -ENOMEM;
118 	p = memcpy(buffer, type, type_len);
119 	p += type_len;
120 	*p++ = ':';
121 	bin2hex(p, hash, hash_len);
122 	p += hash_len * 2;
123 	*p = 0;
124 
125 	kref = keyring_search(make_key_ref(blacklist_keyring, true),
126 			      &key_type_blacklist, buffer, false);
127 	if (!IS_ERR(kref)) {
128 		key_ref_put(kref);
129 		ret = -EKEYREJECTED;
130 	}
131 
132 	kfree(buffer);
133 	return ret;
134 }
135 EXPORT_SYMBOL_GPL(is_hash_blacklisted);
136 
137 /*
138  * Initialise the blacklist
139  */
140 static int __init blacklist_init(void)
141 {
142 	const char *const *bl;
143 
144 	if (register_key_type(&key_type_blacklist) < 0)
145 		panic("Can't allocate system blacklist key type\n");
146 
147 	blacklist_keyring =
148 		keyring_alloc(".blacklist",
149 			      KUIDT_INIT(0), KGIDT_INIT(0),
150 			      current_cred(),
151 			      &internal_keyring_acl,
152 			      KEY_ALLOC_NOT_IN_QUOTA |
153 			      KEY_FLAG_KEEP,
154 			      NULL, NULL);
155 	if (IS_ERR(blacklist_keyring))
156 		panic("Can't allocate system blacklist keyring\n");
157 
158 	for (bl = blacklist_hashes; *bl; bl++)
159 		if (mark_hash_blacklisted(*bl) < 0)
160 			pr_err("- blacklisting failed\n");
161 	return 0;
162 }
163 
164 /*
165  * Must be initialised before we try and load the keys into the keyring.
166  */
167 device_initcall(blacklist_init);
168