18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* System hash blacklist. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved. 58c2ecf20Sopenharmony_ci * Written by David Howells (dhowells@redhat.com) 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "blacklist: "fmt 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/slab.h> 118c2ecf20Sopenharmony_ci#include <linux/key.h> 128c2ecf20Sopenharmony_ci#include <linux/key-type.h> 138c2ecf20Sopenharmony_ci#include <linux/sched.h> 148c2ecf20Sopenharmony_ci#include <linux/ctype.h> 158c2ecf20Sopenharmony_ci#include <linux/err.h> 168c2ecf20Sopenharmony_ci#include <linux/seq_file.h> 178c2ecf20Sopenharmony_ci#include <keys/system_keyring.h> 188c2ecf20Sopenharmony_ci#include "blacklist.h" 198c2ecf20Sopenharmony_ci#include "common.h" 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistatic struct key *blacklist_keyring; 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#ifdef CONFIG_SYSTEM_REVOCATION_LIST 248c2ecf20Sopenharmony_ciextern __initconst const u8 revocation_certificate_list[]; 258c2ecf20Sopenharmony_ciextern __initconst const unsigned long revocation_certificate_list_size; 268c2ecf20Sopenharmony_ci#endif 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/* 298c2ecf20Sopenharmony_ci * The description must be a type prefix, a colon and then an even number of 308c2ecf20Sopenharmony_ci * hex digits. The hash is kept in the description. 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_cistatic int blacklist_vet_description(const char *desc) 338c2ecf20Sopenharmony_ci{ 348c2ecf20Sopenharmony_ci int n = 0; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci if (*desc == ':') 378c2ecf20Sopenharmony_ci return -EINVAL; 388c2ecf20Sopenharmony_ci for (; *desc; desc++) 398c2ecf20Sopenharmony_ci if (*desc == ':') 408c2ecf20Sopenharmony_ci goto found_colon; 418c2ecf20Sopenharmony_ci return -EINVAL; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cifound_colon: 448c2ecf20Sopenharmony_ci desc++; 458c2ecf20Sopenharmony_ci for (; *desc; desc++) { 468c2ecf20Sopenharmony_ci if (!isxdigit(*desc)) 478c2ecf20Sopenharmony_ci return -EINVAL; 488c2ecf20Sopenharmony_ci n++; 498c2ecf20Sopenharmony_ci } 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci if (n == 0 || n & 1) 528c2ecf20Sopenharmony_ci return -EINVAL; 538c2ecf20Sopenharmony_ci return 0; 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* 578c2ecf20Sopenharmony_ci * The hash to be blacklisted is expected to be in the description. There will 588c2ecf20Sopenharmony_ci * be no payload. 598c2ecf20Sopenharmony_ci */ 608c2ecf20Sopenharmony_cistatic int blacklist_preparse(struct key_preparsed_payload *prep) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci if (prep->datalen > 0) 638c2ecf20Sopenharmony_ci return -EINVAL; 648c2ecf20Sopenharmony_ci return 0; 658c2ecf20Sopenharmony_ci} 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_cistatic void blacklist_free_preparse(struct key_preparsed_payload *prep) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci} 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_cistatic void blacklist_describe(const struct key *key, struct seq_file *m) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci seq_puts(m, key->description); 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_cistatic struct key_type key_type_blacklist = { 778c2ecf20Sopenharmony_ci .name = "blacklist", 788c2ecf20Sopenharmony_ci .vet_description = blacklist_vet_description, 798c2ecf20Sopenharmony_ci .preparse = blacklist_preparse, 808c2ecf20Sopenharmony_ci .free_preparse = blacklist_free_preparse, 818c2ecf20Sopenharmony_ci .instantiate = generic_key_instantiate, 828c2ecf20Sopenharmony_ci .describe = blacklist_describe, 838c2ecf20Sopenharmony_ci}; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci/** 868c2ecf20Sopenharmony_ci * mark_hash_blacklisted - Add a hash to the system blacklist 878c2ecf20Sopenharmony_ci * @hash - The hash as a hex string with a type prefix (eg. "tbs:23aa429783") 888c2ecf20Sopenharmony_ci */ 898c2ecf20Sopenharmony_ciint mark_hash_blacklisted(const char *hash) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci key_ref_t key; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci key = key_create_or_update(make_key_ref(blacklist_keyring, true), 948c2ecf20Sopenharmony_ci "blacklist", 958c2ecf20Sopenharmony_ci hash, 968c2ecf20Sopenharmony_ci NULL, 978c2ecf20Sopenharmony_ci 0, 988c2ecf20Sopenharmony_ci ((KEY_POS_ALL & ~KEY_POS_SETATTR) | 998c2ecf20Sopenharmony_ci KEY_USR_VIEW), 1008c2ecf20Sopenharmony_ci KEY_ALLOC_NOT_IN_QUOTA | 1018c2ecf20Sopenharmony_ci KEY_ALLOC_BUILT_IN); 1028c2ecf20Sopenharmony_ci if (IS_ERR(key)) { 1038c2ecf20Sopenharmony_ci pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key)); 1048c2ecf20Sopenharmony_ci return PTR_ERR(key); 1058c2ecf20Sopenharmony_ci } 1068c2ecf20Sopenharmony_ci return 0; 1078c2ecf20Sopenharmony_ci} 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci/** 1108c2ecf20Sopenharmony_ci * is_hash_blacklisted - Determine if a hash is blacklisted 1118c2ecf20Sopenharmony_ci * @hash: The hash to be checked as a binary blob 1128c2ecf20Sopenharmony_ci * @hash_len: The length of the binary hash 1138c2ecf20Sopenharmony_ci * @type: Type of hash 1148c2ecf20Sopenharmony_ci */ 1158c2ecf20Sopenharmony_ciint is_hash_blacklisted(const u8 *hash, size_t hash_len, const char *type) 1168c2ecf20Sopenharmony_ci{ 1178c2ecf20Sopenharmony_ci key_ref_t kref; 1188c2ecf20Sopenharmony_ci size_t type_len = strlen(type); 1198c2ecf20Sopenharmony_ci char *buffer, *p; 1208c2ecf20Sopenharmony_ci int ret = 0; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL); 1238c2ecf20Sopenharmony_ci if (!buffer) 1248c2ecf20Sopenharmony_ci return -ENOMEM; 1258c2ecf20Sopenharmony_ci p = memcpy(buffer, type, type_len); 1268c2ecf20Sopenharmony_ci p += type_len; 1278c2ecf20Sopenharmony_ci *p++ = ':'; 1288c2ecf20Sopenharmony_ci bin2hex(p, hash, hash_len); 1298c2ecf20Sopenharmony_ci p += hash_len * 2; 1308c2ecf20Sopenharmony_ci *p = 0; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci kref = keyring_search(make_key_ref(blacklist_keyring, true), 1338c2ecf20Sopenharmony_ci &key_type_blacklist, buffer, false); 1348c2ecf20Sopenharmony_ci if (!IS_ERR(kref)) { 1358c2ecf20Sopenharmony_ci key_ref_put(kref); 1368c2ecf20Sopenharmony_ci ret = -EKEYREJECTED; 1378c2ecf20Sopenharmony_ci } 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci kfree(buffer); 1408c2ecf20Sopenharmony_ci return ret; 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(is_hash_blacklisted); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ciint is_binary_blacklisted(const u8 *hash, size_t hash_len) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci if (is_hash_blacklisted(hash, hash_len, "bin") == -EKEYREJECTED) 1478c2ecf20Sopenharmony_ci return -EPERM; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci return 0; 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(is_binary_blacklisted); 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci#ifdef CONFIG_SYSTEM_REVOCATION_LIST 1548c2ecf20Sopenharmony_ci/** 1558c2ecf20Sopenharmony_ci * add_key_to_revocation_list - Add a revocation certificate to the blacklist 1568c2ecf20Sopenharmony_ci * @data: The data blob containing the certificate 1578c2ecf20Sopenharmony_ci * @size: The size of data blob 1588c2ecf20Sopenharmony_ci */ 1598c2ecf20Sopenharmony_ciint add_key_to_revocation_list(const char *data, size_t size) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci key_ref_t key; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci key = key_create_or_update(make_key_ref(blacklist_keyring, true), 1648c2ecf20Sopenharmony_ci "asymmetric", 1658c2ecf20Sopenharmony_ci NULL, 1668c2ecf20Sopenharmony_ci data, 1678c2ecf20Sopenharmony_ci size, 1688c2ecf20Sopenharmony_ci ((KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW), 1698c2ecf20Sopenharmony_ci KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci if (IS_ERR(key)) { 1728c2ecf20Sopenharmony_ci pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key)); 1738c2ecf20Sopenharmony_ci return PTR_ERR(key); 1748c2ecf20Sopenharmony_ci } 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci return 0; 1778c2ecf20Sopenharmony_ci} 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci/** 1808c2ecf20Sopenharmony_ci * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked 1818c2ecf20Sopenharmony_ci * @pkcs7: The PKCS#7 message to check 1828c2ecf20Sopenharmony_ci */ 1838c2ecf20Sopenharmony_ciint is_key_on_revocation_list(struct pkcs7_message *pkcs7) 1848c2ecf20Sopenharmony_ci{ 1858c2ecf20Sopenharmony_ci int ret; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci ret = pkcs7_validate_trust(pkcs7, blacklist_keyring); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci if (ret == 0) 1908c2ecf20Sopenharmony_ci return -EKEYREJECTED; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci return -ENOKEY; 1938c2ecf20Sopenharmony_ci} 1948c2ecf20Sopenharmony_ci#endif 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci/* 1978c2ecf20Sopenharmony_ci * Initialise the blacklist 1988c2ecf20Sopenharmony_ci */ 1998c2ecf20Sopenharmony_cistatic int __init blacklist_init(void) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci const char *const *bl; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci if (register_key_type(&key_type_blacklist) < 0) 2048c2ecf20Sopenharmony_ci panic("Can't allocate system blacklist key type\n"); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci blacklist_keyring = 2078c2ecf20Sopenharmony_ci keyring_alloc(".blacklist", 2088c2ecf20Sopenharmony_ci KUIDT_INIT(0), KGIDT_INIT(0), 2098c2ecf20Sopenharmony_ci current_cred(), 2108c2ecf20Sopenharmony_ci (KEY_POS_ALL & ~KEY_POS_SETATTR) | 2118c2ecf20Sopenharmony_ci KEY_USR_VIEW | KEY_USR_READ | 2128c2ecf20Sopenharmony_ci KEY_USR_SEARCH, 2138c2ecf20Sopenharmony_ci KEY_ALLOC_NOT_IN_QUOTA | 2148c2ecf20Sopenharmony_ci KEY_ALLOC_SET_KEEP, 2158c2ecf20Sopenharmony_ci NULL, NULL); 2168c2ecf20Sopenharmony_ci if (IS_ERR(blacklist_keyring)) 2178c2ecf20Sopenharmony_ci panic("Can't allocate system blacklist keyring\n"); 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci for (bl = blacklist_hashes; *bl; bl++) 2208c2ecf20Sopenharmony_ci if (mark_hash_blacklisted(*bl) < 0) 2218c2ecf20Sopenharmony_ci pr_err("- blacklisting failed\n"); 2228c2ecf20Sopenharmony_ci return 0; 2238c2ecf20Sopenharmony_ci} 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci/* 2268c2ecf20Sopenharmony_ci * Must be initialised before we try and load the keys into the keyring. 2278c2ecf20Sopenharmony_ci */ 2288c2ecf20Sopenharmony_cidevice_initcall(blacklist_init); 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci#ifdef CONFIG_SYSTEM_REVOCATION_LIST 2318c2ecf20Sopenharmony_ci/* 2328c2ecf20Sopenharmony_ci * Load the compiled-in list of revocation X.509 certificates. 2338c2ecf20Sopenharmony_ci */ 2348c2ecf20Sopenharmony_cistatic __init int load_revocation_certificate_list(void) 2358c2ecf20Sopenharmony_ci{ 2368c2ecf20Sopenharmony_ci if (revocation_certificate_list_size) 2378c2ecf20Sopenharmony_ci pr_notice("Loading compiled-in revocation X.509 certificates\n"); 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci return load_certificate_list(revocation_certificate_list, revocation_certificate_list_size, 2408c2ecf20Sopenharmony_ci blacklist_keyring); 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_cilate_initcall(load_revocation_certificate_list); 2438c2ecf20Sopenharmony_ci#endif 244