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_has_permitted_context(struct inode *, struct inode *);
143 extern int fscrypt_inherit_context(struct inode *, struct inode *,
146 extern void fscrypt_sb_free(struct super_block *sb);
147 extern int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
148 extern int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
149 extern int fscrypt_ioctl_remove_key_all_users(struct file *filp,
151 extern int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
154 extern int fscrypt_get_encryption_info(struct inode *);
155 extern void fscrypt_put_encryption_info(struct inode *);
156 extern void fscrypt_free_inode(struct inode *);
157 extern int fscrypt_drop_inode(struct inode *inode);
160 extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
161 int lookup, struct fscrypt_name *);
163 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
165 kfree(fname->crypto_buf.name);
168 extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
169 struct fscrypt_str *);
170 extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
171 extern int fscrypt_fname_disk_to_usr(const struct inode *inode,
172 u32 hash, u32 minor_hash,
173 const struct fscrypt_str *iname,
174 struct fscrypt_str *oname);
175 extern bool fscrypt_match_name(const struct fscrypt_name *fname,
176 const u8 *de_name, u32 de_name_len);
177 extern u64 fscrypt_fname_siphash(const struct inode *dir,
178 const struct qstr *name);
181 extern void fscrypt_decrypt_bio(struct bio *);
182 extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
186 extern int fscrypt_file_open(struct inode *inode, struct file *filp);
187 extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
188 struct dentry *dentry);
189 extern int __fscrypt_prepare_rename(struct inode *old_dir,
190 struct dentry *old_dentry,
191 struct inode *new_dir,
192 struct dentry *new_dentry,
194 extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
195 struct fscrypt_name *fname);
196 extern int fscrypt_prepare_setflags(struct inode *inode,
197 unsigned int oldflags, unsigned int flags);
198 extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
199 unsigned int max_len,
200 struct fscrypt_str *disk_link);
201 extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
203 struct fscrypt_str *disk_link);
204 extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
205 unsigned int max_size,
206 struct delayed_call *done);
207 static inline void fscrypt_set_ops(struct super_block *sb,
208 const struct fscrypt_operations *s_cop)
212 #else /* !CONFIG_FS_ENCRYPTION */
214 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
219 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
224 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
229 static inline void fscrypt_handle_d_move(struct dentry *dentry)
234 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
238 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
243 return ERR_PTR(-EOPNOTSUPP);
246 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
249 unsigned int offs, u64 lblk_num,
255 static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
262 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
265 unsigned int offs, u64 lblk_num)
270 static inline bool fscrypt_is_bounce_page(struct page *page)
275 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
278 return ERR_PTR(-EINVAL);
281 static inline void fscrypt_free_bounce_page(struct page *bounce_page)
286 static inline int fscrypt_ioctl_set_policy(struct file *filp,
287 const void __user *arg)
292 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
297 static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
303 static inline int fscrypt_has_permitted_context(struct inode *parent,
309 static inline int fscrypt_inherit_context(struct inode *parent,
311 void *fs_data, bool preload)
317 static inline void fscrypt_sb_free(struct super_block *sb)
321 static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
326 static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
331 static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
337 static inline int fscrypt_ioctl_get_key_status(struct file *filp,
344 static inline int fscrypt_get_encryption_info(struct inode *inode)
349 static inline void fscrypt_put_encryption_info(struct inode *inode)
354 static inline void fscrypt_free_inode(struct inode *inode)
358 static inline int fscrypt_drop_inode(struct inode *inode)
364 static inline int fscrypt_setup_filename(struct inode *dir,
365 const struct qstr *iname,
366 int lookup, struct fscrypt_name *fname)
368 if (IS_ENCRYPTED(dir))
371 memset(fname, 0, sizeof(*fname));
372 fname->usr_fname = iname;
373 fname->disk_name.name = (unsigned char *)iname->name;
374 fname->disk_name.len = iname->len;
378 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
383 static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
384 u32 max_encrypted_len,
385 struct fscrypt_str *crypto_str)
390 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
395 static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
396 u32 hash, u32 minor_hash,
397 const struct fscrypt_str *iname,
398 struct fscrypt_str *oname)
403 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
404 const u8 *de_name, u32 de_name_len)
406 /* Encryption support disabled; use standard comparison */
407 if (de_name_len != fname->disk_name.len)
409 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
412 static inline u64 fscrypt_fname_siphash(const struct inode *dir,
413 const struct qstr *name)
420 static inline void fscrypt_decrypt_bio(struct bio *bio)
424 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
425 sector_t pblk, unsigned int len)
432 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
434 if (IS_ENCRYPTED(inode))
439 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
440 struct dentry *dentry)
445 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
446 struct dentry *old_dentry,
447 struct inode *new_dir,
448 struct dentry *new_dentry,
454 static inline int __fscrypt_prepare_lookup(struct inode *dir,
455 struct dentry *dentry,
456 struct fscrypt_name *fname)
461 static inline int fscrypt_prepare_setflags(struct inode *inode,
462 unsigned int oldflags,
468 static inline int __fscrypt_prepare_symlink(struct inode *dir,
470 unsigned int max_len,
471 struct fscrypt_str *disk_link)
477 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
480 struct fscrypt_str *disk_link)
485 static inline const char *fscrypt_get_symlink(struct inode *inode,
487 unsigned int max_size,
488 struct delayed_call *done)
490 return ERR_PTR(-EOPNOTSUPP);
493 static inline void fscrypt_set_ops(struct super_block *sb,
494 const struct fscrypt_operations *s_cop)
498 #endif /* !CONFIG_FS_ENCRYPTION */
501 * fscrypt_require_key - require an inode's encryption key
502 * @inode: the inode we need the key for
504 * If the inode is encrypted, set up its encryption key if not already done.
505 * Then require that the key be present and return -ENOKEY otherwise.
507 * No locks are needed, and the key will live as long as the struct inode --- so
508 * it won't go away from under you.
510 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
511 * if a problem occurred while setting up the encryption key.
513 static inline int fscrypt_require_key(struct inode *inode)
515 if (IS_ENCRYPTED(inode)) {
516 int err = fscrypt_get_encryption_info(inode);
520 if (!fscrypt_has_encryption_key(inode))
527 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
528 * @old_dentry: an existing dentry for the inode being linked
529 * @dir: the target directory
530 * @dentry: negative dentry for the target filename
532 * A new link can only be added to an encrypted directory if the directory's
533 * encryption key is available --- since otherwise we'd have no way to encrypt
534 * the filename. Therefore, we first set up the directory's encryption key (if
535 * not already done) and return an error if it's unavailable.
537 * We also verify that the link will not violate the constraint that all files
538 * in an encrypted directory tree use the same encryption policy.
540 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
541 * -EXDEV if the link would result in an inconsistent encryption policy, or
542 * another -errno code.
544 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
546 struct dentry *dentry)
548 if (IS_ENCRYPTED(dir))
549 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
554 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
555 * @old_dir: source directory
556 * @old_dentry: dentry for source file
557 * @new_dir: target directory
558 * @new_dentry: dentry for target location (may be negative unless exchanging)
559 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
561 * Prepare for ->rename() where the source and/or target directories may be
562 * encrypted. A new link can only be added to an encrypted directory if the
563 * directory's encryption key is available --- since otherwise we'd have no way
564 * to encrypt the filename. A rename to an existing name, on the other hand,
565 * *is* cryptographically possible without the key. However, we take the more
566 * conservative approach and just forbid all no-key renames.
568 * We also verify that the rename will not violate the constraint that all files
569 * in an encrypted directory tree use the same encryption policy.
571 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
572 * rename would cause inconsistent encryption policies, or another -errno code.
574 static inline int fscrypt_prepare_rename(struct inode *old_dir,
575 struct dentry *old_dentry,
576 struct inode *new_dir,
577 struct dentry *new_dentry,
580 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
581 return __fscrypt_prepare_rename(old_dir, old_dentry,
582 new_dir, new_dentry, flags);
587 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
588 * @dir: directory being searched
589 * @dentry: filename being looked up
590 * @fname: (output) the name to use to search the on-disk directory
592 * Prepare for ->lookup() in a directory which may be encrypted by determining
593 * the name that will actually be used to search the directory on-disk. Lookups
594 * can be done with or without the directory's encryption key; without the key,
595 * filenames are presented in encrypted form. Therefore, we'll try to set up
596 * the directory's encryption key, but even without it the lookup can continue.
598 * This also installs a custom ->d_revalidate() method which will invalidate the
599 * dentry if it was created without the key and the key is later added.
601 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
602 * correctly formed encoded ciphertext name, so a negative dentry should be
603 * created; or another -errno code.
605 static inline int fscrypt_prepare_lookup(struct inode *dir,
606 struct dentry *dentry,
607 struct fscrypt_name *fname)
609 if (IS_ENCRYPTED(dir))
610 return __fscrypt_prepare_lookup(dir, dentry, fname);
612 memset(fname, 0, sizeof(*fname));
613 fname->usr_fname = &dentry->d_name;
614 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
615 fname->disk_name.len = dentry->d_name.len;
620 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
621 * @dentry: dentry through which the inode is being changed
622 * @attr: attributes to change
624 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
625 * most attribute changes are allowed even without the encryption key. However,
626 * without the encryption key we do have to forbid truncates. This is needed
627 * because the size being truncated to may not be a multiple of the filesystem
628 * block size, and in that case we'd have to decrypt the final block, zero the
629 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
630 * filesystem block boundary, but it's simpler to just forbid all truncates ---
631 * and we already forbid all other contents modifications without the key.)
633 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
634 * if a problem occurred while setting up the encryption key.
636 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
639 if (attr->ia_valid & ATTR_SIZE)
640 return fscrypt_require_key(d_inode(dentry));
645 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
646 * @dir: directory in which the symlink is being created
647 * @target: plaintext symlink target
648 * @len: length of @target excluding null terminator
649 * @max_len: space the filesystem has available to store the symlink target
650 * @disk_link: (out) the on-disk symlink target being prepared
652 * This function computes the size the symlink target will require on-disk,
653 * stores it in @disk_link->len, and validates it against @max_len. An
654 * encrypted symlink may be longer than the original.
656 * Additionally, @disk_link->name is set to @target if the symlink will be
657 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
658 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
659 * on-disk target later. (The reason for the two-step process is that some
660 * filesystems need to know the size of the symlink target before creating the
661 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
663 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
664 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
665 * occurred while setting up the encryption key.
667 static inline int fscrypt_prepare_symlink(struct inode *dir,
670 unsigned int max_len,
671 struct fscrypt_str *disk_link)
673 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
674 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
676 disk_link->name = (unsigned char *)target;
677 disk_link->len = len + 1;
678 if (disk_link->len > max_len)
679 return -ENAMETOOLONG;
684 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
685 * @inode: symlink inode
686 * @target: plaintext symlink target
687 * @len: length of @target excluding null terminator
688 * @disk_link: (in/out) the on-disk symlink target being prepared
690 * If the symlink target needs to be encrypted, then this function encrypts it
691 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
692 * previously to compute @disk_link->len. If the filesystem did not allocate a
693 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
694 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
696 * Return: 0 on success, -errno on failure
698 static inline int fscrypt_encrypt_symlink(struct inode *inode,
701 struct fscrypt_str *disk_link)
703 if (IS_ENCRYPTED(inode))
704 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
708 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
709 static inline void fscrypt_finalize_bounce_page(struct page **pagep)
711 struct page *page = *pagep;
713 if (fscrypt_is_bounce_page(page)) {
714 *pagep = fscrypt_pagecache_page(page);
715 fscrypt_free_bounce_page(page);
719 #endif /* _LINUX_FSCRYPT_H */