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