| .. | .. |
|---|
| 15 | 15 | |
|---|
| 16 | 16 | #include <linux/fs.h> |
|---|
| 17 | 17 | #include <linux/mm.h> |
|---|
| 18 | | -#include <linux/parser.h> |
|---|
| 19 | 18 | #include <linux/slab.h> |
|---|
| 20 | 19 | #include <uapi/linux/fscrypt.h> |
|---|
| 21 | 20 | |
|---|
| 22 | 21 | #define FS_CRYPTO_BLOCK_SIZE 16 |
|---|
| 23 | 22 | |
|---|
| 24 | | -union fscrypt_context; |
|---|
| 23 | +union fscrypt_policy; |
|---|
| 25 | 24 | struct fscrypt_info; |
|---|
| 26 | 25 | struct seq_file; |
|---|
| 27 | 26 | |
|---|
| .. | .. |
|---|
| 36 | 35 | u32 hash; |
|---|
| 37 | 36 | u32 minor_hash; |
|---|
| 38 | 37 | struct fscrypt_str crypto_buf; |
|---|
| 39 | | - bool is_ciphertext_name; |
|---|
| 38 | + bool is_nokey_name; |
|---|
| 40 | 39 | }; |
|---|
| 41 | 40 | |
|---|
| 42 | 41 | #define FSTR_INIT(n, l) { .name = n, .len = l } |
|---|
| .. | .. |
|---|
| 62 | 61 | int (*get_context)(struct inode *inode, void *ctx, size_t len); |
|---|
| 63 | 62 | int (*set_context)(struct inode *inode, const void *ctx, size_t len, |
|---|
| 64 | 63 | void *fs_data); |
|---|
| 65 | | - const union fscrypt_context *(*get_dummy_context)( |
|---|
| 66 | | - struct super_block *sb); |
|---|
| 64 | + const union fscrypt_policy *(*get_dummy_policy)(struct super_block *sb); |
|---|
| 67 | 65 | bool (*empty_dir)(struct inode *inode); |
|---|
| 68 | 66 | unsigned int max_namelen; |
|---|
| 69 | 67 | bool (*has_stable_inodes)(struct super_block *sb); |
|---|
| 70 | 68 | void (*get_ino_and_lblk_bits)(struct super_block *sb, |
|---|
| 71 | 69 | int *ino_bits_ret, int *lblk_bits_ret); |
|---|
| 72 | | - bool (*inline_crypt_enabled)(struct super_block *sb); |
|---|
| 73 | 70 | int (*get_num_devices)(struct super_block *sb); |
|---|
| 74 | 71 | void (*get_devices)(struct super_block *sb, |
|---|
| 75 | 72 | struct request_queue **devs); |
|---|
| 73 | + |
|---|
| 74 | + ANDROID_KABI_RESERVE(1); |
|---|
| 75 | + ANDROID_KABI_RESERVE(2); |
|---|
| 76 | + ANDROID_KABI_RESERVE(3); |
|---|
| 77 | + ANDROID_KABI_RESERVE(4); |
|---|
| 78 | + |
|---|
| 79 | + ANDROID_OEM_DATA_ARRAY(1, 4); |
|---|
| 76 | 80 | }; |
|---|
| 77 | 81 | |
|---|
| 78 | | -static inline bool fscrypt_has_encryption_key(const struct inode *inode) |
|---|
| 82 | +static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode) |
|---|
| 79 | 83 | { |
|---|
| 80 | | - /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */ |
|---|
| 81 | | - return READ_ONCE(inode->i_crypt_info) != NULL; |
|---|
| 84 | + /* |
|---|
| 85 | + * Pairs with the cmpxchg_release() in fscrypt_setup_encryption_info(). |
|---|
| 86 | + * I.e., another task may publish ->i_crypt_info concurrently, executing |
|---|
| 87 | + * a RELEASE barrier. We need to use smp_load_acquire() here to safely |
|---|
| 88 | + * ACQUIRE the memory the other task published. |
|---|
| 89 | + */ |
|---|
| 90 | + return smp_load_acquire(&inode->i_crypt_info); |
|---|
| 82 | 91 | } |
|---|
| 83 | 92 | |
|---|
| 84 | 93 | /** |
|---|
| .. | .. |
|---|
| 97 | 106 | return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode); |
|---|
| 98 | 107 | } |
|---|
| 99 | 108 | |
|---|
| 100 | | -static inline const union fscrypt_context * |
|---|
| 101 | | -fscrypt_get_dummy_context(struct super_block *sb) |
|---|
| 102 | | -{ |
|---|
| 103 | | - if (!sb->s_cop->get_dummy_context) |
|---|
| 104 | | - return NULL; |
|---|
| 105 | | - return sb->s_cop->get_dummy_context(sb); |
|---|
| 106 | | -} |
|---|
| 107 | | - |
|---|
| 108 | 109 | /* |
|---|
| 109 | | - * When d_splice_alias() moves a directory's encrypted alias to its decrypted |
|---|
| 110 | | - * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME |
|---|
| 111 | | - * must be cleared. Note that we don't have to support arbitrary moves of this |
|---|
| 112 | | - * flag because fscrypt doesn't allow encrypted aliases to be the source or |
|---|
| 113 | | - * target of a rename(). |
|---|
| 110 | + * When d_splice_alias() moves a directory's no-key alias to its plaintext alias |
|---|
| 111 | + * as a result of the encryption key being added, DCACHE_NOKEY_NAME must be |
|---|
| 112 | + * cleared. Note that we don't have to support arbitrary moves of this flag |
|---|
| 113 | + * because fscrypt doesn't allow no-key names to be the source or target of a |
|---|
| 114 | + * rename(). |
|---|
| 114 | 115 | */ |
|---|
| 115 | 116 | static inline void fscrypt_handle_d_move(struct dentry *dentry) |
|---|
| 116 | 117 | { |
|---|
| 117 | | - dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME; |
|---|
| 118 | + dentry->d_flags &= ~DCACHE_NOKEY_NAME; |
|---|
| 118 | 119 | } |
|---|
| 119 | 120 | |
|---|
| 120 | 121 | /** |
|---|
| .. | .. |
|---|
| 143 | 144 | */ |
|---|
| 144 | 145 | static inline bool fscrypt_is_nokey_name(const struct dentry *dentry) |
|---|
| 145 | 146 | { |
|---|
| 146 | | - return dentry->d_flags & DCACHE_ENCRYPTED_NAME; |
|---|
| 147 | + return dentry->d_flags & DCACHE_NOKEY_NAME; |
|---|
| 147 | 148 | } |
|---|
| 148 | 149 | |
|---|
| 149 | 150 | /* crypto.c */ |
|---|
| .. | .. |
|---|
| 174 | 175 | } |
|---|
| 175 | 176 | |
|---|
| 176 | 177 | void fscrypt_free_bounce_page(struct page *bounce_page); |
|---|
| 177 | | -int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags); |
|---|
| 178 | 178 | |
|---|
| 179 | 179 | /* policy.c */ |
|---|
| 180 | 180 | int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg); |
|---|
| .. | .. |
|---|
| 182 | 182 | int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg); |
|---|
| 183 | 183 | int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg); |
|---|
| 184 | 184 | int fscrypt_has_permitted_context(struct inode *parent, struct inode *child); |
|---|
| 185 | | -int fscrypt_inherit_context(struct inode *parent, struct inode *child, |
|---|
| 186 | | - void *fs_data, bool preload); |
|---|
| 185 | +int fscrypt_set_context(struct inode *inode, void *fs_data); |
|---|
| 187 | 186 | |
|---|
| 188 | | -struct fscrypt_dummy_context { |
|---|
| 189 | | - const union fscrypt_context *ctx; |
|---|
| 187 | +struct fscrypt_dummy_policy { |
|---|
| 188 | + const union fscrypt_policy *policy; |
|---|
| 190 | 189 | }; |
|---|
| 191 | 190 | |
|---|
| 192 | | -int fscrypt_set_test_dummy_encryption(struct super_block *sb, |
|---|
| 193 | | - const substring_t *arg, |
|---|
| 194 | | - struct fscrypt_dummy_context *dummy_ctx); |
|---|
| 191 | +int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg, |
|---|
| 192 | + struct fscrypt_dummy_policy *dummy_policy); |
|---|
| 195 | 193 | void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep, |
|---|
| 196 | 194 | struct super_block *sb); |
|---|
| 197 | 195 | static inline void |
|---|
| 198 | | -fscrypt_free_dummy_context(struct fscrypt_dummy_context *dummy_ctx) |
|---|
| 196 | +fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy) |
|---|
| 199 | 197 | { |
|---|
| 200 | | - kfree(dummy_ctx->ctx); |
|---|
| 201 | | - dummy_ctx->ctx = NULL; |
|---|
| 198 | + kfree(dummy_policy->policy); |
|---|
| 199 | + dummy_policy->policy = NULL; |
|---|
| 202 | 200 | } |
|---|
| 203 | 201 | |
|---|
| 204 | 202 | /* keyring.c */ |
|---|
| 205 | | -void fscrypt_sb_free(struct super_block *sb); |
|---|
| 203 | +void fscrypt_destroy_keyring(struct super_block *sb); |
|---|
| 206 | 204 | int fscrypt_ioctl_add_key(struct file *filp, void __user *arg); |
|---|
| 207 | 205 | int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg); |
|---|
| 208 | 206 | int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg); |
|---|
| 209 | 207 | int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg); |
|---|
| 210 | | -int fscrypt_register_key_removal_notifier(struct notifier_block *nb); |
|---|
| 211 | | -int fscrypt_unregister_key_removal_notifier(struct notifier_block *nb); |
|---|
| 212 | 208 | |
|---|
| 213 | 209 | /* keysetup.c */ |
|---|
| 214 | | -int fscrypt_get_encryption_info(struct inode *inode); |
|---|
| 210 | +int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode, |
|---|
| 211 | + bool *encrypt_ret); |
|---|
| 215 | 212 | void fscrypt_put_encryption_info(struct inode *inode); |
|---|
| 216 | 213 | void fscrypt_free_inode(struct inode *inode); |
|---|
| 217 | 214 | int fscrypt_drop_inode(struct inode *inode); |
|---|
| .. | .. |
|---|
| 225 | 222 | kfree(fname->crypto_buf.name); |
|---|
| 226 | 223 | } |
|---|
| 227 | 224 | |
|---|
| 228 | | -int fscrypt_fname_alloc_buffer(const struct inode *inode, u32 max_encrypted_len, |
|---|
| 225 | +int fscrypt_fname_alloc_buffer(u32 max_encrypted_len, |
|---|
| 229 | 226 | struct fscrypt_str *crypto_str); |
|---|
| 230 | 227 | void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str); |
|---|
| 231 | 228 | int fscrypt_fname_disk_to_usr(const struct inode *inode, |
|---|
| .. | .. |
|---|
| 235 | 232 | bool fscrypt_match_name(const struct fscrypt_name *fname, |
|---|
| 236 | 233 | const u8 *de_name, u32 de_name_len); |
|---|
| 237 | 234 | u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name); |
|---|
| 235 | +int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags); |
|---|
| 238 | 236 | |
|---|
| 239 | 237 | /* bio.c */ |
|---|
| 240 | 238 | void fscrypt_decrypt_bio(struct bio *bio); |
|---|
| .. | .. |
|---|
| 250 | 248 | unsigned int flags); |
|---|
| 251 | 249 | int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry, |
|---|
| 252 | 250 | struct fscrypt_name *fname); |
|---|
| 251 | +int __fscrypt_prepare_readdir(struct inode *dir); |
|---|
| 252 | +int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr); |
|---|
| 253 | 253 | int fscrypt_prepare_setflags(struct inode *inode, |
|---|
| 254 | 254 | unsigned int oldflags, unsigned int flags); |
|---|
| 255 | | -int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len, |
|---|
| 256 | | - unsigned int max_len, |
|---|
| 257 | | - struct fscrypt_str *disk_link); |
|---|
| 255 | +int fscrypt_prepare_symlink(struct inode *dir, const char *target, |
|---|
| 256 | + unsigned int len, unsigned int max_len, |
|---|
| 257 | + struct fscrypt_str *disk_link); |
|---|
| 258 | 258 | int __fscrypt_encrypt_symlink(struct inode *inode, const char *target, |
|---|
| 259 | 259 | unsigned int len, struct fscrypt_str *disk_link); |
|---|
| 260 | 260 | const char *fscrypt_get_symlink(struct inode *inode, const void *caddr, |
|---|
| 261 | 261 | unsigned int max_size, |
|---|
| 262 | 262 | struct delayed_call *done); |
|---|
| 263 | 263 | int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat); |
|---|
| 264 | | - |
|---|
| 264 | +static inline void fscrypt_set_ops(struct super_block *sb, |
|---|
| 265 | + const struct fscrypt_operations *s_cop) |
|---|
| 266 | +{ |
|---|
| 267 | + sb->s_cop = s_cop; |
|---|
| 268 | +} |
|---|
| 265 | 269 | #else /* !CONFIG_FS_ENCRYPTION */ |
|---|
| 266 | 270 | |
|---|
| 267 | | -static inline bool fscrypt_has_encryption_key(const struct inode *inode) |
|---|
| 271 | +static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode) |
|---|
| 268 | 272 | { |
|---|
| 269 | | - return false; |
|---|
| 273 | + return NULL; |
|---|
| 270 | 274 | } |
|---|
| 271 | 275 | |
|---|
| 272 | 276 | static inline bool fscrypt_needs_contents_encryption(const struct inode *inode) |
|---|
| 273 | 277 | { |
|---|
| 274 | 278 | return false; |
|---|
| 275 | | -} |
|---|
| 276 | | - |
|---|
| 277 | | -static inline const union fscrypt_context * |
|---|
| 278 | | -fscrypt_get_dummy_context(struct super_block *sb) |
|---|
| 279 | | -{ |
|---|
| 280 | | - return NULL; |
|---|
| 281 | 279 | } |
|---|
| 282 | 280 | |
|---|
| 283 | 281 | static inline void fscrypt_handle_d_move(struct dentry *dentry) |
|---|
| .. | .. |
|---|
| 370 | 368 | return 0; |
|---|
| 371 | 369 | } |
|---|
| 372 | 370 | |
|---|
| 373 | | -static inline int fscrypt_inherit_context(struct inode *parent, |
|---|
| 374 | | - struct inode *child, |
|---|
| 375 | | - void *fs_data, bool preload) |
|---|
| 371 | +static inline int fscrypt_set_context(struct inode *inode, void *fs_data) |
|---|
| 376 | 372 | { |
|---|
| 377 | 373 | return -EOPNOTSUPP; |
|---|
| 378 | 374 | } |
|---|
| 379 | 375 | |
|---|
| 380 | | -struct fscrypt_dummy_context { |
|---|
| 376 | +struct fscrypt_dummy_policy { |
|---|
| 381 | 377 | }; |
|---|
| 382 | 378 | |
|---|
| 383 | 379 | static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq, |
|---|
| .. | .. |
|---|
| 387 | 383 | } |
|---|
| 388 | 384 | |
|---|
| 389 | 385 | static inline void |
|---|
| 390 | | -fscrypt_free_dummy_context(struct fscrypt_dummy_context *dummy_ctx) |
|---|
| 386 | +fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy) |
|---|
| 391 | 387 | { |
|---|
| 392 | 388 | } |
|---|
| 393 | 389 | |
|---|
| 394 | 390 | /* keyring.c */ |
|---|
| 395 | | -static inline void fscrypt_sb_free(struct super_block *sb) |
|---|
| 391 | +static inline void fscrypt_destroy_keyring(struct super_block *sb) |
|---|
| 396 | 392 | { |
|---|
| 397 | 393 | } |
|---|
| 398 | 394 | |
|---|
| .. | .. |
|---|
| 418 | 414 | return -EOPNOTSUPP; |
|---|
| 419 | 415 | } |
|---|
| 420 | 416 | |
|---|
| 421 | | -static inline int fscrypt_register_key_removal_notifier( |
|---|
| 422 | | - struct notifier_block *nb) |
|---|
| 423 | | -{ |
|---|
| 424 | | - return 0; |
|---|
| 425 | | -} |
|---|
| 426 | | - |
|---|
| 427 | | -static inline int fscrypt_unregister_key_removal_notifier( |
|---|
| 428 | | - struct notifier_block *nb) |
|---|
| 429 | | -{ |
|---|
| 430 | | - return 0; |
|---|
| 431 | | -} |
|---|
| 432 | | - |
|---|
| 433 | 417 | /* keysetup.c */ |
|---|
| 434 | | -static inline int fscrypt_get_encryption_info(struct inode *inode) |
|---|
| 418 | + |
|---|
| 419 | +static inline int fscrypt_prepare_new_inode(struct inode *dir, |
|---|
| 420 | + struct inode *inode, |
|---|
| 421 | + bool *encrypt_ret) |
|---|
| 435 | 422 | { |
|---|
| 436 | | - return -EOPNOTSUPP; |
|---|
| 423 | + if (IS_ENCRYPTED(dir)) |
|---|
| 424 | + return -EOPNOTSUPP; |
|---|
| 425 | + return 0; |
|---|
| 437 | 426 | } |
|---|
| 438 | 427 | |
|---|
| 439 | 428 | static inline void fscrypt_put_encryption_info(struct inode *inode) |
|---|
| .. | .. |
|---|
| 470 | 459 | return; |
|---|
| 471 | 460 | } |
|---|
| 472 | 461 | |
|---|
| 473 | | -static inline int fscrypt_fname_alloc_buffer(const struct inode *inode, |
|---|
| 474 | | - u32 max_encrypted_len, |
|---|
| 462 | +static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len, |
|---|
| 475 | 463 | struct fscrypt_str *crypto_str) |
|---|
| 476 | 464 | { |
|---|
| 477 | 465 | return -EOPNOTSUPP; |
|---|
| .. | .. |
|---|
| 504 | 492 | { |
|---|
| 505 | 493 | WARN_ON_ONCE(1); |
|---|
| 506 | 494 | return 0; |
|---|
| 495 | +} |
|---|
| 496 | + |
|---|
| 497 | +static inline int fscrypt_d_revalidate(struct dentry *dentry, |
|---|
| 498 | + unsigned int flags) |
|---|
| 499 | +{ |
|---|
| 500 | + return 1; |
|---|
| 507 | 501 | } |
|---|
| 508 | 502 | |
|---|
| 509 | 503 | /* bio.c */ |
|---|
| .. | .. |
|---|
| 548 | 542 | return -EOPNOTSUPP; |
|---|
| 549 | 543 | } |
|---|
| 550 | 544 | |
|---|
| 545 | +static inline int __fscrypt_prepare_readdir(struct inode *dir) |
|---|
| 546 | +{ |
|---|
| 547 | + return -EOPNOTSUPP; |
|---|
| 548 | +} |
|---|
| 549 | + |
|---|
| 550 | +static inline int __fscrypt_prepare_setattr(struct dentry *dentry, |
|---|
| 551 | + struct iattr *attr) |
|---|
| 552 | +{ |
|---|
| 553 | + return -EOPNOTSUPP; |
|---|
| 554 | +} |
|---|
| 555 | + |
|---|
| 551 | 556 | static inline int fscrypt_prepare_setflags(struct inode *inode, |
|---|
| 552 | 557 | unsigned int oldflags, |
|---|
| 553 | 558 | unsigned int flags) |
|---|
| .. | .. |
|---|
| 555 | 560 | return 0; |
|---|
| 556 | 561 | } |
|---|
| 557 | 562 | |
|---|
| 558 | | -static inline int __fscrypt_prepare_symlink(struct inode *dir, |
|---|
| 559 | | - unsigned int len, |
|---|
| 560 | | - unsigned int max_len, |
|---|
| 561 | | - struct fscrypt_str *disk_link) |
|---|
| 563 | +static inline int fscrypt_prepare_symlink(struct inode *dir, |
|---|
| 564 | + const char *target, |
|---|
| 565 | + unsigned int len, |
|---|
| 566 | + unsigned int max_len, |
|---|
| 567 | + struct fscrypt_str *disk_link) |
|---|
| 562 | 568 | { |
|---|
| 563 | | - return -EOPNOTSUPP; |
|---|
| 569 | + if (IS_ENCRYPTED(dir)) |
|---|
| 570 | + return -EOPNOTSUPP; |
|---|
| 571 | + disk_link->name = (unsigned char *)target; |
|---|
| 572 | + disk_link->len = len + 1; |
|---|
| 573 | + if (disk_link->len > max_len) |
|---|
| 574 | + return -ENAMETOOLONG; |
|---|
| 575 | + return 0; |
|---|
| 564 | 576 | } |
|---|
| 565 | | - |
|---|
| 566 | 577 | |
|---|
| 567 | 578 | static inline int __fscrypt_encrypt_symlink(struct inode *inode, |
|---|
| 568 | 579 | const char *target, |
|---|
| .. | .. |
|---|
| 586 | 597 | return -EOPNOTSUPP; |
|---|
| 587 | 598 | } |
|---|
| 588 | 599 | |
|---|
| 600 | +static inline void fscrypt_set_ops(struct super_block *sb, |
|---|
| 601 | + const struct fscrypt_operations *s_cop) |
|---|
| 602 | +{ |
|---|
| 603 | +} |
|---|
| 604 | + |
|---|
| 589 | 605 | #endif /* !CONFIG_FS_ENCRYPTION */ |
|---|
| 590 | 606 | |
|---|
| 591 | 607 | /* inline_crypt.c */ |
|---|
| 592 | 608 | #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT |
|---|
| 593 | | -extern bool fscrypt_inode_uses_inline_crypto(const struct inode *inode); |
|---|
| 594 | 609 | |
|---|
| 595 | | -extern bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode); |
|---|
| 610 | +bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode); |
|---|
| 596 | 611 | |
|---|
| 597 | | -extern void fscrypt_set_bio_crypt_ctx(struct bio *bio, |
|---|
| 598 | | - const struct inode *inode, |
|---|
| 599 | | - u64 first_lblk, gfp_t gfp_mask); |
|---|
| 612 | +void fscrypt_set_bio_crypt_ctx(struct bio *bio, |
|---|
| 613 | + const struct inode *inode, u64 first_lblk, |
|---|
| 614 | + gfp_t gfp_mask); |
|---|
| 600 | 615 | |
|---|
| 601 | | -extern void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio, |
|---|
| 602 | | - const struct buffer_head *first_bh, |
|---|
| 603 | | - gfp_t gfp_mask); |
|---|
| 616 | +void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio, |
|---|
| 617 | + const struct buffer_head *first_bh, |
|---|
| 618 | + gfp_t gfp_mask); |
|---|
| 604 | 619 | |
|---|
| 605 | | -extern bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode, |
|---|
| 606 | | - u64 next_lblk); |
|---|
| 620 | +bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode, |
|---|
| 621 | + u64 next_lblk); |
|---|
| 607 | 622 | |
|---|
| 608 | | -extern bool fscrypt_mergeable_bio_bh(struct bio *bio, |
|---|
| 609 | | - const struct buffer_head *next_bh); |
|---|
| 623 | +bool fscrypt_mergeable_bio_bh(struct bio *bio, |
|---|
| 624 | + const struct buffer_head *next_bh); |
|---|
| 610 | 625 | |
|---|
| 611 | 626 | bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter); |
|---|
| 612 | 627 | |
|---|
| 613 | | -int fscrypt_limit_dio_pages(const struct inode *inode, loff_t pos, |
|---|
| 614 | | - int nr_pages); |
|---|
| 628 | +u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks); |
|---|
| 615 | 629 | |
|---|
| 616 | 630 | #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ |
|---|
| 617 | | -static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode) |
|---|
| 631 | + |
|---|
| 632 | +static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode) |
|---|
| 618 | 633 | { |
|---|
| 619 | 634 | return false; |
|---|
| 620 | | -} |
|---|
| 621 | | - |
|---|
| 622 | | -static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode) |
|---|
| 623 | | -{ |
|---|
| 624 | | - return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode); |
|---|
| 625 | 635 | } |
|---|
| 626 | 636 | |
|---|
| 627 | 637 | static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio, |
|---|
| .. | .. |
|---|
| 654 | 664 | return !fscrypt_needs_contents_encryption(inode); |
|---|
| 655 | 665 | } |
|---|
| 656 | 666 | |
|---|
| 657 | | -static inline int fscrypt_limit_dio_pages(const struct inode *inode, loff_t pos, |
|---|
| 658 | | - int nr_pages) |
|---|
| 667 | +static inline u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, |
|---|
| 668 | + u64 nr_blocks) |
|---|
| 659 | 669 | { |
|---|
| 660 | | - return nr_pages; |
|---|
| 670 | + return nr_blocks; |
|---|
| 661 | 671 | } |
|---|
| 662 | 672 | #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ |
|---|
| 663 | 673 | |
|---|
| .. | .. |
|---|
| 676 | 686 | #endif |
|---|
| 677 | 687 | |
|---|
| 678 | 688 | /** |
|---|
| 679 | | - * fscrypt_require_key() - require an inode's encryption key |
|---|
| 680 | | - * @inode: the inode we need the key for |
|---|
| 689 | + * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline |
|---|
| 690 | + * encryption |
|---|
| 691 | + * @inode: an inode. If encrypted, its key must be set up. |
|---|
| 681 | 692 | * |
|---|
| 682 | | - * If the inode is encrypted, set up its encryption key if not already done. |
|---|
| 683 | | - * Then require that the key be present and return -ENOKEY otherwise. |
|---|
| 684 | | - * |
|---|
| 685 | | - * No locks are needed, and the key will live as long as the struct inode --- so |
|---|
| 686 | | - * it won't go away from under you. |
|---|
| 687 | | - * |
|---|
| 688 | | - * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code |
|---|
| 689 | | - * if a problem occurred while setting up the encryption key. |
|---|
| 693 | + * Return: true if the inode requires file contents encryption and if the |
|---|
| 694 | + * encryption should be done in the block layer via blk-crypto rather |
|---|
| 695 | + * than in the filesystem layer. |
|---|
| 690 | 696 | */ |
|---|
| 691 | | -static inline int fscrypt_require_key(struct inode *inode) |
|---|
| 697 | +static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode) |
|---|
| 692 | 698 | { |
|---|
| 693 | | - if (IS_ENCRYPTED(inode)) { |
|---|
| 694 | | - int err = fscrypt_get_encryption_info(inode); |
|---|
| 699 | + return fscrypt_needs_contents_encryption(inode) && |
|---|
| 700 | + __fscrypt_inode_uses_inline_crypto(inode); |
|---|
| 701 | +} |
|---|
| 695 | 702 | |
|---|
| 696 | | - if (err) |
|---|
| 697 | | - return err; |
|---|
| 698 | | - if (!fscrypt_has_encryption_key(inode)) |
|---|
| 699 | | - return -ENOKEY; |
|---|
| 700 | | - } |
|---|
| 701 | | - return 0; |
|---|
| 703 | +/** |
|---|
| 704 | + * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer |
|---|
| 705 | + * encryption |
|---|
| 706 | + * @inode: an inode. If encrypted, its key must be set up. |
|---|
| 707 | + * |
|---|
| 708 | + * Return: true if the inode requires file contents encryption and if the |
|---|
| 709 | + * encryption should be done in the filesystem layer rather than in the |
|---|
| 710 | + * block layer via blk-crypto. |
|---|
| 711 | + */ |
|---|
| 712 | +static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode) |
|---|
| 713 | +{ |
|---|
| 714 | + return fscrypt_needs_contents_encryption(inode) && |
|---|
| 715 | + !__fscrypt_inode_uses_inline_crypto(inode); |
|---|
| 716 | +} |
|---|
| 717 | + |
|---|
| 718 | +/** |
|---|
| 719 | + * fscrypt_has_encryption_key() - check whether an inode has had its key set up |
|---|
| 720 | + * @inode: the inode to check |
|---|
| 721 | + * |
|---|
| 722 | + * Return: %true if the inode has had its encryption key set up, else %false. |
|---|
| 723 | + * |
|---|
| 724 | + * Usually this should be preceded by fscrypt_get_encryption_info() to try to |
|---|
| 725 | + * set up the key first. |
|---|
| 726 | + */ |
|---|
| 727 | +static inline bool fscrypt_has_encryption_key(const struct inode *inode) |
|---|
| 728 | +{ |
|---|
| 729 | + return fscrypt_get_info(inode) != NULL; |
|---|
| 702 | 730 | } |
|---|
| 703 | 731 | |
|---|
| 704 | 732 | /** |
|---|
| .. | .. |
|---|
| 710 | 738 | * |
|---|
| 711 | 739 | * A new link can only be added to an encrypted directory if the directory's |
|---|
| 712 | 740 | * encryption key is available --- since otherwise we'd have no way to encrypt |
|---|
| 713 | | - * the filename. Therefore, we first set up the directory's encryption key (if |
|---|
| 714 | | - * not already done) and return an error if it's unavailable. |
|---|
| 741 | + * the filename. |
|---|
| 715 | 742 | * |
|---|
| 716 | 743 | * We also verify that the link will not violate the constraint that all files |
|---|
| 717 | 744 | * in an encrypted directory tree use the same encryption policy. |
|---|
| .. | .. |
|---|
| 771 | 798 | * @fname: (output) the name to use to search the on-disk directory |
|---|
| 772 | 799 | * |
|---|
| 773 | 800 | * Prepare for ->lookup() in a directory which may be encrypted by determining |
|---|
| 774 | | - * the name that will actually be used to search the directory on-disk. Lookups |
|---|
| 775 | | - * can be done with or without the directory's encryption key; without the key, |
|---|
| 776 | | - * filenames are presented in encrypted form. Therefore, we'll try to set up |
|---|
| 777 | | - * the directory's encryption key, but even without it the lookup can continue. |
|---|
| 801 | + * the name that will actually be used to search the directory on-disk. If the |
|---|
| 802 | + * directory's encryption policy is supported by this kernel and its encryption |
|---|
| 803 | + * key is available, then the lookup is assumed to be by plaintext name; |
|---|
| 804 | + * otherwise, it is assumed to be by no-key name. |
|---|
| 778 | 805 | * |
|---|
| 779 | | - * After calling this function, a filesystem should ensure that it's dentry |
|---|
| 780 | | - * operations contain fscrypt_d_revalidate if DCACHE_ENCRYPTED_NAME was set, |
|---|
| 781 | | - * so that the dentry can be invalidated if the key is later added. |
|---|
| 806 | + * This will set DCACHE_NOKEY_NAME on the dentry if the lookup is by no-key |
|---|
| 807 | + * name. In this case the filesystem must assign the dentry a dentry_operations |
|---|
| 808 | + * which contains fscrypt_d_revalidate (or contains a d_revalidate method that |
|---|
| 809 | + * calls fscrypt_d_revalidate), so that the dentry will be invalidated if the |
|---|
| 810 | + * directory's encryption key is later added. |
|---|
| 782 | 811 | * |
|---|
| 783 | | - * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a |
|---|
| 784 | | - * correctly formed encoded ciphertext name, so a negative dentry should be |
|---|
| 785 | | - * created; or another -errno code. |
|---|
| 812 | + * Return: 0 on success; -ENOENT if the directory's key is unavailable but the |
|---|
| 813 | + * filename isn't a valid no-key name, so a negative dentry should be created; |
|---|
| 814 | + * or another -errno code. |
|---|
| 786 | 815 | */ |
|---|
| 787 | 816 | static inline int fscrypt_prepare_lookup(struct inode *dir, |
|---|
| 788 | 817 | struct dentry *dentry, |
|---|
| .. | .. |
|---|
| 795 | 824 | fname->usr_fname = &dentry->d_name; |
|---|
| 796 | 825 | fname->disk_name.name = (unsigned char *)dentry->d_name.name; |
|---|
| 797 | 826 | fname->disk_name.len = dentry->d_name.len; |
|---|
| 827 | + return 0; |
|---|
| 828 | +} |
|---|
| 829 | + |
|---|
| 830 | +/** |
|---|
| 831 | + * fscrypt_prepare_readdir() - prepare to read a possibly-encrypted directory |
|---|
| 832 | + * @dir: the directory inode |
|---|
| 833 | + * |
|---|
| 834 | + * If the directory is encrypted and it doesn't already have its encryption key |
|---|
| 835 | + * set up, try to set it up so that the filenames will be listed in plaintext |
|---|
| 836 | + * form rather than in no-key form. |
|---|
| 837 | + * |
|---|
| 838 | + * Return: 0 on success; -errno on error. Note that the encryption key being |
|---|
| 839 | + * unavailable is not considered an error. It is also not an error if |
|---|
| 840 | + * the encryption policy is unsupported by this kernel; that is treated |
|---|
| 841 | + * like the key being unavailable, so that files can still be deleted. |
|---|
| 842 | + */ |
|---|
| 843 | +static inline int fscrypt_prepare_readdir(struct inode *dir) |
|---|
| 844 | +{ |
|---|
| 845 | + if (IS_ENCRYPTED(dir)) |
|---|
| 846 | + return __fscrypt_prepare_readdir(dir); |
|---|
| 798 | 847 | return 0; |
|---|
| 799 | 848 | } |
|---|
| 800 | 849 | |
|---|
| .. | .. |
|---|
| 819 | 868 | static inline int fscrypt_prepare_setattr(struct dentry *dentry, |
|---|
| 820 | 869 | struct iattr *attr) |
|---|
| 821 | 870 | { |
|---|
| 822 | | - if (attr->ia_valid & ATTR_SIZE) |
|---|
| 823 | | - return fscrypt_require_key(d_inode(dentry)); |
|---|
| 824 | | - return 0; |
|---|
| 825 | | -} |
|---|
| 826 | | - |
|---|
| 827 | | -/** |
|---|
| 828 | | - * fscrypt_prepare_symlink() - prepare to create a possibly-encrypted symlink |
|---|
| 829 | | - * @dir: directory in which the symlink is being created |
|---|
| 830 | | - * @target: plaintext symlink target |
|---|
| 831 | | - * @len: length of @target excluding null terminator |
|---|
| 832 | | - * @max_len: space the filesystem has available to store the symlink target |
|---|
| 833 | | - * @disk_link: (out) the on-disk symlink target being prepared |
|---|
| 834 | | - * |
|---|
| 835 | | - * This function computes the size the symlink target will require on-disk, |
|---|
| 836 | | - * stores it in @disk_link->len, and validates it against @max_len. An |
|---|
| 837 | | - * encrypted symlink may be longer than the original. |
|---|
| 838 | | - * |
|---|
| 839 | | - * Additionally, @disk_link->name is set to @target if the symlink will be |
|---|
| 840 | | - * unencrypted, but left NULL if the symlink will be encrypted. For encrypted |
|---|
| 841 | | - * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the |
|---|
| 842 | | - * on-disk target later. (The reason for the two-step process is that some |
|---|
| 843 | | - * filesystems need to know the size of the symlink target before creating the |
|---|
| 844 | | - * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.) |
|---|
| 845 | | - * |
|---|
| 846 | | - * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long, |
|---|
| 847 | | - * -ENOKEY if the encryption key is missing, or another -errno code if a problem |
|---|
| 848 | | - * occurred while setting up the encryption key. |
|---|
| 849 | | - */ |
|---|
| 850 | | -static inline int fscrypt_prepare_symlink(struct inode *dir, |
|---|
| 851 | | - const char *target, |
|---|
| 852 | | - unsigned int len, |
|---|
| 853 | | - unsigned int max_len, |
|---|
| 854 | | - struct fscrypt_str *disk_link) |
|---|
| 855 | | -{ |
|---|
| 856 | | - if (IS_ENCRYPTED(dir) || fscrypt_get_dummy_context(dir->i_sb) != NULL) |
|---|
| 857 | | - return __fscrypt_prepare_symlink(dir, len, max_len, disk_link); |
|---|
| 858 | | - |
|---|
| 859 | | - disk_link->name = (unsigned char *)target; |
|---|
| 860 | | - disk_link->len = len + 1; |
|---|
| 861 | | - if (disk_link->len > max_len) |
|---|
| 862 | | - return -ENAMETOOLONG; |
|---|
| 871 | + if (IS_ENCRYPTED(d_inode(dentry))) |
|---|
| 872 | + return __fscrypt_prepare_setattr(dentry, attr); |
|---|
| 863 | 873 | return 0; |
|---|
| 864 | 874 | } |
|---|
| 865 | 875 | |
|---|