1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Key setup facility for FS encryption support. 4 * 5 * Copyright (C) 2015, Google, Inc. 6 * 7 * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar. 8 * Heavily modified since then. 9 */ 10 11 #include <crypto/skcipher.h> 12 #include <linux/key.h> 13 14 #include "fscrypt_private.h" 15 16 struct fscrypt_mode fscrypt_modes[] = { 17 [FSCRYPT_MODE_AES_256_XTS] = { 18 .friendly_name = "AES-256-XTS", 19 .cipher_str = "xts(aes)", 20 .keysize = 64, 21 .ivsize = 16, 22 }, 23 [FSCRYPT_MODE_AES_256_CTS] = { 24 .friendly_name = "AES-256-CTS-CBC", 25 .cipher_str = "cts(cbc(aes))", 26 .keysize = 32, 27 .ivsize = 16, 28 }, 29 [FSCRYPT_MODE_AES_128_CBC] = { 30 .friendly_name = "AES-128-CBC-ESSIV", 31 .cipher_str = "essiv(cbc(aes),sha256)", 32 .keysize = 16, 33 .ivsize = 16, 34 }, 35 [FSCRYPT_MODE_AES_128_CTS] = { 36 .friendly_name = "AES-128-CTS-CBC", 37 .cipher_str = "cts(cbc(aes))", 38 .keysize = 16, 39 .ivsize = 16, 40 }, 41 [FSCRYPT_MODE_ADIANTUM] = { 42 .friendly_name = "Adiantum", 43 .cipher_str = "adiantum(xchacha12,aes)", 44 .keysize = 32, 45 .ivsize = 32, 46 }, 47 }; 48 49 static DEFINE_MUTEX(fscrypt_mode_key_setup_mutex); 50 51 static struct fscrypt_mode * 52 select_encryption_mode(const union fscrypt_policy *policy, 53 const struct inode *inode) 54 { 55 if (S_ISREG(inode->i_mode)) 56 return &fscrypt_modes[fscrypt_policy_contents_mode(policy)]; 57 58 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) 59 return &fscrypt_modes[fscrypt_policy_fnames_mode(policy)]; 60 61 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n", 62 inode->i_ino, (inode->i_mode & S_IFMT)); 63 return ERR_PTR(-EINVAL); 64 } 65 66 /* Create a symmetric cipher object for the given encryption mode and key */ 67 struct crypto_skcipher *fscrypt_allocate_skcipher(struct fscrypt_mode *mode, 68 const u8 *raw_key, 69 const struct inode *inode) 70 { 71 struct crypto_skcipher *tfm; 72 int err; 73 74 tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0); 75 if (IS_ERR(tfm)) { 76 if (PTR_ERR(tfm) == -ENOENT) { 77 fscrypt_warn(inode, 78 "Missing crypto API support for %s (API name: \"%s\")", 79 mode->friendly_name, mode->cipher_str); 80 return ERR_PTR(-ENOPKG); 81 } 82 fscrypt_err(inode, "Error allocating '%s' transform: %ld", 83 mode->cipher_str, PTR_ERR(tfm)); 84 return tfm; 85 } 86 if (!xchg(&mode->logged_impl_name, 1)) { 87 /* 88 * fscrypt performance can vary greatly depending on which 89 * crypto algorithm implementation is used. Help people debug 90 * performance problems by logging the ->cra_driver_name the 91 * first time a mode is used. 92 */ 93 pr_info("fscrypt: %s using implementation \"%s\"\n", 94 mode->friendly_name, crypto_skcipher_driver_name(tfm)); 95 } 96 if (WARN_ON(crypto_skcipher_ivsize(tfm) != mode->ivsize)) { 97 err = -EINVAL; 98 goto err_free_tfm; 99 } 100 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); 101 err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize); 102 if (err) 103 goto err_free_tfm; 104 105 return tfm; 106 107 err_free_tfm: 108 crypto_free_skcipher(tfm); 109 return ERR_PTR(err); 110 } 111 112 /* Given a per-file encryption key, set up the file's crypto transform object */ 113 int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key) 114 { 115 struct crypto_skcipher *tfm; 116 117 tfm = fscrypt_allocate_skcipher(ci->ci_mode, raw_key, ci->ci_inode); 118 if (IS_ERR(tfm)) 119 return PTR_ERR(tfm); 120 121 ci->ci_ctfm = tfm; 122 ci->ci_owns_key = true; 123 return 0; 124 } 125 126 static int setup_per_mode_enc_key(struct fscrypt_info *ci, 127 struct fscrypt_master_key *mk, 128 struct crypto_skcipher **tfms, 129 u8 hkdf_context, bool include_fs_uuid) 130 { 131 const struct inode *inode = ci->ci_inode; 132 const struct super_block *sb = inode->i_sb; 133 struct fscrypt_mode *mode = ci->ci_mode; 134 const u8 mode_num = mode - fscrypt_modes; 135 struct crypto_skcipher *tfm; 136 u8 mode_key[FSCRYPT_MAX_KEY_SIZE]; 137 u8 hkdf_info[sizeof(mode_num) + sizeof(sb->s_uuid)]; 138 unsigned int hkdf_infolen = 0; 139 int err; 140 141 if (WARN_ON(mode_num > __FSCRYPT_MODE_MAX)) 142 return -EINVAL; 143 144 /* pairs with smp_store_release() below */ 145 tfm = READ_ONCE(tfms[mode_num]); 146 if (likely(tfm != NULL)) { 147 ci->ci_ctfm = tfm; 148 return 0; 149 } 150 151 mutex_lock(&fscrypt_mode_key_setup_mutex); 152 153 if (tfms[mode_num]) 154 goto done_unlock; 155 156 BUILD_BUG_ON(sizeof(mode_num) != 1); 157 BUILD_BUG_ON(sizeof(sb->s_uuid) != 16); 158 BUILD_BUG_ON(sizeof(hkdf_info) != 17); 159 hkdf_info[hkdf_infolen++] = mode_num; 160 if (include_fs_uuid) { 161 memcpy(&hkdf_info[hkdf_infolen], &sb->s_uuid, 162 sizeof(sb->s_uuid)); 163 hkdf_infolen += sizeof(sb->s_uuid); 164 } 165 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, 166 hkdf_context, hkdf_info, hkdf_infolen, 167 mode_key, mode->keysize); 168 if (err) 169 goto out_unlock; 170 tfm = fscrypt_allocate_skcipher(mode, mode_key, inode); 171 memzero_explicit(mode_key, mode->keysize); 172 if (IS_ERR(tfm)) { 173 err = PTR_ERR(tfm); 174 goto out_unlock; 175 } 176 /* pairs with READ_ONCE() above */ 177 smp_store_release(&tfms[mode_num], tfm); 178 done_unlock: 179 ci->ci_ctfm = tfm; 180 err = 0; 181 out_unlock: 182 mutex_unlock(&fscrypt_mode_key_setup_mutex); 183 return err; 184 } 185 186 int fscrypt_derive_dirhash_key(struct fscrypt_info *ci, 187 const struct fscrypt_master_key *mk) 188 { 189 int err; 190 191 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, HKDF_CONTEXT_DIRHASH_KEY, 192 ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE, 193 (u8 *)&ci->ci_dirhash_key, 194 sizeof(ci->ci_dirhash_key)); 195 if (err) 196 return err; 197 ci->ci_dirhash_key_initialized = true; 198 return 0; 199 } 200 201 static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci, 202 struct fscrypt_master_key *mk) 203 { 204 int err; 205 206 err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_32_keys, 207 HKDF_CONTEXT_IV_INO_LBLK_32_KEY, true); 208 if (err) 209 return err; 210 211 /* pairs with smp_store_release() below */ 212 if (!smp_load_acquire(&mk->mk_ino_hash_key_initialized)) { 213 214 mutex_lock(&fscrypt_mode_key_setup_mutex); 215 216 if (mk->mk_ino_hash_key_initialized) 217 goto unlock; 218 219 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, 220 HKDF_CONTEXT_INODE_HASH_KEY, NULL, 0, 221 (u8 *)&mk->mk_ino_hash_key, 222 sizeof(mk->mk_ino_hash_key)); 223 if (err) 224 goto unlock; 225 /* pairs with smp_load_acquire() above */ 226 smp_store_release(&mk->mk_ino_hash_key_initialized, true); 227 unlock: 228 mutex_unlock(&fscrypt_mode_key_setup_mutex); 229 if (err) 230 return err; 231 } 232 233 ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino, 234 &mk->mk_ino_hash_key); 235 return 0; 236 } 237 238 static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci, 239 struct fscrypt_master_key *mk) 240 { 241 int err; 242 243 if (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) { 244 /* 245 * DIRECT_KEY: instead of deriving per-file encryption keys, the 246 * per-file nonce will be included in all the IVs. But unlike 247 * v1 policies, for v2 policies in this case we don't encrypt 248 * with the master key directly but rather derive a per-mode 249 * encryption key. This ensures that the master key is 250 * consistently used only for HKDF, avoiding key reuse issues. 251 */ 252 err = setup_per_mode_enc_key(ci, mk, mk->mk_direct_keys, 253 HKDF_CONTEXT_DIRECT_KEY, false); 254 } else if (ci->ci_policy.v2.flags & 255 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) { 256 /* 257 * IV_INO_LBLK_64: encryption keys are derived from (master_key, 258 * mode_num, filesystem_uuid), and inode number is included in 259 * the IVs. This format is optimized for use with inline 260 * encryption hardware compliant with the UFS standard. 261 */ 262 err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_64_keys, 263 HKDF_CONTEXT_IV_INO_LBLK_64_KEY, 264 true); 265 } else if (ci->ci_policy.v2.flags & 266 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) { 267 err = fscrypt_setup_iv_ino_lblk_32_key(ci, mk); 268 } else { 269 u8 derived_key[FSCRYPT_MAX_KEY_SIZE]; 270 271 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, 272 HKDF_CONTEXT_PER_FILE_ENC_KEY, 273 ci->ci_nonce, 274 FS_KEY_DERIVATION_NONCE_SIZE, 275 derived_key, ci->ci_mode->keysize); 276 if (err) 277 return err; 278 279 err = fscrypt_set_per_file_enc_key(ci, derived_key); 280 memzero_explicit(derived_key, ci->ci_mode->keysize); 281 } 282 if (err) 283 return err; 284 285 /* Derive a secret dirhash key for directories that need it. */ 286 if (S_ISDIR(ci->ci_inode->i_mode) && IS_CASEFOLDED(ci->ci_inode)) { 287 err = fscrypt_derive_dirhash_key(ci, mk); 288 if (err) 289 return err; 290 } 291 292 return 0; 293 } 294 295 /* 296 * Find the master key, then set up the inode's actual encryption key. 297 * 298 * If the master key is found in the filesystem-level keyring, then the 299 * corresponding 'struct key' is returned in *master_key_ret with 300 * ->mk_secret_sem read-locked. This is needed to ensure that only one task 301 * links the fscrypt_info into ->mk_decrypted_inodes (as multiple tasks may race 302 * to create an fscrypt_info for the same inode), and to synchronize the master 303 * key being removed with a new inode starting to use it. 304 */ 305 static int setup_file_encryption_key(struct fscrypt_info *ci, 306 struct key **master_key_ret) 307 { 308 struct key *key; 309 struct fscrypt_master_key *mk = NULL; 310 struct fscrypt_key_specifier mk_spec; 311 int err; 312 313 switch (ci->ci_policy.version) { 314 case FSCRYPT_POLICY_V1: 315 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR; 316 memcpy(mk_spec.u.descriptor, 317 ci->ci_policy.v1.master_key_descriptor, 318 FSCRYPT_KEY_DESCRIPTOR_SIZE); 319 break; 320 case FSCRYPT_POLICY_V2: 321 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; 322 memcpy(mk_spec.u.identifier, 323 ci->ci_policy.v2.master_key_identifier, 324 FSCRYPT_KEY_IDENTIFIER_SIZE); 325 break; 326 default: 327 WARN_ON(1); 328 return -EINVAL; 329 } 330 331 key = fscrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec); 332 if (IS_ERR(key)) { 333 if (key != ERR_PTR(-ENOKEY) || 334 ci->ci_policy.version != FSCRYPT_POLICY_V1) 335 return PTR_ERR(key); 336 337 /* 338 * As a legacy fallback for v1 policies, search for the key in 339 * the current task's subscribed keyrings too. Don't move this 340 * to before the search of ->s_master_keys, since users 341 * shouldn't be able to override filesystem-level keys. 342 */ 343 return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci); 344 } 345 346 mk = key->payload.data[0]; 347 down_read(&mk->mk_secret_sem); 348 349 /* Has the secret been removed (via FS_IOC_REMOVE_ENCRYPTION_KEY)? */ 350 if (!is_master_key_secret_present(&mk->mk_secret)) { 351 err = -ENOKEY; 352 goto out_release_key; 353 } 354 355 /* 356 * Require that the master key be at least as long as the derived key. 357 * Otherwise, the derived key cannot possibly contain as much entropy as 358 * that required by the encryption mode it will be used for. For v1 359 * policies it's also required for the KDF to work at all. 360 */ 361 if (mk->mk_secret.size < ci->ci_mode->keysize) { 362 fscrypt_warn(NULL, 363 "key with %s %*phN is too short (got %u bytes, need %u+ bytes)", 364 master_key_spec_type(&mk_spec), 365 master_key_spec_len(&mk_spec), (u8 *)&mk_spec.u, 366 mk->mk_secret.size, ci->ci_mode->keysize); 367 err = -ENOKEY; 368 goto out_release_key; 369 } 370 371 switch (ci->ci_policy.version) { 372 case FSCRYPT_POLICY_V1: 373 err = fscrypt_setup_v1_file_key(ci, mk->mk_secret.raw); 374 break; 375 case FSCRYPT_POLICY_V2: 376 err = fscrypt_setup_v2_file_key(ci, mk); 377 break; 378 default: 379 WARN_ON(1); 380 err = -EINVAL; 381 break; 382 } 383 if (err) 384 goto out_release_key; 385 386 *master_key_ret = key; 387 return 0; 388 389 out_release_key: 390 up_read(&mk->mk_secret_sem); 391 key_put(key); 392 return err; 393 } 394 395 static void put_crypt_info(struct fscrypt_info *ci) 396 { 397 struct key *key; 398 399 if (!ci) 400 return; 401 402 if (ci->ci_direct_key) 403 fscrypt_put_direct_key(ci->ci_direct_key); 404 else if (ci->ci_owns_key) 405 crypto_free_skcipher(ci->ci_ctfm); 406 407 key = ci->ci_master_key; 408 if (key) { 409 struct fscrypt_master_key *mk = key->payload.data[0]; 410 411 /* 412 * Remove this inode from the list of inodes that were unlocked 413 * with the master key. 414 * 415 * In addition, if we're removing the last inode from a key that 416 * already had its secret removed, invalidate the key so that it 417 * gets removed from ->s_master_keys. 418 */ 419 spin_lock(&mk->mk_decrypted_inodes_lock); 420 list_del(&ci->ci_master_key_link); 421 spin_unlock(&mk->mk_decrypted_inodes_lock); 422 if (refcount_dec_and_test(&mk->mk_refcount)) 423 key_invalidate(key); 424 key_put(key); 425 } 426 memzero_explicit(ci, sizeof(*ci)); 427 kmem_cache_free(fscrypt_info_cachep, ci); 428 } 429 430 int fscrypt_get_encryption_info(struct inode *inode) 431 { 432 struct fscrypt_info *crypt_info; 433 union fscrypt_context ctx; 434 struct fscrypt_mode *mode; 435 struct key *master_key = NULL; 436 int res; 437 438 if (fscrypt_has_encryption_key(inode)) 439 return 0; 440 441 res = fscrypt_initialize(inode->i_sb->s_cop->flags); 442 if (res) 443 return res; 444 445 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); 446 if (res < 0) { 447 const union fscrypt_context *dummy_ctx = 448 fscrypt_get_dummy_context(inode->i_sb); 449 450 if (IS_ENCRYPTED(inode) || !dummy_ctx) { 451 fscrypt_warn(inode, 452 "Error %d getting encryption context", 453 res); 454 return res; 455 } 456 /* Fake up a context for an unencrypted directory */ 457 res = fscrypt_context_size(dummy_ctx); 458 memcpy(&ctx, dummy_ctx, res); 459 } 460 461 crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS); 462 if (!crypt_info) 463 return -ENOMEM; 464 465 crypt_info->ci_inode = inode; 466 467 res = fscrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res); 468 if (res) { 469 fscrypt_warn(inode, 470 "Unrecognized or corrupt encryption context"); 471 goto out; 472 } 473 474 memcpy(crypt_info->ci_nonce, fscrypt_context_nonce(&ctx), 475 FS_KEY_DERIVATION_NONCE_SIZE); 476 477 if (!fscrypt_supported_policy(&crypt_info->ci_policy, inode)) { 478 res = -EINVAL; 479 goto out; 480 } 481 482 mode = select_encryption_mode(&crypt_info->ci_policy, inode); 483 if (IS_ERR(mode)) { 484 res = PTR_ERR(mode); 485 goto out; 486 } 487 WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE); 488 crypt_info->ci_mode = mode; 489 490 res = setup_file_encryption_key(crypt_info, &master_key); 491 if (res) 492 goto out; 493 494 if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) { 495 if (master_key) { 496 struct fscrypt_master_key *mk = 497 master_key->payload.data[0]; 498 499 refcount_inc(&mk->mk_refcount); 500 crypt_info->ci_master_key = key_get(master_key); 501 spin_lock(&mk->mk_decrypted_inodes_lock); 502 list_add(&crypt_info->ci_master_key_link, 503 &mk->mk_decrypted_inodes); 504 spin_unlock(&mk->mk_decrypted_inodes_lock); 505 } 506 crypt_info = NULL; 507 } 508 res = 0; 509 out: 510 if (master_key) { 511 struct fscrypt_master_key *mk = master_key->payload.data[0]; 512 513 up_read(&mk->mk_secret_sem); 514 key_put(master_key); 515 } 516 if (res == -ENOKEY) 517 res = 0; 518 put_crypt_info(crypt_info); 519 return res; 520 } 521 EXPORT_SYMBOL(fscrypt_get_encryption_info); 522 523 /** 524 * fscrypt_put_encryption_info() - free most of an inode's fscrypt data 525 * @inode: an inode being evicted 526 * 527 * Free the inode's fscrypt_info. Filesystems must call this when the inode is 528 * being evicted. An RCU grace period need not have elapsed yet. 529 */ 530 void fscrypt_put_encryption_info(struct inode *inode) 531 { 532 put_crypt_info(inode->i_crypt_info); 533 inode->i_crypt_info = NULL; 534 } 535 EXPORT_SYMBOL(fscrypt_put_encryption_info); 536 537 /** 538 * fscrypt_free_inode() - free an inode's fscrypt data requiring RCU delay 539 * @inode: an inode being freed 540 * 541 * Free the inode's cached decrypted symlink target, if any. Filesystems must 542 * call this after an RCU grace period, just before they free the inode. 543 */ 544 void fscrypt_free_inode(struct inode *inode) 545 { 546 if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) { 547 kfree(inode->i_link); 548 inode->i_link = NULL; 549 } 550 } 551 EXPORT_SYMBOL(fscrypt_free_inode); 552 553 /** 554 * fscrypt_drop_inode() - check whether the inode's master key has been removed 555 * @inode: an inode being considered for eviction 556 * 557 * Filesystems supporting fscrypt must call this from their ->drop_inode() 558 * method so that encrypted inodes are evicted as soon as they're no longer in 559 * use and their master key has been removed. 560 * 561 * Return: 1 if fscrypt wants the inode to be evicted now, otherwise 0 562 */ 563 int fscrypt_drop_inode(struct inode *inode) 564 { 565 const struct fscrypt_info *ci = READ_ONCE(inode->i_crypt_info); 566 const struct fscrypt_master_key *mk; 567 568 /* 569 * If ci is NULL, then the inode doesn't have an encryption key set up 570 * so it's irrelevant. If ci_master_key is NULL, then the master key 571 * was provided via the legacy mechanism of the process-subscribed 572 * keyrings, so we don't know whether it's been removed or not. 573 */ 574 if (!ci || !ci->ci_master_key) 575 return 0; 576 mk = ci->ci_master_key->payload.data[0]; 577 578 /* 579 * With proper, non-racy use of FS_IOC_REMOVE_ENCRYPTION_KEY, all inodes 580 * protected by the key were cleaned by sync_filesystem(). But if 581 * userspace is still using the files, inodes can be dirtied between 582 * then and now. We mustn't lose any writes, so skip dirty inodes here. 583 */ 584 if (inode->i_state & I_DIRTY_ALL) 585 return 0; 586 587 /* 588 * Note: since we aren't holding ->mk_secret_sem, the result here can 589 * immediately become outdated. But there's no correctness problem with 590 * unnecessarily evicting. Nor is there a correctness problem with not 591 * evicting while iput() is racing with the key being removed, since 592 * then the thread removing the key will either evict the inode itself 593 * or will correctly detect that it wasn't evicted due to the race. 594 */ 595 return !is_master_key_secret_present(&mk->mk_secret); 596 } 597 EXPORT_SYMBOL_GPL(fscrypt_drop_inode); 598