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 <linux/verification.h> 19 #include <keys/system_keyring.h> 20 #include "blacklist.h" 21 #include "common.h" 22 23 /* 24 * According to crypto/asymmetric_keys/x509_cert_parser.c:x509_note_pkey_algo(), 25 * the size of the currently longest supported hash algorithm is 512 bits, 26 * which translates into 128 hex characters. 27 */ 28 #define MAX_HASH_LEN 128 29 30 #define BLACKLIST_KEY_PERM (KEY_POS_SEARCH | KEY_POS_VIEW | \ 31 KEY_USR_SEARCH | KEY_USR_VIEW) 32 33 static const char tbs_prefix[] = "tbs"; 34 static const char bin_prefix[] = "bin"; 35 36 static struct key *blacklist_keyring; 37 38 #ifdef CONFIG_SYSTEM_REVOCATION_LIST 39 extern __initconst const u8 revocation_certificate_list[]; 40 extern __initconst const unsigned long revocation_certificate_list_size; 41 #endif 42 43 /* 44 * The description must be a type prefix, a colon and then an even number of 45 * hex digits. The hash is kept in the description. 46 */ 47 static int blacklist_vet_description(const char *desc) 48 { 49 int i, prefix_len, tbs_step = 0, bin_step = 0; 50 51 /* The following algorithm only works if prefix lengths match. */ 52 BUILD_BUG_ON(sizeof(tbs_prefix) != sizeof(bin_prefix)); 53 prefix_len = sizeof(tbs_prefix) - 1; 54 for (i = 0; *desc; desc++, i++) { 55 if (*desc == ':') { 56 if (tbs_step == prefix_len) 57 goto found_colon; 58 if (bin_step == prefix_len) 59 goto found_colon; 60 return -EINVAL; 61 } 62 if (i >= prefix_len) 63 return -EINVAL; 64 if (*desc == tbs_prefix[i]) 65 tbs_step++; 66 if (*desc == bin_prefix[i]) 67 bin_step++; 68 } 69 return -EINVAL; 70 71 found_colon: 72 desc++; 73 for (i = 0; *desc && i < MAX_HASH_LEN; desc++, i++) { 74 if (!isxdigit(*desc) || isupper(*desc)) 75 return -EINVAL; 76 } 77 if (*desc) 78 /* The hash is greater than MAX_HASH_LEN. */ 79 return -ENOPKG; 80 81 /* Checks for an even number of hexadecimal characters. */ 82 if (i == 0 || i & 1) 83 return -EINVAL; 84 return 0; 85 } 86 87 static int blacklist_key_instantiate(struct key *key, 88 struct key_preparsed_payload *prep) 89 { 90 #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE 91 int err; 92 #endif 93 94 /* Sets safe default permissions for keys loaded by user space. */ 95 key->perm = BLACKLIST_KEY_PERM; 96 97 /* 98 * Skips the authentication step for builtin hashes, they are not 99 * signed but still trusted. 100 */ 101 if (key->flags & (1 << KEY_FLAG_BUILTIN)) 102 goto out; 103 104 #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE 105 /* 106 * Verifies the description's PKCS#7 signature against the builtin 107 * trusted keyring. 108 */ 109 err = verify_pkcs7_signature(key->description, 110 strlen(key->description), prep->data, prep->datalen, 111 NULL, VERIFYING_UNSPECIFIED_SIGNATURE, NULL, NULL); 112 if (err) 113 return err; 114 #else 115 /* 116 * It should not be possible to come here because the keyring doesn't 117 * have KEY_USR_WRITE and the only other way to call this function is 118 * for builtin hashes. 119 */ 120 WARN_ON_ONCE(1); 121 return -EPERM; 122 #endif 123 124 out: 125 return generic_key_instantiate(key, prep); 126 } 127 128 static int blacklist_key_update(struct key *key, 129 struct key_preparsed_payload *prep) 130 { 131 return -EPERM; 132 } 133 134 static void blacklist_describe(const struct key *key, struct seq_file *m) 135 { 136 seq_puts(m, key->description); 137 } 138 139 static struct key_type key_type_blacklist = { 140 .name = "blacklist", 141 .vet_description = blacklist_vet_description, 142 .instantiate = blacklist_key_instantiate, 143 .update = blacklist_key_update, 144 .describe = blacklist_describe, 145 }; 146 147 static char *get_raw_hash(const u8 *hash, size_t hash_len, 148 enum blacklist_hash_type hash_type) 149 { 150 size_t type_len; 151 const char *type_prefix; 152 char *buffer, *p; 153 154 switch (hash_type) { 155 case BLACKLIST_HASH_X509_TBS: 156 type_len = sizeof(tbs_prefix) - 1; 157 type_prefix = tbs_prefix; 158 break; 159 case BLACKLIST_HASH_BINARY: 160 type_len = sizeof(bin_prefix) - 1; 161 type_prefix = bin_prefix; 162 break; 163 default: 164 WARN_ON_ONCE(1); 165 return ERR_PTR(-EINVAL); 166 } 167 buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL); 168 if (!buffer) 169 return ERR_PTR(-ENOMEM); 170 p = memcpy(buffer, type_prefix, type_len); 171 p += type_len; 172 *p++ = ':'; 173 bin2hex(p, hash, hash_len); 174 p += hash_len * 2; 175 *p = '\0'; 176 return buffer; 177 } 178 179 /** 180 * mark_raw_hash_blacklisted - Add a hash to the system blacklist 181 * @hash: The hash as a hex string with a type prefix (eg. "tbs:23aa429783") 182 */ 183 static int mark_raw_hash_blacklisted(const char *hash) 184 { 185 key_ref_t key; 186 187 key = key_create_or_update(make_key_ref(blacklist_keyring, true), 188 "blacklist", 189 hash, 190 NULL, 191 0, 192 BLACKLIST_KEY_PERM, 193 KEY_ALLOC_NOT_IN_QUOTA | 194 KEY_ALLOC_BUILT_IN); 195 if (IS_ERR(key)) { 196 pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key)); 197 return PTR_ERR(key); 198 } 199 return 0; 200 } 201 202 int mark_hash_blacklisted(const u8 *hash, size_t hash_len, 203 enum blacklist_hash_type hash_type) 204 { 205 const char *buffer; 206 int err; 207 208 buffer = get_raw_hash(hash, hash_len, hash_type); 209 if (IS_ERR(buffer)) 210 return PTR_ERR(buffer); 211 err = mark_raw_hash_blacklisted(buffer); 212 kfree(buffer); 213 return err; 214 } 215 216 /** 217 * is_hash_blacklisted - Determine if a hash is blacklisted 218 * @hash: The hash to be checked as a binary blob 219 * @hash_len: The length of the binary hash 220 * @hash_type: Type of hash 221 */ 222 int is_hash_blacklisted(const u8 *hash, size_t hash_len, 223 enum blacklist_hash_type hash_type) 224 { 225 key_ref_t kref; 226 const char *buffer; 227 int ret = 0; 228 229 buffer = get_raw_hash(hash, hash_len, hash_type); 230 if (IS_ERR(buffer)) 231 return PTR_ERR(buffer); 232 kref = keyring_search(make_key_ref(blacklist_keyring, true), 233 &key_type_blacklist, buffer, false); 234 if (!IS_ERR(kref)) { 235 key_ref_put(kref); 236 ret = -EKEYREJECTED; 237 } 238 239 kfree(buffer); 240 return ret; 241 } 242 EXPORT_SYMBOL_GPL(is_hash_blacklisted); 243 244 int is_binary_blacklisted(const u8 *hash, size_t hash_len) 245 { 246 if (is_hash_blacklisted(hash, hash_len, BLACKLIST_HASH_BINARY) == 247 -EKEYREJECTED) 248 return -EPERM; 249 250 return 0; 251 } 252 EXPORT_SYMBOL_GPL(is_binary_blacklisted); 253 254 #ifdef CONFIG_SYSTEM_REVOCATION_LIST 255 /** 256 * add_key_to_revocation_list - Add a revocation certificate to the blacklist 257 * @data: The data blob containing the certificate 258 * @size: The size of data blob 259 */ 260 int add_key_to_revocation_list(const char *data, size_t size) 261 { 262 key_ref_t key; 263 264 key = key_create_or_update(make_key_ref(blacklist_keyring, true), 265 "asymmetric", 266 NULL, 267 data, 268 size, 269 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH 270 | KEY_USR_VIEW, 271 KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN 272 | KEY_ALLOC_BYPASS_RESTRICTION); 273 274 if (IS_ERR(key)) { 275 pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key)); 276 return PTR_ERR(key); 277 } 278 279 return 0; 280 } 281 282 /** 283 * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked 284 * @pkcs7: The PKCS#7 message to check 285 */ 286 int is_key_on_revocation_list(struct pkcs7_message *pkcs7) 287 { 288 int ret; 289 290 ret = pkcs7_validate_trust(pkcs7, blacklist_keyring); 291 292 if (ret == 0) 293 return -EKEYREJECTED; 294 295 return -ENOKEY; 296 } 297 #endif 298 299 static int restrict_link_for_blacklist(struct key *dest_keyring, 300 const struct key_type *type, const union key_payload *payload, 301 struct key *restrict_key) 302 { 303 if (type == &key_type_blacklist) 304 return 0; 305 return -EOPNOTSUPP; 306 } 307 308 /* 309 * Initialise the blacklist 310 * 311 * The blacklist_init() function is registered as an initcall via 312 * device_initcall(). As a result if the blacklist_init() function fails for 313 * any reason the kernel continues to execute. While cleanly returning -ENODEV 314 * could be acceptable for some non-critical kernel parts, if the blacklist 315 * keyring fails to load it defeats the certificate/key based deny list for 316 * signed modules. If a critical piece of security functionality that users 317 * expect to be present fails to initialize, panic()ing is likely the right 318 * thing to do. 319 */ 320 static int __init blacklist_init(void) 321 { 322 const char *const *bl; 323 struct key_restriction *restriction; 324 325 if (register_key_type(&key_type_blacklist) < 0) 326 panic("Can't allocate system blacklist key type\n"); 327 328 restriction = kzalloc(sizeof(*restriction), GFP_KERNEL); 329 if (!restriction) 330 panic("Can't allocate blacklist keyring restriction\n"); 331 restriction->check = restrict_link_for_blacklist; 332 333 blacklist_keyring = 334 keyring_alloc(".blacklist", 335 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(), 336 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH | 337 KEY_POS_WRITE | 338 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH 339 #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE 340 | KEY_USR_WRITE 341 #endif 342 , KEY_ALLOC_NOT_IN_QUOTA | 343 KEY_ALLOC_SET_KEEP, 344 restriction, NULL); 345 if (IS_ERR(blacklist_keyring)) 346 panic("Can't allocate system blacklist keyring\n"); 347 348 for (bl = blacklist_hashes; *bl; bl++) 349 if (mark_raw_hash_blacklisted(*bl) < 0) 350 pr_err("- blacklisting failed\n"); 351 return 0; 352 } 353 354 /* 355 * Must be initialised before we try and load the keys into the keyring. 356 */ 357 device_initcall(blacklist_init); 358 359 #ifdef CONFIG_SYSTEM_REVOCATION_LIST 360 /* 361 * Load the compiled-in list of revocation X.509 certificates. 362 */ 363 static __init int load_revocation_certificate_list(void) 364 { 365 if (revocation_certificate_list_size) 366 pr_notice("Loading compiled-in revocation X.509 certificates\n"); 367 368 return load_certificate_list(revocation_certificate_list, revocation_certificate_list_size, 369 blacklist_keyring); 370 } 371 late_initcall(load_revocation_certificate_list); 372 #endif 373