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