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
32 const struct qstr *usr_fname;
33 struct fscrypt_str disk_name;
36 struct fscrypt_str crypto_buf;
37 bool is_ciphertext_name;
40 #define FSTR_INIT(n, l) { .name = n, .len = l }
41 #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
42 #define fname_name(p) ((p)->disk_name.name)
43 #define fname_len(p) ((p)->disk_name.len)
45 /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
46 #define FSCRYPT_SET_CONTEXT_MAX_SIZE 40
48 #ifdef CONFIG_FS_ENCRYPTION
50 * fscrypt superblock flags
52 #define FS_CFLG_OWN_PAGES (1U << 1)
55 * crypto operations for filesystems
57 struct fscrypt_operations {
59 const char *key_prefix;
60 int (*get_context)(struct inode *, void *, size_t);
61 int (*set_context)(struct inode *, const void *, size_t, void *);
62 bool (*dummy_context)(struct inode *);
63 bool (*empty_dir)(struct inode *);
64 unsigned int max_namelen;
72 struct work_struct work;
74 struct list_head free_list; /* Free list */
79 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
81 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
82 return READ_ONCE(inode->i_crypt_info) != NULL;
85 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
87 return inode->i_sb->s_cop->dummy_context &&
88 inode->i_sb->s_cop->dummy_context(inode);
92 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
93 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
94 * must be cleared. Note that we don't have to support arbitrary moves of this
95 * flag because fscrypt doesn't allow encrypted aliases to be the source or
96 * target of a rename().
98 static inline void fscrypt_handle_d_move(struct dentry *dentry)
100 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
104 extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
105 extern struct fscrypt_ctx *fscrypt_get_ctx(gfp_t);
106 extern void fscrypt_release_ctx(struct fscrypt_ctx *);
108 extern struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
112 extern int fscrypt_encrypt_block_inplace(const struct inode *inode,
113 struct page *page, unsigned int len,
114 unsigned int offs, u64 lblk_num,
117 extern int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
119 extern int fscrypt_decrypt_block_inplace(const struct inode *inode,
120 struct page *page, unsigned int len,
121 unsigned int offs, u64 lblk_num);
123 static inline bool fscrypt_is_bounce_page(struct page *page)
125 return page->mapping == NULL;
128 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
130 return (struct page *)page_private(bounce_page);
133 extern void fscrypt_free_bounce_page(struct page *bounce_page);
136 extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
137 extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
138 extern int fscrypt_ioctl_get_policy_ex(struct file *, void __user *);
139 extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
140 extern int fscrypt_inherit_context(struct inode *, struct inode *,
143 extern void fscrypt_sb_free(struct super_block *sb);
144 extern int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
145 extern int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
146 extern int fscrypt_ioctl_remove_key_all_users(struct file *filp,
148 extern int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
151 extern int fscrypt_get_encryption_info(struct inode *);
152 extern void fscrypt_put_encryption_info(struct inode *);
153 extern void fscrypt_free_inode(struct inode *);
154 extern int fscrypt_drop_inode(struct inode *inode);
157 extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
158 int lookup, struct fscrypt_name *);
160 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
162 kfree(fname->crypto_buf.name);
165 extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
166 struct fscrypt_str *);
167 extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
168 extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
169 const struct fscrypt_str *, struct fscrypt_str *);
171 #define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
173 /* Extracts the second-to-last ciphertext block; see explanation below */
174 #define FSCRYPT_FNAME_DIGEST(name, len) \
175 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
176 FS_CRYPTO_BLOCK_SIZE))
178 #define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
181 * fscrypt_digested_name - alternate identifier for an on-disk filename
183 * When userspace lists an encrypted directory without access to the key,
184 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
185 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
186 * full ciphertext (base64-encoded). This is necessary to allow supporting
187 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
189 * To make it possible for filesystems to still find the correct directory entry
190 * despite not knowing the full on-disk name, we encode any filesystem-specific
191 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
192 * followed by the second-to-last ciphertext block of the filename. Due to the
193 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
194 * depends on the full plaintext. (Note that ciphertext stealing causes the
195 * last two blocks to appear "flipped".) This makes accidental collisions very
196 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
197 * share the same filesystem-specific hashes.
199 * However, this scheme isn't immune to intentional collisions, which can be
200 * created by anyone able to create arbitrary plaintext filenames and view them
201 * without the key. Making the "digest" be a real cryptographic hash like
202 * SHA-256 over the full ciphertext would prevent this, although it would be
203 * less efficient and harder to implement, especially since the filesystem would
204 * need to calculate it for each directory entry examined during a search.
206 struct fscrypt_digested_name {
209 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
213 * fscrypt_match_name() - test whether the given name matches a directory entry
214 * @fname: the name being searched for
215 * @de_name: the name from the directory entry
216 * @de_name_len: the length of @de_name in bytes
218 * Normally @fname->disk_name will be set, and in that case we simply compare
219 * that to the name stored in the directory entry. The only exception is that
220 * if we don't have the key for an encrypted directory and a filename in it is
221 * very long, then we won't have the full disk_name and we'll instead need to
222 * match against the fscrypt_digested_name.
224 * Return: %true if the name matches, otherwise %false.
226 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
227 const u8 *de_name, u32 de_name_len)
229 if (unlikely(!fname->disk_name.name)) {
230 const struct fscrypt_digested_name *n =
231 (const void *)fname->crypto_buf.name;
232 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
234 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
236 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
237 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
240 if (de_name_len != fname->disk_name.len)
242 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
246 extern void fscrypt_decrypt_bio(struct bio *);
247 extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
249 extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
253 extern int fscrypt_file_open(struct inode *inode, struct file *filp);
254 extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
255 struct dentry *dentry);
256 extern int __fscrypt_prepare_rename(struct inode *old_dir,
257 struct dentry *old_dentry,
258 struct inode *new_dir,
259 struct dentry *new_dentry,
261 extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
262 struct fscrypt_name *fname);
263 extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
264 unsigned int max_len,
265 struct fscrypt_str *disk_link);
266 extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
268 struct fscrypt_str *disk_link);
269 extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
270 unsigned int max_size,
271 struct delayed_call *done);
272 static inline void fscrypt_set_ops(struct super_block *sb,
273 const struct fscrypt_operations *s_cop)
277 #else /* !CONFIG_FS_ENCRYPTION */
279 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
284 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
289 static inline void fscrypt_handle_d_move(struct dentry *dentry)
294 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
298 static inline struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
300 return ERR_PTR(-EOPNOTSUPP);
303 static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
308 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
313 return ERR_PTR(-EOPNOTSUPP);
316 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
319 unsigned int offs, u64 lblk_num,
325 static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
332 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
335 unsigned int offs, u64 lblk_num)
340 static inline bool fscrypt_is_bounce_page(struct page *page)
345 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
348 return ERR_PTR(-EINVAL);
351 static inline void fscrypt_free_bounce_page(struct page *bounce_page)
356 static inline int fscrypt_ioctl_set_policy(struct file *filp,
357 const void __user *arg)
362 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
367 static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
373 static inline int fscrypt_has_permitted_context(struct inode *parent,
379 static inline int fscrypt_inherit_context(struct inode *parent,
381 void *fs_data, bool preload)
387 static inline void fscrypt_sb_free(struct super_block *sb)
391 static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
396 static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
401 static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
407 static inline int fscrypt_ioctl_get_key_status(struct file *filp,
414 static inline int fscrypt_get_encryption_info(struct inode *inode)
419 static inline void fscrypt_put_encryption_info(struct inode *inode)
424 static inline void fscrypt_free_inode(struct inode *inode)
428 static inline int fscrypt_drop_inode(struct inode *inode)
434 static inline int fscrypt_setup_filename(struct inode *dir,
435 const struct qstr *iname,
436 int lookup, struct fscrypt_name *fname)
438 if (IS_ENCRYPTED(dir))
441 memset(fname, 0, sizeof(*fname));
442 fname->usr_fname = iname;
443 fname->disk_name.name = (unsigned char *)iname->name;
444 fname->disk_name.len = iname->len;
448 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
453 static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
454 u32 max_encrypted_len,
455 struct fscrypt_str *crypto_str)
460 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
465 static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
466 u32 hash, u32 minor_hash,
467 const struct fscrypt_str *iname,
468 struct fscrypt_str *oname)
473 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
474 const u8 *de_name, u32 de_name_len)
476 /* Encryption support disabled; use standard comparison */
477 if (de_name_len != fname->disk_name.len)
479 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
483 static inline void fscrypt_decrypt_bio(struct bio *bio)
487 static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
492 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
493 sector_t pblk, unsigned int len)
500 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
502 if (IS_ENCRYPTED(inode))
507 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
508 struct dentry *dentry)
513 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
514 struct dentry *old_dentry,
515 struct inode *new_dir,
516 struct dentry *new_dentry,
522 static inline int __fscrypt_prepare_lookup(struct inode *dir,
523 struct dentry *dentry,
524 struct fscrypt_name *fname)
529 static inline int __fscrypt_prepare_symlink(struct inode *dir,
531 unsigned int max_len,
532 struct fscrypt_str *disk_link)
538 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
541 struct fscrypt_str *disk_link)
546 static inline const char *fscrypt_get_symlink(struct inode *inode,
548 unsigned int max_size,
549 struct delayed_call *done)
551 return ERR_PTR(-EOPNOTSUPP);
554 static inline void fscrypt_set_ops(struct super_block *sb,
555 const struct fscrypt_operations *s_cop)
559 #endif /* !CONFIG_FS_ENCRYPTION */
562 * fscrypt_require_key - require an inode's encryption key
563 * @inode: the inode we need the key for
565 * If the inode is encrypted, set up its encryption key if not already done.
566 * Then require that the key be present and return -ENOKEY otherwise.
568 * No locks are needed, and the key will live as long as the struct inode --- so
569 * it won't go away from under you.
571 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
572 * if a problem occurred while setting up the encryption key.
574 static inline int fscrypt_require_key(struct inode *inode)
576 if (IS_ENCRYPTED(inode)) {
577 int err = fscrypt_get_encryption_info(inode);
581 if (!fscrypt_has_encryption_key(inode))
588 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
589 * @old_dentry: an existing dentry for the inode being linked
590 * @dir: the target directory
591 * @dentry: negative dentry for the target filename
593 * A new link can only be added to an encrypted directory if the directory's
594 * encryption key is available --- since otherwise we'd have no way to encrypt
595 * the filename. Therefore, we first set up the directory's encryption key (if
596 * not already done) and return an error if it's unavailable.
598 * We also verify that the link will not violate the constraint that all files
599 * in an encrypted directory tree use the same encryption policy.
601 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
602 * -EXDEV if the link would result in an inconsistent encryption policy, or
603 * another -errno code.
605 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
607 struct dentry *dentry)
609 if (IS_ENCRYPTED(dir))
610 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
615 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
616 * @old_dir: source directory
617 * @old_dentry: dentry for source file
618 * @new_dir: target directory
619 * @new_dentry: dentry for target location (may be negative unless exchanging)
620 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
622 * Prepare for ->rename() where the source and/or target directories may be
623 * encrypted. A new link can only be added to an encrypted directory if the
624 * directory's encryption key is available --- since otherwise we'd have no way
625 * to encrypt the filename. A rename to an existing name, on the other hand,
626 * *is* cryptographically possible without the key. However, we take the more
627 * conservative approach and just forbid all no-key renames.
629 * We also verify that the rename will not violate the constraint that all files
630 * in an encrypted directory tree use the same encryption policy.
632 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
633 * rename would cause inconsistent encryption policies, or another -errno code.
635 static inline int fscrypt_prepare_rename(struct inode *old_dir,
636 struct dentry *old_dentry,
637 struct inode *new_dir,
638 struct dentry *new_dentry,
641 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
642 return __fscrypt_prepare_rename(old_dir, old_dentry,
643 new_dir, new_dentry, flags);
648 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
649 * @dir: directory being searched
650 * @dentry: filename being looked up
651 * @fname: (output) the name to use to search the on-disk directory
653 * Prepare for ->lookup() in a directory which may be encrypted by determining
654 * the name that will actually be used to search the directory on-disk. Lookups
655 * can be done with or without the directory's encryption key; without the key,
656 * filenames are presented in encrypted form. Therefore, we'll try to set up
657 * the directory's encryption key, but even without it the lookup can continue.
659 * This also installs a custom ->d_revalidate() method which will invalidate the
660 * dentry if it was created without the key and the key is later added.
662 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
663 * correctly formed encoded ciphertext name, so a negative dentry should be
664 * created; or another -errno code.
666 static inline int fscrypt_prepare_lookup(struct inode *dir,
667 struct dentry *dentry,
668 struct fscrypt_name *fname)
670 if (IS_ENCRYPTED(dir))
671 return __fscrypt_prepare_lookup(dir, dentry, fname);
673 memset(fname, 0, sizeof(*fname));
674 fname->usr_fname = &dentry->d_name;
675 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
676 fname->disk_name.len = dentry->d_name.len;
681 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
682 * @dentry: dentry through which the inode is being changed
683 * @attr: attributes to change
685 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
686 * most attribute changes are allowed even without the encryption key. However,
687 * without the encryption key we do have to forbid truncates. This is needed
688 * because the size being truncated to may not be a multiple of the filesystem
689 * block size, and in that case we'd have to decrypt the final block, zero the
690 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
691 * filesystem block boundary, but it's simpler to just forbid all truncates ---
692 * and we already forbid all other contents modifications without the key.)
694 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
695 * if a problem occurred while setting up the encryption key.
697 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
700 if (attr->ia_valid & ATTR_SIZE)
701 return fscrypt_require_key(d_inode(dentry));
706 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
707 * @dir: directory in which the symlink is being created
708 * @target: plaintext symlink target
709 * @len: length of @target excluding null terminator
710 * @max_len: space the filesystem has available to store the symlink target
711 * @disk_link: (out) the on-disk symlink target being prepared
713 * This function computes the size the symlink target will require on-disk,
714 * stores it in @disk_link->len, and validates it against @max_len. An
715 * encrypted symlink may be longer than the original.
717 * Additionally, @disk_link->name is set to @target if the symlink will be
718 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
719 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
720 * on-disk target later. (The reason for the two-step process is that some
721 * filesystems need to know the size of the symlink target before creating the
722 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
724 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
725 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
726 * occurred while setting up the encryption key.
728 static inline int fscrypt_prepare_symlink(struct inode *dir,
731 unsigned int max_len,
732 struct fscrypt_str *disk_link)
734 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
735 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
737 disk_link->name = (unsigned char *)target;
738 disk_link->len = len + 1;
739 if (disk_link->len > max_len)
740 return -ENAMETOOLONG;
745 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
746 * @inode: symlink inode
747 * @target: plaintext symlink target
748 * @len: length of @target excluding null terminator
749 * @disk_link: (in/out) the on-disk symlink target being prepared
751 * If the symlink target needs to be encrypted, then this function encrypts it
752 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
753 * previously to compute @disk_link->len. If the filesystem did not allocate a
754 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
755 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
757 * Return: 0 on success, -errno on failure
759 static inline int fscrypt_encrypt_symlink(struct inode *inode,
762 struct fscrypt_str *disk_link)
764 if (IS_ENCRYPTED(inode))
765 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
769 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
770 static inline void fscrypt_finalize_bounce_page(struct page **pagep)
772 struct page *page = *pagep;
774 if (fscrypt_is_bounce_page(page)) {
775 *pagep = fscrypt_pagecache_page(page);
776 fscrypt_free_bounce_page(page);
780 #endif /* _LINUX_FSCRYPT_H */