1 // SPDX-License-Identifier: GPL-2.0-or-later 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* System hash blacklist. 2 /* System hash blacklist. 3 * 3 * 4 * Copyright (C) 2016 Red Hat, Inc. All Rights 4 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.c 5 * Written by David Howells (dhowells@redhat.com) 6 */ 6 */ 7 7 8 #define pr_fmt(fmt) "blacklist: "fmt 8 #define pr_fmt(fmt) "blacklist: "fmt 9 #include <linux/module.h> 9 #include <linux/module.h> 10 #include <linux/slab.h> 10 #include <linux/slab.h> 11 #include <linux/key.h> 11 #include <linux/key.h> 12 #include <linux/key-type.h> 12 #include <linux/key-type.h> 13 #include <linux/sched.h> 13 #include <linux/sched.h> 14 #include <linux/ctype.h> 14 #include <linux/ctype.h> 15 #include <linux/err.h> 15 #include <linux/err.h> 16 #include <linux/seq_file.h> 16 #include <linux/seq_file.h> 17 #include <linux/uidgid.h> 17 #include <linux/uidgid.h> 18 #include <keys/asymmetric-type.h> << 19 #include <keys/system_keyring.h> 18 #include <keys/system_keyring.h> 20 #include "blacklist.h" 19 #include "blacklist.h" 21 !! 20 #include "common.h" 22 /* << 23 * According to crypto/asymmetric_keys/x509_ce << 24 * the size of the currently longest supported << 25 * which translates into 128 hex characters. << 26 */ << 27 #define MAX_HASH_LEN 128 << 28 << 29 #define BLACKLIST_KEY_PERM (KEY_POS_SEARCH | K << 30 KEY_USR_SEARCH | K << 31 << 32 static const char tbs_prefix[] = "tbs"; << 33 static const char bin_prefix[] = "bin"; << 34 21 35 static struct key *blacklist_keyring; 22 static struct key *blacklist_keyring; 36 23 37 #ifdef CONFIG_SYSTEM_REVOCATION_LIST 24 #ifdef CONFIG_SYSTEM_REVOCATION_LIST 38 extern __initconst const u8 revocation_certifi 25 extern __initconst const u8 revocation_certificate_list[]; 39 extern __initconst const unsigned long revocat 26 extern __initconst const unsigned long revocation_certificate_list_size; 40 #endif 27 #endif 41 28 42 /* 29 /* 43 * The description must be a type prefix, a co 30 * The description must be a type prefix, a colon and then an even number of 44 * hex digits. The hash is kept in the descri 31 * hex digits. The hash is kept in the description. 45 */ 32 */ 46 static int blacklist_vet_description(const cha 33 static int blacklist_vet_description(const char *desc) 47 { 34 { 48 int i, prefix_len, tbs_step = 0, bin_s !! 35 int n = 0; 49 36 50 /* The following algorithm only works !! 37 if (*desc == ':') 51 BUILD_BUG_ON(sizeof(tbs_prefix) != siz !! 38 return -EINVAL; 52 prefix_len = sizeof(tbs_prefix) - 1; !! 39 for (; *desc; desc++) 53 for (i = 0; *desc; desc++, i++) { !! 40 if (*desc == ':') 54 if (*desc == ':') { !! 41 goto found_colon; 55 if (tbs_step == prefix << 56 goto found_col << 57 if (bin_step == prefix << 58 goto found_col << 59 return -EINVAL; << 60 } << 61 if (i >= prefix_len) << 62 return -EINVAL; << 63 if (*desc == tbs_prefix[i]) << 64 tbs_step++; << 65 if (*desc == bin_prefix[i]) << 66 bin_step++; << 67 } << 68 return -EINVAL; 42 return -EINVAL; 69 43 70 found_colon: 44 found_colon: 71 desc++; 45 desc++; 72 for (i = 0; *desc && i < MAX_HASH_LEN; !! 46 for (; *desc; desc++) { 73 if (!isxdigit(*desc) || isuppe 47 if (!isxdigit(*desc) || isupper(*desc)) 74 return -EINVAL; 48 return -EINVAL; >> 49 n++; 75 } 50 } 76 if (*desc) << 77 /* The hash is greater than MA << 78 return -ENOPKG; << 79 51 80 /* Checks for an even number of hexade !! 52 if (n == 0 || n & 1) 81 if (i == 0 || i & 1) << 82 return -EINVAL; 53 return -EINVAL; 83 return 0; 54 return 0; 84 } 55 } 85 56 86 static int blacklist_key_instantiate(struct ke !! 57 /* 87 struct key_preparsed_payload * !! 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) 88 { 62 { 89 #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE !! 63 if (prep->datalen > 0) 90 int err; !! 64 return -EINVAL; 91 #endif !! 65 return 0; 92 << 93 /* Sets safe default permissions for k << 94 key->perm = BLACKLIST_KEY_PERM; << 95 << 96 /* << 97 * Skips the authentication step for b << 98 * signed but still trusted. << 99 */ << 100 if (key->flags & (1 << KEY_FLAG_BUILTI << 101 goto out; << 102 << 103 #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE << 104 /* << 105 * Verifies the description's PKCS#7 s << 106 * trusted keyring. << 107 */ << 108 err = verify_pkcs7_signature(key->desc << 109 strlen(key->descriptio << 110 NULL, VERIFYING_UNSPEC << 111 if (err) << 112 return err; << 113 #else << 114 /* << 115 * It should not be possible to come h << 116 * have KEY_USR_WRITE and the only oth << 117 * for builtin hashes. << 118 */ << 119 WARN_ON_ONCE(1); << 120 return -EPERM; << 121 #endif << 122 << 123 out: << 124 return generic_key_instantiate(key, pr << 125 } 66 } 126 67 127 static int blacklist_key_update(struct key *ke !! 68 static void blacklist_free_preparse(struct key_preparsed_payload *prep) 128 struct key_preparsed_payload * << 129 { 69 { 130 return -EPERM; << 131 } 70 } 132 71 133 static void blacklist_describe(const struct ke 72 static void blacklist_describe(const struct key *key, struct seq_file *m) 134 { 73 { 135 seq_puts(m, key->description); 74 seq_puts(m, key->description); 136 } 75 } 137 76 138 static struct key_type key_type_blacklist = { 77 static struct key_type key_type_blacklist = { 139 .name = "blacklist", 78 .name = "blacklist", 140 .vet_description = blacklist_ve 79 .vet_description = blacklist_vet_description, 141 .instantiate = blacklist_ke !! 80 .preparse = blacklist_preparse, 142 .update = blacklist_ke !! 81 .free_preparse = blacklist_free_preparse, >> 82 .instantiate = generic_key_instantiate, 143 .describe = blacklist_de 83 .describe = blacklist_describe, 144 }; 84 }; 145 85 146 static char *get_raw_hash(const u8 *hash, size << 147 enum blacklist_hash_type hash_ << 148 { << 149 size_t type_len; << 150 const char *type_prefix; << 151 char *buffer, *p; << 152 << 153 switch (hash_type) { << 154 case BLACKLIST_HASH_X509_TBS: << 155 type_len = sizeof(tbs_prefix) << 156 type_prefix = tbs_prefix; << 157 break; << 158 case BLACKLIST_HASH_BINARY: << 159 type_len = sizeof(bin_prefix) << 160 type_prefix = bin_prefix; << 161 break; << 162 default: << 163 WARN_ON_ONCE(1); << 164 return ERR_PTR(-EINVAL); << 165 } << 166 buffer = kmalloc(type_len + 1 + hash_l << 167 if (!buffer) << 168 return ERR_PTR(-ENOMEM); << 169 p = memcpy(buffer, type_prefix, type_l << 170 p += type_len; << 171 *p++ = ':'; << 172 bin2hex(p, hash, hash_len); << 173 p += hash_len * 2; << 174 *p = '\0'; << 175 return buffer; << 176 } << 177 << 178 /** 86 /** 179 * mark_raw_hash_blacklisted - Add a hash to t !! 87 * mark_hash_blacklisted - Add a hash to the system blacklist 180 * @hash: The hash as a hex string with a type 88 * @hash: The hash as a hex string with a type prefix (eg. "tbs:23aa429783") 181 */ 89 */ 182 static int mark_raw_hash_blacklisted(const cha !! 90 int mark_hash_blacklisted(const char *hash) 183 { 91 { 184 key_ref_t key; 92 key_ref_t key; 185 93 186 key = key_create(make_key_ref(blacklis !! 94 key = key_create_or_update(make_key_ref(blacklist_keyring, true), 187 "blacklist", !! 95 "blacklist", 188 hash, !! 96 hash, 189 NULL, !! 97 NULL, 190 0, !! 98 0, 191 BLACKLIST_KEY_PERM, !! 99 ((KEY_POS_ALL & ~KEY_POS_SETATTR) | 192 KEY_ALLOC_NOT_IN_QUOT !! 100 KEY_USR_VIEW), 193 KEY_ALLOC_BUILT_IN); !! 101 KEY_ALLOC_NOT_IN_QUOTA | >> 102 KEY_ALLOC_BUILT_IN); 194 if (IS_ERR(key)) { 103 if (IS_ERR(key)) { 195 if (PTR_ERR(key) == -EEXIST) !! 104 pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key)); 196 pr_warn("Duplicate bla << 197 else << 198 pr_err("Problem blackl << 199 return PTR_ERR(key); 105 return PTR_ERR(key); 200 } 106 } 201 return 0; 107 return 0; 202 } 108 } 203 109 204 int mark_hash_blacklisted(const u8 *hash, size << 205 enum blacklist_hash_type hash_ << 206 { << 207 const char *buffer; << 208 int err; << 209 << 210 buffer = get_raw_hash(hash, hash_len, << 211 if (IS_ERR(buffer)) << 212 return PTR_ERR(buffer); << 213 err = mark_raw_hash_blacklisted(buffer << 214 kfree(buffer); << 215 return err; << 216 } << 217 << 218 /** 110 /** 219 * is_hash_blacklisted - Determine if a hash i 111 * is_hash_blacklisted - Determine if a hash is blacklisted 220 * @hash: The hash to be checked as a binary b 112 * @hash: The hash to be checked as a binary blob 221 * @hash_len: The length of the binary hash 113 * @hash_len: The length of the binary hash 222 * @hash_type: Type of hash !! 114 * @type: Type of hash 223 */ 115 */ 224 int is_hash_blacklisted(const u8 *hash, size_t !! 116 int is_hash_blacklisted(const u8 *hash, size_t hash_len, const char *type) 225 enum blacklist_hash_type hash_ << 226 { 117 { 227 key_ref_t kref; 118 key_ref_t kref; 228 const char *buffer; !! 119 size_t type_len = strlen(type); >> 120 char *buffer, *p; 229 int ret = 0; 121 int ret = 0; 230 122 231 buffer = get_raw_hash(hash, hash_len, !! 123 buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL); 232 if (IS_ERR(buffer)) !! 124 if (!buffer) 233 return PTR_ERR(buffer); !! 125 return -ENOMEM; >> 126 p = memcpy(buffer, type, type_len); >> 127 p += type_len; >> 128 *p++ = ':'; >> 129 bin2hex(p, hash, hash_len); >> 130 p += hash_len * 2; >> 131 *p = 0; >> 132 234 kref = keyring_search(make_key_ref(bla 133 kref = keyring_search(make_key_ref(blacklist_keyring, true), 235 &key_type_blackl 134 &key_type_blacklist, buffer, false); 236 if (!IS_ERR(kref)) { 135 if (!IS_ERR(kref)) { 237 key_ref_put(kref); 136 key_ref_put(kref); 238 ret = -EKEYREJECTED; 137 ret = -EKEYREJECTED; 239 } 138 } 240 139 241 kfree(buffer); 140 kfree(buffer); 242 return ret; 141 return ret; 243 } 142 } 244 EXPORT_SYMBOL_GPL(is_hash_blacklisted); 143 EXPORT_SYMBOL_GPL(is_hash_blacklisted); 245 144 246 int is_binary_blacklisted(const u8 *hash, size 145 int is_binary_blacklisted(const u8 *hash, size_t hash_len) 247 { 146 { 248 if (is_hash_blacklisted(hash, hash_len !! 147 if (is_hash_blacklisted(hash, hash_len, "bin") == -EKEYREJECTED) 249 -EKEYREJECTED) << 250 return -EPERM; 148 return -EPERM; 251 149 252 return 0; 150 return 0; 253 } 151 } 254 EXPORT_SYMBOL_GPL(is_binary_blacklisted); 152 EXPORT_SYMBOL_GPL(is_binary_blacklisted); 255 153 256 #ifdef CONFIG_SYSTEM_REVOCATION_LIST 154 #ifdef CONFIG_SYSTEM_REVOCATION_LIST 257 /** 155 /** 258 * add_key_to_revocation_list - Add a revocati 156 * add_key_to_revocation_list - Add a revocation certificate to the blacklist 259 * @data: The data blob containing the certifi 157 * @data: The data blob containing the certificate 260 * @size: The size of data blob 158 * @size: The size of data blob 261 */ 159 */ 262 int add_key_to_revocation_list(const char *dat 160 int add_key_to_revocation_list(const char *data, size_t size) 263 { 161 { 264 key_ref_t key; 162 key_ref_t key; 265 163 266 key = key_create_or_update(make_key_re 164 key = key_create_or_update(make_key_ref(blacklist_keyring, true), 267 "asymmetric 165 "asymmetric", 268 NULL, 166 NULL, 269 data, 167 data, 270 size, 168 size, 271 KEY_POS_VIE !! 169 ((KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW), 272 | KEY_USR_V !! 170 KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN); 273 KEY_ALLOC_N << 274 | KEY_ALLOC << 275 171 276 if (IS_ERR(key)) { 172 if (IS_ERR(key)) { 277 pr_err("Problem with revocatio 173 pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key)); 278 return PTR_ERR(key); 174 return PTR_ERR(key); 279 } 175 } 280 176 281 return 0; 177 return 0; 282 } 178 } 283 179 284 /** 180 /** 285 * is_key_on_revocation_list - Determine if th 181 * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked 286 * @pkcs7: The PKCS#7 message to check 182 * @pkcs7: The PKCS#7 message to check 287 */ 183 */ 288 int is_key_on_revocation_list(struct pkcs7_mes 184 int is_key_on_revocation_list(struct pkcs7_message *pkcs7) 289 { 185 { 290 int ret; 186 int ret; 291 187 292 ret = pkcs7_validate_trust(pkcs7, blac 188 ret = pkcs7_validate_trust(pkcs7, blacklist_keyring); 293 189 294 if (ret == 0) 190 if (ret == 0) 295 return -EKEYREJECTED; 191 return -EKEYREJECTED; 296 192 297 return -ENOKEY; 193 return -ENOKEY; 298 } 194 } 299 #endif 195 #endif 300 196 301 static int restrict_link_for_blacklist(struct << 302 const struct key_type *type, c << 303 struct key *restrict_key) << 304 { << 305 if (type == &key_type_blacklist) << 306 return 0; << 307 return -EOPNOTSUPP; << 308 } << 309 << 310 /* 197 /* 311 * Initialise the blacklist 198 * Initialise the blacklist 312 * << 313 * The blacklist_init() function is registered << 314 * device_initcall(). As a result if the blac << 315 * any reason the kernel continues to execute. << 316 * could be acceptable for some non-critical k << 317 * keyring fails to load it defeats the certif << 318 * signed modules. If a critical piece of sec << 319 * expect to be present fails to initialize, p << 320 * thing to do. << 321 */ 199 */ 322 static int __init blacklist_init(void) 200 static int __init blacklist_init(void) 323 { 201 { 324 const char *const *bl; 202 const char *const *bl; 325 struct key_restriction *restriction; << 326 203 327 if (register_key_type(&key_type_blackl 204 if (register_key_type(&key_type_blacklist) < 0) 328 panic("Can't allocate system b 205 panic("Can't allocate system blacklist key type\n"); 329 206 330 restriction = kzalloc(sizeof(*restrict << 331 if (!restriction) << 332 panic("Can't allocate blacklis << 333 restriction->check = restrict_link_for << 334 << 335 blacklist_keyring = 207 blacklist_keyring = 336 keyring_alloc(".blacklist", 208 keyring_alloc(".blacklist", 337 GLOBAL_ROOT_UID, 209 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(), 338 KEY_POS_VIEW | K !! 210 (KEY_POS_ALL & ~KEY_POS_SETATTR) | 339 KEY_POS_WRITE | !! 211 KEY_USR_VIEW | KEY_USR_READ | 340 KEY_USR_VIEW | K !! 212 KEY_USR_SEARCH, 341 #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE !! 213 KEY_ALLOC_NOT_IN_QUOTA | 342 | KEY_USR_WRITE << 343 #endif << 344 , KEY_ALLOC_NOT_ << 345 KEY_ALLOC_SET_KE 214 KEY_ALLOC_SET_KEEP, 346 restriction, NUL !! 215 NULL, NULL); 347 if (IS_ERR(blacklist_keyring)) 216 if (IS_ERR(blacklist_keyring)) 348 panic("Can't allocate system b 217 panic("Can't allocate system blacklist keyring\n"); 349 218 350 for (bl = blacklist_hashes; *bl; bl++) 219 for (bl = blacklist_hashes; *bl; bl++) 351 if (mark_raw_hash_blacklisted( !! 220 if (mark_hash_blacklisted(*bl) < 0) 352 pr_err("- blacklisting 221 pr_err("- blacklisting failed\n"); 353 return 0; 222 return 0; 354 } 223 } 355 224 356 /* 225 /* 357 * Must be initialised before we try and load 226 * Must be initialised before we try and load the keys into the keyring. 358 */ 227 */ 359 device_initcall(blacklist_init); 228 device_initcall(blacklist_init); 360 229 361 #ifdef CONFIG_SYSTEM_REVOCATION_LIST 230 #ifdef CONFIG_SYSTEM_REVOCATION_LIST 362 /* 231 /* 363 * Load the compiled-in list of revocation X.5 232 * Load the compiled-in list of revocation X.509 certificates. 364 */ 233 */ 365 static __init int load_revocation_certificate_ 234 static __init int load_revocation_certificate_list(void) 366 { 235 { 367 if (revocation_certificate_list_size) 236 if (revocation_certificate_list_size) 368 pr_notice("Loading compiled-in 237 pr_notice("Loading compiled-in revocation X.509 certificates\n"); 369 238 370 return x509_load_certificate_list(revo !! 239 return load_certificate_list(revocation_certificate_list, revocation_certificate_list_size, 371 revo !! 240 blacklist_keyring); 372 blac << 373 } 241 } 374 late_initcall(load_revocation_certificate_list 242 late_initcall(load_revocation_certificate_list); 375 #endif 243 #endif 376 244
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.