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