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/slab.h>
19 #include <uapi/linux/fscrypt.h>
21 #define FS_CRYPTO_BLOCK_SIZE 16
31 const struct qstr *usr_fname;
32 struct fscrypt_str disk_name;
35 struct fscrypt_str crypto_buf;
36 bool is_ciphertext_name;
39 #define FSTR_INIT(n, l) { .name = n, .len = l }
40 #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
41 #define fname_name(p) ((p)->disk_name.name)
42 #define fname_len(p) ((p)->disk_name.len)
44 /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
45 #define FSCRYPT_SET_CONTEXT_MAX_SIZE 40
47 #ifdef CONFIG_FS_ENCRYPTION
49 * fscrypt superblock flags
51 #define FS_CFLG_OWN_PAGES (1U << 1)
54 * crypto operations for filesystems
56 struct fscrypt_operations {
58 const char *key_prefix;
59 int (*get_context)(struct inode *, void *, size_t);
60 int (*set_context)(struct inode *, const void *, size_t, void *);
61 bool (*dummy_context)(struct inode *);
62 bool (*empty_dir)(struct inode *);
63 unsigned int max_namelen;
64 bool (*has_stable_inodes)(struct super_block *sb);
65 void (*get_ino_and_lblk_bits)(struct super_block *sb,
66 int *ino_bits_ret, int *lblk_bits_ret);
69 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
71 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
72 return READ_ONCE(inode->i_crypt_info) != NULL;
76 * fscrypt_needs_contents_encryption() - check whether an inode needs
79 * Return: %true iff the inode is an encrypted regular file and the kernel was
80 * built with fscrypt support.
82 * If you need to know whether the encrypt bit is set even when the kernel was
83 * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
85 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
87 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
90 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
92 return inode->i_sb->s_cop->dummy_context &&
93 inode->i_sb->s_cop->dummy_context(inode);
97 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
98 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
99 * must be cleared. Note that we don't have to support arbitrary moves of this
100 * flag because fscrypt doesn't allow encrypted aliases to be the source or
101 * target of a rename().
103 static inline void fscrypt_handle_d_move(struct dentry *dentry)
105 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
109 extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
111 extern struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
115 extern int fscrypt_encrypt_block_inplace(const struct inode *inode,
116 struct page *page, unsigned int len,
117 unsigned int offs, u64 lblk_num,
120 extern int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
122 extern int fscrypt_decrypt_block_inplace(const struct inode *inode,
123 struct page *page, unsigned int len,
124 unsigned int offs, u64 lblk_num);
126 static inline bool fscrypt_is_bounce_page(struct page *page)
128 return page->mapping == NULL;
131 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
133 return (struct page *)page_private(bounce_page);
136 extern void fscrypt_free_bounce_page(struct page *bounce_page);
139 extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
140 extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
141 extern int fscrypt_ioctl_get_policy_ex(struct file *, void __user *);
142 extern int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
143 extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
144 extern int fscrypt_inherit_context(struct inode *, struct inode *,
147 extern void fscrypt_sb_free(struct super_block *sb);
148 extern int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
149 extern int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
150 extern int fscrypt_ioctl_remove_key_all_users(struct file *filp,
152 extern int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
155 extern int fscrypt_get_encryption_info(struct inode *);
156 extern void fscrypt_put_encryption_info(struct inode *);
157 extern void fscrypt_free_inode(struct inode *);
158 extern int fscrypt_drop_inode(struct inode *inode);
161 extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
162 int lookup, struct fscrypt_name *);
164 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
166 kfree(fname->crypto_buf.name);
169 extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
170 struct fscrypt_str *);
171 extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
172 extern int fscrypt_fname_disk_to_usr(const struct inode *inode,
173 u32 hash, u32 minor_hash,
174 const struct fscrypt_str *iname,
175 struct fscrypt_str *oname);
176 extern bool fscrypt_match_name(const struct fscrypt_name *fname,
177 const u8 *de_name, u32 de_name_len);
178 extern u64 fscrypt_fname_siphash(const struct inode *dir,
179 const struct qstr *name);
182 extern void fscrypt_decrypt_bio(struct bio *);
183 extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
187 extern int fscrypt_file_open(struct inode *inode, struct file *filp);
188 extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
189 struct dentry *dentry);
190 extern int __fscrypt_prepare_rename(struct inode *old_dir,
191 struct dentry *old_dentry,
192 struct inode *new_dir,
193 struct dentry *new_dentry,
195 extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
196 struct fscrypt_name *fname);
197 extern int fscrypt_prepare_setflags(struct inode *inode,
198 unsigned int oldflags, unsigned int flags);
199 extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
200 unsigned int max_len,
201 struct fscrypt_str *disk_link);
202 extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
204 struct fscrypt_str *disk_link);
205 extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
206 unsigned int max_size,
207 struct delayed_call *done);
208 static inline void fscrypt_set_ops(struct super_block *sb,
209 const struct fscrypt_operations *s_cop)
213 #else /* !CONFIG_FS_ENCRYPTION */
215 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
220 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
225 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
230 static inline void fscrypt_handle_d_move(struct dentry *dentry)
235 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
239 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
244 return ERR_PTR(-EOPNOTSUPP);
247 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
250 unsigned int offs, u64 lblk_num,
256 static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
263 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
266 unsigned int offs, u64 lblk_num)
271 static inline bool fscrypt_is_bounce_page(struct page *page)
276 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
279 return ERR_PTR(-EINVAL);
282 static inline void fscrypt_free_bounce_page(struct page *bounce_page)
287 static inline int fscrypt_ioctl_set_policy(struct file *filp,
288 const void __user *arg)
293 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
298 static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
304 static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
309 static inline int fscrypt_has_permitted_context(struct inode *parent,
315 static inline int fscrypt_inherit_context(struct inode *parent,
317 void *fs_data, bool preload)
323 static inline void fscrypt_sb_free(struct super_block *sb)
327 static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
332 static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
337 static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
343 static inline int fscrypt_ioctl_get_key_status(struct file *filp,
350 static inline int fscrypt_get_encryption_info(struct inode *inode)
355 static inline void fscrypt_put_encryption_info(struct inode *inode)
360 static inline void fscrypt_free_inode(struct inode *inode)
364 static inline int fscrypt_drop_inode(struct inode *inode)
370 static inline int fscrypt_setup_filename(struct inode *dir,
371 const struct qstr *iname,
372 int lookup, struct fscrypt_name *fname)
374 if (IS_ENCRYPTED(dir))
377 memset(fname, 0, sizeof(*fname));
378 fname->usr_fname = iname;
379 fname->disk_name.name = (unsigned char *)iname->name;
380 fname->disk_name.len = iname->len;
384 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
389 static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
390 u32 max_encrypted_len,
391 struct fscrypt_str *crypto_str)
396 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
401 static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
402 u32 hash, u32 minor_hash,
403 const struct fscrypt_str *iname,
404 struct fscrypt_str *oname)
409 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
410 const u8 *de_name, u32 de_name_len)
412 /* Encryption support disabled; use standard comparison */
413 if (de_name_len != fname->disk_name.len)
415 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
418 static inline u64 fscrypt_fname_siphash(const struct inode *dir,
419 const struct qstr *name)
426 static inline void fscrypt_decrypt_bio(struct bio *bio)
430 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
431 sector_t pblk, unsigned int len)
438 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
440 if (IS_ENCRYPTED(inode))
445 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
446 struct dentry *dentry)
451 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
452 struct dentry *old_dentry,
453 struct inode *new_dir,
454 struct dentry *new_dentry,
460 static inline int __fscrypt_prepare_lookup(struct inode *dir,
461 struct dentry *dentry,
462 struct fscrypt_name *fname)
467 static inline int fscrypt_prepare_setflags(struct inode *inode,
468 unsigned int oldflags,
474 static inline int __fscrypt_prepare_symlink(struct inode *dir,
476 unsigned int max_len,
477 struct fscrypt_str *disk_link)
483 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
486 struct fscrypt_str *disk_link)
491 static inline const char *fscrypt_get_symlink(struct inode *inode,
493 unsigned int max_size,
494 struct delayed_call *done)
496 return ERR_PTR(-EOPNOTSUPP);
499 static inline void fscrypt_set_ops(struct super_block *sb,
500 const struct fscrypt_operations *s_cop)
504 #endif /* !CONFIG_FS_ENCRYPTION */
507 * fscrypt_require_key - require an inode's encryption key
508 * @inode: the inode we need the key for
510 * If the inode is encrypted, set up its encryption key if not already done.
511 * Then require that the key be present and return -ENOKEY otherwise.
513 * No locks are needed, and the key will live as long as the struct inode --- so
514 * it won't go away from under you.
516 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
517 * if a problem occurred while setting up the encryption key.
519 static inline int fscrypt_require_key(struct inode *inode)
521 if (IS_ENCRYPTED(inode)) {
522 int err = fscrypt_get_encryption_info(inode);
526 if (!fscrypt_has_encryption_key(inode))
533 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
534 * @old_dentry: an existing dentry for the inode being linked
535 * @dir: the target directory
536 * @dentry: negative dentry for the target filename
538 * A new link can only be added to an encrypted directory if the directory's
539 * encryption key is available --- since otherwise we'd have no way to encrypt
540 * the filename. Therefore, we first set up the directory's encryption key (if
541 * not already done) and return an error if it's unavailable.
543 * We also verify that the link will not violate the constraint that all files
544 * in an encrypted directory tree use the same encryption policy.
546 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
547 * -EXDEV if the link would result in an inconsistent encryption policy, or
548 * another -errno code.
550 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
552 struct dentry *dentry)
554 if (IS_ENCRYPTED(dir))
555 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
560 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
561 * @old_dir: source directory
562 * @old_dentry: dentry for source file
563 * @new_dir: target directory
564 * @new_dentry: dentry for target location (may be negative unless exchanging)
565 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
567 * Prepare for ->rename() where the source and/or target directories may be
568 * encrypted. A new link can only be added to an encrypted directory if the
569 * directory's encryption key is available --- since otherwise we'd have no way
570 * to encrypt the filename. A rename to an existing name, on the other hand,
571 * *is* cryptographically possible without the key. However, we take the more
572 * conservative approach and just forbid all no-key renames.
574 * We also verify that the rename will not violate the constraint that all files
575 * in an encrypted directory tree use the same encryption policy.
577 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
578 * rename would cause inconsistent encryption policies, or another -errno code.
580 static inline int fscrypt_prepare_rename(struct inode *old_dir,
581 struct dentry *old_dentry,
582 struct inode *new_dir,
583 struct dentry *new_dentry,
586 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
587 return __fscrypt_prepare_rename(old_dir, old_dentry,
588 new_dir, new_dentry, flags);
593 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
594 * @dir: directory being searched
595 * @dentry: filename being looked up
596 * @fname: (output) the name to use to search the on-disk directory
598 * Prepare for ->lookup() in a directory which may be encrypted by determining
599 * the name that will actually be used to search the directory on-disk. Lookups
600 * can be done with or without the directory's encryption key; without the key,
601 * filenames are presented in encrypted form. Therefore, we'll try to set up
602 * the directory's encryption key, but even without it the lookup can continue.
604 * This also installs a custom ->d_revalidate() method which will invalidate the
605 * dentry if it was created without the key and the key is later added.
607 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
608 * correctly formed encoded ciphertext name, so a negative dentry should be
609 * created; or another -errno code.
611 static inline int fscrypt_prepare_lookup(struct inode *dir,
612 struct dentry *dentry,
613 struct fscrypt_name *fname)
615 if (IS_ENCRYPTED(dir))
616 return __fscrypt_prepare_lookup(dir, dentry, fname);
618 memset(fname, 0, sizeof(*fname));
619 fname->usr_fname = &dentry->d_name;
620 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
621 fname->disk_name.len = dentry->d_name.len;
626 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
627 * @dentry: dentry through which the inode is being changed
628 * @attr: attributes to change
630 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
631 * most attribute changes are allowed even without the encryption key. However,
632 * without the encryption key we do have to forbid truncates. This is needed
633 * because the size being truncated to may not be a multiple of the filesystem
634 * block size, and in that case we'd have to decrypt the final block, zero the
635 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
636 * filesystem block boundary, but it's simpler to just forbid all truncates ---
637 * and we already forbid all other contents modifications without the key.)
639 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
640 * if a problem occurred while setting up the encryption key.
642 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
645 if (attr->ia_valid & ATTR_SIZE)
646 return fscrypt_require_key(d_inode(dentry));
651 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
652 * @dir: directory in which the symlink is being created
653 * @target: plaintext symlink target
654 * @len: length of @target excluding null terminator
655 * @max_len: space the filesystem has available to store the symlink target
656 * @disk_link: (out) the on-disk symlink target being prepared
658 * This function computes the size the symlink target will require on-disk,
659 * stores it in @disk_link->len, and validates it against @max_len. An
660 * encrypted symlink may be longer than the original.
662 * Additionally, @disk_link->name is set to @target if the symlink will be
663 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
664 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
665 * on-disk target later. (The reason for the two-step process is that some
666 * filesystems need to know the size of the symlink target before creating the
667 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
669 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
670 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
671 * occurred while setting up the encryption key.
673 static inline int fscrypt_prepare_symlink(struct inode *dir,
676 unsigned int max_len,
677 struct fscrypt_str *disk_link)
679 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
680 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
682 disk_link->name = (unsigned char *)target;
683 disk_link->len = len + 1;
684 if (disk_link->len > max_len)
685 return -ENAMETOOLONG;
690 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
691 * @inode: symlink inode
692 * @target: plaintext symlink target
693 * @len: length of @target excluding null terminator
694 * @disk_link: (in/out) the on-disk symlink target being prepared
696 * If the symlink target needs to be encrypted, then this function encrypts it
697 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
698 * previously to compute @disk_link->len. If the filesystem did not allocate a
699 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
700 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
702 * Return: 0 on success, -errno on failure
704 static inline int fscrypt_encrypt_symlink(struct inode *inode,
707 struct fscrypt_str *disk_link)
709 if (IS_ENCRYPTED(inode))
710 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
714 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
715 static inline void fscrypt_finalize_bounce_page(struct page **pagep)
717 struct page *page = *pagep;
719 if (fscrypt_is_bounce_page(page)) {
720 *pagep = fscrypt_pagecache_page(page);
721 fscrypt_free_bounce_page(page);
725 #endif /* _LINUX_FSCRYPT_H */