1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Filesystem-level keyring for fscrypt 4 * 5 * Copyright 2019 Google LLC 6 */ 7 8 /* 9 * This file implements management of fscrypt master keys in the 10 * filesystem-level keyring, including the ioctls: 11 * 12 * - FS_IOC_ADD_ENCRYPTION_KEY 13 * - FS_IOC_REMOVE_ENCRYPTION_KEY 14 * - FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS 15 * - FS_IOC_GET_ENCRYPTION_KEY_STATUS 16 * 17 * See the "User API" section of Documentation/filesystems/fscrypt.rst for more 18 * information about these ioctls. 19 */ 20 21 #include <crypto/skcipher.h> 22 #include <linux/export.h> 23 #include <linux/key-type.h> 24 #include <linux/once.h> 25 #include <linux/random.h> 26 #include <linux/seq_file.h> 27 #include <linux/unaligned.h> 28 29 #include "fscrypt_private.h" 30 31 /* The master encryption keys for a filesystem (->s_master_keys) */ 32 struct fscrypt_keyring { 33 /* 34 * Lock that protects ->key_hashtable. It does *not* protect the 35 * fscrypt_master_key structs themselves. 36 */ 37 spinlock_t lock; 38 39 /* Hash table that maps fscrypt_key_specifier to fscrypt_master_key */ 40 struct hlist_head key_hashtable[128]; 41 }; 42 43 static void wipe_master_key_secret(struct fscrypt_master_key_secret *secret) 44 { 45 memzero_explicit(secret, sizeof(*secret)); 46 } 47 48 static void move_master_key_secret(struct fscrypt_master_key_secret *dst, 49 struct fscrypt_master_key_secret *src) 50 { 51 memcpy(dst, src, sizeof(*dst)); 52 memzero_explicit(src, sizeof(*src)); 53 } 54 55 static void fscrypt_free_master_key(struct rcu_head *head) 56 { 57 struct fscrypt_master_key *mk = 58 container_of(head, struct fscrypt_master_key, mk_rcu_head); 59 /* 60 * The master key secret and any embedded subkeys should have already 61 * been wiped when the last active reference to the fscrypt_master_key 62 * struct was dropped; doing it here would be unnecessarily late. 63 * Nevertheless, use kfree_sensitive() in case anything was missed. 64 */ 65 kfree_sensitive(mk); 66 } 67 68 static void clear_mk_users(struct fscrypt_master_key *mk); 69 70 void fscrypt_put_master_key(struct fscrypt_master_key *mk) 71 { 72 if (!refcount_dec_and_test(&mk->mk_struct_refs)) 73 return; 74 /* 75 * No structural references left, so clear ->mk_users, and also free the 76 * fscrypt_master_key struct itself after an RCU grace period ensures 77 * that concurrent keyring lookups can no longer find it. 78 */ 79 WARN_ON_ONCE(refcount_read(&mk->mk_active_refs) != 0); 80 clear_mk_users(mk); 81 call_rcu(&mk->mk_rcu_head, fscrypt_free_master_key); 82 } 83 84 void fscrypt_put_master_key_activeref(struct super_block *sb, 85 struct fscrypt_master_key *mk) 86 { 87 struct fscrypt_mode_key *node, *tmp; 88 89 if (!refcount_dec_and_test(&mk->mk_active_refs)) 90 return; 91 /* 92 * No active references left, so complete the full removal of this 93 * fscrypt_master_key struct by removing it from the keyring and 94 * destroying any non-file-scoped subkeys. 95 */ 96 97 if (WARN_ON_ONCE(!sb->s_master_keys)) 98 return; 99 spin_lock(&sb->s_master_keys->lock); 100 hlist_del_rcu(&mk->mk_node); 101 spin_unlock(&sb->s_master_keys->lock); 102 103 /* 104 * ->mk_active_refs == 0 implies that ->mk_present is false and 105 * ->mk_decrypted_inodes is empty. 106 */ 107 WARN_ON_ONCE(mk->mk_present); 108 WARN_ON_ONCE(!list_empty(&mk->mk_decrypted_inodes)); 109 110 /* 111 * Destroy any non-file-scoped subkeys. Since ->mk_active_refs == 0, 112 * they're no longer referenced by any inodes. Nor can key setup run 113 * and use them again. So they're no longer needed. (This implies no 114 * concurrent readers, so we don't need list_del_rcu() for example.) 115 */ 116 list_for_each_entry_safe(node, tmp, &mk->mk_mode_keys, link) { 117 fscrypt_destroy_prepared_key(sb, &node->key); 118 list_del(&node->link); 119 kfree(node); 120 } 121 memzero_explicit(&mk->mk_ino_hash_key, 122 sizeof(mk->mk_ino_hash_key)); 123 mk->mk_ino_hash_key_initialized = false; 124 125 /* Drop the structural ref associated with the active refs. */ 126 fscrypt_put_master_key(mk); 127 } 128 129 /* 130 * This transitions the key state from present to incompletely removed, and then 131 * potentially to absent (depending on whether inodes remain). 132 */ 133 static void fscrypt_initiate_key_removal(struct super_block *sb, 134 struct fscrypt_master_key *mk) 135 { 136 WRITE_ONCE(mk->mk_present, false); 137 wipe_master_key_secret(&mk->mk_secret); 138 fscrypt_put_master_key_activeref(sb, mk); 139 } 140 141 static inline bool valid_key_spec(const struct fscrypt_key_specifier *spec) 142 { 143 if (spec->__reserved) 144 return false; 145 return master_key_spec_len(spec) != 0; 146 } 147 148 /* 149 * fscrypt_master_key_user - a user's claim to a master key 150 */ 151 struct fscrypt_master_key_user { 152 struct list_head link; 153 kuid_t uid; 154 /* 155 * This 'struct key' contains no secret. It exists solely to charge the 156 * appropriate user's key quota. 157 */ 158 struct key *quota_key; 159 }; 160 161 static int fscrypt_user_key_instantiate(struct key *key, 162 struct key_preparsed_payload *prep) 163 { 164 /* 165 * We just charge FSCRYPT_MAX_RAW_KEY_SIZE bytes to the user's key quota 166 * for each key, regardless of the exact key size. The amount of memory 167 * actually used is greater than the size of the raw key anyway. 168 */ 169 return key_payload_reserve(key, FSCRYPT_MAX_RAW_KEY_SIZE); 170 } 171 172 static void fscrypt_user_key_describe(const struct key *key, struct seq_file *m) 173 { 174 seq_puts(m, key->description); 175 } 176 177 /* 178 * Type of fscrypt_master_key_user::quota_key. This contains no secret; it 179 * exists solely to charge a user's key quota. 180 * 181 * Note that the name of this key type really should be something like 182 * ".fscrypt-user" instead of simply ".fscrypt". But the shorter name is chosen 183 * mainly for simplicity of presentation in /proc/keys when read by a non-root 184 * user. And it is expected to be rare that a key is actually added by multiple 185 * users, since users should keep their encryption keys confidential. 186 */ 187 static struct key_type key_type_fscrypt_user = { 188 .name = ".fscrypt", 189 .instantiate = fscrypt_user_key_instantiate, 190 .describe = fscrypt_user_key_describe, 191 }; 192 193 #define FSCRYPT_MK_USER_DESCRIPTION_SIZE \ 194 (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + CONST_STRLEN(".uid.") + 10 + 1) 195 196 /* Create ->s_master_keys if needed. Synchronized by fscrypt_add_key_mutex. */ 197 static int allocate_filesystem_keyring(struct super_block *sb) 198 { 199 struct fscrypt_keyring *keyring; 200 201 if (sb->s_master_keys) 202 return 0; 203 204 keyring = kzalloc_obj(*keyring); 205 if (!keyring) 206 return -ENOMEM; 207 spin_lock_init(&keyring->lock); 208 /* 209 * Pairs with the smp_load_acquire() in fscrypt_find_master_key(). 210 * I.e., here we publish ->s_master_keys with a RELEASE barrier so that 211 * concurrent tasks can ACQUIRE it. 212 */ 213 smp_store_release(&sb->s_master_keys, keyring); 214 return 0; 215 } 216 217 /* 218 * Release all encryption keys that have been added to the filesystem, along 219 * with the keyring that contains them. 220 * 221 * This is called at unmount time, after all potentially-encrypted inodes have 222 * been evicted. The filesystem's underlying block device(s) are still 223 * available at this time; this is important because after user file accesses 224 * have been allowed, this function may need to evict keys from the keyslots of 225 * an inline crypto engine, which requires the block device(s). 226 */ 227 void fscrypt_destroy_keyring(struct super_block *sb) 228 { 229 struct fscrypt_keyring *keyring = sb->s_master_keys; 230 size_t i; 231 232 if (!keyring) 233 return; 234 235 for (i = 0; i < ARRAY_SIZE(keyring->key_hashtable); i++) { 236 struct hlist_head *bucket = &keyring->key_hashtable[i]; 237 struct fscrypt_master_key *mk; 238 struct hlist_node *tmp; 239 240 hlist_for_each_entry_safe(mk, tmp, bucket, mk_node) { 241 /* 242 * Since all potentially-encrypted inodes were already 243 * evicted, every key remaining in the keyring should 244 * have an empty inode list, and should only still be in 245 * the keyring due to the single active ref associated 246 * with ->mk_present. There should be no structural 247 * refs beyond the one associated with the active ref. 248 */ 249 WARN_ON_ONCE(refcount_read(&mk->mk_active_refs) != 1); 250 WARN_ON_ONCE(refcount_read(&mk->mk_struct_refs) != 1); 251 WARN_ON_ONCE(!mk->mk_present); 252 fscrypt_initiate_key_removal(sb, mk); 253 } 254 } 255 kfree_sensitive(keyring); 256 sb->s_master_keys = NULL; 257 } 258 259 static struct hlist_head * 260 fscrypt_mk_hash_bucket(struct fscrypt_keyring *keyring, 261 const struct fscrypt_key_specifier *mk_spec) 262 { 263 /* 264 * Since key specifiers should be "random" values, it is sufficient to 265 * use a trivial hash function that just takes the first several bits of 266 * the key specifier. 267 */ 268 unsigned long i = get_unaligned((unsigned long *)&mk_spec->u); 269 270 return &keyring->key_hashtable[i % ARRAY_SIZE(keyring->key_hashtable)]; 271 } 272 273 /* 274 * Find the specified master key struct in ->s_master_keys and take a structural 275 * ref to it. The structural ref guarantees that the key struct continues to 276 * exist, but it does *not* guarantee that ->s_master_keys continues to contain 277 * the key struct. The structural ref needs to be dropped by 278 * fscrypt_put_master_key(). Returns NULL if the key struct is not found. 279 */ 280 struct fscrypt_master_key * 281 fscrypt_find_master_key(struct super_block *sb, 282 const struct fscrypt_key_specifier *mk_spec) 283 { 284 struct fscrypt_keyring *keyring; 285 struct hlist_head *bucket; 286 struct fscrypt_master_key *mk; 287 288 /* 289 * Pairs with the smp_store_release() in allocate_filesystem_keyring(). 290 * I.e., another task can publish ->s_master_keys concurrently, 291 * executing a RELEASE barrier. We need to use smp_load_acquire() here 292 * to safely ACQUIRE the memory the other task published. 293 */ 294 keyring = smp_load_acquire(&sb->s_master_keys); 295 if (keyring == NULL) 296 return NULL; /* No keyring yet, so no keys yet. */ 297 298 bucket = fscrypt_mk_hash_bucket(keyring, mk_spec); 299 rcu_read_lock(); 300 switch (mk_spec->type) { 301 case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR: 302 hlist_for_each_entry_rcu(mk, bucket, mk_node) { 303 if (mk->mk_spec.type == 304 FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR && 305 memcmp(mk->mk_spec.u.descriptor, 306 mk_spec->u.descriptor, 307 FSCRYPT_KEY_DESCRIPTOR_SIZE) == 0 && 308 refcount_inc_not_zero(&mk->mk_struct_refs)) 309 goto out; 310 } 311 break; 312 case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER: 313 hlist_for_each_entry_rcu(mk, bucket, mk_node) { 314 if (mk->mk_spec.type == 315 FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER && 316 memcmp(mk->mk_spec.u.identifier, 317 mk_spec->u.identifier, 318 FSCRYPT_KEY_IDENTIFIER_SIZE) == 0 && 319 refcount_inc_not_zero(&mk->mk_struct_refs)) 320 goto out; 321 } 322 break; 323 } 324 mk = NULL; 325 out: 326 rcu_read_unlock(); 327 return mk; 328 } 329 330 /* Find the current user's claim in ->mk_users. ->mk_sem must be held. */ 331 static struct fscrypt_master_key_user * 332 find_master_key_user(struct fscrypt_master_key *mk) 333 { 334 struct fscrypt_master_key_user *mk_user; 335 kuid_t uid = current_fsuid(); 336 337 list_for_each_entry(mk_user, &mk->mk_users, link) { 338 if (uid_eq(mk_user->uid, uid)) 339 return mk_user; 340 } 341 return NULL; 342 } 343 344 /* 345 * Give the current user a claim in ->mk_users. This charges the user's quota 346 * and marks the master key as added by the current user, so that it cannot be 347 * removed by another user with the key. Either ->mk_sem must be held for 348 * write, or the master key must be still undergoing initialization. 349 */ 350 static int add_master_key_user(struct fscrypt_master_key *mk) 351 { 352 kuid_t uid = current_fsuid(); 353 char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE]; 354 struct key *quota_key; 355 struct fscrypt_master_key_user *mk_user; 356 int err; 357 358 snprintf(description, sizeof(description), "%*phN.uid.%u", 359 FSCRYPT_KEY_IDENTIFIER_SIZE, mk->mk_spec.u.identifier, 360 __kuid_val(uid)); 361 quota_key = key_alloc(&key_type_fscrypt_user, description, uid, 362 current_gid(), current_cred(), 363 KEY_POS_SEARCH | KEY_USR_VIEW, 0, NULL); 364 if (IS_ERR(quota_key)) 365 return PTR_ERR(quota_key); 366 367 err = key_instantiate_and_link(quota_key, NULL, 0, NULL, NULL); 368 if (err) { 369 key_put(quota_key); 370 return err; 371 } 372 373 mk_user = kzalloc_obj(*mk_user); 374 if (!mk_user) { 375 key_put(quota_key); 376 return -ENOMEM; 377 } 378 mk_user->uid = uid; 379 mk_user->quota_key = quota_key; 380 list_add(&mk_user->link, &mk->mk_users); 381 return 0; 382 } 383 384 static void unlink_and_free_mk_user(struct fscrypt_master_key_user *mk_user) 385 { 386 list_del(&mk_user->link); 387 key_put(mk_user->quota_key); 388 kfree(mk_user); 389 } 390 391 /* 392 * Remove the current user's claim from ->mk_users. 393 * ->mk_sem must be held for write. 394 * 395 * Returns 0 if removed or -ENOKEY if not found. 396 */ 397 static int remove_master_key_user(struct fscrypt_master_key *mk) 398 { 399 struct fscrypt_master_key_user *mk_user; 400 401 mk_user = find_master_key_user(mk); 402 if (!mk_user) 403 return -ENOKEY; 404 unlink_and_free_mk_user(mk_user); 405 return 0; 406 } 407 408 /* 409 * Clear ->mk_users. Either ->mk_sem must be held for write, or 'mk' must have 410 * no structural references left. 411 */ 412 static void clear_mk_users(struct fscrypt_master_key *mk) 413 { 414 struct fscrypt_master_key_user *mk_user, *tmp; 415 416 list_for_each_entry_safe(mk_user, tmp, &mk->mk_users, link) 417 unlink_and_free_mk_user(mk_user); 418 } 419 420 /* 421 * Allocate a new fscrypt_master_key, transfer the given secret over to it, and 422 * insert it into sb->s_master_keys. 423 */ 424 static int add_new_master_key(struct super_block *sb, 425 struct fscrypt_master_key_secret *secret, 426 const struct fscrypt_key_specifier *mk_spec) 427 { 428 struct fscrypt_keyring *keyring = sb->s_master_keys; 429 struct fscrypt_master_key *mk; 430 int err; 431 432 mk = kzalloc_obj(*mk); 433 if (!mk) 434 return -ENOMEM; 435 436 init_rwsem(&mk->mk_sem); 437 refcount_set(&mk->mk_struct_refs, 1); 438 mk->mk_spec = *mk_spec; 439 440 INIT_LIST_HEAD(&mk->mk_users); 441 442 INIT_LIST_HEAD(&mk->mk_decrypted_inodes); 443 spin_lock_init(&mk->mk_decrypted_inodes_lock); 444 445 INIT_LIST_HEAD(&mk->mk_mode_keys); 446 447 if (mk_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) { 448 err = add_master_key_user(mk); 449 if (err) 450 goto out_put; 451 } 452 453 move_master_key_secret(&mk->mk_secret, secret); 454 mk->mk_present = true; 455 refcount_set(&mk->mk_active_refs, 1); /* ->mk_present is true */ 456 457 spin_lock(&keyring->lock); 458 hlist_add_head_rcu(&mk->mk_node, 459 fscrypt_mk_hash_bucket(keyring, mk_spec)); 460 spin_unlock(&keyring->lock); 461 return 0; 462 463 out_put: 464 fscrypt_put_master_key(mk); 465 return err; 466 } 467 468 #define KEY_DEAD 1 469 470 static int add_existing_master_key(struct fscrypt_master_key *mk, 471 struct fscrypt_master_key_secret *secret) 472 { 473 int err; 474 475 /* 476 * For v2 policy keys (FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER): If the current 477 * user is already in ->mk_users, then there's nothing to do. 478 * Otherwise, add the user to ->mk_users. 479 */ 480 if (mk->mk_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) { 481 if (find_master_key_user(mk) != NULL) 482 return 0; 483 err = add_master_key_user(mk); 484 if (err) 485 return err; 486 } 487 488 /* If the key is incompletely removed, make it present again. */ 489 if (!mk->mk_present) { 490 if (!refcount_inc_not_zero(&mk->mk_active_refs)) { 491 /* 492 * Raced with the last active ref being dropped, so the 493 * key has become, or is about to become, "absent". 494 * Therefore, we need to allocate a new key struct. 495 */ 496 return KEY_DEAD; 497 } 498 move_master_key_secret(&mk->mk_secret, secret); 499 WRITE_ONCE(mk->mk_present, true); 500 } 501 502 return 0; 503 } 504 505 static int do_add_master_key(struct super_block *sb, 506 struct fscrypt_master_key_secret *secret, 507 const struct fscrypt_key_specifier *mk_spec) 508 { 509 static DEFINE_MUTEX(fscrypt_add_key_mutex); 510 struct fscrypt_master_key *mk; 511 int err; 512 513 guard(mutex)(&fscrypt_add_key_mutex); /* serialize find + link */ 514 515 mk = fscrypt_find_master_key(sb, mk_spec); 516 if (!mk) { 517 /* Didn't find the key in ->s_master_keys. Add it. */ 518 err = allocate_filesystem_keyring(sb); 519 if (!err) 520 err = add_new_master_key(sb, secret, mk_spec); 521 } else { 522 /* 523 * Found the key in ->s_master_keys. Add the user to ->mk_users 524 * if needed, and make the key "present" again if possible. 525 */ 526 down_write(&mk->mk_sem); 527 err = add_existing_master_key(mk, secret); 528 up_write(&mk->mk_sem); 529 if (err == KEY_DEAD) { 530 /* 531 * We found a key struct, but it's already been fully 532 * removed. Ignore the old struct and add a new one. 533 * fscrypt_add_key_mutex means we don't need to worry 534 * about concurrent adds. 535 */ 536 err = add_new_master_key(sb, secret, mk_spec); 537 } 538 fscrypt_put_master_key(mk); 539 } 540 return err; 541 } 542 543 static int add_master_key(struct super_block *sb, 544 struct fscrypt_master_key_secret *secret, 545 struct fscrypt_key_specifier *key_spec) 546 { 547 int err; 548 549 if (key_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) { 550 u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE]; 551 u8 *kdf_key = secret->bytes; 552 unsigned int kdf_key_size = secret->size; 553 u8 keyid_kdf_ctx = HKDF_CONTEXT_KEY_IDENTIFIER_FOR_RAW_KEY; 554 555 /* 556 * For raw keys, the fscrypt master key is used directly as the 557 * fscrypt KDF key. For hardware-wrapped keys, we have to pass 558 * the master key to the hardware to derive the KDF key, which 559 * is then only used to derive non-file-contents subkeys. 560 */ 561 if (secret->is_hw_wrapped) { 562 err = fscrypt_derive_sw_secret(sb, secret->bytes, 563 secret->size, sw_secret); 564 if (err) { 565 memzero_explicit(sw_secret, sizeof(sw_secret)); 566 return err; 567 } 568 kdf_key = sw_secret; 569 kdf_key_size = sizeof(sw_secret); 570 /* 571 * To avoid weird behavior if someone manages to 572 * determine sw_secret and add it as a raw key, ensure 573 * that hardware-wrapped keys and raw keys will have 574 * different key identifiers by deriving their key 575 * identifiers using different KDF contexts. 576 */ 577 keyid_kdf_ctx = 578 HKDF_CONTEXT_KEY_IDENTIFIER_FOR_HW_WRAPPED_KEY; 579 } 580 fscrypt_init_hkdf(&secret->hkdf, kdf_key, kdf_key_size); 581 /* 582 * Now that the KDF context is initialized, the raw KDF key is 583 * no longer needed. 584 */ 585 memzero_explicit(kdf_key, kdf_key_size); 586 587 /* Calculate the key identifier */ 588 fscrypt_hkdf_expand(&secret->hkdf, keyid_kdf_ctx, NULL, 0, 589 key_spec->u.identifier, 590 FSCRYPT_KEY_IDENTIFIER_SIZE); 591 } 592 return do_add_master_key(sb, secret, key_spec); 593 } 594 595 /* 596 * Validate the size of an fscrypt master key being added. Note that this is 597 * just an initial check, as we don't know which ciphers will be used yet. 598 * There is a stricter size check later when the key is actually used by a file. 599 */ 600 static inline bool fscrypt_valid_key_size(size_t size, u32 add_key_flags) 601 { 602 u32 max_size = (add_key_flags & FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED) ? 603 FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE : 604 FSCRYPT_MAX_RAW_KEY_SIZE; 605 606 return size >= FSCRYPT_MIN_KEY_SIZE && size <= max_size; 607 } 608 609 static int fscrypt_provisioning_key_preparse(struct key_preparsed_payload *prep) 610 { 611 const struct fscrypt_provisioning_key_payload *payload = prep->data; 612 613 if (prep->datalen < sizeof(*payload)) 614 return -EINVAL; 615 616 if (!fscrypt_valid_key_size(prep->datalen - sizeof(*payload), 617 payload->flags)) 618 return -EINVAL; 619 620 if (payload->type != FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR && 621 payload->type != FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) 622 return -EINVAL; 623 624 if (payload->flags & ~FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED) 625 return -EINVAL; 626 627 prep->payload.data[0] = kmemdup(payload, prep->datalen, GFP_KERNEL); 628 if (!prep->payload.data[0]) 629 return -ENOMEM; 630 631 prep->quotalen = prep->datalen; 632 return 0; 633 } 634 635 static void fscrypt_provisioning_key_free_preparse( 636 struct key_preparsed_payload *prep) 637 { 638 kfree_sensitive(prep->payload.data[0]); 639 } 640 641 static void fscrypt_provisioning_key_describe(const struct key *key, 642 struct seq_file *m) 643 { 644 seq_puts(m, key->description); 645 if (key_is_positive(key)) { 646 const struct fscrypt_provisioning_key_payload *payload = 647 key->payload.data[0]; 648 649 seq_printf(m, ": %u [%u]", key->datalen, payload->type); 650 } 651 } 652 653 static void fscrypt_provisioning_key_destroy(struct key *key) 654 { 655 kfree_sensitive(key->payload.data[0]); 656 } 657 658 static struct key_type key_type_fscrypt_provisioning = { 659 .name = "fscrypt-provisioning", 660 .preparse = fscrypt_provisioning_key_preparse, 661 .free_preparse = fscrypt_provisioning_key_free_preparse, 662 .instantiate = generic_key_instantiate, 663 .describe = fscrypt_provisioning_key_describe, 664 .destroy = fscrypt_provisioning_key_destroy, 665 }; 666 667 /* 668 * Retrieve the key from the Linux keyring key specified by 'key_id', and store 669 * it into 'secret'. 670 * 671 * The key must be of type "fscrypt-provisioning" and must have the 'type' and 672 * 'flags' field of the payload set to the given values, indicating that the key 673 * is intended for use for the specified purpose. We don't use the "logon" key 674 * type because there's no way to completely restrict the use of such keys; they 675 * can be used by any kernel API that accepts "logon" keys and doesn't require a 676 * specific service prefix. 677 * 678 * The ability to specify the key via Linux keyring key is intended for cases 679 * where userspace needs to re-add keys after the filesystem is unmounted and 680 * re-mounted. Most users should just provide the key directly instead. 681 */ 682 static int get_keyring_key(u32 key_id, u32 type, u32 flags, 683 struct fscrypt_master_key_secret *secret) 684 { 685 key_ref_t ref; 686 struct key *key; 687 const struct fscrypt_provisioning_key_payload *payload; 688 int err; 689 690 ref = lookup_user_key(key_id, 0, KEY_NEED_SEARCH); 691 if (IS_ERR(ref)) 692 return PTR_ERR(ref); 693 key = key_ref_to_ptr(ref); 694 695 if (key->type != &key_type_fscrypt_provisioning) 696 goto bad_key; 697 payload = key->payload.data[0]; 698 699 /* 700 * Don't allow fscrypt v1 keys to be used as v2 keys and vice versa. 701 * Similarly, don't allow hardware-wrapped keys to be used as 702 * non-hardware-wrapped keys and vice versa. 703 */ 704 if (payload->type != type || payload->flags != flags) 705 goto bad_key; 706 707 secret->size = key->datalen - sizeof(*payload); 708 memcpy(secret->bytes, payload->raw, secret->size); 709 err = 0; 710 goto out_put; 711 712 bad_key: 713 err = -EKEYREJECTED; 714 out_put: 715 key_ref_put(ref); 716 return err; 717 } 718 719 /* 720 * Add a master encryption key to the filesystem, causing all files which were 721 * encrypted with it to appear "unlocked" (decrypted) when accessed. 722 * 723 * When adding a key for use by v1 encryption policies, this ioctl is 724 * privileged, and userspace must provide the 'key_descriptor'. 725 * 726 * When adding a key for use by v2+ encryption policies, this ioctl is 727 * unprivileged. This is needed, in general, to allow non-root users to use 728 * encryption without encountering the visibility problems of process-subscribed 729 * keyrings and the inability to properly remove keys. This works by having 730 * each key identified by its cryptographically secure hash --- the 731 * 'key_identifier'. The cryptographic hash ensures that a malicious user 732 * cannot add the wrong key for a given identifier. Furthermore, each added key 733 * is charged to the appropriate user's quota for the keyrings service, which 734 * prevents a malicious user from adding too many keys. Finally, we forbid a 735 * user from removing a key while other users have added it too, which prevents 736 * a user who knows another user's key from causing a denial-of-service by 737 * removing it at an inopportune time. (We tolerate that a user who knows a key 738 * can prevent other users from removing it.) 739 * 740 * For more details, see the "FS_IOC_ADD_ENCRYPTION_KEY" section of 741 * Documentation/filesystems/fscrypt.rst. 742 */ 743 int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg) 744 { 745 struct super_block *sb = file_inode(filp)->i_sb; 746 struct fscrypt_add_key_arg __user *uarg = _uarg; 747 struct fscrypt_add_key_arg arg; 748 struct fscrypt_master_key_secret secret; 749 int err; 750 751 if (copy_from_user(&arg, uarg, sizeof(arg))) 752 return -EFAULT; 753 754 if (!valid_key_spec(&arg.key_spec)) 755 return -EINVAL; 756 757 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved))) 758 return -EINVAL; 759 760 /* 761 * Only root can add keys that are identified by an arbitrary descriptor 762 * rather than by a cryptographic hash --- since otherwise a malicious 763 * user could add the wrong key. 764 */ 765 if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR && 766 !capable(CAP_SYS_ADMIN)) 767 return -EACCES; 768 769 memset(&secret, 0, sizeof(secret)); 770 771 if (arg.flags) { 772 if (arg.flags & ~FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED) 773 return -EINVAL; 774 if (arg.key_spec.type != FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) 775 return -EINVAL; 776 secret.is_hw_wrapped = true; 777 } 778 779 if (arg.key_id) { 780 if (arg.raw_size != 0) 781 return -EINVAL; 782 err = get_keyring_key(arg.key_id, arg.key_spec.type, arg.flags, 783 &secret); 784 if (err) 785 goto out_wipe_secret; 786 } else { 787 if (!fscrypt_valid_key_size(arg.raw_size, arg.flags)) 788 return -EINVAL; 789 secret.size = arg.raw_size; 790 err = -EFAULT; 791 if (copy_from_user(secret.bytes, uarg->raw, secret.size)) 792 goto out_wipe_secret; 793 } 794 795 err = add_master_key(sb, &secret, &arg.key_spec); 796 if (err) 797 goto out_wipe_secret; 798 799 /* Return the key identifier to userspace, if applicable */ 800 err = -EFAULT; 801 if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER && 802 copy_to_user(uarg->key_spec.u.identifier, arg.key_spec.u.identifier, 803 FSCRYPT_KEY_IDENTIFIER_SIZE)) 804 goto out_wipe_secret; 805 err = 0; 806 out_wipe_secret: 807 wipe_master_key_secret(&secret); 808 return err; 809 } 810 EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key); 811 812 static void 813 fscrypt_get_test_dummy_secret(struct fscrypt_master_key_secret *secret) 814 { 815 static u8 test_key[FSCRYPT_MAX_RAW_KEY_SIZE]; 816 817 get_random_once(test_key, sizeof(test_key)); 818 819 memset(secret, 0, sizeof(*secret)); 820 secret->size = sizeof(test_key); 821 memcpy(secret->bytes, test_key, sizeof(test_key)); 822 } 823 824 void fscrypt_get_test_dummy_key_identifier( 825 u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]) 826 { 827 struct fscrypt_master_key_secret secret; 828 829 fscrypt_get_test_dummy_secret(&secret); 830 fscrypt_init_hkdf(&secret.hkdf, secret.bytes, secret.size); 831 fscrypt_hkdf_expand(&secret.hkdf, 832 HKDF_CONTEXT_KEY_IDENTIFIER_FOR_RAW_KEY, NULL, 0, 833 key_identifier, FSCRYPT_KEY_IDENTIFIER_SIZE); 834 wipe_master_key_secret(&secret); 835 } 836 837 /** 838 * fscrypt_add_test_dummy_key() - add the test dummy encryption key 839 * @sb: the filesystem instance to add the key to 840 * @key_spec: the key specifier of the test dummy encryption key 841 * 842 * Add the key for the test_dummy_encryption mount option to the filesystem. To 843 * prevent misuse of this mount option, a per-boot random key is used instead of 844 * a hardcoded one. This makes it so that any encrypted files created using 845 * this option won't be accessible after a reboot. 846 * 847 * Return: 0 on success, -errno on failure 848 */ 849 int fscrypt_add_test_dummy_key(struct super_block *sb, 850 struct fscrypt_key_specifier *key_spec) 851 { 852 struct fscrypt_master_key_secret secret; 853 int err; 854 855 fscrypt_get_test_dummy_secret(&secret); 856 err = add_master_key(sb, &secret, key_spec); 857 wipe_master_key_secret(&secret); 858 return err; 859 } 860 861 /* 862 * Verify that the current user has added a master key with the given identifier 863 * (returns -ENOKEY if not). This is needed to prevent a user from encrypting 864 * their files using some other user's key which they don't actually know. 865 * Cryptographically this isn't much of a problem, but the semantics of this 866 * would be a bit weird, so it's best to just forbid it. 867 * 868 * The system administrator (CAP_FOWNER) can override this, which should be 869 * enough for any use cases where encryption policies are being set using keys 870 * that were chosen ahead of time but aren't available at the moment. 871 * 872 * Note that the key may have already removed by the time this returns, but 873 * that's okay; we just care whether the key was there at some point. 874 * 875 * Return: 0 if the key is added, -ENOKEY if it isn't, or another -errno code 876 */ 877 int fscrypt_verify_key_added(struct super_block *sb, 878 const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]) 879 { 880 struct fscrypt_key_specifier mk_spec; 881 struct fscrypt_master_key *mk; 882 int err; 883 884 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; 885 memcpy(mk_spec.u.identifier, identifier, FSCRYPT_KEY_IDENTIFIER_SIZE); 886 887 mk = fscrypt_find_master_key(sb, &mk_spec); 888 if (!mk) { 889 err = -ENOKEY; 890 goto out; 891 } 892 down_read(&mk->mk_sem); 893 if (find_master_key_user(mk) != NULL) 894 err = 0; 895 else 896 err = -ENOKEY; 897 up_read(&mk->mk_sem); 898 fscrypt_put_master_key(mk); 899 out: 900 if (err == -ENOKEY && capable(CAP_FOWNER)) 901 err = 0; 902 return err; 903 } 904 905 /* 906 * Try to evict the inode's dentries from the dentry cache. If the inode is a 907 * directory, then it can have at most one dentry; however, that dentry may be 908 * pinned by child dentries, so first try to evict the children too. 909 */ 910 static void shrink_dcache_inode(struct inode *inode) 911 { 912 struct dentry *dentry; 913 914 if (S_ISDIR(inode->i_mode)) { 915 dentry = d_find_any_alias(inode); 916 if (dentry) { 917 shrink_dcache_parent(dentry); 918 dput(dentry); 919 } 920 } 921 d_prune_aliases(inode); 922 } 923 924 static void evict_dentries_for_decrypted_inodes(struct fscrypt_master_key *mk) 925 { 926 struct fscrypt_inode_info *ci; 927 struct inode *inode; 928 struct inode *toput_inode = NULL; 929 930 spin_lock(&mk->mk_decrypted_inodes_lock); 931 932 list_for_each_entry(ci, &mk->mk_decrypted_inodes, ci_master_key_link) { 933 inode = ci->ci_inode; 934 spin_lock(&inode->i_lock); 935 if (inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW)) { 936 spin_unlock(&inode->i_lock); 937 continue; 938 } 939 __iget(inode); 940 spin_unlock(&inode->i_lock); 941 spin_unlock(&mk->mk_decrypted_inodes_lock); 942 943 shrink_dcache_inode(inode); 944 iput(toput_inode); 945 toput_inode = inode; 946 947 spin_lock(&mk->mk_decrypted_inodes_lock); 948 } 949 950 spin_unlock(&mk->mk_decrypted_inodes_lock); 951 iput(toput_inode); 952 } 953 954 static int check_for_busy_inodes(struct super_block *sb, 955 struct fscrypt_master_key *mk) 956 { 957 struct list_head *pos; 958 size_t busy_count = 0; 959 char ino_str[50] = ""; 960 u64 ino; 961 962 spin_lock(&mk->mk_decrypted_inodes_lock); 963 964 list_for_each(pos, &mk->mk_decrypted_inodes) 965 busy_count++; 966 967 if (busy_count == 0) { 968 spin_unlock(&mk->mk_decrypted_inodes_lock); 969 return 0; 970 } 971 972 { 973 /* select an example file to show for debugging purposes */ 974 struct inode *inode = 975 list_first_entry(&mk->mk_decrypted_inodes, 976 struct fscrypt_inode_info, 977 ci_master_key_link)->ci_inode; 978 ino = inode->i_ino; 979 } 980 spin_unlock(&mk->mk_decrypted_inodes_lock); 981 982 /* If the inode is currently being created, ino may still be 0. */ 983 if (ino) 984 snprintf(ino_str, sizeof(ino_str), ", including ino %llu", ino); 985 986 fscrypt_warn(NULL, 987 "%s: %zu inode(s) still busy after removing key with %s %*phN%s", 988 sb->s_id, busy_count, master_key_spec_type(&mk->mk_spec), 989 master_key_spec_len(&mk->mk_spec), (u8 *)&mk->mk_spec.u, 990 ino_str); 991 return -EBUSY; 992 } 993 994 static int try_to_lock_encrypted_files(struct super_block *sb, 995 struct fscrypt_master_key *mk) 996 { 997 int err1; 998 int err2; 999 1000 /* 1001 * An inode can't be evicted while it is dirty or has dirty pages. 1002 * Thus, we first have to clean the inodes in ->mk_decrypted_inodes. 1003 * 1004 * Just do it the easy way: call sync_filesystem(). It's overkill, but 1005 * it works, and it's more important to minimize the amount of caches we 1006 * drop than the amount of data we sync. Also, unprivileged users can 1007 * already call sync_filesystem() via sys_syncfs() or sys_sync(). 1008 */ 1009 down_read(&sb->s_umount); 1010 err1 = sync_filesystem(sb); 1011 up_read(&sb->s_umount); 1012 /* If a sync error occurs, still try to evict as much as possible. */ 1013 1014 /* 1015 * Inodes are pinned by their dentries, so we have to evict their 1016 * dentries. shrink_dcache_sb() would suffice, but would be overkill 1017 * and inappropriate for use by unprivileged users. So instead go 1018 * through the inodes' alias lists and try to evict each dentry. 1019 */ 1020 evict_dentries_for_decrypted_inodes(mk); 1021 1022 /* 1023 * evict_dentries_for_decrypted_inodes() already iput() each inode in 1024 * the list; any inodes for which that dropped the last reference will 1025 * have been evicted due to fscrypt_drop_inode() detecting the key 1026 * removal and telling the VFS to evict the inode. So to finish, we 1027 * just need to check whether any inodes couldn't be evicted. 1028 */ 1029 err2 = check_for_busy_inodes(sb, mk); 1030 1031 return err1 ?: err2; 1032 } 1033 1034 /* 1035 * Try to remove an fscrypt master encryption key. 1036 * 1037 * FS_IOC_REMOVE_ENCRYPTION_KEY (all_users=false) removes the current user's 1038 * claim to the key, then removes the key itself if no other users have claims. 1039 * FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS (all_users=true) always removes the 1040 * key itself. 1041 * 1042 * To "remove the key itself", first we transition the key to the "incompletely 1043 * removed" state, so that no more inodes can be unlocked with it. Then we try 1044 * to evict all cached inodes that had been unlocked with the key. 1045 * 1046 * If all inodes were evicted, then we unlink the fscrypt_master_key from the 1047 * keyring. Otherwise it remains in the keyring in the "incompletely removed" 1048 * state where it tracks the list of remaining inodes. Userspace can execute 1049 * the ioctl again later to retry eviction, or alternatively can re-add the key. 1050 * 1051 * For more details, see the "Removing keys" section of 1052 * Documentation/filesystems/fscrypt.rst. 1053 */ 1054 static int do_remove_key(struct file *filp, void __user *_uarg, bool all_users) 1055 { 1056 struct super_block *sb = file_inode(filp)->i_sb; 1057 struct fscrypt_remove_key_arg __user *uarg = _uarg; 1058 struct fscrypt_remove_key_arg arg; 1059 struct fscrypt_master_key *mk; 1060 u32 status_flags = 0; 1061 int err; 1062 bool inodes_remain; 1063 1064 if (copy_from_user(&arg, uarg, sizeof(arg))) 1065 return -EFAULT; 1066 1067 if (!valid_key_spec(&arg.key_spec)) 1068 return -EINVAL; 1069 1070 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved))) 1071 return -EINVAL; 1072 1073 /* 1074 * Only root can add and remove keys that are identified by an arbitrary 1075 * descriptor rather than by a cryptographic hash. 1076 */ 1077 if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR && 1078 !capable(CAP_SYS_ADMIN)) 1079 return -EACCES; 1080 1081 /* Find the key being removed. */ 1082 mk = fscrypt_find_master_key(sb, &arg.key_spec); 1083 if (!mk) 1084 return -ENOKEY; 1085 down_write(&mk->mk_sem); 1086 1087 /* If relevant, remove current user's (or all users) claim to the key */ 1088 if (!list_empty(&mk->mk_users)) { 1089 if (all_users) { 1090 clear_mk_users(mk); 1091 err = 0; 1092 } else { 1093 err = remove_master_key_user(mk); 1094 } 1095 if (err) { 1096 up_write(&mk->mk_sem); 1097 goto out_put_key; 1098 } 1099 if (!list_empty(&mk->mk_users)) { 1100 /* 1101 * Other users have still added the key too. We removed 1102 * the current user's claim to the key, but we still 1103 * can't remove the key itself. 1104 */ 1105 status_flags |= 1106 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS; 1107 err = 0; 1108 up_write(&mk->mk_sem); 1109 goto out_put_key; 1110 } 1111 } 1112 1113 /* No user claims remaining. Initiate removal of the key. */ 1114 err = -ENOKEY; 1115 if (mk->mk_present) { 1116 fscrypt_initiate_key_removal(sb, mk); 1117 err = 0; 1118 } 1119 inodes_remain = refcount_read(&mk->mk_active_refs) > 0; 1120 up_write(&mk->mk_sem); 1121 1122 if (inodes_remain) { 1123 /* Some inodes still reference this key; try to evict them. */ 1124 err = try_to_lock_encrypted_files(sb, mk); 1125 if (err == -EBUSY) { 1126 status_flags |= 1127 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY; 1128 err = 0; 1129 } 1130 } 1131 /* 1132 * We return 0 if we successfully did something: removed a claim to the 1133 * key, initiated removal of the key, or tried locking the files again. 1134 * Users need to check the informational status flags if they care 1135 * whether the key has been fully removed including all files locked. 1136 */ 1137 out_put_key: 1138 fscrypt_put_master_key(mk); 1139 if (err == 0) 1140 err = put_user(status_flags, &uarg->removal_status_flags); 1141 return err; 1142 } 1143 1144 int fscrypt_ioctl_remove_key(struct file *filp, void __user *uarg) 1145 { 1146 return do_remove_key(filp, uarg, false); 1147 } 1148 EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key); 1149 1150 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *uarg) 1151 { 1152 if (!capable(CAP_SYS_ADMIN)) 1153 return -EACCES; 1154 return do_remove_key(filp, uarg, true); 1155 } 1156 EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key_all_users); 1157 1158 /* 1159 * Retrieve the status of an fscrypt master encryption key. 1160 * 1161 * We set ->status to indicate whether the key is absent, present, or 1162 * incompletely removed. (For an explanation of what these statuses mean and 1163 * how they are represented internally, see struct fscrypt_master_key.) This 1164 * field allows applications to easily determine the status of an encrypted 1165 * directory without using a hack such as trying to open a regular file in it 1166 * (which can confuse the "incompletely removed" status with absent or present). 1167 * 1168 * In addition, for v2 policy keys we allow applications to determine, via 1169 * ->status_flags and ->user_count, whether the key has been added by the 1170 * current user, by other users, or by both. Most applications should not need 1171 * this, since ordinarily only one user should know a given key. However, if a 1172 * secret key is shared by multiple users, applications may wish to add an 1173 * already-present key to prevent other users from removing it. This ioctl can 1174 * be used to check whether that really is the case before the work is done to 1175 * add the key --- which might e.g. require prompting the user for a passphrase. 1176 * 1177 * For more details, see the "FS_IOC_GET_ENCRYPTION_KEY_STATUS" section of 1178 * Documentation/filesystems/fscrypt.rst. 1179 */ 1180 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *uarg) 1181 { 1182 struct super_block *sb = file_inode(filp)->i_sb; 1183 struct fscrypt_get_key_status_arg arg; 1184 struct fscrypt_master_key *mk; 1185 kuid_t uid; 1186 const struct fscrypt_master_key_user *mk_user; 1187 int err; 1188 1189 if (copy_from_user(&arg, uarg, sizeof(arg))) 1190 return -EFAULT; 1191 1192 if (!valid_key_spec(&arg.key_spec)) 1193 return -EINVAL; 1194 1195 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved))) 1196 return -EINVAL; 1197 1198 arg.status_flags = 0; 1199 arg.user_count = 0; 1200 memset(arg.__out_reserved, 0, sizeof(arg.__out_reserved)); 1201 1202 mk = fscrypt_find_master_key(sb, &arg.key_spec); 1203 if (!mk) { 1204 arg.status = FSCRYPT_KEY_STATUS_ABSENT; 1205 err = 0; 1206 goto out; 1207 } 1208 down_read(&mk->mk_sem); 1209 1210 if (!mk->mk_present) { 1211 arg.status = refcount_read(&mk->mk_active_refs) > 0 ? 1212 FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED : 1213 FSCRYPT_KEY_STATUS_ABSENT /* raced with full removal */; 1214 err = 0; 1215 goto out_release_key; 1216 } 1217 1218 arg.status = FSCRYPT_KEY_STATUS_PRESENT; 1219 1220 uid = current_fsuid(); 1221 list_for_each_entry(mk_user, &mk->mk_users, link) { 1222 arg.user_count++; 1223 if (uid_eq(mk_user->uid, uid)) 1224 arg.status_flags |= 1225 FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF; 1226 } 1227 err = 0; 1228 out_release_key: 1229 up_read(&mk->mk_sem); 1230 fscrypt_put_master_key(mk); 1231 out: 1232 if (!err && copy_to_user(uarg, &arg, sizeof(arg))) 1233 err = -EFAULT; 1234 return err; 1235 } 1236 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_key_status); 1237 1238 void __init fscrypt_init_keyring(void) 1239 { 1240 int err; 1241 1242 /* 1243 * Note that register_key_type() fails only if a key type with the same 1244 * name already exists, which should never happen here. 1245 */ 1246 err = register_key_type(&key_type_fscrypt_user); 1247 if (err) 1248 panic("failed to register .fscrypt key type (%d)", err); 1249 err = register_key_type(&key_type_fscrypt_provisioning); 1250 if (err) 1251 panic("failed to register fscrypt-provisioning key type (%d)", 1252 err); 1253 } 1254