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