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
56 #define LLCRYPT_DIGESTED_CHAR_OLD '_'
57 #define LLCRYPT_DIGESTED_CHAR '+'
59 #ifdef CONFIG_LL_ENCRYPTION
61 * llcrypt superblock flags
63 #define LL_CFLG_OWN_PAGES (1U << 1)
66 * crypto operations for filesystems
68 struct llcrypt_operations {
70 const char *key_prefix;
71 int (*get_context)(struct inode *, void *, size_t);
72 int (*set_context)(struct inode *, const void *, size_t, void *);
73 bool (*dummy_context)(struct inode *);
74 bool (*empty_dir)(struct inode *);
75 unsigned int max_namelen;
83 struct work_struct work;
85 struct list_head free_list; /* Free list */
90 extern bool llcrypt_has_encryption_key(const struct inode *inode);
92 static inline bool llcrypt_dummy_context_enabled(struct inode *inode)
94 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
99 return lsi->lsi_cop->dummy_context &&
100 lsi->lsi_cop->dummy_context(inode);
104 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
105 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
106 * must be cleared. Note that we don't have to support arbitrary moves of this
107 * flag because llcrypt doesn't allow encrypted aliases to be the source or
108 * target of a rename().
110 static inline void llcrypt_handle_d_move(struct dentry *dentry)
112 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
116 extern int __init llcrypt_init(void);
117 extern void __exit llcrypt_exit(void);
118 extern void llcrypt_enqueue_decrypt_work(struct work_struct *);
119 extern struct llcrypt_ctx *llcrypt_get_ctx(gfp_t);
120 extern void llcrypt_release_ctx(struct llcrypt_ctx *);
122 extern struct page *llcrypt_encrypt_pagecache_blocks(struct page *page,
126 extern int llcrypt_encrypt_block(const struct inode *inode, struct page *src,
127 struct page *dst, unsigned int len,
128 unsigned int offs, u64 lblk_num, gfp_t gfp_flags);
130 extern int llcrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
133 extern int llcrypt_decrypt_block(const struct inode *inode, struct page *src,
134 struct page *dst, unsigned int len,
135 unsigned int offs, u64 lblk_num, gfp_t gfp_flags);
137 static inline int llcrypt_decrypt_block_inplace(const struct inode *inode,
143 return llcrypt_decrypt_block(inode, page, page, len, offs, lblk_num,
147 static inline bool llcrypt_is_bounce_page(struct page *page)
149 return page->mapping == NULL;
152 static inline struct page *llcrypt_pagecache_page(struct page *bounce_page)
154 return (struct page *)page_private(bounce_page);
157 extern void llcrypt_free_bounce_page(struct page *bounce_page);
160 extern int llcrypt_ioctl_set_policy(struct file *, const void __user *);
161 extern int llcrypt_ioctl_get_policy(struct file *, void __user *);
162 extern int llcrypt_ioctl_get_policy_ex(struct file *, void __user *);
163 extern int llcrypt_has_permitted_context(struct inode *, struct inode *);
164 extern int llcrypt_inherit_context(struct inode *, struct inode *,
166 extern bool llcrypt_policy_has_filename_enc(struct inode *inode);
168 extern void llcrypt_sb_free(struct super_block *sb);
169 extern int llcrypt_ioctl_add_key(struct file *filp, void __user *arg);
170 extern int llcrypt_ioctl_remove_key(struct file *filp, void __user *arg);
171 extern int llcrypt_ioctl_remove_key_all_users(struct file *filp,
173 extern int llcrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
176 extern int llcrypt_get_encryption_info(struct inode *);
177 extern void llcrypt_put_encryption_info(struct inode *);
178 extern void llcrypt_free_inode(struct inode *);
179 extern int llcrypt_drop_inode(struct inode *inode);
182 extern int llcrypt_setup_filename(struct inode *, const struct qstr *,
183 int lookup, struct llcrypt_name *);
185 static inline void llcrypt_free_filename(struct llcrypt_name *fname)
187 kfree(fname->crypto_buf.name);
190 extern int llcrypt_fname_alloc_buffer(const struct inode *, u32,
191 struct llcrypt_str *);
192 extern void llcrypt_fname_free_buffer(struct llcrypt_str *);
193 extern int llcrypt_fname_disk_to_usr(struct inode *, u32, u32,
194 const struct llcrypt_str *, struct llcrypt_str *);
196 #define LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
198 /* Extracts the second-to-last ciphertext block; see explanation below */
199 #define LLCRYPT_FNAME_DIGEST(name, len) \
200 ((name) + round_down((len) - LL_CRYPTO_BLOCK_SIZE - 1, \
201 LL_CRYPTO_BLOCK_SIZE))
203 #define LLCRYPT_FNAME_DIGEST_SIZE LL_CRYPTO_BLOCK_SIZE
206 * llcrypt_digested_name - alternate identifier for an on-disk filename
208 * When userspace lists an encrypted directory without access to the key,
209 * filenames whose ciphertext is longer than LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE
210 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
211 * full ciphertext (base64-encoded). This is necessary to allow supporting
212 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
214 * To make it possible for filesystems to still find the correct directory entry
215 * despite not knowing the full on-disk name, we encode any filesystem-specific
216 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
217 * followed by the second-to-last ciphertext block of the filename. Due to the
218 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
219 * depends on the full plaintext. (Note that ciphertext stealing causes the
220 * last two blocks to appear "flipped".) This makes accidental collisions very
221 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
222 * share the same filesystem-specific hashes.
224 * However, this scheme isn't immune to intentional collisions, which can be
225 * created by anyone able to create arbitrary plaintext filenames and view them
226 * without the key. Making the "digest" be a real cryptographic hash like
227 * SHA-256 over the full ciphertext would prevent this, although it would be
228 * less efficient and harder to implement, especially since the filesystem would
229 * need to calculate it for each directory entry examined during a search.
231 struct llcrypt_digested_name {
234 u8 digest[LLCRYPT_FNAME_DIGEST_SIZE];
238 * llcrypt_match_name() - test whether the given name matches a directory entry
239 * @fname: the name being searched for
240 * @de_name: the name from the directory entry
241 * @de_name_len: the length of @de_name in bytes
243 * Normally @fname->disk_name will be set, and in that case we simply compare
244 * that to the name stored in the directory entry. The only exception is that
245 * if we don't have the key for an encrypted directory and a filename in it is
246 * very long, then we won't have the full disk_name and we'll instead need to
247 * match against the llcrypt_digested_name.
249 * Return: %true if the name matches, otherwise %false.
251 static inline bool llcrypt_match_name(const struct llcrypt_name *fname,
252 const u8 *de_name, u32 de_name_len)
254 if (unlikely(!fname->disk_name.name)) {
255 const struct llcrypt_digested_name *n =
256 (const void *)fname->crypto_buf.name;
257 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
259 if (de_name_len <= LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
261 return !memcmp(LLCRYPT_FNAME_DIGEST(de_name, de_name_len),
262 n->digest, LLCRYPT_FNAME_DIGEST_SIZE);
265 if (de_name_len != fname->disk_name.len)
267 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
271 extern int llcrypt_file_open(struct inode *inode, struct file *filp);
272 extern int __llcrypt_prepare_link(struct inode *inode, struct inode *dir,
273 struct dentry *dentry);
274 extern int __llcrypt_prepare_rename(struct inode *old_dir,
275 struct dentry *old_dentry,
276 struct inode *new_dir,
277 struct dentry *new_dentry,
279 extern int __llcrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
280 struct llcrypt_name *fname);
281 extern int __llcrypt_prepare_symlink(struct inode *dir, unsigned int len,
282 unsigned int max_len,
283 struct llcrypt_str *disk_link);
284 extern int __llcrypt_encrypt_symlink(struct inode *inode, const char *target,
286 struct llcrypt_str *disk_link);
287 extern const char *llcrypt_get_symlink(struct inode *inode, const void *caddr,
288 unsigned int max_size,
289 struct delayed_call *done);
290 static inline void llcrypt_set_ops(struct super_block *sb,
291 const struct llcrypt_operations *lsi_cop)
293 struct lustre_sb_info *lsi = s2lsi(sb);
296 lsi->lsi_cop = lsi_cop;
298 #else /* !CONFIG_LL_ENCRYPTION */
300 struct llcrypt_operations;
301 #define llcrypt_init() 0
302 #define llcrypt_exit() {}
305 #define IS_ENCRYPTED(x) 0
307 static inline bool llcrypt_has_encryption_key(const struct inode *inode)
312 static inline bool llcrypt_dummy_context_enabled(struct inode *inode)
317 static inline void llcrypt_handle_d_move(struct dentry *dentry)
322 static inline void llcrypt_enqueue_decrypt_work(struct work_struct *work)
326 static inline struct llcrypt_ctx *llcrypt_get_ctx(gfp_t gfp_flags)
328 return ERR_PTR(-EOPNOTSUPP);
331 static inline void llcrypt_release_ctx(struct llcrypt_ctx *ctx)
336 static inline struct page *llcrypt_encrypt_pagecache_blocks(struct page *page,
341 return ERR_PTR(-EOPNOTSUPP);
344 static inline int llcrypt_encrypt_block(const struct inode *inode,
345 struct page *src, struct page *dst,
346 unsigned int len, unsigned int offs,
347 u64 lblk_num, gfp_t gfp_flags)
352 static inline int llcrypt_decrypt_pagecache_blocks(struct page *page,
359 static inline int llcrypt_decrypt_block(const struct inode *inode,
360 struct page *src, struct page *dst,
361 unsigned int len, unsigned int offs,
362 u64 lblk_num, gfp_t gfp_flags)
367 static inline int llcrypt_decrypt_block_inplace(const struct inode *inode,
370 unsigned int offs, u64 lblk_num)
375 static inline bool llcrypt_is_bounce_page(struct page *page)
380 static inline struct page *llcrypt_pagecache_page(struct page *bounce_page)
383 return ERR_PTR(-EINVAL);
386 static inline void llcrypt_free_bounce_page(struct page *bounce_page)
391 static inline int llcrypt_ioctl_set_policy(struct file *filp,
392 const void __user *arg)
397 static inline int llcrypt_ioctl_get_policy(struct file *filp, void __user *arg)
402 static inline int llcrypt_ioctl_get_policy_ex(struct file *filp,
408 static inline int llcrypt_has_permitted_context(struct inode *parent,
414 static inline int llcrypt_inherit_context(struct inode *parent,
416 void *fs_data, bool preload)
420 static inline bool llcrypt_policy_has_filename_enc(struct inode *inode)
426 static inline void llcrypt_sb_free(struct super_block *sb)
430 static inline int llcrypt_ioctl_add_key(struct file *filp, void __user *arg)
435 static inline int llcrypt_ioctl_remove_key(struct file *filp, void __user *arg)
440 static inline int llcrypt_ioctl_remove_key_all_users(struct file *filp,
446 static inline int llcrypt_ioctl_get_key_status(struct file *filp,
453 static inline int llcrypt_get_encryption_info(struct inode *inode)
458 static inline void llcrypt_put_encryption_info(struct inode *inode)
463 static inline void llcrypt_free_inode(struct inode *inode)
467 static inline int llcrypt_drop_inode(struct inode *inode)
473 static inline int llcrypt_setup_filename(struct inode *dir,
474 const struct qstr *iname,
475 int lookup, struct llcrypt_name *fname)
477 if (IS_ENCRYPTED(dir))
480 memset(fname, 0, sizeof(*fname));
481 fname->usr_fname = iname;
482 fname->disk_name.name = (unsigned char *)iname->name;
483 fname->disk_name.len = iname->len;
487 static inline void llcrypt_free_filename(struct llcrypt_name *fname)
492 static inline int llcrypt_fname_alloc_buffer(const struct inode *inode,
493 u32 max_encrypted_len,
494 struct llcrypt_str *crypto_str)
499 static inline void llcrypt_fname_free_buffer(struct llcrypt_str *crypto_str)
504 static inline int llcrypt_fname_disk_to_usr(struct inode *inode,
505 u32 hash, u32 minor_hash,
506 const struct llcrypt_str *iname,
507 struct llcrypt_str *oname)
512 static inline bool llcrypt_match_name(const struct llcrypt_name *fname,
513 const u8 *de_name, u32 de_name_len)
515 /* Encryption support disabled; use standard comparison */
516 if (de_name_len != fname->disk_name.len)
518 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
523 static inline int llcrypt_file_open(struct inode *inode, struct file *filp)
525 if (IS_ENCRYPTED(inode))
530 static inline int __llcrypt_prepare_link(struct inode *inode, struct inode *dir,
531 struct dentry *dentry)
536 static inline int __llcrypt_prepare_rename(struct inode *old_dir,
537 struct dentry *old_dentry,
538 struct inode *new_dir,
539 struct dentry *new_dentry,
545 static inline int __llcrypt_prepare_lookup(struct inode *dir,
546 struct dentry *dentry,
547 struct llcrypt_name *fname)
552 static inline int __llcrypt_prepare_symlink(struct inode *dir,
554 unsigned int max_len,
555 struct llcrypt_str *disk_link)
561 static inline int __llcrypt_encrypt_symlink(struct inode *inode,
564 struct llcrypt_str *disk_link)
569 #define llcrypt_get_symlink(inode, caddr, max_size, done) ERR_PTR(-EOPNOTSUPP)
571 static inline void llcrypt_set_ops(struct super_block *sb,
572 const struct llcrypt_operations *lsi_cop)
576 #endif /* !CONFIG_LL_ENCRYPTION */
579 * llcrypt_require_key - require an inode's encryption key
580 * @inode: the inode we need the key for
582 * If the inode is encrypted, set up its encryption key if not already done.
583 * Then require that the key be present and return -ENOKEY otherwise.
585 * No locks are needed, and the key will live as long as the struct inode --- so
586 * it won't go away from under you.
588 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
589 * if a problem occurred while setting up the encryption key.
591 static inline int llcrypt_require_key(struct inode *inode)
593 if (IS_ENCRYPTED(inode)) {
594 int err = llcrypt_get_encryption_info(inode);
598 if (!llcrypt_has_encryption_key(inode))
605 * llcrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
606 * @old_dentry: an existing dentry for the inode being linked
607 * @dir: the target directory
608 * @dentry: negative dentry for the target filename
610 * A new link can only be added to an encrypted directory if the directory's
611 * encryption key is available --- since otherwise we'd have no way to encrypt
612 * the filename. Therefore, we first set up the directory's encryption key (if
613 * not already done) and return an error if it's unavailable.
615 * We also verify that the link will not violate the constraint that all files
616 * in an encrypted directory tree use the same encryption policy.
618 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
619 * -EXDEV if the link would result in an inconsistent encryption policy, or
620 * another -errno code.
622 static inline int llcrypt_prepare_link(struct dentry *old_dentry,
624 struct dentry *dentry)
626 if (IS_ENCRYPTED(dir))
627 return __llcrypt_prepare_link(d_inode(old_dentry), dir, dentry);
632 * llcrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
633 * @old_dir: source directory
634 * @old_dentry: dentry for source file
635 * @new_dir: target directory
636 * @new_dentry: dentry for target location (may be negative unless exchanging)
637 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
639 * Prepare for ->rename() where the source and/or target directories may be
640 * encrypted. A new link can only be added to an encrypted directory if the
641 * directory's encryption key is available --- since otherwise we'd have no way
642 * to encrypt the filename. A rename to an existing name, on the other hand,
643 * *is* cryptographically possible without the key. However, we take the more
644 * conservative approach and just forbid all no-key renames.
646 * We also verify that the rename will not violate the constraint that all files
647 * in an encrypted directory tree use the same encryption policy.
649 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
650 * rename would cause inconsistent encryption policies, or another -errno code.
652 static inline int llcrypt_prepare_rename(struct inode *old_dir,
653 struct dentry *old_dentry,
654 struct inode *new_dir,
655 struct dentry *new_dentry,
658 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
659 return __llcrypt_prepare_rename(old_dir, old_dentry,
660 new_dir, new_dentry, flags);
665 * llcrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
666 * @dir: directory being searched
667 * @dentry: filename being looked up
668 * @fname: (output) the name to use to search the on-disk directory
670 * Prepare for ->lookup() in a directory which may be encrypted by determining
671 * the name that will actually be used to search the directory on-disk. Lookups
672 * can be done with or without the directory's encryption key; without the key,
673 * filenames are presented in encrypted form. Therefore, we'll try to set up
674 * the directory's encryption key, but even without it the lookup can continue.
676 * This also installs a custom ->d_revalidate() method which will invalidate the
677 * dentry if it was created without the key and the key is later added.
679 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
680 * correctly formed encoded ciphertext name, so a negative dentry should be
681 * created; or another -errno code.
683 static inline int llcrypt_prepare_lookup(struct inode *dir,
684 struct dentry *dentry,
685 struct llcrypt_name *fname)
687 if (IS_ENCRYPTED(dir))
688 return __llcrypt_prepare_lookup(dir, dentry, fname);
690 memset(fname, 0, sizeof(*fname));
691 fname->usr_fname = &dentry->d_name;
692 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
693 fname->disk_name.len = dentry->d_name.len;
698 * llcrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
699 * @dentry: dentry through which the inode is being changed
700 * @attr: attributes to change
702 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
703 * most attribute changes are allowed even without the encryption key. However,
704 * without the encryption key we do have to forbid truncates. This is needed
705 * because the size being truncated to may not be a multiple of the filesystem
706 * block size, and in that case we'd have to decrypt the final block, zero the
707 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
708 * filesystem block boundary, but it's simpler to just forbid all truncates ---
709 * and we already forbid all other contents modifications without the key.)
711 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
712 * if a problem occurred while setting up the encryption key.
714 static inline int llcrypt_prepare_setattr(struct dentry *dentry,
717 if (attr->ia_valid & ATTR_SIZE)
718 return llcrypt_require_key(d_inode(dentry));
723 * llcrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
724 * @dir: directory in which the symlink is being created
725 * @target: plaintext symlink target
726 * @len: length of @target excluding null terminator
727 * @max_len: space the filesystem has available to store the symlink target
728 * @disk_link: (out) the on-disk symlink target being prepared
730 * This function computes the size the symlink target will require on-disk,
731 * stores it in @disk_link->len, and validates it against @max_len. An
732 * encrypted symlink may be longer than the original.
734 * Additionally, @disk_link->name is set to @target if the symlink will be
735 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
736 * symlinks, the filesystem must call llcrypt_encrypt_symlink() to create the
737 * on-disk target later. (The reason for the two-step process is that some
738 * filesystems need to know the size of the symlink target before creating the
739 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
741 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
742 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
743 * occurred while setting up the encryption key.
745 static inline int llcrypt_prepare_symlink(struct inode *dir,
748 unsigned int max_len,
749 struct llcrypt_str *disk_link)
751 if ((IS_ENCRYPTED(dir) || llcrypt_dummy_context_enabled(dir)) &&
752 llcrypt_policy_has_filename_enc(dir))
753 return __llcrypt_prepare_symlink(dir, len, max_len, disk_link);
755 disk_link->name = (unsigned char *)target;
756 disk_link->len = len + 1;
757 if (disk_link->len > max_len)
758 return -ENAMETOOLONG;
763 * llcrypt_encrypt_symlink - encrypt the symlink target if needed
764 * @inode: symlink inode
765 * @target: plaintext symlink target
766 * @len: length of @target excluding null terminator
767 * @disk_link: (in/out) the on-disk symlink target being prepared
769 * If the symlink target needs to be encrypted, then this function encrypts it
770 * into @disk_link->name. llcrypt_prepare_symlink() must have been called
771 * previously to compute @disk_link->len. If the filesystem did not allocate a
772 * buffer for @disk_link->name after calling llcrypt_prepare_link(), then one
773 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
775 * Return: 0 on success, -errno on failure
777 static inline int llcrypt_encrypt_symlink(struct inode *inode,
780 struct llcrypt_str *disk_link)
782 if (IS_ENCRYPTED(inode))
783 return __llcrypt_encrypt_symlink(inode, target, len, disk_link);
787 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
788 static inline void llcrypt_finalize_bounce_page(struct page **pagep)
790 struct page *page = *pagep;
792 if (llcrypt_is_bounce_page(page)) {
793 *pagep = llcrypt_pagecache_page(page);
794 llcrypt_free_bounce_page(page);
798 #endif /* _LINUX_LLCRYPT_H */