• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
blacklist_vet_description(const char * desc)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  */
blacklist_preparse(struct key_preparsed_payload * prep)54 static int blacklist_preparse(struct key_preparsed_payload *prep)
55 {
56 	if (prep->datalen > 0)
57 		return -EINVAL;
58 	return 0;
59 }
60 
blacklist_free_preparse(struct key_preparsed_payload * prep)61 static void blacklist_free_preparse(struct key_preparsed_payload *prep)
62 {
63 }
64 
blacklist_describe(const struct key * key,struct seq_file * m)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  */
mark_hash_blacklisted(const char * hash)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 				   ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
93 				    KEY_USR_VIEW),
94 				   KEY_ALLOC_NOT_IN_QUOTA |
95 				   KEY_ALLOC_BUILT_IN);
96 	if (IS_ERR(key)) {
97 		pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key));
98 		return PTR_ERR(key);
99 	}
100 	return 0;
101 }
102 
103 /**
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
108  */
is_hash_blacklisted(const u8 * hash,size_t hash_len,const char * type)109 int is_hash_blacklisted(const u8 *hash, size_t hash_len, const char *type)
110 {
111 	key_ref_t kref;
112 	size_t type_len = strlen(type);
113 	char *buffer, *p;
114 	int ret = 0;
115 
116 	buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL);
117 	if (!buffer)
118 		return -ENOMEM;
119 	p = memcpy(buffer, type, type_len);
120 	p += type_len;
121 	*p++ = ':';
122 	bin2hex(p, hash, hash_len);
123 	p += hash_len * 2;
124 	*p = 0;
125 
126 	kref = keyring_search(make_key_ref(blacklist_keyring, true),
127 			      &key_type_blacklist, buffer, false);
128 	if (!IS_ERR(kref)) {
129 		key_ref_put(kref);
130 		ret = -EKEYREJECTED;
131 	}
132 
133 	kfree(buffer);
134 	return ret;
135 }
136 EXPORT_SYMBOL_GPL(is_hash_blacklisted);
137 
is_binary_blacklisted(const u8 * hash,size_t hash_len)138 int is_binary_blacklisted(const u8 *hash, size_t hash_len)
139 {
140 	if (is_hash_blacklisted(hash, hash_len, "bin") == -EKEYREJECTED)
141 		return -EPERM;
142 
143 	return 0;
144 }
145 EXPORT_SYMBOL_GPL(is_binary_blacklisted);
146 
147 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
148 /**
149  * add_key_to_revocation_list - Add a revocation certificate to the blacklist
150  * @data: The data blob containing the certificate
151  * @size: The size of data blob
152  */
add_key_to_revocation_list(const char * data,size_t size)153 int add_key_to_revocation_list(const char *data, size_t size)
154 {
155 	key_ref_t key;
156 
157 	key = key_create_or_update(make_key_ref(blacklist_keyring, true),
158 				   "asymmetric",
159 				   NULL,
160 				   data,
161 				   size,
162 				   ((KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW),
163 				   KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN);
164 
165 	if (IS_ERR(key)) {
166 		pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key));
167 		return PTR_ERR(key);
168 	}
169 
170 	return 0;
171 }
172 
173 /**
174  * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked
175  * @pkcs7: The PKCS#7 message to check
176  */
is_key_on_revocation_list(struct pkcs7_message * pkcs7)177 int is_key_on_revocation_list(struct pkcs7_message *pkcs7)
178 {
179 	int ret;
180 
181 	ret = pkcs7_validate_trust(pkcs7, blacklist_keyring);
182 
183 	if (ret == 0)
184 		return -EKEYREJECTED;
185 
186 	return -ENOKEY;
187 }
188 #endif
189 
190 /*
191  * Initialise the blacklist
192  */
blacklist_init(void)193 static int __init blacklist_init(void)
194 {
195 	const char *const *bl;
196 
197 	if (register_key_type(&key_type_blacklist) < 0)
198 		panic("Can't allocate system blacklist key type\n");
199 
200 	blacklist_keyring =
201 		keyring_alloc(".blacklist",
202 			      KUIDT_INIT(0), KGIDT_INIT(0),
203 			      current_cred(),
204 			      (KEY_POS_ALL & ~KEY_POS_SETATTR) |
205 			      KEY_USR_VIEW | KEY_USR_READ |
206 			      KEY_USR_SEARCH,
207 			      KEY_ALLOC_NOT_IN_QUOTA |
208 			      KEY_ALLOC_SET_KEEP,
209 			      NULL, NULL);
210 	if (IS_ERR(blacklist_keyring))
211 		panic("Can't allocate system blacklist keyring\n");
212 
213 	for (bl = blacklist_hashes; *bl; bl++)
214 		if (mark_hash_blacklisted(*bl) < 0)
215 			pr_err("- blacklisting failed\n");
216 	return 0;
217 }
218 
219 /*
220  * Must be initialised before we try and load the keys into the keyring.
221  */
222 device_initcall(blacklist_init);
223