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