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 <linux/uidgid.h> 18 #include <keys/system_keyring.h> 19 #include "blacklist.h" 20 #include "common.h" 21 22 static struct key *blacklist_keyring; 23 24 #ifdef CONFIG_SYSTEM_REVOCATION_LIST 25 extern __initconst const u8 revocation_certificate_list[]; 26 extern __initconst const unsigned long revocation_certificate_list_size; 27 #endif 28 29 /* 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. 32 */ 33 static int blacklist_vet_description(const char *desc) 34 { 35 int n = 0; 36 37 if (*desc == ':') 38 return -EINVAL; 39 for (; *desc; desc++) 40 if (*desc == ':') 41 goto found_colon; 42 return -EINVAL; 43 44 found_colon: 45 desc++; 46 for (; *desc; desc++) { 47 if (!isxdigit(*desc) || isupper(*desc)) 48 return -EINVAL; 49 n++; 50 } 51 52 if (n == 0 || n & 1) 53 return -EINVAL; 54 return 0; 55 } 56 57 /* 58 * The hash to be blacklisted is expected to be in the description. There will 59 * be no payload. 60 */ 61 static int blacklist_preparse(struct key_preparsed_payload *prep) 62 { 63 if (prep->datalen > 0) 64 return -EINVAL; 65 return 0; 66 } 67 68 static void blacklist_free_preparse(struct key_preparsed_payload *prep) 69 { 70 } 71 72 static void blacklist_describe(const struct key *key, struct seq_file *m) 73 { 74 seq_puts(m, key->description); 75 } 76 77 static struct key_type key_type_blacklist = { 78 .name = "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, 84 }; 85 86 static char *get_raw_hash(const u8 *hash, size_t hash_len, 87 enum blacklist_hash_type hash_type) 88 { 89 size_t type_len; 90 const char *type_prefix; 91 char *buffer, *p; 92 93 switch (hash_type) { 94 case BLACKLIST_HASH_X509_TBS: 95 type_len = sizeof(tbs_prefix) - 1; 96 type_prefix = tbs_prefix; 97 break; 98 case BLACKLIST_HASH_BINARY: 99 type_len = sizeof(bin_prefix) - 1; 100 type_prefix = bin_prefix; 101 break; 102 default: 103 WARN_ON_ONCE(1); 104 return ERR_PTR(-EINVAL); 105 } 106 buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL); 107 if (!buffer) 108 return ERR_PTR(-ENOMEM); 109 p = memcpy(buffer, type_prefix, type_len); 110 p += type_len; 111 *p++ = ':'; 112 bin2hex(p, hash, hash_len); 113 p += hash_len * 2; 114 *p = '\0'; 115 return buffer; 116 } 117 118 /** 119 * mark_raw_hash_blacklisted - Add a hash to the system blacklist 120 * @hash: The hash as a hex string with a type prefix (eg. "tbs:23aa429783") 121 */ 122 static int mark_raw_hash_blacklisted(const char *hash) 123 { 124 key_ref_t key; 125 126 key = key_create_or_update(make_key_ref(blacklist_keyring, true), 127 "blacklist", 128 hash, 129 NULL, 130 0, 131 ((KEY_POS_ALL & ~KEY_POS_SETATTR) | 132 KEY_USR_VIEW), 133 KEY_ALLOC_NOT_IN_QUOTA | 134 KEY_ALLOC_BUILT_IN); 135 if (IS_ERR(key)) { 136 pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key)); 137 return PTR_ERR(key); 138 } 139 return 0; 140 } 141 142 int mark_hash_blacklisted(const u8 *hash, size_t hash_len, 143 enum blacklist_hash_type hash_type) 144 { 145 const char *buffer; 146 int err; 147 148 buffer = get_raw_hash(hash, hash_len, hash_type); 149 if (IS_ERR(buffer)) 150 return PTR_ERR(buffer); 151 err = mark_raw_hash_blacklisted(buffer); 152 kfree(buffer); 153 return err; 154 } 155 156 /** 157 * is_hash_blacklisted - Determine if a hash is blacklisted 158 * @hash: The hash to be checked as a binary blob 159 * @hash_len: The length of the binary hash 160 * @hash_type: Type of hash 161 */ 162 int is_hash_blacklisted(const u8 *hash, size_t hash_len, 163 enum blacklist_hash_type hash_type) 164 { 165 key_ref_t kref; 166 const char *buffer; 167 int ret = 0; 168 169 buffer = get_raw_hash(hash, hash_len, hash_type); 170 if (IS_ERR(buffer)) 171 return PTR_ERR(buffer); 172 kref = keyring_search(make_key_ref(blacklist_keyring, true), 173 &key_type_blacklist, buffer, false); 174 if (!IS_ERR(kref)) { 175 key_ref_put(kref); 176 ret = -EKEYREJECTED; 177 } 178 179 kfree(buffer); 180 return ret; 181 } 182 EXPORT_SYMBOL_GPL(is_hash_blacklisted); 183 184 int is_binary_blacklisted(const u8 *hash, size_t hash_len) 185 { 186 if (is_hash_blacklisted(hash, hash_len, BLACKLIST_HASH_BINARY) == 187 -EKEYREJECTED) 188 return -EPERM; 189 190 return 0; 191 } 192 EXPORT_SYMBOL_GPL(is_binary_blacklisted); 193 194 #ifdef CONFIG_SYSTEM_REVOCATION_LIST 195 /** 196 * add_key_to_revocation_list - Add a revocation certificate to the blacklist 197 * @data: The data blob containing the certificate 198 * @size: The size of data blob 199 */ 200 int add_key_to_revocation_list(const char *data, size_t size) 201 { 202 key_ref_t key; 203 204 key = key_create_or_update(make_key_ref(blacklist_keyring, true), 205 "asymmetric", 206 NULL, 207 data, 208 size, 209 ((KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW), 210 KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN); 211 212 if (IS_ERR(key)) { 213 pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key)); 214 return PTR_ERR(key); 215 } 216 217 return 0; 218 } 219 220 /** 221 * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked 222 * @pkcs7: The PKCS#7 message to check 223 */ 224 int is_key_on_revocation_list(struct pkcs7_message *pkcs7) 225 { 226 int ret; 227 228 ret = pkcs7_validate_trust(pkcs7, blacklist_keyring); 229 230 if (ret == 0) 231 return -EKEYREJECTED; 232 233 return -ENOKEY; 234 } 235 #endif 236 237 /* 238 * Initialise the blacklist 239 */ 240 static int __init blacklist_init(void) 241 { 242 const char *const *bl; 243 244 if (register_key_type(&key_type_blacklist) < 0) 245 panic("Can't allocate system blacklist key type\n"); 246 247 blacklist_keyring = 248 keyring_alloc(".blacklist", 249 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(), 250 (KEY_POS_ALL & ~KEY_POS_SETATTR) | 251 KEY_USR_VIEW | KEY_USR_READ | 252 KEY_USR_SEARCH, 253 KEY_ALLOC_NOT_IN_QUOTA | 254 KEY_ALLOC_SET_KEEP, 255 NULL, NULL); 256 if (IS_ERR(blacklist_keyring)) 257 panic("Can't allocate system blacklist keyring\n"); 258 259 for (bl = blacklist_hashes; *bl; bl++) 260 if (mark_raw_hash_blacklisted(*bl) < 0) 261 pr_err("- blacklisting failed\n"); 262 return 0; 263 } 264 265 /* 266 * Must be initialised before we try and load the keys into the keyring. 267 */ 268 device_initcall(blacklist_init); 269 270 #ifdef CONFIG_SYSTEM_REVOCATION_LIST 271 /* 272 * Load the compiled-in list of revocation X.509 certificates. 273 */ 274 static __init int load_revocation_certificate_list(void) 275 { 276 if (revocation_certificate_list_size) 277 pr_notice("Loading compiled-in revocation X.509 certificates\n"); 278 279 return load_certificate_list(revocation_certificate_list, revocation_certificate_list_size, 280 blacklist_keyring); 281 } 282 late_initcall(load_revocation_certificate_list); 283 #endif 284