1 /* SPDX-License-Identifier: GPL-2.0 */
3 * llcrypt.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.
14 * Linux commit 219d54332a09
17 #ifndef _LINUX_LLCRYPT_H
18 #define _LINUX_LLCRYPT_H
20 #ifndef DCACHE_ENCRYPTED_NAME
21 #define DCACHE_ENCRYPTED_NAME 0x02000000
26 #include <linux/slab.h>
27 #include <lustre_disk.h>
28 #include <uapi/linux/llcrypt.h>
30 #define LL_CRYPTO_BLOCK_SIZE 16
41 const struct qstr *usr_fname;
42 struct llcrypt_str disk_name;
45 struct llcrypt_str crypto_buf;
46 bool is_ciphertext_name;
49 #define LLTR_INIT(n, l) { .name = n, .len = l }
50 #define LLTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
51 #define lname_name(p) ((p)->disk_name.name)
52 #define lname_len(p) ((p)->disk_name.len)
54 /* Maximum value for the third parameter of llcrypt_operations.set_context(). */
55 #define LLCRYPT_SET_CONTEXT_MAX_SIZE 40
57 #ifdef CONFIG_LL_ENCRYPTION
59 * llcrypt superblock flags
61 #define LL_CFLG_OWN_PAGES (1U << 1)
64 * crypto operations for filesystems
66 struct llcrypt_operations {
68 const char *key_prefix;
69 int (*get_context)(struct inode *, void *, size_t);
70 int (*set_context)(struct inode *, const void *, size_t, void *);
71 bool (*dummy_context)(struct inode *);
72 bool (*empty_dir)(struct inode *);
73 unsigned int max_namelen;
81 struct work_struct work;
83 struct list_head free_list; /* Free list */
88 extern bool llcrypt_has_encryption_key(const struct inode *inode);
90 static inline bool llcrypt_dummy_context_enabled(struct inode *inode)
92 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
97 return lsi->lsi_cop->dummy_context &&
98 lsi->lsi_cop->dummy_context(inode);
102 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
103 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
104 * must be cleared. Note that we don't have to support arbitrary moves of this
105 * flag because llcrypt doesn't allow encrypted aliases to be the source or
106 * target of a rename().
108 static inline void llcrypt_handle_d_move(struct dentry *dentry)
110 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
114 extern int __init llcrypt_init(void);
115 extern void __exit llcrypt_exit(void);
116 extern void llcrypt_enqueue_decrypt_work(struct work_struct *);
117 extern struct llcrypt_ctx *llcrypt_get_ctx(gfp_t);
118 extern void llcrypt_release_ctx(struct llcrypt_ctx *);
120 extern struct page *llcrypt_encrypt_pagecache_blocks(struct page *page,
124 extern int llcrypt_encrypt_block_inplace(const struct inode *inode,
125 struct page *page, unsigned int len,
126 unsigned int offs, u64 lblk_num,
129 extern int llcrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
131 extern int llcrypt_decrypt_block_inplace(const struct inode *inode,
132 struct page *page, unsigned int len,
133 unsigned int offs, u64 lblk_num);
135 static inline bool llcrypt_is_bounce_page(struct page *page)
137 return page->mapping == NULL;
140 static inline struct page *llcrypt_pagecache_page(struct page *bounce_page)
142 return (struct page *)page_private(bounce_page);
145 extern void llcrypt_free_bounce_page(struct page *bounce_page);
148 extern int llcrypt_ioctl_set_policy(struct file *, const void __user *);
149 extern int llcrypt_ioctl_get_policy(struct file *, void __user *);
150 extern int llcrypt_ioctl_get_policy_ex(struct file *, void __user *);
151 extern int llcrypt_has_permitted_context(struct inode *, struct inode *);
152 extern int llcrypt_inherit_context(struct inode *, struct inode *,
154 extern bool llcrypt_policy_has_filename_enc(struct inode *inode);
156 extern void llcrypt_sb_free(struct super_block *sb);
157 extern int llcrypt_ioctl_add_key(struct file *filp, void __user *arg);
158 extern int llcrypt_ioctl_remove_key(struct file *filp, void __user *arg);
159 extern int llcrypt_ioctl_remove_key_all_users(struct file *filp,
161 extern int llcrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
164 extern int llcrypt_get_encryption_info(struct inode *);
165 extern void llcrypt_put_encryption_info(struct inode *);
166 extern void llcrypt_free_inode(struct inode *);
167 extern int llcrypt_drop_inode(struct inode *inode);
170 extern int llcrypt_setup_filename(struct inode *, const struct qstr *,
171 int lookup, struct llcrypt_name *);
173 static inline void llcrypt_free_filename(struct llcrypt_name *fname)
175 kfree(fname->crypto_buf.name);
178 extern int llcrypt_fname_alloc_buffer(const struct inode *, u32,
179 struct llcrypt_str *);
180 extern void llcrypt_fname_free_buffer(struct llcrypt_str *);
181 extern int llcrypt_fname_disk_to_usr(struct inode *, u32, u32,
182 const struct llcrypt_str *, struct llcrypt_str *);
184 #define LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
186 /* Extracts the second-to-last ciphertext block; see explanation below */
187 #define LLCRYPT_FNAME_DIGEST(name, len) \
188 ((name) + round_down((len) - LL_CRYPTO_BLOCK_SIZE - 1, \
189 LL_CRYPTO_BLOCK_SIZE))
191 #define LLCRYPT_FNAME_DIGEST_SIZE LL_CRYPTO_BLOCK_SIZE
194 * llcrypt_digested_name - alternate identifier for an on-disk filename
196 * When userspace lists an encrypted directory without access to the key,
197 * filenames whose ciphertext is longer than LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE
198 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
199 * full ciphertext (base64-encoded). This is necessary to allow supporting
200 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
202 * To make it possible for filesystems to still find the correct directory entry
203 * despite not knowing the full on-disk name, we encode any filesystem-specific
204 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
205 * followed by the second-to-last ciphertext block of the filename. Due to the
206 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
207 * depends on the full plaintext. (Note that ciphertext stealing causes the
208 * last two blocks to appear "flipped".) This makes accidental collisions very
209 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
210 * share the same filesystem-specific hashes.
212 * However, this scheme isn't immune to intentional collisions, which can be
213 * created by anyone able to create arbitrary plaintext filenames and view them
214 * without the key. Making the "digest" be a real cryptographic hash like
215 * SHA-256 over the full ciphertext would prevent this, although it would be
216 * less efficient and harder to implement, especially since the filesystem would
217 * need to calculate it for each directory entry examined during a search.
219 struct llcrypt_digested_name {
222 u8 digest[LLCRYPT_FNAME_DIGEST_SIZE];
226 * llcrypt_match_name() - test whether the given name matches a directory entry
227 * @fname: the name being searched for
228 * @de_name: the name from the directory entry
229 * @de_name_len: the length of @de_name in bytes
231 * Normally @fname->disk_name will be set, and in that case we simply compare
232 * that to the name stored in the directory entry. The only exception is that
233 * if we don't have the key for an encrypted directory and a filename in it is
234 * very long, then we won't have the full disk_name and we'll instead need to
235 * match against the llcrypt_digested_name.
237 * Return: %true if the name matches, otherwise %false.
239 static inline bool llcrypt_match_name(const struct llcrypt_name *fname,
240 const u8 *de_name, u32 de_name_len)
242 if (unlikely(!fname->disk_name.name)) {
243 const struct llcrypt_digested_name *n =
244 (const void *)fname->crypto_buf.name;
245 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
247 if (de_name_len <= LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
249 return !memcmp(LLCRYPT_FNAME_DIGEST(de_name, de_name_len),
250 n->digest, LLCRYPT_FNAME_DIGEST_SIZE);
253 if (de_name_len != fname->disk_name.len)
255 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
259 extern void llcrypt_decrypt_bio(struct bio *);
260 extern void llcrypt_enqueue_decrypt_bio(struct llcrypt_ctx *ctx,
262 extern int llcrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
266 extern int llcrypt_file_open(struct inode *inode, struct file *filp);
267 extern int __llcrypt_prepare_link(struct inode *inode, struct inode *dir,
268 struct dentry *dentry);
269 extern int __llcrypt_prepare_rename(struct inode *old_dir,
270 struct dentry *old_dentry,
271 struct inode *new_dir,
272 struct dentry *new_dentry,
274 extern int __llcrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
275 struct llcrypt_name *fname);
276 extern int __llcrypt_prepare_symlink(struct inode *dir, unsigned int len,
277 unsigned int max_len,
278 struct llcrypt_str *disk_link);
279 extern int __llcrypt_encrypt_symlink(struct inode *inode, const char *target,
281 struct llcrypt_str *disk_link);
282 extern const char *llcrypt_get_symlink(struct inode *inode, const void *caddr,
283 unsigned int max_size,
284 struct delayed_call *done);
285 static inline void llcrypt_set_ops(struct super_block *sb,
286 const struct llcrypt_operations *lsi_cop)
288 struct lustre_sb_info *lsi = s2lsi(sb);
291 lsi->lsi_cop = lsi_cop;
293 #else /* !CONFIG_LL_ENCRYPTION */
295 struct llcrypt_operations;
296 #define llcrypt_init() 0
297 #define llcrypt_exit() {}
300 #define IS_ENCRYPTED(x) 0
302 static inline bool llcrypt_has_encryption_key(const struct inode *inode)
307 static inline bool llcrypt_dummy_context_enabled(struct inode *inode)
312 static inline void llcrypt_handle_d_move(struct dentry *dentry)
317 static inline void llcrypt_enqueue_decrypt_work(struct work_struct *work)
321 static inline struct llcrypt_ctx *llcrypt_get_ctx(gfp_t gfp_flags)
323 return ERR_PTR(-EOPNOTSUPP);
326 static inline void llcrypt_release_ctx(struct llcrypt_ctx *ctx)
331 static inline struct page *llcrypt_encrypt_pagecache_blocks(struct page *page,
336 return ERR_PTR(-EOPNOTSUPP);
339 static inline int llcrypt_encrypt_block_inplace(const struct inode *inode,
342 unsigned int offs, u64 lblk_num,
348 static inline int llcrypt_decrypt_pagecache_blocks(struct page *page,
355 static inline int llcrypt_decrypt_block_inplace(const struct inode *inode,
358 unsigned int offs, u64 lblk_num)
363 static inline bool llcrypt_is_bounce_page(struct page *page)
368 static inline struct page *llcrypt_pagecache_page(struct page *bounce_page)
371 return ERR_PTR(-EINVAL);
374 static inline void llcrypt_free_bounce_page(struct page *bounce_page)
379 static inline int llcrypt_ioctl_set_policy(struct file *filp,
380 const void __user *arg)
385 static inline int llcrypt_ioctl_get_policy(struct file *filp, void __user *arg)
390 static inline int llcrypt_ioctl_get_policy_ex(struct file *filp,
396 static inline int llcrypt_has_permitted_context(struct inode *parent,
402 static inline int llcrypt_inherit_context(struct inode *parent,
404 void *fs_data, bool preload)
408 static inline bool llcrypt_policy_has_filename_enc(struct inode *inode)
414 static inline void llcrypt_sb_free(struct super_block *sb)
418 static inline int llcrypt_ioctl_add_key(struct file *filp, void __user *arg)
423 static inline int llcrypt_ioctl_remove_key(struct file *filp, void __user *arg)
428 static inline int llcrypt_ioctl_remove_key_all_users(struct file *filp,
434 static inline int llcrypt_ioctl_get_key_status(struct file *filp,
441 static inline int llcrypt_get_encryption_info(struct inode *inode)
446 static inline void llcrypt_put_encryption_info(struct inode *inode)
451 static inline void llcrypt_free_inode(struct inode *inode)
455 static inline int llcrypt_drop_inode(struct inode *inode)
461 static inline int llcrypt_setup_filename(struct inode *dir,
462 const struct qstr *iname,
463 int lookup, struct llcrypt_name *fname)
465 if (IS_ENCRYPTED(dir))
468 memset(fname, 0, sizeof(*fname));
469 fname->usr_fname = iname;
470 fname->disk_name.name = (unsigned char *)iname->name;
471 fname->disk_name.len = iname->len;
475 static inline void llcrypt_free_filename(struct llcrypt_name *fname)
480 static inline int llcrypt_fname_alloc_buffer(const struct inode *inode,
481 u32 max_encrypted_len,
482 struct llcrypt_str *crypto_str)
487 static inline void llcrypt_fname_free_buffer(struct llcrypt_str *crypto_str)
492 static inline int llcrypt_fname_disk_to_usr(struct inode *inode,
493 u32 hash, u32 minor_hash,
494 const struct llcrypt_str *iname,
495 struct llcrypt_str *oname)
500 static inline bool llcrypt_match_name(const struct llcrypt_name *fname,
501 const u8 *de_name, u32 de_name_len)
503 /* Encryption support disabled; use standard comparison */
504 if (de_name_len != fname->disk_name.len)
506 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
510 static inline void llcrypt_decrypt_bio(struct bio *bio)
514 static inline void llcrypt_enqueue_decrypt_bio(struct llcrypt_ctx *ctx,
519 static inline int llcrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
520 sector_t pblk, unsigned int len)
527 static inline int llcrypt_file_open(struct inode *inode, struct file *filp)
529 if (IS_ENCRYPTED(inode))
534 static inline int __llcrypt_prepare_link(struct inode *inode, struct inode *dir,
535 struct dentry *dentry)
540 static inline int __llcrypt_prepare_rename(struct inode *old_dir,
541 struct dentry *old_dentry,
542 struct inode *new_dir,
543 struct dentry *new_dentry,
549 static inline int __llcrypt_prepare_lookup(struct inode *dir,
550 struct dentry *dentry,
551 struct llcrypt_name *fname)
556 static inline int __llcrypt_prepare_symlink(struct inode *dir,
558 unsigned int max_len,
559 struct llcrypt_str *disk_link)
565 static inline int __llcrypt_encrypt_symlink(struct inode *inode,
568 struct llcrypt_str *disk_link)
573 #define llcrypt_get_symlink(inode, caddr, max_size, done) ERR_PTR(-EOPNOTSUPP)
575 static inline void llcrypt_set_ops(struct super_block *sb,
576 const struct llcrypt_operations *lsi_cop)
580 #endif /* !CONFIG_LL_ENCRYPTION */
583 * llcrypt_require_key - require an inode's encryption key
584 * @inode: the inode we need the key for
586 * If the inode is encrypted, set up its encryption key if not already done.
587 * Then require that the key be present and return -ENOKEY otherwise.
589 * No locks are needed, and the key will live as long as the struct inode --- so
590 * it won't go away from under you.
592 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
593 * if a problem occurred while setting up the encryption key.
595 static inline int llcrypt_require_key(struct inode *inode)
597 if (IS_ENCRYPTED(inode)) {
598 int err = llcrypt_get_encryption_info(inode);
602 if (!llcrypt_has_encryption_key(inode))
609 * llcrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
610 * @old_dentry: an existing dentry for the inode being linked
611 * @dir: the target directory
612 * @dentry: negative dentry for the target filename
614 * A new link can only be added to an encrypted directory if the directory's
615 * encryption key is available --- since otherwise we'd have no way to encrypt
616 * the filename. Therefore, we first set up the directory's encryption key (if
617 * not already done) and return an error if it's unavailable.
619 * We also verify that the link will not violate the constraint that all files
620 * in an encrypted directory tree use the same encryption policy.
622 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
623 * -EXDEV if the link would result in an inconsistent encryption policy, or
624 * another -errno code.
626 static inline int llcrypt_prepare_link(struct dentry *old_dentry,
628 struct dentry *dentry)
630 if (IS_ENCRYPTED(dir))
631 return __llcrypt_prepare_link(d_inode(old_dentry), dir, dentry);
636 * llcrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
637 * @old_dir: source directory
638 * @old_dentry: dentry for source file
639 * @new_dir: target directory
640 * @new_dentry: dentry for target location (may be negative unless exchanging)
641 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
643 * Prepare for ->rename() where the source and/or target directories may be
644 * encrypted. A new link can only be added to an encrypted directory if the
645 * directory's encryption key is available --- since otherwise we'd have no way
646 * to encrypt the filename. A rename to an existing name, on the other hand,
647 * *is* cryptographically possible without the key. However, we take the more
648 * conservative approach and just forbid all no-key renames.
650 * We also verify that the rename will not violate the constraint that all files
651 * in an encrypted directory tree use the same encryption policy.
653 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
654 * rename would cause inconsistent encryption policies, or another -errno code.
656 static inline int llcrypt_prepare_rename(struct inode *old_dir,
657 struct dentry *old_dentry,
658 struct inode *new_dir,
659 struct dentry *new_dentry,
662 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
663 return __llcrypt_prepare_rename(old_dir, old_dentry,
664 new_dir, new_dentry, flags);
669 * llcrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
670 * @dir: directory being searched
671 * @dentry: filename being looked up
672 * @fname: (output) the name to use to search the on-disk directory
674 * Prepare for ->lookup() in a directory which may be encrypted by determining
675 * the name that will actually be used to search the directory on-disk. Lookups
676 * can be done with or without the directory's encryption key; without the key,
677 * filenames are presented in encrypted form. Therefore, we'll try to set up
678 * the directory's encryption key, but even without it the lookup can continue.
680 * This also installs a custom ->d_revalidate() method which will invalidate the
681 * dentry if it was created without the key and the key is later added.
683 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
684 * correctly formed encoded ciphertext name, so a negative dentry should be
685 * created; or another -errno code.
687 static inline int llcrypt_prepare_lookup(struct inode *dir,
688 struct dentry *dentry,
689 struct llcrypt_name *fname)
691 if (IS_ENCRYPTED(dir))
692 return __llcrypt_prepare_lookup(dir, dentry, fname);
694 memset(fname, 0, sizeof(*fname));
695 fname->usr_fname = &dentry->d_name;
696 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
697 fname->disk_name.len = dentry->d_name.len;
702 * llcrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
703 * @dentry: dentry through which the inode is being changed
704 * @attr: attributes to change
706 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
707 * most attribute changes are allowed even without the encryption key. However,
708 * without the encryption key we do have to forbid truncates. This is needed
709 * because the size being truncated to may not be a multiple of the filesystem
710 * block size, and in that case we'd have to decrypt the final block, zero the
711 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
712 * filesystem block boundary, but it's simpler to just forbid all truncates ---
713 * and we already forbid all other contents modifications without the key.)
715 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
716 * if a problem occurred while setting up the encryption key.
718 static inline int llcrypt_prepare_setattr(struct dentry *dentry,
721 if (attr->ia_valid & ATTR_SIZE)
722 return llcrypt_require_key(d_inode(dentry));
727 * llcrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
728 * @dir: directory in which the symlink is being created
729 * @target: plaintext symlink target
730 * @len: length of @target excluding null terminator
731 * @max_len: space the filesystem has available to store the symlink target
732 * @disk_link: (out) the on-disk symlink target being prepared
734 * This function computes the size the symlink target will require on-disk,
735 * stores it in @disk_link->len, and validates it against @max_len. An
736 * encrypted symlink may be longer than the original.
738 * Additionally, @disk_link->name is set to @target if the symlink will be
739 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
740 * symlinks, the filesystem must call llcrypt_encrypt_symlink() to create the
741 * on-disk target later. (The reason for the two-step process is that some
742 * filesystems need to know the size of the symlink target before creating the
743 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
745 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
746 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
747 * occurred while setting up the encryption key.
749 static inline int llcrypt_prepare_symlink(struct inode *dir,
752 unsigned int max_len,
753 struct llcrypt_str *disk_link)
755 if ((IS_ENCRYPTED(dir) &&
756 likely(llcrypt_policy_has_filename_enc(dir))) ||
757 llcrypt_dummy_context_enabled(dir))
758 return __llcrypt_prepare_symlink(dir, len, max_len, disk_link);
760 disk_link->name = (unsigned char *)target;
761 disk_link->len = len + 1;
762 if (disk_link->len > max_len)
763 return -ENAMETOOLONG;
768 * llcrypt_encrypt_symlink - encrypt the symlink target if needed
769 * @inode: symlink inode
770 * @target: plaintext symlink target
771 * @len: length of @target excluding null terminator
772 * @disk_link: (in/out) the on-disk symlink target being prepared
774 * If the symlink target needs to be encrypted, then this function encrypts it
775 * into @disk_link->name. llcrypt_prepare_symlink() must have been called
776 * previously to compute @disk_link->len. If the filesystem did not allocate a
777 * buffer for @disk_link->name after calling llcrypt_prepare_link(), then one
778 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
780 * Return: 0 on success, -errno on failure
782 static inline int llcrypt_encrypt_symlink(struct inode *inode,
785 struct llcrypt_str *disk_link)
787 if (IS_ENCRYPTED(inode))
788 return __llcrypt_encrypt_symlink(inode, target, len, disk_link);
792 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
793 static inline void llcrypt_finalize_bounce_page(struct page **pagep)
795 struct page *page = *pagep;
797 if (llcrypt_is_bounce_page(page)) {
798 *pagep = llcrypt_pagecache_page(page);
799 llcrypt_free_bounce_page(page);
803 #endif /* _LINUX_LLCRYPT_H */