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/asymmetric-type.h>
19 #include <keys/system_keyring.h>
20 #include "blacklist.h"
23 * According to crypto/asymmetric_keys/x509_cert_parser.c:x509_note_pkey_algo(),
24 * the size of the currently longest supported hash algorithm is 512 bits,
25 * which translates into 128 hex characters.
27 #define MAX_HASH_LEN 128
29 #define BLACKLIST_KEY_PERM (KEY_POS_SEARCH | KEY_POS_VIEW | \
30 KEY_USR_SEARCH | KEY_USR_VIEW)
32 static const char tbs_prefix
[] = "tbs";
33 static const char bin_prefix
[] = "bin";
35 static struct key
*blacklist_keyring
;
37 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
38 extern __initconst
const u8 revocation_certificate_list
[];
39 extern __initconst
const unsigned long revocation_certificate_list_size
;
43 * The description must be a type prefix, a colon and then an even number of
44 * hex digits. The hash is kept in the description.
46 static int blacklist_vet_description(const char *desc
)
48 int i
, prefix_len
, tbs_step
= 0, bin_step
= 0;
50 /* The following algorithm only works if prefix lengths match. */
51 BUILD_BUG_ON(sizeof(tbs_prefix
) != sizeof(bin_prefix
));
52 prefix_len
= sizeof(tbs_prefix
) - 1;
53 for (i
= 0; *desc
; desc
++, i
++) {
55 if (tbs_step
== prefix_len
)
57 if (bin_step
== prefix_len
)
63 if (*desc
== tbs_prefix
[i
])
65 if (*desc
== bin_prefix
[i
])
72 for (i
= 0; *desc
&& i
< MAX_HASH_LEN
; desc
++, i
++) {
73 if (!isxdigit(*desc
) || isupper(*desc
))
77 /* The hash is greater than MAX_HASH_LEN. */
80 /* Checks for an even number of hexadecimal characters. */
86 static int blacklist_key_instantiate(struct key
*key
,
87 struct key_preparsed_payload
*prep
)
89 #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
93 /* Sets safe default permissions for keys loaded by user space. */
94 key
->perm
= BLACKLIST_KEY_PERM
;
97 * Skips the authentication step for builtin hashes, they are not
98 * signed but still trusted.
100 if (key
->flags
& (1 << KEY_FLAG_BUILTIN
))
103 #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
105 * Verifies the description's PKCS#7 signature against the builtin
108 err
= verify_pkcs7_signature(key
->description
,
109 strlen(key
->description
), prep
->data
, prep
->datalen
,
110 NULL
, VERIFYING_UNSPECIFIED_SIGNATURE
, NULL
, NULL
);
115 * It should not be possible to come here because the keyring doesn't
116 * have KEY_USR_WRITE and the only other way to call this function is
117 * for builtin hashes.
124 return generic_key_instantiate(key
, prep
);
127 static int blacklist_key_update(struct key
*key
,
128 struct key_preparsed_payload
*prep
)
133 static void blacklist_describe(const struct key
*key
, struct seq_file
*m
)
135 seq_puts(m
, key
->description
);
138 static struct key_type key_type_blacklist
= {
140 .vet_description
= blacklist_vet_description
,
141 .instantiate
= blacklist_key_instantiate
,
142 .update
= blacklist_key_update
,
143 .describe
= blacklist_describe
,
146 static char *get_raw_hash(const u8
*hash
, size_t hash_len
,
147 enum blacklist_hash_type hash_type
)
150 const char *type_prefix
;
154 case BLACKLIST_HASH_X509_TBS
:
155 type_len
= sizeof(tbs_prefix
) - 1;
156 type_prefix
= tbs_prefix
;
158 case BLACKLIST_HASH_BINARY
:
159 type_len
= sizeof(bin_prefix
) - 1;
160 type_prefix
= bin_prefix
;
164 return ERR_PTR(-EINVAL
);
166 buffer
= kmalloc(type_len
+ 1 + hash_len
* 2 + 1, GFP_KERNEL
);
168 return ERR_PTR(-ENOMEM
);
169 p
= memcpy(buffer
, type_prefix
, type_len
);
172 bin2hex(p
, hash
, hash_len
);
179 * mark_raw_hash_blacklisted - Add a hash to the system blacklist
180 * @hash: The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
182 static int mark_raw_hash_blacklisted(const char *hash
)
186 key
= key_create(make_key_ref(blacklist_keyring
, true),
192 KEY_ALLOC_NOT_IN_QUOTA
|
195 if (PTR_ERR(key
) == -EEXIST
)
196 pr_warn("Duplicate blacklisted hash %s\n", hash
);
198 pr_err("Problem blacklisting hash %s: %pe\n", hash
, key
);
204 int mark_hash_blacklisted(const u8
*hash
, size_t hash_len
,
205 enum blacklist_hash_type hash_type
)
210 buffer
= get_raw_hash(hash
, hash_len
, hash_type
);
212 return PTR_ERR(buffer
);
213 err
= mark_raw_hash_blacklisted(buffer
);
219 * is_hash_blacklisted - Determine if a hash is blacklisted
220 * @hash: The hash to be checked as a binary blob
221 * @hash_len: The length of the binary hash
222 * @hash_type: Type of hash
224 int is_hash_blacklisted(const u8
*hash
, size_t hash_len
,
225 enum blacklist_hash_type hash_type
)
231 buffer
= get_raw_hash(hash
, hash_len
, hash_type
);
233 return PTR_ERR(buffer
);
234 kref
= keyring_search(make_key_ref(blacklist_keyring
, true),
235 &key_type_blacklist
, buffer
, false);
244 EXPORT_SYMBOL_GPL(is_hash_blacklisted
);
246 int is_binary_blacklisted(const u8
*hash
, size_t hash_len
)
248 if (is_hash_blacklisted(hash
, hash_len
, BLACKLIST_HASH_BINARY
) ==
254 EXPORT_SYMBOL_GPL(is_binary_blacklisted
);
256 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
258 * add_key_to_revocation_list - Add a revocation certificate to the blacklist
259 * @data: The data blob containing the certificate
260 * @size: The size of data blob
262 int add_key_to_revocation_list(const char *data
, size_t size
)
266 key
= key_create_or_update(make_key_ref(blacklist_keyring
, true),
271 KEY_POS_VIEW
| KEY_POS_READ
| KEY_POS_SEARCH
273 KEY_ALLOC_NOT_IN_QUOTA
| KEY_ALLOC_BUILT_IN
274 | KEY_ALLOC_BYPASS_RESTRICTION
);
277 pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key
));
285 * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked
286 * @pkcs7: The PKCS#7 message to check
288 int is_key_on_revocation_list(struct pkcs7_message
*pkcs7
)
292 ret
= pkcs7_validate_trust(pkcs7
, blacklist_keyring
);
295 return -EKEYREJECTED
;
301 static int restrict_link_for_blacklist(struct key
*dest_keyring
,
302 const struct key_type
*type
, const union key_payload
*payload
,
303 struct key
*restrict_key
)
305 if (type
== &key_type_blacklist
)
311 * Initialise the blacklist
313 * The blacklist_init() function is registered as an initcall via
314 * device_initcall(). As a result if the blacklist_init() function fails for
315 * any reason the kernel continues to execute. While cleanly returning -ENODEV
316 * could be acceptable for some non-critical kernel parts, if the blacklist
317 * keyring fails to load it defeats the certificate/key based deny list for
318 * signed modules. If a critical piece of security functionality that users
319 * expect to be present fails to initialize, panic()ing is likely the right
322 static int __init
blacklist_init(void)
324 const char *const *bl
;
325 struct key_restriction
*restriction
;
327 if (register_key_type(&key_type_blacklist
) < 0)
328 panic("Can't allocate system blacklist key type\n");
330 restriction
= kzalloc(sizeof(*restriction
), GFP_KERNEL
);
332 panic("Can't allocate blacklist keyring restriction\n");
333 restriction
->check
= restrict_link_for_blacklist
;
336 keyring_alloc(".blacklist",
337 GLOBAL_ROOT_UID
, GLOBAL_ROOT_GID
, current_cred(),
338 KEY_POS_VIEW
| KEY_POS_READ
| KEY_POS_SEARCH
|
340 KEY_USR_VIEW
| KEY_USR_READ
| KEY_USR_SEARCH
341 #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
344 , KEY_ALLOC_NOT_IN_QUOTA
|
347 if (IS_ERR(blacklist_keyring
))
348 panic("Can't allocate system blacklist keyring\n");
350 for (bl
= blacklist_hashes
; *bl
; bl
++)
351 if (mark_raw_hash_blacklisted(*bl
) < 0)
352 pr_err("- blacklisting failed\n");
357 * Must be initialised before we try and load the keys into the keyring.
359 device_initcall(blacklist_init
);
361 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
363 * Load the compiled-in list of revocation X.509 certificates.
365 static __init
int load_revocation_certificate_list(void)
367 if (revocation_certificate_list_size
)
368 pr_notice("Loading compiled-in revocation X.509 certificates\n");
370 return x509_load_certificate_list(revocation_certificate_list
,
371 revocation_certificate_list_size
,
374 late_initcall(load_revocation_certificate_list
);