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