| 1 | |
| 2 | |
| 3 | *
|
| 4 | * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
|
| 5 | * Written by David Howells ([email protected])
|
| 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/hex.h>
|
| 17 | #include <linux/seq_file.h>
|
| 18 | #include <linux/uidgid.h>
|
| 19 | #include <keys/asymmetric-type.h>
|
| 20 | #include <keys/system_keyring.h>
|
| 21 | #include "blacklist.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 |
|
| 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 |
|
| 79 | return -ENOPKG;
|
| 80 |
|
| 81 |
|
| 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 |
|
| 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(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 | if (PTR_ERR(key) == -EEXIST)
|
| 197 | pr_warn("Duplicate blacklisted hash %s\n", hash);
|
| 198 | else
|
| 199 | pr_err("Problem blacklisting hash %s: %pe\n", hash, key);
|
| 200 | return PTR_ERR(key);
|
| 201 | }
|
| 202 | return 0;
|
| 203 | }
|
| 204 |
|
| 205 | int mark_hash_blacklisted(const u8 *hash, size_t hash_len,
|
| 206 | enum blacklist_hash_type hash_type)
|
| 207 | {
|
| 208 | const char *buffer;
|
| 209 | int err;
|
| 210 |
|
| 211 | buffer = get_raw_hash(hash, hash_len, hash_type);
|
| 212 | if (IS_ERR(buffer))
|
| 213 | return PTR_ERR(buffer);
|
| 214 | err = mark_raw_hash_blacklisted(buffer);
|
| 215 | kfree(buffer);
|
| 216 | return err;
|
| 217 | }
|
| 218 |
|
| 219 | |
| 220 | * is_hash_blacklisted - Determine if a hash is blacklisted
|
| 221 | * @hash: The hash to be checked as a binary blob
|
| 222 | * @hash_len: The length of the binary hash
|
| 223 | * @hash_type: Type of hash
|
| 224 |
|
| 225 | int is_hash_blacklisted(const u8 *hash, size_t hash_len,
|
| 226 | enum blacklist_hash_type hash_type)
|
| 227 | {
|
| 228 | key_ref_t kref;
|
| 229 | const char *buffer;
|
| 230 | int ret = 0;
|
| 231 |
|
| 232 | buffer = get_raw_hash(hash, hash_len, hash_type);
|
| 233 | if (IS_ERR(buffer))
|
| 234 | return PTR_ERR(buffer);
|
| 235 | kref = keyring_search(make_key_ref(blacklist_keyring, true),
|
| 236 | &key_type_blacklist, buffer, false);
|
| 237 | if (!IS_ERR(kref)) {
|
| 238 | key_ref_put(kref);
|
| 239 | ret = -EKEYREJECTED;
|
| 240 | }
|
| 241 |
|
| 242 | kfree(buffer);
|
| 243 | return ret;
|
| 244 | }
|
| 245 | EXPORT_SYMBOL_GPL(is_hash_blacklisted);
|
| 246 |
|
| 247 | int is_binary_blacklisted(const u8 *hash, size_t hash_len)
|
| 248 | {
|
| 249 | if (is_hash_blacklisted(hash, hash_len, BLACKLIST_HASH_BINARY) ==
|
| 250 | -EKEYREJECTED)
|
| 251 | return -EPERM;
|
| 252 |
|
| 253 | return 0;
|
| 254 | }
|
| 255 | EXPORT_SYMBOL_GPL(is_binary_blacklisted);
|
| 256 |
|
| 257 | #ifdef CONFIG_SYSTEM_REVOCATION_LIST
|
| 258 | |
| 259 | * add_key_to_revocation_list - Add a revocation certificate to the blacklist
|
| 260 | * @data: The data blob containing the certificate
|
| 261 | * @size: The size of data blob
|
| 262 |
|
| 263 | int add_key_to_revocation_list(const char *data, size_t size)
|
| 264 | {
|
| 265 | key_ref_t key;
|
| 266 |
|
| 267 | key = key_create_or_update(make_key_ref(blacklist_keyring, true),
|
| 268 | "asymmetric",
|
| 269 | NULL,
|
| 270 | data,
|
| 271 | size,
|
| 272 | KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH
|
| 273 | | KEY_USR_VIEW,
|
| 274 | KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN
|
| 275 | | KEY_ALLOC_BYPASS_RESTRICTION);
|
| 276 |
|
| 277 | if (IS_ERR(key)) {
|
| 278 | pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key));
|
| 279 | return PTR_ERR(key);
|
| 280 | }
|
| 281 |
|
| 282 | return 0;
|
| 283 | }
|
| 284 |
|
| 285 | |
| 286 | * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked
|
| 287 | * @pkcs7: The PKCS#7 message to check
|
| 288 |
|
| 289 | int is_key_on_revocation_list(struct pkcs7_message *pkcs7)
|
| 290 | {
|
| 291 | int ret;
|
| 292 |
|
| 293 | ret = pkcs7_validate_trust(pkcs7, blacklist_keyring);
|
| 294 |
|
| 295 | if (ret == 0)
|
| 296 | return -EKEYREJECTED;
|
| 297 |
|
| 298 | return -ENOKEY;
|
| 299 | }
|
| 300 | #endif
|
| 301 |
|
| 302 | static int restrict_link_for_blacklist(struct key *dest_keyring,
|
| 303 | const struct key_type *type, const union key_payload *payload,
|
| 304 | struct key *restrict_key)
|
| 305 | {
|
| 306 | if (type == &key_type_blacklist)
|
| 307 | return 0;
|
| 308 | return -EOPNOTSUPP;
|
| 309 | }
|
| 310 |
|
| 311 | |
| 312 | * Initialise the blacklist
|
| 313 | *
|
| 314 | * The blacklist_init() function is registered as an initcall via
|
| 315 | * device_initcall(). As a result if the blacklist_init() function fails for
|
| 316 | * any reason the kernel continues to execute. While cleanly returning -ENODEV
|
| 317 | * could be acceptable for some non-critical kernel parts, if the blacklist
|
| 318 | * keyring fails to load it defeats the certificate/key based deny list for
|
| 319 | * signed modules. If a critical piece of security functionality that users
|
| 320 | * expect to be present fails to initialize, panic()ing is likely the right
|
| 321 | * thing to do.
|
| 322 |
|
| 323 | static int __init blacklist_init(void)
|
| 324 | {
|
| 325 | const char *const *bl;
|
| 326 | struct key_restriction *restriction;
|
| 327 |
|
| 328 | if (register_key_type(&key_type_blacklist) < 0)
|
| 329 | panic("Can't allocate system blacklist key type\n");
|
| 330 |
|
| 331 | restriction = kzalloc_obj(*restriction);
|
| 332 | if (!restriction)
|
| 333 | panic("Can't allocate blacklist keyring restriction\n");
|
| 334 | restriction->check = restrict_link_for_blacklist;
|
| 335 |
|
| 336 | blacklist_keyring =
|
| 337 | keyring_alloc(".blacklist",
|
| 338 | GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
|
| 339 | KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
|
| 340 | KEY_POS_WRITE |
|
| 341 | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH
|
| 342 | #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
|
| 343 | | KEY_USR_WRITE
|
| 344 | #endif
|
| 345 | , KEY_ALLOC_NOT_IN_QUOTA |
|
| 346 | KEY_ALLOC_SET_KEEP,
|
| 347 | restriction, NULL);
|
| 348 | if (IS_ERR(blacklist_keyring))
|
| 349 | panic("Can't allocate system blacklist keyring\n");
|
| 350 |
|
| 351 | for (bl = blacklist_hashes; *bl; bl++)
|
| 352 | if (mark_raw_hash_blacklisted(*bl) < 0)
|
| 353 | pr_err("- blacklisting failed\n");
|
| 354 | return 0;
|
| 355 | }
|
| 356 |
|
| 357 | |
| 358 | * Must be initialised before we try and load the keys into the keyring.
|
| 359 |
|
| 360 | device_initcall(blacklist_init);
|
| 361 |
|
| 362 | #ifdef CONFIG_SYSTEM_REVOCATION_LIST
|
| 363 | |
| 364 | * Load the compiled-in list of revocation X.509 certificates.
|
| 365 |
|
| 366 | static __init int load_revocation_certificate_list(void)
|
| 367 | {
|
| 368 | if (revocation_certificate_list_size)
|
| 369 | pr_notice("Loading compiled-in revocation X.509 certificates\n");
|
| 370 |
|
| 371 | return x509_load_certificate_list(revocation_certificate_list,
|
| 372 | revocation_certificate_list_size,
|
| 373 | blacklist_keyring);
|
| 374 | }
|
| 375 | late_initcall(load_revocation_certificate_list);
|
| 376 | #endif
|