1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * fscrypt.h: declarations for per-file encryption 4 * 5 * Filesystems that implement per-file encryption must include this header 6 * file. 7 * 8 * Copyright (C) 2015, Google, Inc. 9 * 10 * Written by Michael Halcrow, 2015. 11 * Modified by Jaegeuk Kim, 2015. 12 */ 13 #ifndef _LINUX_FSCRYPT_H 14 #define _LINUX_FSCRYPT_H 15 16 #include <linux/fs.h> 17 #include <linux/mm.h> 18 #include <linux/slab.h> 19 #include <uapi/linux/fscrypt.h> 20 21 /* 22 * The lengths of all file contents blocks must be divisible by this value. 23 * This is needed to ensure that all contents encryption modes will work, as 24 * some of the supported modes don't support arbitrarily byte-aligned messages. 25 * 26 * Since the needed alignment is 16 bytes, most filesystems will meet this 27 * requirement naturally, as typical block sizes are powers of 2. However, if a 28 * filesystem can generate arbitrarily byte-aligned block lengths (e.g., via 29 * compression), then it will need to pad to this alignment before encryption. 30 */ 31 #define FSCRYPT_CONTENTS_ALIGNMENT 16 32 33 union fscrypt_policy; 34 struct fscrypt_info; 35 struct fs_parameter; 36 struct seq_file; 37 38 struct fscrypt_str { 39 unsigned char *name; 40 u32 len; 41 }; 42 43 struct fscrypt_name { 44 const struct qstr *usr_fname; 45 struct fscrypt_str disk_name; 46 u32 hash; 47 u32 minor_hash; 48 struct fscrypt_str crypto_buf; 49 bool is_nokey_name; 50 }; 51 52 #define FSTR_INIT(n, l) { .name = n, .len = l } 53 #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len) 54 #define fname_name(p) ((p)->disk_name.name) 55 #define fname_len(p) ((p)->disk_name.len) 56 57 /* Maximum value for the third parameter of fscrypt_operations.set_context(). */ 58 #define FSCRYPT_SET_CONTEXT_MAX_SIZE 40 59 60 #ifdef CONFIG_FS_ENCRYPTION 61 62 /* Crypto operations for filesystems */ 63 struct fscrypt_operations { 64 65 /* 66 * If set, then fs/crypto/ will allocate a global bounce page pool the 67 * first time an encryption key is set up for a file. The bounce page 68 * pool is required by the following functions: 69 * 70 * - fscrypt_encrypt_pagecache_blocks() 71 * - fscrypt_zeroout_range() for files not using inline crypto 72 * 73 * If the filesystem doesn't use those, it doesn't need to set this. 74 */ 75 unsigned int needs_bounce_pages : 1; 76 77 /* 78 * If set, then fs/crypto/ will allow the use of encryption settings 79 * that assume inode numbers fit in 32 bits (i.e. 80 * FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64}), provided that the other 81 * prerequisites for these settings are also met. This is only useful 82 * if the filesystem wants to support inline encryption hardware that is 83 * limited to 32-bit or 64-bit data unit numbers and where programming 84 * keyslots is very slow. 85 */ 86 unsigned int has_32bit_inodes : 1; 87 88 /* 89 * This field exists only for backwards compatibility reasons and should 90 * only be set by the filesystems that are setting it already. It 91 * contains the filesystem-specific key description prefix that is 92 * accepted for "logon" keys for v1 fscrypt policies. This 93 * functionality is deprecated in favor of the generic prefix 94 * "fscrypt:", which itself is deprecated in favor of the filesystem 95 * keyring ioctls such as FS_IOC_ADD_ENCRYPTION_KEY. Filesystems that 96 * are newly adding fscrypt support should not set this field. 97 */ 98 const char *legacy_key_prefix; 99 100 /* 101 * Get the fscrypt context of the given inode. 102 * 103 * @inode: the inode whose context to get 104 * @ctx: the buffer into which to get the context 105 * @len: length of the @ctx buffer in bytes 106 * 107 * Return: On success, returns the length of the context in bytes; this 108 * may be less than @len. On failure, returns -ENODATA if the 109 * inode doesn't have a context, -ERANGE if the context is 110 * longer than @len, or another -errno code. 111 */ 112 int (*get_context)(struct inode *inode, void *ctx, size_t len); 113 114 /* 115 * Set an fscrypt context on the given inode. 116 * 117 * @inode: the inode whose context to set. The inode won't already have 118 * an fscrypt context. 119 * @ctx: the context to set 120 * @len: length of @ctx in bytes (at most FSCRYPT_SET_CONTEXT_MAX_SIZE) 121 * @fs_data: If called from fscrypt_set_context(), this will be the 122 * value the filesystem passed to fscrypt_set_context(). 123 * Otherwise (i.e. when called from 124 * FS_IOC_SET_ENCRYPTION_POLICY) this will be NULL. 125 * 126 * i_rwsem will be held for write. 127 * 128 * Return: 0 on success, -errno on failure. 129 */ 130 int (*set_context)(struct inode *inode, const void *ctx, size_t len, 131 void *fs_data); 132 133 /* 134 * Get the dummy fscrypt policy in use on the filesystem (if any). 135 * 136 * Filesystems only need to implement this function if they support the 137 * test_dummy_encryption mount option. 138 * 139 * Return: A pointer to the dummy fscrypt policy, if the filesystem is 140 * mounted with test_dummy_encryption; otherwise NULL. 141 */ 142 const union fscrypt_policy *(*get_dummy_policy)(struct super_block *sb); 143 144 /* 145 * Check whether a directory is empty. i_rwsem will be held for write. 146 */ 147 bool (*empty_dir)(struct inode *inode); 148 149 /* 150 * Check whether the filesystem's inode numbers and UUID are stable, 151 * meaning that they will never be changed even by offline operations 152 * such as filesystem shrinking and therefore can be used in the 153 * encryption without the possibility of files becoming unreadable. 154 * 155 * Filesystems only need to implement this function if they want to 156 * support the FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64} flags. These 157 * flags are designed to work around the limitations of UFS and eMMC 158 * inline crypto hardware, and they shouldn't be used in scenarios where 159 * such hardware isn't being used. 160 * 161 * Leaving this NULL is equivalent to always returning false. 162 */ 163 bool (*has_stable_inodes)(struct super_block *sb); 164 165 /* 166 * Return an array of pointers to the block devices to which the 167 * filesystem may write encrypted file contents, NULL if the filesystem 168 * only has a single such block device, or an ERR_PTR() on error. 169 * 170 * On successful non-NULL return, *num_devs is set to the number of 171 * devices in the returned array. The caller must free the returned 172 * array using kfree(). 173 * 174 * If the filesystem can use multiple block devices (other than block 175 * devices that aren't used for encrypted file contents, such as 176 * external journal devices), and wants to support inline encryption, 177 * then it must implement this function. Otherwise it's not needed. 178 */ 179 struct block_device **(*get_devices)(struct super_block *sb, 180 unsigned int *num_devs); 181 }; 182 183 static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode) 184 { 185 /* 186 * Pairs with the cmpxchg_release() in fscrypt_setup_encryption_info(). 187 * I.e., another task may publish ->i_crypt_info concurrently, executing 188 * a RELEASE barrier. We need to use smp_load_acquire() here to safely 189 * ACQUIRE the memory the other task published. 190 */ 191 return smp_load_acquire(&inode->i_crypt_info); 192 } 193 194 /** 195 * fscrypt_needs_contents_encryption() - check whether an inode needs 196 * contents encryption 197 * @inode: the inode to check 198 * 199 * Return: %true iff the inode is an encrypted regular file and the kernel was 200 * built with fscrypt support. 201 * 202 * If you need to know whether the encrypt bit is set even when the kernel was 203 * built without fscrypt support, you must use IS_ENCRYPTED() directly instead. 204 */ 205 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode) 206 { 207 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode); 208 } 209 210 /* 211 * When d_splice_alias() moves a directory's no-key alias to its plaintext alias 212 * as a result of the encryption key being added, DCACHE_NOKEY_NAME must be 213 * cleared. Note that we don't have to support arbitrary moves of this flag 214 * because fscrypt doesn't allow no-key names to be the source or target of a 215 * rename(). 216 */ 217 static inline void fscrypt_handle_d_move(struct dentry *dentry) 218 { 219 dentry->d_flags &= ~DCACHE_NOKEY_NAME; 220 } 221 222 /** 223 * fscrypt_is_nokey_name() - test whether a dentry is a no-key name 224 * @dentry: the dentry to check 225 * 226 * This returns true if the dentry is a no-key dentry. A no-key dentry is a 227 * dentry that was created in an encrypted directory that hasn't had its 228 * encryption key added yet. Such dentries may be either positive or negative. 229 * 230 * When a filesystem is asked to create a new filename in an encrypted directory 231 * and the new filename's dentry is a no-key dentry, it must fail the operation 232 * with ENOKEY. This includes ->create(), ->mkdir(), ->mknod(), ->symlink(), 233 * ->rename(), and ->link(). (However, ->rename() and ->link() are already 234 * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().) 235 * 236 * This is necessary because creating a filename requires the directory's 237 * encryption key, but just checking for the key on the directory inode during 238 * the final filesystem operation doesn't guarantee that the key was available 239 * during the preceding dentry lookup. And the key must have already been 240 * available during the dentry lookup in order for it to have been checked 241 * whether the filename already exists in the directory and for the new file's 242 * dentry not to be invalidated due to it incorrectly having the no-key flag. 243 * 244 * Return: %true if the dentry is a no-key name 245 */ 246 static inline bool fscrypt_is_nokey_name(const struct dentry *dentry) 247 { 248 return dentry->d_flags & DCACHE_NOKEY_NAME; 249 } 250 251 /* crypto.c */ 252 void fscrypt_enqueue_decrypt_work(struct work_struct *); 253 254 struct page *fscrypt_encrypt_pagecache_blocks(struct page *page, 255 unsigned int len, 256 unsigned int offs, 257 gfp_t gfp_flags); 258 int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page, 259 unsigned int len, unsigned int offs, 260 u64 lblk_num, gfp_t gfp_flags); 261 262 int fscrypt_decrypt_pagecache_blocks(struct folio *folio, size_t len, 263 size_t offs); 264 int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page, 265 unsigned int len, unsigned int offs, 266 u64 lblk_num); 267 268 static inline bool fscrypt_is_bounce_page(struct page *page) 269 { 270 return page->mapping == NULL; 271 } 272 273 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page) 274 { 275 return (struct page *)page_private(bounce_page); 276 } 277 278 static inline bool fscrypt_is_bounce_folio(struct folio *folio) 279 { 280 return folio->mapping == NULL; 281 } 282 283 static inline struct folio *fscrypt_pagecache_folio(struct folio *bounce_folio) 284 { 285 return bounce_folio->private; 286 } 287 288 void fscrypt_free_bounce_page(struct page *bounce_page); 289 290 /* policy.c */ 291 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg); 292 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg); 293 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg); 294 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg); 295 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child); 296 int fscrypt_context_for_new_inode(void *ctx, struct inode *inode); 297 int fscrypt_set_context(struct inode *inode, void *fs_data); 298 299 struct fscrypt_dummy_policy { 300 const union fscrypt_policy *policy; 301 }; 302 303 int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param, 304 struct fscrypt_dummy_policy *dummy_policy); 305 bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1, 306 const struct fscrypt_dummy_policy *p2); 307 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep, 308 struct super_block *sb); 309 static inline bool 310 fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy) 311 { 312 return dummy_policy->policy != NULL; 313 } 314 static inline void 315 fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy) 316 { 317 kfree(dummy_policy->policy); 318 dummy_policy->policy = NULL; 319 } 320 321 /* keyring.c */ 322 void fscrypt_destroy_keyring(struct super_block *sb); 323 int fscrypt_ioctl_add_key(struct file *filp, void __user *arg); 324 int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg); 325 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg); 326 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg); 327 328 /* keysetup.c */ 329 int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode, 330 bool *encrypt_ret); 331 void fscrypt_put_encryption_info(struct inode *inode); 332 void fscrypt_free_inode(struct inode *inode); 333 int fscrypt_drop_inode(struct inode *inode); 334 335 /* fname.c */ 336 int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname, 337 u8 *out, unsigned int olen); 338 bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len, 339 u32 max_len, u32 *encrypted_len_ret); 340 int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname, 341 int lookup, struct fscrypt_name *fname); 342 343 static inline void fscrypt_free_filename(struct fscrypt_name *fname) 344 { 345 kfree(fname->crypto_buf.name); 346 } 347 348 int fscrypt_fname_alloc_buffer(u32 max_encrypted_len, 349 struct fscrypt_str *crypto_str); 350 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str); 351 int fscrypt_fname_disk_to_usr(const struct inode *inode, 352 u32 hash, u32 minor_hash, 353 const struct fscrypt_str *iname, 354 struct fscrypt_str *oname); 355 bool fscrypt_match_name(const struct fscrypt_name *fname, 356 const u8 *de_name, u32 de_name_len); 357 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name); 358 int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags); 359 360 /* bio.c */ 361 bool fscrypt_decrypt_bio(struct bio *bio); 362 int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, 363 sector_t pblk, unsigned int len); 364 365 /* hooks.c */ 366 int fscrypt_file_open(struct inode *inode, struct file *filp); 367 int __fscrypt_prepare_link(struct inode *inode, struct inode *dir, 368 struct dentry *dentry); 369 int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry, 370 struct inode *new_dir, struct dentry *new_dentry, 371 unsigned int flags); 372 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry, 373 struct fscrypt_name *fname); 374 int fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry); 375 int __fscrypt_prepare_readdir(struct inode *dir); 376 int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr); 377 int fscrypt_prepare_setflags(struct inode *inode, 378 unsigned int oldflags, unsigned int flags); 379 int fscrypt_prepare_symlink(struct inode *dir, const char *target, 380 unsigned int len, unsigned int max_len, 381 struct fscrypt_str *disk_link); 382 int __fscrypt_encrypt_symlink(struct inode *inode, const char *target, 383 unsigned int len, struct fscrypt_str *disk_link); 384 const char *fscrypt_get_symlink(struct inode *inode, const void *caddr, 385 unsigned int max_size, 386 struct delayed_call *done); 387 int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat); 388 static inline void fscrypt_set_ops(struct super_block *sb, 389 const struct fscrypt_operations *s_cop) 390 { 391 sb->s_cop = s_cop; 392 } 393 #else /* !CONFIG_FS_ENCRYPTION */ 394 395 static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode) 396 { 397 return NULL; 398 } 399 400 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode) 401 { 402 return false; 403 } 404 405 static inline void fscrypt_handle_d_move(struct dentry *dentry) 406 { 407 } 408 409 static inline bool fscrypt_is_nokey_name(const struct dentry *dentry) 410 { 411 return false; 412 } 413 414 /* crypto.c */ 415 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work) 416 { 417 } 418 419 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page, 420 unsigned int len, 421 unsigned int offs, 422 gfp_t gfp_flags) 423 { 424 return ERR_PTR(-EOPNOTSUPP); 425 } 426 427 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode, 428 struct page *page, 429 unsigned int len, 430 unsigned int offs, u64 lblk_num, 431 gfp_t gfp_flags) 432 { 433 return -EOPNOTSUPP; 434 } 435 436 static inline int fscrypt_decrypt_pagecache_blocks(struct folio *folio, 437 size_t len, size_t offs) 438 { 439 return -EOPNOTSUPP; 440 } 441 442 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode, 443 struct page *page, 444 unsigned int len, 445 unsigned int offs, u64 lblk_num) 446 { 447 return -EOPNOTSUPP; 448 } 449 450 static inline bool fscrypt_is_bounce_page(struct page *page) 451 { 452 return false; 453 } 454 455 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page) 456 { 457 WARN_ON_ONCE(1); 458 return ERR_PTR(-EINVAL); 459 } 460 461 static inline bool fscrypt_is_bounce_folio(struct folio *folio) 462 { 463 return false; 464 } 465 466 static inline struct folio *fscrypt_pagecache_folio(struct folio *bounce_folio) 467 { 468 WARN_ON_ONCE(1); 469 return ERR_PTR(-EINVAL); 470 } 471 472 static inline void fscrypt_free_bounce_page(struct page *bounce_page) 473 { 474 } 475 476 /* policy.c */ 477 static inline int fscrypt_ioctl_set_policy(struct file *filp, 478 const void __user *arg) 479 { 480 return -EOPNOTSUPP; 481 } 482 483 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) 484 { 485 return -EOPNOTSUPP; 486 } 487 488 static inline int fscrypt_ioctl_get_policy_ex(struct file *filp, 489 void __user *arg) 490 { 491 return -EOPNOTSUPP; 492 } 493 494 static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg) 495 { 496 return -EOPNOTSUPP; 497 } 498 499 static inline int fscrypt_has_permitted_context(struct inode *parent, 500 struct inode *child) 501 { 502 return 0; 503 } 504 505 static inline int fscrypt_set_context(struct inode *inode, void *fs_data) 506 { 507 return -EOPNOTSUPP; 508 } 509 510 struct fscrypt_dummy_policy { 511 }; 512 513 static inline int 514 fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param, 515 struct fscrypt_dummy_policy *dummy_policy) 516 { 517 return -EINVAL; 518 } 519 520 static inline bool 521 fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1, 522 const struct fscrypt_dummy_policy *p2) 523 { 524 return true; 525 } 526 527 static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq, 528 char sep, 529 struct super_block *sb) 530 { 531 } 532 533 static inline bool 534 fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy) 535 { 536 return false; 537 } 538 539 static inline void 540 fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy) 541 { 542 } 543 544 /* keyring.c */ 545 static inline void fscrypt_destroy_keyring(struct super_block *sb) 546 { 547 } 548 549 static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg) 550 { 551 return -EOPNOTSUPP; 552 } 553 554 static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg) 555 { 556 return -EOPNOTSUPP; 557 } 558 559 static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp, 560 void __user *arg) 561 { 562 return -EOPNOTSUPP; 563 } 564 565 static inline int fscrypt_ioctl_get_key_status(struct file *filp, 566 void __user *arg) 567 { 568 return -EOPNOTSUPP; 569 } 570 571 /* keysetup.c */ 572 573 static inline int fscrypt_prepare_new_inode(struct inode *dir, 574 struct inode *inode, 575 bool *encrypt_ret) 576 { 577 if (IS_ENCRYPTED(dir)) 578 return -EOPNOTSUPP; 579 return 0; 580 } 581 582 static inline void fscrypt_put_encryption_info(struct inode *inode) 583 { 584 return; 585 } 586 587 static inline void fscrypt_free_inode(struct inode *inode) 588 { 589 } 590 591 static inline int fscrypt_drop_inode(struct inode *inode) 592 { 593 return 0; 594 } 595 596 /* fname.c */ 597 static inline int fscrypt_setup_filename(struct inode *dir, 598 const struct qstr *iname, 599 int lookup, struct fscrypt_name *fname) 600 { 601 if (IS_ENCRYPTED(dir)) 602 return -EOPNOTSUPP; 603 604 memset(fname, 0, sizeof(*fname)); 605 fname->usr_fname = iname; 606 fname->disk_name.name = (unsigned char *)iname->name; 607 fname->disk_name.len = iname->len; 608 return 0; 609 } 610 611 static inline void fscrypt_free_filename(struct fscrypt_name *fname) 612 { 613 return; 614 } 615 616 static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len, 617 struct fscrypt_str *crypto_str) 618 { 619 return -EOPNOTSUPP; 620 } 621 622 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str) 623 { 624 return; 625 } 626 627 static inline int fscrypt_fname_disk_to_usr(const struct inode *inode, 628 u32 hash, u32 minor_hash, 629 const struct fscrypt_str *iname, 630 struct fscrypt_str *oname) 631 { 632 return -EOPNOTSUPP; 633 } 634 635 static inline bool fscrypt_match_name(const struct fscrypt_name *fname, 636 const u8 *de_name, u32 de_name_len) 637 { 638 /* Encryption support disabled; use standard comparison */ 639 if (de_name_len != fname->disk_name.len) 640 return false; 641 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len); 642 } 643 644 static inline u64 fscrypt_fname_siphash(const struct inode *dir, 645 const struct qstr *name) 646 { 647 WARN_ON_ONCE(1); 648 return 0; 649 } 650 651 static inline int fscrypt_d_revalidate(struct dentry *dentry, 652 unsigned int flags) 653 { 654 return 1; 655 } 656 657 /* bio.c */ 658 static inline bool fscrypt_decrypt_bio(struct bio *bio) 659 { 660 return true; 661 } 662 663 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, 664 sector_t pblk, unsigned int len) 665 { 666 return -EOPNOTSUPP; 667 } 668 669 /* hooks.c */ 670 671 static inline int fscrypt_file_open(struct inode *inode, struct file *filp) 672 { 673 if (IS_ENCRYPTED(inode)) 674 return -EOPNOTSUPP; 675 return 0; 676 } 677 678 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir, 679 struct dentry *dentry) 680 { 681 return -EOPNOTSUPP; 682 } 683 684 static inline int __fscrypt_prepare_rename(struct inode *old_dir, 685 struct dentry *old_dentry, 686 struct inode *new_dir, 687 struct dentry *new_dentry, 688 unsigned int flags) 689 { 690 return -EOPNOTSUPP; 691 } 692 693 static inline int __fscrypt_prepare_lookup(struct inode *dir, 694 struct dentry *dentry, 695 struct fscrypt_name *fname) 696 { 697 return -EOPNOTSUPP; 698 } 699 700 static inline int fscrypt_prepare_lookup_partial(struct inode *dir, 701 struct dentry *dentry) 702 { 703 return -EOPNOTSUPP; 704 } 705 706 static inline int __fscrypt_prepare_readdir(struct inode *dir) 707 { 708 return -EOPNOTSUPP; 709 } 710 711 static inline int __fscrypt_prepare_setattr(struct dentry *dentry, 712 struct iattr *attr) 713 { 714 return -EOPNOTSUPP; 715 } 716 717 static inline int fscrypt_prepare_setflags(struct inode *inode, 718 unsigned int oldflags, 719 unsigned int flags) 720 { 721 return 0; 722 } 723 724 static inline int fscrypt_prepare_symlink(struct inode *dir, 725 const char *target, 726 unsigned int len, 727 unsigned int max_len, 728 struct fscrypt_str *disk_link) 729 { 730 if (IS_ENCRYPTED(dir)) 731 return -EOPNOTSUPP; 732 disk_link->name = (unsigned char *)target; 733 disk_link->len = len + 1; 734 if (disk_link->len > max_len) 735 return -ENAMETOOLONG; 736 return 0; 737 } 738 739 static inline int __fscrypt_encrypt_symlink(struct inode *inode, 740 const char *target, 741 unsigned int len, 742 struct fscrypt_str *disk_link) 743 { 744 return -EOPNOTSUPP; 745 } 746 747 static inline const char *fscrypt_get_symlink(struct inode *inode, 748 const void *caddr, 749 unsigned int max_size, 750 struct delayed_call *done) 751 { 752 return ERR_PTR(-EOPNOTSUPP); 753 } 754 755 static inline int fscrypt_symlink_getattr(const struct path *path, 756 struct kstat *stat) 757 { 758 return -EOPNOTSUPP; 759 } 760 761 static inline void fscrypt_set_ops(struct super_block *sb, 762 const struct fscrypt_operations *s_cop) 763 { 764 } 765 766 #endif /* !CONFIG_FS_ENCRYPTION */ 767 768 /* inline_crypt.c */ 769 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT 770 771 bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode); 772 773 void fscrypt_set_bio_crypt_ctx(struct bio *bio, 774 const struct inode *inode, u64 first_lblk, 775 gfp_t gfp_mask); 776 777 void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio, 778 const struct buffer_head *first_bh, 779 gfp_t gfp_mask); 780 781 bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode, 782 u64 next_lblk); 783 784 bool fscrypt_mergeable_bio_bh(struct bio *bio, 785 const struct buffer_head *next_bh); 786 787 bool fscrypt_dio_supported(struct inode *inode); 788 789 u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks); 790 791 #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ 792 793 static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode) 794 { 795 return false; 796 } 797 798 static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio, 799 const struct inode *inode, 800 u64 first_lblk, gfp_t gfp_mask) { } 801 802 static inline void fscrypt_set_bio_crypt_ctx_bh( 803 struct bio *bio, 804 const struct buffer_head *first_bh, 805 gfp_t gfp_mask) { } 806 807 static inline bool fscrypt_mergeable_bio(struct bio *bio, 808 const struct inode *inode, 809 u64 next_lblk) 810 { 811 return true; 812 } 813 814 static inline bool fscrypt_mergeable_bio_bh(struct bio *bio, 815 const struct buffer_head *next_bh) 816 { 817 return true; 818 } 819 820 static inline bool fscrypt_dio_supported(struct inode *inode) 821 { 822 return !fscrypt_needs_contents_encryption(inode); 823 } 824 825 static inline u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, 826 u64 nr_blocks) 827 { 828 return nr_blocks; 829 } 830 #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ 831 832 /** 833 * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline 834 * encryption 835 * @inode: an inode. If encrypted, its key must be set up. 836 * 837 * Return: true if the inode requires file contents encryption and if the 838 * encryption should be done in the block layer via blk-crypto rather 839 * than in the filesystem layer. 840 */ 841 static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode) 842 { 843 return fscrypt_needs_contents_encryption(inode) && 844 __fscrypt_inode_uses_inline_crypto(inode); 845 } 846 847 /** 848 * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer 849 * encryption 850 * @inode: an inode. If encrypted, its key must be set up. 851 * 852 * Return: true if the inode requires file contents encryption and if the 853 * encryption should be done in the filesystem layer rather than in the 854 * block layer via blk-crypto. 855 */ 856 static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode) 857 { 858 return fscrypt_needs_contents_encryption(inode) && 859 !__fscrypt_inode_uses_inline_crypto(inode); 860 } 861 862 /** 863 * fscrypt_has_encryption_key() - check whether an inode has had its key set up 864 * @inode: the inode to check 865 * 866 * Return: %true if the inode has had its encryption key set up, else %false. 867 * 868 * Usually this should be preceded by fscrypt_get_encryption_info() to try to 869 * set up the key first. 870 */ 871 static inline bool fscrypt_has_encryption_key(const struct inode *inode) 872 { 873 return fscrypt_get_info(inode) != NULL; 874 } 875 876 /** 877 * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted 878 * directory 879 * @old_dentry: an existing dentry for the inode being linked 880 * @dir: the target directory 881 * @dentry: negative dentry for the target filename 882 * 883 * A new link can only be added to an encrypted directory if the directory's 884 * encryption key is available --- since otherwise we'd have no way to encrypt 885 * the filename. 886 * 887 * We also verify that the link will not violate the constraint that all files 888 * in an encrypted directory tree use the same encryption policy. 889 * 890 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing, 891 * -EXDEV if the link would result in an inconsistent encryption policy, or 892 * another -errno code. 893 */ 894 static inline int fscrypt_prepare_link(struct dentry *old_dentry, 895 struct inode *dir, 896 struct dentry *dentry) 897 { 898 if (IS_ENCRYPTED(dir)) 899 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry); 900 return 0; 901 } 902 903 /** 904 * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted 905 * directories 906 * @old_dir: source directory 907 * @old_dentry: dentry for source file 908 * @new_dir: target directory 909 * @new_dentry: dentry for target location (may be negative unless exchanging) 910 * @flags: rename flags (we care at least about %RENAME_EXCHANGE) 911 * 912 * Prepare for ->rename() where the source and/or target directories may be 913 * encrypted. A new link can only be added to an encrypted directory if the 914 * directory's encryption key is available --- since otherwise we'd have no way 915 * to encrypt the filename. A rename to an existing name, on the other hand, 916 * *is* cryptographically possible without the key. However, we take the more 917 * conservative approach and just forbid all no-key renames. 918 * 919 * We also verify that the rename will not violate the constraint that all files 920 * in an encrypted directory tree use the same encryption policy. 921 * 922 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the 923 * rename would cause inconsistent encryption policies, or another -errno code. 924 */ 925 static inline int fscrypt_prepare_rename(struct inode *old_dir, 926 struct dentry *old_dentry, 927 struct inode *new_dir, 928 struct dentry *new_dentry, 929 unsigned int flags) 930 { 931 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir)) 932 return __fscrypt_prepare_rename(old_dir, old_dentry, 933 new_dir, new_dentry, flags); 934 return 0; 935 } 936 937 /** 938 * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted 939 * directory 940 * @dir: directory being searched 941 * @dentry: filename being looked up 942 * @fname: (output) the name to use to search the on-disk directory 943 * 944 * Prepare for ->lookup() in a directory which may be encrypted by determining 945 * the name that will actually be used to search the directory on-disk. If the 946 * directory's encryption policy is supported by this kernel and its encryption 947 * key is available, then the lookup is assumed to be by plaintext name; 948 * otherwise, it is assumed to be by no-key name. 949 * 950 * This will set DCACHE_NOKEY_NAME on the dentry if the lookup is by no-key 951 * name. In this case the filesystem must assign the dentry a dentry_operations 952 * which contains fscrypt_d_revalidate (or contains a d_revalidate method that 953 * calls fscrypt_d_revalidate), so that the dentry will be invalidated if the 954 * directory's encryption key is later added. 955 * 956 * Return: 0 on success; -ENOENT if the directory's key is unavailable but the 957 * filename isn't a valid no-key name, so a negative dentry should be created; 958 * or another -errno code. 959 */ 960 static inline int fscrypt_prepare_lookup(struct inode *dir, 961 struct dentry *dentry, 962 struct fscrypt_name *fname) 963 { 964 if (IS_ENCRYPTED(dir)) 965 return __fscrypt_prepare_lookup(dir, dentry, fname); 966 967 memset(fname, 0, sizeof(*fname)); 968 fname->usr_fname = &dentry->d_name; 969 fname->disk_name.name = (unsigned char *)dentry->d_name.name; 970 fname->disk_name.len = dentry->d_name.len; 971 return 0; 972 } 973 974 /** 975 * fscrypt_prepare_readdir() - prepare to read a possibly-encrypted directory 976 * @dir: the directory inode 977 * 978 * If the directory is encrypted and it doesn't already have its encryption key 979 * set up, try to set it up so that the filenames will be listed in plaintext 980 * form rather than in no-key form. 981 * 982 * Return: 0 on success; -errno on error. Note that the encryption key being 983 * unavailable is not considered an error. It is also not an error if 984 * the encryption policy is unsupported by this kernel; that is treated 985 * like the key being unavailable, so that files can still be deleted. 986 */ 987 static inline int fscrypt_prepare_readdir(struct inode *dir) 988 { 989 if (IS_ENCRYPTED(dir)) 990 return __fscrypt_prepare_readdir(dir); 991 return 0; 992 } 993 994 /** 995 * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's 996 * attributes 997 * @dentry: dentry through which the inode is being changed 998 * @attr: attributes to change 999 * 1000 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file, 1001 * most attribute changes are allowed even without the encryption key. However, 1002 * without the encryption key we do have to forbid truncates. This is needed 1003 * because the size being truncated to may not be a multiple of the filesystem 1004 * block size, and in that case we'd have to decrypt the final block, zero the 1005 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a 1006 * filesystem block boundary, but it's simpler to just forbid all truncates --- 1007 * and we already forbid all other contents modifications without the key.) 1008 * 1009 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code 1010 * if a problem occurred while setting up the encryption key. 1011 */ 1012 static inline int fscrypt_prepare_setattr(struct dentry *dentry, 1013 struct iattr *attr) 1014 { 1015 if (IS_ENCRYPTED(d_inode(dentry))) 1016 return __fscrypt_prepare_setattr(dentry, attr); 1017 return 0; 1018 } 1019 1020 /** 1021 * fscrypt_encrypt_symlink() - encrypt the symlink target if needed 1022 * @inode: symlink inode 1023 * @target: plaintext symlink target 1024 * @len: length of @target excluding null terminator 1025 * @disk_link: (in/out) the on-disk symlink target being prepared 1026 * 1027 * If the symlink target needs to be encrypted, then this function encrypts it 1028 * into @disk_link->name. fscrypt_prepare_symlink() must have been called 1029 * previously to compute @disk_link->len. If the filesystem did not allocate a 1030 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one 1031 * will be kmalloc()'ed and the filesystem will be responsible for freeing it. 1032 * 1033 * Return: 0 on success, -errno on failure 1034 */ 1035 static inline int fscrypt_encrypt_symlink(struct inode *inode, 1036 const char *target, 1037 unsigned int len, 1038 struct fscrypt_str *disk_link) 1039 { 1040 if (IS_ENCRYPTED(inode)) 1041 return __fscrypt_encrypt_symlink(inode, target, len, disk_link); 1042 return 0; 1043 } 1044 1045 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */ 1046 static inline void fscrypt_finalize_bounce_page(struct page **pagep) 1047 { 1048 struct page *page = *pagep; 1049 1050 if (fscrypt_is_bounce_page(page)) { 1051 *pagep = fscrypt_pagecache_page(page); 1052 fscrypt_free_bounce_page(page); 1053 } 1054 } 1055 1056 #endif /* _LINUX_FSCRYPT_H */ 1057