1 /* SPDX-License-Identifier: GPL-2.0 */
3 * fscrypt.h: declarations for per-file encryption
5 * Filesystems that implement per-file encryption must include this header
8 * Copyright (C) 2015, Google, Inc.
10 * Written by Michael Halcrow, 2015.
11 * Modified by Jaegeuk Kim, 2015.
13 #ifndef _LINUX_FSCRYPT_H
14 #define _LINUX_FSCRYPT_H
18 #include <linux/parser.h>
19 #include <linux/slab.h>
20 #include <uapi/linux/fscrypt.h>
22 #define FS_CRYPTO_BLOCK_SIZE 16
24 union fscrypt_context;
34 const struct qstr *usr_fname;
35 struct fscrypt_str disk_name;
38 struct fscrypt_str crypto_buf;
39 bool is_ciphertext_name;
42 #define FSTR_INIT(n, l) { .name = n, .len = l }
43 #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
44 #define fname_name(p) ((p)->disk_name.name)
45 #define fname_len(p) ((p)->disk_name.len)
47 /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
48 #define FSCRYPT_SET_CONTEXT_MAX_SIZE 40
50 #ifdef CONFIG_FS_ENCRYPTION
52 * fscrypt superblock flags
54 #define FS_CFLG_OWN_PAGES (1U << 1)
57 * crypto operations for filesystems
59 struct fscrypt_operations {
61 const char *key_prefix;
62 int (*get_context)(struct inode *inode, void *ctx, size_t len);
63 int (*set_context)(struct inode *inode, const void *ctx, size_t len,
65 const union fscrypt_context *(*get_dummy_context)(
66 struct super_block *sb);
67 bool (*empty_dir)(struct inode *inode);
68 unsigned int max_namelen;
69 bool (*has_stable_inodes)(struct super_block *sb);
70 void (*get_ino_and_lblk_bits)(struct super_block *sb,
71 int *ino_bits_ret, int *lblk_bits_ret);
72 int (*get_num_devices)(struct super_block *sb);
73 void (*get_devices)(struct super_block *sb,
74 struct request_queue **devs);
77 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
79 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
80 return READ_ONCE(inode->i_crypt_info) != NULL;
84 * fscrypt_needs_contents_encryption() - check whether an inode needs
86 * @inode: the inode to check
88 * Return: %true iff the inode is an encrypted regular file and the kernel was
89 * built with fscrypt support.
91 * If you need to know whether the encrypt bit is set even when the kernel was
92 * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
94 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
96 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
99 static inline const union fscrypt_context *
100 fscrypt_get_dummy_context(struct super_block *sb)
102 if (!sb->s_cop->get_dummy_context)
104 return sb->s_cop->get_dummy_context(sb);
108 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
109 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
110 * must be cleared. Note that we don't have to support arbitrary moves of this
111 * flag because fscrypt doesn't allow encrypted aliases to be the source or
112 * target of a rename().
114 static inline void fscrypt_handle_d_move(struct dentry *dentry)
116 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
120 void fscrypt_enqueue_decrypt_work(struct work_struct *);
122 struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
126 int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
127 unsigned int len, unsigned int offs,
128 u64 lblk_num, gfp_t gfp_flags);
130 int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
132 int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
133 unsigned int len, unsigned int offs,
136 static inline bool fscrypt_is_bounce_page(struct page *page)
138 return page->mapping == NULL;
141 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
143 return (struct page *)page_private(bounce_page);
146 void fscrypt_free_bounce_page(struct page *bounce_page);
149 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
150 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
151 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
152 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
153 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
154 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
155 void *fs_data, bool preload);
157 struct fscrypt_dummy_context {
158 const union fscrypt_context *ctx;
161 int fscrypt_set_test_dummy_encryption(struct super_block *sb,
162 const substring_t *arg,
163 struct fscrypt_dummy_context *dummy_ctx);
164 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
165 struct super_block *sb);
167 fscrypt_free_dummy_context(struct fscrypt_dummy_context *dummy_ctx)
169 kfree(dummy_ctx->ctx);
170 dummy_ctx->ctx = NULL;
174 void fscrypt_sb_free(struct super_block *sb);
175 int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
176 int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
177 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
178 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
181 int fscrypt_get_encryption_info(struct inode *inode);
182 void fscrypt_put_encryption_info(struct inode *inode);
183 void fscrypt_free_inode(struct inode *inode);
184 int fscrypt_drop_inode(struct inode *inode);
187 int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
188 int lookup, struct fscrypt_name *fname);
190 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
192 kfree(fname->crypto_buf.name);
195 int fscrypt_fname_alloc_buffer(const struct inode *inode, u32 max_encrypted_len,
196 struct fscrypt_str *crypto_str);
197 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
198 int fscrypt_fname_disk_to_usr(const struct inode *inode,
199 u32 hash, u32 minor_hash,
200 const struct fscrypt_str *iname,
201 struct fscrypt_str *oname);
202 bool fscrypt_match_name(const struct fscrypt_name *fname,
203 const u8 *de_name, u32 de_name_len);
204 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
207 void fscrypt_decrypt_bio(struct bio *bio);
208 int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
209 sector_t pblk, unsigned int len);
212 int fscrypt_file_open(struct inode *inode, struct file *filp);
213 int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
214 struct dentry *dentry);
215 int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
216 struct inode *new_dir, struct dentry *new_dentry,
218 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
219 struct fscrypt_name *fname);
220 int fscrypt_prepare_setflags(struct inode *inode,
221 unsigned int oldflags, unsigned int flags);
222 int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
223 unsigned int max_len,
224 struct fscrypt_str *disk_link);
225 int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
226 unsigned int len, struct fscrypt_str *disk_link);
227 const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
228 unsigned int max_size,
229 struct delayed_call *done);
230 static inline void fscrypt_set_ops(struct super_block *sb,
231 const struct fscrypt_operations *s_cop)
235 #else /* !CONFIG_FS_ENCRYPTION */
237 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
242 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
247 static inline const union fscrypt_context *
248 fscrypt_get_dummy_context(struct super_block *sb)
253 static inline void fscrypt_handle_d_move(struct dentry *dentry)
258 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
262 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
267 return ERR_PTR(-EOPNOTSUPP);
270 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
273 unsigned int offs, u64 lblk_num,
279 static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
286 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
289 unsigned int offs, u64 lblk_num)
294 static inline bool fscrypt_is_bounce_page(struct page *page)
299 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
302 return ERR_PTR(-EINVAL);
305 static inline void fscrypt_free_bounce_page(struct page *bounce_page)
310 static inline int fscrypt_ioctl_set_policy(struct file *filp,
311 const void __user *arg)
316 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
321 static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
327 static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
332 static inline int fscrypt_has_permitted_context(struct inode *parent,
338 static inline int fscrypt_inherit_context(struct inode *parent,
340 void *fs_data, bool preload)
345 struct fscrypt_dummy_context {
348 static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
350 struct super_block *sb)
355 fscrypt_free_dummy_context(struct fscrypt_dummy_context *dummy_ctx)
360 static inline void fscrypt_sb_free(struct super_block *sb)
364 static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
369 static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
374 static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
380 static inline int fscrypt_ioctl_get_key_status(struct file *filp,
387 static inline int fscrypt_get_encryption_info(struct inode *inode)
392 static inline void fscrypt_put_encryption_info(struct inode *inode)
397 static inline void fscrypt_free_inode(struct inode *inode)
401 static inline int fscrypt_drop_inode(struct inode *inode)
407 static inline int fscrypt_setup_filename(struct inode *dir,
408 const struct qstr *iname,
409 int lookup, struct fscrypt_name *fname)
411 if (IS_ENCRYPTED(dir))
414 memset(fname, 0, sizeof(*fname));
415 fname->usr_fname = iname;
416 fname->disk_name.name = (unsigned char *)iname->name;
417 fname->disk_name.len = iname->len;
421 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
426 static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
427 u32 max_encrypted_len,
428 struct fscrypt_str *crypto_str)
433 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
438 static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
439 u32 hash, u32 minor_hash,
440 const struct fscrypt_str *iname,
441 struct fscrypt_str *oname)
446 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
447 const u8 *de_name, u32 de_name_len)
449 /* Encryption support disabled; use standard comparison */
450 if (de_name_len != fname->disk_name.len)
452 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
455 static inline u64 fscrypt_fname_siphash(const struct inode *dir,
456 const struct qstr *name)
463 static inline void fscrypt_decrypt_bio(struct bio *bio)
467 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
468 sector_t pblk, unsigned int len)
475 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
477 if (IS_ENCRYPTED(inode))
482 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
483 struct dentry *dentry)
488 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
489 struct dentry *old_dentry,
490 struct inode *new_dir,
491 struct dentry *new_dentry,
497 static inline int __fscrypt_prepare_lookup(struct inode *dir,
498 struct dentry *dentry,
499 struct fscrypt_name *fname)
504 static inline int fscrypt_prepare_setflags(struct inode *inode,
505 unsigned int oldflags,
511 static inline int __fscrypt_prepare_symlink(struct inode *dir,
513 unsigned int max_len,
514 struct fscrypt_str *disk_link)
520 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
523 struct fscrypt_str *disk_link)
528 static inline const char *fscrypt_get_symlink(struct inode *inode,
530 unsigned int max_size,
531 struct delayed_call *done)
533 return ERR_PTR(-EOPNOTSUPP);
536 static inline void fscrypt_set_ops(struct super_block *sb,
537 const struct fscrypt_operations *s_cop)
541 #endif /* !CONFIG_FS_ENCRYPTION */
544 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
546 bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
548 void fscrypt_set_bio_crypt_ctx(struct bio *bio,
549 const struct inode *inode, u64 first_lblk,
552 void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
553 const struct buffer_head *first_bh,
556 bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
559 bool fscrypt_mergeable_bio_bh(struct bio *bio,
560 const struct buffer_head *next_bh);
562 #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
564 static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
569 static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
570 const struct inode *inode,
571 u64 first_lblk, gfp_t gfp_mask) { }
573 static inline void fscrypt_set_bio_crypt_ctx_bh(
575 const struct buffer_head *first_bh,
578 static inline bool fscrypt_mergeable_bio(struct bio *bio,
579 const struct inode *inode,
585 static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
586 const struct buffer_head *next_bh)
590 #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
593 * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
595 * @inode: an inode. If encrypted, its key must be set up.
597 * Return: true if the inode requires file contents encryption and if the
598 * encryption should be done in the block layer via blk-crypto rather
599 * than in the filesystem layer.
601 static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
603 return fscrypt_needs_contents_encryption(inode) &&
604 __fscrypt_inode_uses_inline_crypto(inode);
608 * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
610 * @inode: an inode. If encrypted, its key must be set up.
612 * Return: true if the inode requires file contents encryption and if the
613 * encryption should be done in the filesystem layer rather than in the
614 * block layer via blk-crypto.
616 static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
618 return fscrypt_needs_contents_encryption(inode) &&
619 !__fscrypt_inode_uses_inline_crypto(inode);
623 * fscrypt_require_key() - require an inode's encryption key
624 * @inode: the inode we need the key for
626 * If the inode is encrypted, set up its encryption key if not already done.
627 * Then require that the key be present and return -ENOKEY otherwise.
629 * No locks are needed, and the key will live as long as the struct inode --- so
630 * it won't go away from under you.
632 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
633 * if a problem occurred while setting up the encryption key.
635 static inline int fscrypt_require_key(struct inode *inode)
637 if (IS_ENCRYPTED(inode)) {
638 int err = fscrypt_get_encryption_info(inode);
642 if (!fscrypt_has_encryption_key(inode))
649 * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
651 * @old_dentry: an existing dentry for the inode being linked
652 * @dir: the target directory
653 * @dentry: negative dentry for the target filename
655 * A new link can only be added to an encrypted directory if the directory's
656 * encryption key is available --- since otherwise we'd have no way to encrypt
657 * the filename. Therefore, we first set up the directory's encryption key (if
658 * not already done) and return an error if it's unavailable.
660 * We also verify that the link will not violate the constraint that all files
661 * in an encrypted directory tree use the same encryption policy.
663 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
664 * -EXDEV if the link would result in an inconsistent encryption policy, or
665 * another -errno code.
667 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
669 struct dentry *dentry)
671 if (IS_ENCRYPTED(dir))
672 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
677 * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
679 * @old_dir: source directory
680 * @old_dentry: dentry for source file
681 * @new_dir: target directory
682 * @new_dentry: dentry for target location (may be negative unless exchanging)
683 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
685 * Prepare for ->rename() where the source and/or target directories may be
686 * encrypted. A new link can only be added to an encrypted directory if the
687 * directory's encryption key is available --- since otherwise we'd have no way
688 * to encrypt the filename. A rename to an existing name, on the other hand,
689 * *is* cryptographically possible without the key. However, we take the more
690 * conservative approach and just forbid all no-key renames.
692 * We also verify that the rename will not violate the constraint that all files
693 * in an encrypted directory tree use the same encryption policy.
695 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
696 * rename would cause inconsistent encryption policies, or another -errno code.
698 static inline int fscrypt_prepare_rename(struct inode *old_dir,
699 struct dentry *old_dentry,
700 struct inode *new_dir,
701 struct dentry *new_dentry,
704 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
705 return __fscrypt_prepare_rename(old_dir, old_dentry,
706 new_dir, new_dentry, flags);
711 * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
713 * @dir: directory being searched
714 * @dentry: filename being looked up
715 * @fname: (output) the name to use to search the on-disk directory
717 * Prepare for ->lookup() in a directory which may be encrypted by determining
718 * the name that will actually be used to search the directory on-disk. Lookups
719 * can be done with or without the directory's encryption key; without the key,
720 * filenames are presented in encrypted form. Therefore, we'll try to set up
721 * the directory's encryption key, but even without it the lookup can continue.
723 * This also installs a custom ->d_revalidate() method which will invalidate the
724 * dentry if it was created without the key and the key is later added.
726 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
727 * correctly formed encoded ciphertext name, so a negative dentry should be
728 * created; or another -errno code.
730 static inline int fscrypt_prepare_lookup(struct inode *dir,
731 struct dentry *dentry,
732 struct fscrypt_name *fname)
734 if (IS_ENCRYPTED(dir))
735 return __fscrypt_prepare_lookup(dir, dentry, fname);
737 memset(fname, 0, sizeof(*fname));
738 fname->usr_fname = &dentry->d_name;
739 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
740 fname->disk_name.len = dentry->d_name.len;
745 * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
747 * @dentry: dentry through which the inode is being changed
748 * @attr: attributes to change
750 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
751 * most attribute changes are allowed even without the encryption key. However,
752 * without the encryption key we do have to forbid truncates. This is needed
753 * because the size being truncated to may not be a multiple of the filesystem
754 * block size, and in that case we'd have to decrypt the final block, zero the
755 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
756 * filesystem block boundary, but it's simpler to just forbid all truncates ---
757 * and we already forbid all other contents modifications without the key.)
759 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
760 * if a problem occurred while setting up the encryption key.
762 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
765 if (attr->ia_valid & ATTR_SIZE)
766 return fscrypt_require_key(d_inode(dentry));
771 * fscrypt_prepare_symlink() - prepare to create a possibly-encrypted symlink
772 * @dir: directory in which the symlink is being created
773 * @target: plaintext symlink target
774 * @len: length of @target excluding null terminator
775 * @max_len: space the filesystem has available to store the symlink target
776 * @disk_link: (out) the on-disk symlink target being prepared
778 * This function computes the size the symlink target will require on-disk,
779 * stores it in @disk_link->len, and validates it against @max_len. An
780 * encrypted symlink may be longer than the original.
782 * Additionally, @disk_link->name is set to @target if the symlink will be
783 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
784 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
785 * on-disk target later. (The reason for the two-step process is that some
786 * filesystems need to know the size of the symlink target before creating the
787 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
789 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
790 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
791 * occurred while setting up the encryption key.
793 static inline int fscrypt_prepare_symlink(struct inode *dir,
796 unsigned int max_len,
797 struct fscrypt_str *disk_link)
799 if (IS_ENCRYPTED(dir) || fscrypt_get_dummy_context(dir->i_sb) != NULL)
800 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
802 disk_link->name = (unsigned char *)target;
803 disk_link->len = len + 1;
804 if (disk_link->len > max_len)
805 return -ENAMETOOLONG;
810 * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
811 * @inode: symlink inode
812 * @target: plaintext symlink target
813 * @len: length of @target excluding null terminator
814 * @disk_link: (in/out) the on-disk symlink target being prepared
816 * If the symlink target needs to be encrypted, then this function encrypts it
817 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
818 * previously to compute @disk_link->len. If the filesystem did not allocate a
819 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
820 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
822 * Return: 0 on success, -errno on failure
824 static inline int fscrypt_encrypt_symlink(struct inode *inode,
827 struct fscrypt_str *disk_link)
829 if (IS_ENCRYPTED(inode))
830 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
834 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
835 static inline void fscrypt_finalize_bounce_page(struct page **pagep)
837 struct page *page = *pagep;
839 if (fscrypt_is_bounce_page(page)) {
840 *pagep = fscrypt_pagecache_page(page);
841 fscrypt_free_bounce_page(page);
845 #endif /* _LINUX_FSCRYPT_H */