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"
21 static struct key
*blacklist_keyring
;
23 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
24 extern __initconst
const u8 revocation_certificate_list
[];
25 extern __initconst
const unsigned long revocation_certificate_list_size
;
29 * The description must be a type prefix, a colon and then an even number of
30 * hex digits. The hash is kept in the description.
32 static int blacklist_vet_description(const char *desc
)
45 for (; *desc
; desc
++) {
57 * The hash to be blacklisted is expected to be in the description. There will
60 static int blacklist_preparse(struct key_preparsed_payload
*prep
)
62 if (prep
->datalen
> 0)
67 static void blacklist_free_preparse(struct key_preparsed_payload
*prep
)
71 static void blacklist_describe(const struct key
*key
, struct seq_file
*m
)
73 seq_puts(m
, key
->description
);
76 static struct key_type key_type_blacklist
= {
78 .vet_description
= blacklist_vet_description
,
79 .preparse
= blacklist_preparse
,
80 .free_preparse
= blacklist_free_preparse
,
81 .instantiate
= generic_key_instantiate
,
82 .describe
= blacklist_describe
,
86 * mark_hash_blacklisted - Add a hash to the system blacklist
87 * @hash - The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
89 int mark_hash_blacklisted(const char *hash
)
93 key
= key_create_or_update(make_key_ref(blacklist_keyring
, true),
98 ((KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
100 KEY_ALLOC_NOT_IN_QUOTA
|
103 pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key
));
110 * is_hash_blacklisted - Determine if a hash is blacklisted
111 * @hash: The hash to be checked as a binary blob
112 * @hash_len: The length of the binary hash
113 * @type: Type of hash
115 int is_hash_blacklisted(const u8
*hash
, size_t hash_len
, const char *type
)
118 size_t type_len
= strlen(type
);
122 buffer
= kmalloc(type_len
+ 1 + hash_len
* 2 + 1, GFP_KERNEL
);
125 p
= memcpy(buffer
, type
, type_len
);
128 bin2hex(p
, hash
, hash_len
);
132 kref
= keyring_search(make_key_ref(blacklist_keyring
, true),
133 &key_type_blacklist
, buffer
, false);
142 EXPORT_SYMBOL_GPL(is_hash_blacklisted
);
144 int is_binary_blacklisted(const u8
*hash
, size_t hash_len
)
146 if (is_hash_blacklisted(hash
, hash_len
, "bin") == -EKEYREJECTED
)
151 EXPORT_SYMBOL_GPL(is_binary_blacklisted
);
153 #ifdef CONFIG_SYSTEM_REVOCATION_LIST
155 * add_key_to_revocation_list - Add a revocation certificate to the blacklist
156 * @data: The data blob containing the certificate
157 * @size: The size of data blob
159 int add_key_to_revocation_list(const char *data
, size_t size
)
163 key
= key_create_or_update(make_key_ref(blacklist_keyring
, true),
168 ((KEY_POS_ALL
& ~KEY_POS_SETATTR
) | KEY_USR_VIEW
),
169 KEY_ALLOC_NOT_IN_QUOTA
| KEY_ALLOC_BUILT_IN
);
172 pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key
));
175 pr_notice("Revoked X.509 cert '%s'\n",
176 key_ref_to_ptr(key
)->description
);
183 * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked
184 * @pkcs7: The PKCS#7 message to check
186 int is_key_on_revocation_list(struct pkcs7_message
*pkcs7
)
190 ret
= pkcs7_validate_trust(pkcs7
, blacklist_keyring
);
193 return -EKEYREJECTED
;
200 * Initialise the blacklist
202 static int __init
blacklist_init(void)
204 const char *const *bl
;
206 if (register_key_type(&key_type_blacklist
) < 0)
207 panic("Can't allocate system blacklist key type\n");
210 keyring_alloc(".blacklist",
211 KUIDT_INIT(0), KGIDT_INIT(0),
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
);