1// SPDX-License-Identifier: GPL-2.0 2/* 3 * Encryption policy functions for per-file encryption support. 4 * 5 * Copyright (C) 2015, Google, Inc. 6 * Copyright (C) 2015, Motorola Mobility. 7 * 8 * Originally written by Michael Halcrow, 2015. 9 * Modified by Jaegeuk Kim, 2015. 10 * Modified by Eric Biggers, 2019 for v2 policy support. 11 */ 12 13#include <linux/random.h> 14#include <linux/seq_file.h> 15#include <linux/string.h> 16#include <linux/mount.h> 17#include "fscrypt_private.h" 18 19/** 20 * fscrypt_policies_equal() - check whether two encryption policies are the same 21 * @policy1: the first policy 22 * @policy2: the second policy 23 * 24 * Return: %true if equal, else %false 25 */ 26bool fscrypt_policies_equal(const union fscrypt_policy *policy1, 27 const union fscrypt_policy *policy2) 28{ 29 if (policy1->version != policy2->version) 30 return false; 31 32 return !memcmp(policy1, policy2, fscrypt_policy_size(policy1)); 33} 34 35static const union fscrypt_policy * 36fscrypt_get_dummy_policy(struct super_block *sb) 37{ 38 if (!sb->s_cop->get_dummy_policy) 39 return NULL; 40 return sb->s_cop->get_dummy_policy(sb); 41} 42 43static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode) 44{ 45 if (contents_mode == FSCRYPT_MODE_AES_256_XTS && 46 filenames_mode == FSCRYPT_MODE_AES_256_CTS) 47 return true; 48 49 if (contents_mode == FSCRYPT_MODE_AES_128_CBC && 50 filenames_mode == FSCRYPT_MODE_AES_128_CTS) 51 return true; 52 53 if (contents_mode == FSCRYPT_MODE_ADIANTUM && 54 filenames_mode == FSCRYPT_MODE_ADIANTUM) 55 return true; 56 57 return false; 58} 59 60static bool supported_direct_key_modes(const struct inode *inode, 61 u32 contents_mode, u32 filenames_mode) 62{ 63 const struct fscrypt_mode *mode; 64 65 if (contents_mode != filenames_mode) { 66 fscrypt_warn(inode, 67 "Direct key flag not allowed with different contents and filenames modes"); 68 return false; 69 } 70 mode = &fscrypt_modes[contents_mode]; 71 72 if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) { 73 fscrypt_warn(inode, "Direct key flag not allowed with %s", 74 mode->friendly_name); 75 return false; 76 } 77 return true; 78} 79 80static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy, 81 const struct inode *inode, 82 const char *type, 83 int max_ino_bits, int max_lblk_bits) 84{ 85 struct super_block *sb = inode->i_sb; 86 int ino_bits = 64, lblk_bits = 64; 87 88 /* 89 * IV_INO_LBLK_* exist only because of hardware limitations, and 90 * currently the only known use case for them involves AES-256-XTS. 91 * That's also all we test currently. For these reasons, for now only 92 * allow AES-256-XTS here. This can be relaxed later if a use case for 93 * IV_INO_LBLK_* with other encryption modes arises. 94 */ 95 if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) { 96 fscrypt_warn(inode, 97 "Can't use %s policy with contents mode other than AES-256-XTS", 98 type); 99 return false; 100 } 101 102 /* 103 * It's unsafe to include inode numbers in the IVs if the filesystem can 104 * potentially renumber inodes, e.g. via filesystem shrinking. 105 */ 106 if (!sb->s_cop->has_stable_inodes || 107 !sb->s_cop->has_stable_inodes(sb)) { 108 fscrypt_warn(inode, 109 "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers", 110 type, sb->s_id); 111 return false; 112 } 113 if (sb->s_cop->get_ino_and_lblk_bits) 114 sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits); 115 if (ino_bits > max_ino_bits) { 116 fscrypt_warn(inode, 117 "Can't use %s policy on filesystem '%s' because its inode numbers are too long", 118 type, sb->s_id); 119 return false; 120 } 121 if (lblk_bits > max_lblk_bits) { 122 fscrypt_warn(inode, 123 "Can't use %s policy on filesystem '%s' because its block numbers are too long", 124 type, sb->s_id); 125 return false; 126 } 127 return true; 128} 129 130static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy, 131 const struct inode *inode) 132{ 133 if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode, 134 policy->filenames_encryption_mode)) { 135 fscrypt_warn(inode, 136 "Unsupported encryption modes (contents %d, filenames %d)", 137 policy->contents_encryption_mode, 138 policy->filenames_encryption_mode); 139 return false; 140 } 141 142 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK | 143 FSCRYPT_POLICY_FLAG_DIRECT_KEY)) { 144 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)", 145 policy->flags); 146 return false; 147 } 148 149 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) && 150 !supported_direct_key_modes(inode, policy->contents_encryption_mode, 151 policy->filenames_encryption_mode)) 152 return false; 153 154 if (IS_CASEFOLDED(inode)) { 155 /* With v1, there's no way to derive dirhash keys. */ 156 fscrypt_warn(inode, 157 "v1 policies can't be used on casefolded directories"); 158 return false; 159 } 160 161 return true; 162} 163 164static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy, 165 const struct inode *inode) 166{ 167 int count = 0; 168 169 if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode, 170 policy->filenames_encryption_mode)) { 171 fscrypt_warn(inode, 172 "Unsupported encryption modes (contents %d, filenames %d)", 173 policy->contents_encryption_mode, 174 policy->filenames_encryption_mode); 175 return false; 176 } 177 178 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK | 179 FSCRYPT_POLICY_FLAG_DIRECT_KEY | 180 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 | 181 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) { 182 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)", 183 policy->flags); 184 return false; 185 } 186 187 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY); 188 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64); 189 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32); 190 if (count > 1) { 191 fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)", 192 policy->flags); 193 return false; 194 } 195 196 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) && 197 !supported_direct_key_modes(inode, policy->contents_encryption_mode, 198 policy->filenames_encryption_mode)) 199 return false; 200 201 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) && 202 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64", 203 32, 32)) 204 return false; 205 206 /* 207 * IV_INO_LBLK_32 hashes the inode number, so in principle it can 208 * support any ino_bits. However, currently the inode number is gotten 209 * from inode::i_ino which is 'unsigned long'. So for now the 210 * implementation limit is 32 bits. 211 */ 212 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) && 213 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32", 214 32, 32)) 215 return false; 216 217 if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) { 218 fscrypt_warn(inode, "Reserved bits set in encryption policy"); 219 return false; 220 } 221 222 return true; 223} 224 225/** 226 * fscrypt_supported_policy() - check whether an encryption policy is supported 227 * @policy_u: the encryption policy 228 * @inode: the inode on which the policy will be used 229 * 230 * Given an encryption policy, check whether all its encryption modes and other 231 * settings are supported by this kernel on the given inode. (But we don't 232 * currently don't check for crypto API support here, so attempting to use an 233 * algorithm not configured into the crypto API will still fail later.) 234 * 235 * Return: %true if supported, else %false 236 */ 237bool fscrypt_supported_policy(const union fscrypt_policy *policy_u, 238 const struct inode *inode) 239{ 240 switch (policy_u->version) { 241 case FSCRYPT_POLICY_V1: 242 return fscrypt_supported_v1_policy(&policy_u->v1, inode); 243 case FSCRYPT_POLICY_V2: 244 return fscrypt_supported_v2_policy(&policy_u->v2, inode); 245 } 246 return false; 247} 248 249/** 250 * fscrypt_new_context() - create a new fscrypt_context 251 * @ctx_u: output context 252 * @policy_u: input policy 253 * @nonce: nonce to use 254 * 255 * Create an fscrypt_context for an inode that is being assigned the given 256 * encryption policy. @nonce must be a new random nonce. 257 * 258 * Return: the size of the new context in bytes. 259 */ 260static int fscrypt_new_context(union fscrypt_context *ctx_u, 261 const union fscrypt_policy *policy_u, 262 const u8 nonce[FSCRYPT_FILE_NONCE_SIZE]) 263{ 264 memset(ctx_u, 0, sizeof(*ctx_u)); 265 266 switch (policy_u->version) { 267 case FSCRYPT_POLICY_V1: { 268 const struct fscrypt_policy_v1 *policy = &policy_u->v1; 269 struct fscrypt_context_v1 *ctx = &ctx_u->v1; 270 271 ctx->version = FSCRYPT_CONTEXT_V1; 272 ctx->contents_encryption_mode = 273 policy->contents_encryption_mode; 274 ctx->filenames_encryption_mode = 275 policy->filenames_encryption_mode; 276 ctx->flags = policy->flags; 277 memcpy(ctx->master_key_descriptor, 278 policy->master_key_descriptor, 279 sizeof(ctx->master_key_descriptor)); 280 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE); 281 return sizeof(*ctx); 282 } 283 case FSCRYPT_POLICY_V2: { 284 const struct fscrypt_policy_v2 *policy = &policy_u->v2; 285 struct fscrypt_context_v2 *ctx = &ctx_u->v2; 286 287 ctx->version = FSCRYPT_CONTEXT_V2; 288 ctx->contents_encryption_mode = 289 policy->contents_encryption_mode; 290 ctx->filenames_encryption_mode = 291 policy->filenames_encryption_mode; 292 ctx->flags = policy->flags; 293 memcpy(ctx->master_key_identifier, 294 policy->master_key_identifier, 295 sizeof(ctx->master_key_identifier)); 296 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE); 297 return sizeof(*ctx); 298 } 299 } 300 BUG(); 301} 302 303/** 304 * fscrypt_policy_from_context() - convert an fscrypt_context to 305 * an fscrypt_policy 306 * @policy_u: output policy 307 * @ctx_u: input context 308 * @ctx_size: size of input context in bytes 309 * 310 * Given an fscrypt_context, build the corresponding fscrypt_policy. 311 * 312 * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized 313 * version number or size. 314 * 315 * This does *not* validate the settings within the policy itself, e.g. the 316 * modes, flags, and reserved bits. Use fscrypt_supported_policy() for that. 317 */ 318int fscrypt_policy_from_context(union fscrypt_policy *policy_u, 319 const union fscrypt_context *ctx_u, 320 int ctx_size) 321{ 322 memset(policy_u, 0, sizeof(*policy_u)); 323 324 if (!fscrypt_context_is_valid(ctx_u, ctx_size)) 325 return -EINVAL; 326 327 switch (ctx_u->version) { 328 case FSCRYPT_CONTEXT_V1: { 329 const struct fscrypt_context_v1 *ctx = &ctx_u->v1; 330 struct fscrypt_policy_v1 *policy = &policy_u->v1; 331 332 policy->version = FSCRYPT_POLICY_V1; 333 policy->contents_encryption_mode = 334 ctx->contents_encryption_mode; 335 policy->filenames_encryption_mode = 336 ctx->filenames_encryption_mode; 337 policy->flags = ctx->flags; 338 memcpy(policy->master_key_descriptor, 339 ctx->master_key_descriptor, 340 sizeof(policy->master_key_descriptor)); 341 return 0; 342 } 343 case FSCRYPT_CONTEXT_V2: { 344 const struct fscrypt_context_v2 *ctx = &ctx_u->v2; 345 struct fscrypt_policy_v2 *policy = &policy_u->v2; 346 347 policy->version = FSCRYPT_POLICY_V2; 348 policy->contents_encryption_mode = 349 ctx->contents_encryption_mode; 350 policy->filenames_encryption_mode = 351 ctx->filenames_encryption_mode; 352 policy->flags = ctx->flags; 353 memcpy(policy->__reserved, ctx->__reserved, 354 sizeof(policy->__reserved)); 355 memcpy(policy->master_key_identifier, 356 ctx->master_key_identifier, 357 sizeof(policy->master_key_identifier)); 358 return 0; 359 } 360 } 361 /* unreachable */ 362 return -EINVAL; 363} 364 365/* Retrieve an inode's encryption policy */ 366static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy) 367{ 368 const struct fscrypt_info *ci; 369 union fscrypt_context ctx; 370 int ret; 371 372 ci = fscrypt_get_info(inode); 373 if (ci) { 374 /* key available, use the cached policy */ 375 *policy = ci->ci_policy; 376 return 0; 377 } 378 379 if (!IS_ENCRYPTED(inode)) 380 return -ENODATA; 381 382 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); 383 if (ret < 0) 384 return (ret == -ERANGE) ? -EINVAL : ret; 385 386 return fscrypt_policy_from_context(policy, &ctx, ret); 387} 388 389static int set_encryption_policy(struct inode *inode, 390 const union fscrypt_policy *policy) 391{ 392 u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; 393 union fscrypt_context ctx; 394 int ctxsize; 395 int err; 396 397 if (!fscrypt_supported_policy(policy, inode)) 398 return -EINVAL; 399 400 switch (policy->version) { 401 case FSCRYPT_POLICY_V1: 402 /* 403 * The original encryption policy version provided no way of 404 * verifying that the correct master key was supplied, which was 405 * insecure in scenarios where multiple users have access to the 406 * same encrypted files (even just read-only access). The new 407 * encryption policy version fixes this and also implies use of 408 * an improved key derivation function and allows non-root users 409 * to securely remove keys. So as long as compatibility with 410 * old kernels isn't required, it is recommended to use the new 411 * policy version for all new encrypted directories. 412 */ 413 pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n", 414 current->comm, current->pid); 415 break; 416 case FSCRYPT_POLICY_V2: 417 err = fscrypt_verify_key_added(inode->i_sb, 418 policy->v2.master_key_identifier); 419 if (err) 420 return err; 421 if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) 422 pr_warn_once("%s (pid %d) is setting an IV_INO_LBLK_32 encryption policy. This should only be used if there are certain hardware limitations.\n", 423 current->comm, current->pid); 424 break; 425 default: 426 WARN_ON(1); 427 return -EINVAL; 428 } 429 430 get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE); 431 ctxsize = fscrypt_new_context(&ctx, policy, nonce); 432 433 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL); 434} 435 436int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) 437{ 438 union fscrypt_policy policy; 439 union fscrypt_policy existing_policy; 440 struct inode *inode = file_inode(filp); 441 u8 version; 442 int size; 443 int ret; 444 445 if (get_user(policy.version, (const u8 __user *)arg)) 446 return -EFAULT; 447 448 size = fscrypt_policy_size(&policy); 449 if (size <= 0) 450 return -EINVAL; 451 452 /* 453 * We should just copy the remaining 'size - 1' bytes here, but a 454 * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to 455 * think that size can be 0 here (despite the check above!) *and* that 456 * it's a compile-time constant. Thus it would think copy_from_user() 457 * is passed compile-time constant ULONG_MAX, causing the compile-time 458 * buffer overflow check to fail, breaking the build. This only occurred 459 * when building an i386 kernel with -Os and branch profiling enabled. 460 * 461 * Work around it by just copying the first byte again... 462 */ 463 version = policy.version; 464 if (copy_from_user(&policy, arg, size)) 465 return -EFAULT; 466 policy.version = version; 467 468 if (!inode_owner_or_capable(inode)) 469 return -EACCES; 470 471 ret = mnt_want_write_file(filp); 472 if (ret) 473 return ret; 474 475 inode_lock(inode); 476 477 ret = fscrypt_get_policy(inode, &existing_policy); 478 if (ret == -ENODATA) { 479 if (!S_ISDIR(inode->i_mode)) 480 ret = -ENOTDIR; 481 else if (IS_DEADDIR(inode)) 482 ret = -ENOENT; 483 else if (!inode->i_sb->s_cop->empty_dir(inode)) 484 ret = -ENOTEMPTY; 485 else 486 ret = set_encryption_policy(inode, &policy); 487 } else if (ret == -EINVAL || 488 (ret == 0 && !fscrypt_policies_equal(&policy, 489 &existing_policy))) { 490 /* The file already uses a different encryption policy. */ 491 ret = -EEXIST; 492 } 493 494 inode_unlock(inode); 495 496 mnt_drop_write_file(filp); 497 return ret; 498} 499EXPORT_SYMBOL(fscrypt_ioctl_set_policy); 500 501/* Original ioctl version; can only get the original policy version */ 502int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) 503{ 504 union fscrypt_policy policy; 505 int err; 506 507 err = fscrypt_get_policy(file_inode(filp), &policy); 508 if (err) 509 return err; 510 511 if (policy.version != FSCRYPT_POLICY_V1) 512 return -EINVAL; 513 514 if (copy_to_user(arg, &policy, sizeof(policy.v1))) 515 return -EFAULT; 516 return 0; 517} 518EXPORT_SYMBOL(fscrypt_ioctl_get_policy); 519 520/* Extended ioctl version; can get policies of any version */ 521int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg) 522{ 523 struct fscrypt_get_policy_ex_arg arg; 524 union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy; 525 size_t policy_size; 526 int err; 527 528 /* arg is policy_size, then policy */ 529 BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0); 530 BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) != 531 offsetof(typeof(arg), policy)); 532 BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy)); 533 534 err = fscrypt_get_policy(file_inode(filp), policy); 535 if (err) 536 return err; 537 policy_size = fscrypt_policy_size(policy); 538 539 if (copy_from_user(&arg, uarg, sizeof(arg.policy_size))) 540 return -EFAULT; 541 542 if (policy_size > arg.policy_size) 543 return -EOVERFLOW; 544 arg.policy_size = policy_size; 545 546 if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size)) 547 return -EFAULT; 548 return 0; 549} 550EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex); 551 552/* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */ 553int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg) 554{ 555 struct inode *inode = file_inode(filp); 556 union fscrypt_context ctx; 557 int ret; 558 559 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); 560 if (ret < 0) 561 return ret; 562 if (!fscrypt_context_is_valid(&ctx, ret)) 563 return -EINVAL; 564 if (copy_to_user(arg, fscrypt_context_nonce(&ctx), 565 FSCRYPT_FILE_NONCE_SIZE)) 566 return -EFAULT; 567 return 0; 568} 569EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce); 570 571/** 572 * fscrypt_has_permitted_context() - is a file's encryption policy permitted 573 * within its directory? 574 * 575 * @parent: inode for parent directory 576 * @child: inode for file being looked up, opened, or linked into @parent 577 * 578 * Filesystems must call this before permitting access to an inode in a 579 * situation where the parent directory is encrypted (either before allowing 580 * ->lookup() to succeed, or for a regular file before allowing it to be opened) 581 * and before any operation that involves linking an inode into an encrypted 582 * directory, including link, rename, and cross rename. It enforces the 583 * constraint that within a given encrypted directory tree, all files use the 584 * same encryption policy. The pre-access check is needed to detect potentially 585 * malicious offline violations of this constraint, while the link and rename 586 * checks are needed to prevent online violations of this constraint. 587 * 588 * Return: 1 if permitted, 0 if forbidden. 589 */ 590int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) 591{ 592 union fscrypt_policy parent_policy, child_policy; 593 int err; 594 595 /* No restrictions on file types which are never encrypted */ 596 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) && 597 !S_ISLNK(child->i_mode)) 598 return 1; 599 600 /* No restrictions if the parent directory is unencrypted */ 601 if (!IS_ENCRYPTED(parent)) 602 return 1; 603 604 /* Encrypted directories must not contain unencrypted files */ 605 if (!IS_ENCRYPTED(child)) 606 return 0; 607 608 /* 609 * Both parent and child are encrypted, so verify they use the same 610 * encryption policy. Compare the fscrypt_info structs if the keys are 611 * available, otherwise retrieve and compare the fscrypt_contexts. 612 * 613 * Note that the fscrypt_context retrieval will be required frequently 614 * when accessing an encrypted directory tree without the key. 615 * Performance-wise this is not a big deal because we already don't 616 * really optimize for file access without the key (to the extent that 617 * such access is even possible), given that any attempted access 618 * already causes a fscrypt_context retrieval and keyring search. 619 * 620 * In any case, if an unexpected error occurs, fall back to "forbidden". 621 */ 622 623 err = fscrypt_get_encryption_info(parent); 624 if (err) 625 return 0; 626 err = fscrypt_get_encryption_info(child); 627 if (err) 628 return 0; 629 630 err = fscrypt_get_policy(parent, &parent_policy); 631 if (err) 632 return 0; 633 634 err = fscrypt_get_policy(child, &child_policy); 635 if (err) 636 return 0; 637 638 return fscrypt_policies_equal(&parent_policy, &child_policy); 639} 640EXPORT_SYMBOL(fscrypt_has_permitted_context); 641 642/* 643 * Return the encryption policy that new files in the directory will inherit, or 644 * NULL if none, or an ERR_PTR() on error. If the directory is encrypted, also 645 * ensure that its key is set up, so that the new filename can be encrypted. 646 */ 647const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir) 648{ 649 int err; 650 651 if (IS_ENCRYPTED(dir)) { 652 err = fscrypt_require_key(dir); 653 if (err) 654 return ERR_PTR(err); 655 return &dir->i_crypt_info->ci_policy; 656 } 657 658 return fscrypt_get_dummy_policy(dir->i_sb); 659} 660 661/** 662 * fscrypt_set_context() - Set the fscrypt context of a new inode 663 * @inode: a new inode 664 * @fs_data: private data given by FS and passed to ->set_context() 665 * 666 * This should be called after fscrypt_prepare_new_inode(), generally during a 667 * filesystem transaction. Everything here must be %GFP_NOFS-safe. 668 * 669 * Return: 0 on success, -errno on failure 670 */ 671int fscrypt_set_context(struct inode *inode, void *fs_data) 672{ 673 struct fscrypt_info *ci = inode->i_crypt_info; 674 union fscrypt_context ctx; 675 int ctxsize; 676 677 /* fscrypt_prepare_new_inode() should have set up the key already. */ 678 if (WARN_ON_ONCE(!ci)) 679 return -ENOKEY; 680 681 BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE); 682 ctxsize = fscrypt_new_context(&ctx, &ci->ci_policy, ci->ci_nonce); 683 684 /* 685 * This may be the first time the inode number is available, so do any 686 * delayed key setup that requires the inode number. 687 */ 688 if (ci->ci_policy.version == FSCRYPT_POLICY_V2 && 689 (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) 690 fscrypt_hash_inode_number(ci, ci->ci_master_key); 691 692 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data); 693} 694EXPORT_SYMBOL_GPL(fscrypt_set_context); 695 696/** 697 * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption' 698 * @sb: the filesystem on which test_dummy_encryption is being specified 699 * @arg: the argument to the test_dummy_encryption option. May be NULL. 700 * @dummy_policy: the filesystem's current dummy policy (input/output, see 701 * below) 702 * 703 * Handle the test_dummy_encryption mount option by creating a dummy encryption 704 * policy, saving it in @dummy_policy, and adding the corresponding dummy 705 * encryption key to the filesystem. If the @dummy_policy is already set, then 706 * instead validate that it matches @arg. Don't support changing it via 707 * remount, as that is difficult to do safely. 708 * 709 * Return: 0 on success (dummy policy set, or the same policy is already set); 710 * -EEXIST if a different dummy policy is already set; 711 * or another -errno value. 712 */ 713int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg, 714 struct fscrypt_dummy_policy *dummy_policy) 715{ 716 struct fscrypt_key_specifier key_spec = { 0 }; 717 int version; 718 union fscrypt_policy *policy = NULL; 719 int err; 720 721 if (!arg) 722 arg = "v2"; 723 724 if (!strcmp(arg, "v1")) { 725 version = FSCRYPT_POLICY_V1; 726 key_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR; 727 memset(key_spec.u.descriptor, 0x42, 728 FSCRYPT_KEY_DESCRIPTOR_SIZE); 729 } else if (!strcmp(arg, "v2")) { 730 version = FSCRYPT_POLICY_V2; 731 key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; 732 /* key_spec.u.identifier gets filled in when adding the key */ 733 } else { 734 err = -EINVAL; 735 goto out; 736 } 737 738 policy = kzalloc(sizeof(*policy), GFP_KERNEL); 739 if (!policy) { 740 err = -ENOMEM; 741 goto out; 742 } 743 744 err = fscrypt_add_test_dummy_key(sb, &key_spec); 745 if (err) 746 goto out; 747 748 policy->version = version; 749 switch (policy->version) { 750 case FSCRYPT_POLICY_V1: 751 policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS; 752 policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS; 753 memcpy(policy->v1.master_key_descriptor, key_spec.u.descriptor, 754 FSCRYPT_KEY_DESCRIPTOR_SIZE); 755 break; 756 case FSCRYPT_POLICY_V2: 757 policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS; 758 policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS; 759 memcpy(policy->v2.master_key_identifier, key_spec.u.identifier, 760 FSCRYPT_KEY_IDENTIFIER_SIZE); 761 break; 762 default: 763 WARN_ON(1); 764 err = -EINVAL; 765 goto out; 766 } 767 768 if (dummy_policy->policy) { 769 if (fscrypt_policies_equal(policy, dummy_policy->policy)) 770 err = 0; 771 else 772 err = -EEXIST; 773 goto out; 774 } 775 dummy_policy->policy = policy; 776 policy = NULL; 777 err = 0; 778out: 779 kfree(policy); 780 return err; 781} 782EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption); 783 784/** 785 * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption' 786 * @seq: the seq_file to print the option to 787 * @sep: the separator character to use 788 * @sb: the filesystem whose options are being shown 789 * 790 * Show the test_dummy_encryption mount option, if it was specified. 791 * This is mainly used for /proc/mounts. 792 */ 793void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep, 794 struct super_block *sb) 795{ 796 const union fscrypt_policy *policy = fscrypt_get_dummy_policy(sb); 797 int vers; 798 799 if (!policy) 800 return; 801 802 vers = policy->version; 803 if (vers == FSCRYPT_POLICY_V1) /* Handle numbering quirk */ 804 vers = 1; 805 806 seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, vers); 807} 808EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption); 809