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/system_keyring.h>
19 #include "blacklist.h"
22 static struct key
*blacklist_keyring
;
24 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
25 extern __initconst
const u8 revocation_certificate_list
[];
26 extern __initconst
const unsigned long revocation_certificate_list_size
;
30 * The description must be a type prefix, a colon and then an even number of
31 * hex digits. The hash is kept in the description.
33 static int blacklist_vet_description(const char *desc
)
46 for (; *desc
; desc
++) {
47 if (!isxdigit(*desc
) || isupper(*desc
))
58 * The hash to be blacklisted is expected to be in the description. There will
61 static int blacklist_preparse(struct key_preparsed_payload
*prep
)
63 if (prep
->datalen
> 0)
68 static void blacklist_free_preparse(struct key_preparsed_payload
*prep
)
72 static void blacklist_describe(const struct key
*key
, struct seq_file
*m
)
74 seq_puts(m
, key
->description
);
77 static struct key_type key_type_blacklist
= {
79 .vet_description
= blacklist_vet_description
,
80 .preparse
= blacklist_preparse
,
81 .free_preparse
= blacklist_free_preparse
,
82 .instantiate
= generic_key_instantiate
,
83 .describe
= blacklist_describe
,
87 * mark_hash_blacklisted - Add a hash to the system blacklist
88 * @hash: The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
90 int mark_hash_blacklisted(const char *hash
)
94 key
= key_create_or_update(make_key_ref(blacklist_keyring
, true),
99 ((KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
101 KEY_ALLOC_NOT_IN_QUOTA
|
104 pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key
));
111 * is_hash_blacklisted - Determine if a hash is blacklisted
112 * @hash: The hash to be checked as a binary blob
113 * @hash_len: The length of the binary hash
114 * @type: Type of hash
116 int is_hash_blacklisted(const u8
*hash
, size_t hash_len
, const char *type
)
119 size_t type_len
= strlen(type
);
123 buffer
= kmalloc(type_len
+ 1 + hash_len
* 2 + 1, GFP_KERNEL
);
126 p
= memcpy(buffer
, type
, type_len
);
129 bin2hex(p
, hash
, hash_len
);
133 kref
= keyring_search(make_key_ref(blacklist_keyring
, true),
134 &key_type_blacklist
, buffer
, false);
143 EXPORT_SYMBOL_GPL(is_hash_blacklisted
);
145 int is_binary_blacklisted(const u8
*hash
, size_t hash_len
)
147 if (is_hash_blacklisted(hash
, hash_len
, "bin") == -EKEYREJECTED
)
152 EXPORT_SYMBOL_GPL(is_binary_blacklisted
);
154 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
156 * add_key_to_revocation_list - Add a revocation certificate to the blacklist
157 * @data: The data blob containing the certificate
158 * @size: The size of data blob
160 int add_key_to_revocation_list(const char *data
, size_t size
)
164 key
= key_create_or_update(make_key_ref(blacklist_keyring
, true),
169 ((KEY_POS_ALL
& ~KEY_POS_SETATTR
) | KEY_USR_VIEW
),
170 KEY_ALLOC_NOT_IN_QUOTA
| KEY_ALLOC_BUILT_IN
);
173 pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key
));
176 pr_notice("Revoked X.509 cert '%s'\n",
177 key_ref_to_ptr(key
)->description
);
184 * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked
185 * @pkcs7: The PKCS#7 message to check
187 int is_key_on_revocation_list(struct pkcs7_message
*pkcs7
)
191 ret
= pkcs7_validate_trust(pkcs7
, blacklist_keyring
);
194 return -EKEYREJECTED
;
201 * Initialise the blacklist
203 static int __init
blacklist_init(void)
205 const char *const *bl
;
207 if (register_key_type(&key_type_blacklist
) < 0)
208 panic("Can't allocate system blacklist key type\n");
211 keyring_alloc(".blacklist",
212 GLOBAL_ROOT_UID
, GLOBAL_ROOT_GID
, current_cred(),
213 (KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
214 KEY_USR_VIEW
| KEY_USR_READ
|
216 KEY_ALLOC_NOT_IN_QUOTA
|
219 if (IS_ERR(blacklist_keyring
))
220 panic("Can't allocate system blacklist keyring\n");
222 for (bl
= blacklist_hashes
; *bl
; bl
++)
223 if (mark_hash_blacklisted(*bl
) < 0)
224 pr_err("- blacklisting failed\n");
229 * Must be initialised before we try and load the keys into the keyring.
231 device_initcall(blacklist_init
);
233 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
235 * Load the compiled-in list of revocation X.509 certificates.
237 static __init
int load_revocation_certificate_list(void)
239 if (revocation_certificate_list_size
)
240 pr_notice("Loading compiled-in revocation X.509 certificates\n");
242 return load_certificate_list(revocation_certificate_list
, revocation_certificate_list_size
,
245 late_initcall(load_revocation_certificate_list
);