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