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>
20 #define FS_CRYPTO_BLOCK_SIZE 16
31 const struct qstr *usr_fname;
32 struct fscrypt_str disk_name;
35 struct fscrypt_str crypto_buf;
38 #define FSTR_INIT(n, l) { .name = n, .len = l }
39 #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
40 #define fname_name(p) ((p)->disk_name.name)
41 #define fname_len(p) ((p)->disk_name.len)
43 /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
44 #define FSCRYPT_SET_CONTEXT_MAX_SIZE 28
46 #ifdef CONFIG_FS_ENCRYPTION
48 * fscrypt superblock flags
50 #define FS_CFLG_OWN_PAGES (1U << 1)
53 * crypto operations for filesystems
55 struct fscrypt_operations {
57 const char *key_prefix;
58 int (*get_context)(struct inode *, void *, size_t);
59 int (*set_context)(struct inode *, const void *, size_t, void *);
60 bool (*dummy_context)(struct inode *);
61 bool (*empty_dir)(struct inode *);
62 unsigned int max_namelen;
68 struct page *bounce_page; /* Ciphertext page */
69 struct page *control_page; /* Original page */
73 struct work_struct work;
75 struct list_head free_list; /* Free list */
80 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
82 return (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 extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
93 extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t);
94 extern void fscrypt_release_ctx(struct fscrypt_ctx *);
95 extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
96 unsigned int, unsigned int,
98 extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
101 static inline struct page *fscrypt_control_page(struct page *page)
103 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
106 extern void fscrypt_restore_control_page(struct page *);
109 extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
110 extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
111 extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
112 extern int fscrypt_inherit_context(struct inode *, struct inode *,
115 extern int fscrypt_get_encryption_info(struct inode *);
116 extern void fscrypt_put_encryption_info(struct inode *);
119 extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
120 int lookup, struct fscrypt_name *);
122 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
124 kfree(fname->crypto_buf.name);
127 extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
128 struct fscrypt_str *);
129 extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
130 extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
131 const struct fscrypt_str *, struct fscrypt_str *);
133 #define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
135 /* Extracts the second-to-last ciphertext block; see explanation below */
136 #define FSCRYPT_FNAME_DIGEST(name, len) \
137 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
138 FS_CRYPTO_BLOCK_SIZE))
140 #define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
143 * fscrypt_digested_name - alternate identifier for an on-disk filename
145 * When userspace lists an encrypted directory without access to the key,
146 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
147 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
148 * full ciphertext (base64-encoded). This is necessary to allow supporting
149 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
151 * To make it possible for filesystems to still find the correct directory entry
152 * despite not knowing the full on-disk name, we encode any filesystem-specific
153 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
154 * followed by the second-to-last ciphertext block of the filename. Due to the
155 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
156 * depends on the full plaintext. (Note that ciphertext stealing causes the
157 * last two blocks to appear "flipped".) This makes accidental collisions very
158 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
159 * share the same filesystem-specific hashes.
161 * However, this scheme isn't immune to intentional collisions, which can be
162 * created by anyone able to create arbitrary plaintext filenames and view them
163 * without the key. Making the "digest" be a real cryptographic hash like
164 * SHA-256 over the full ciphertext would prevent this, although it would be
165 * less efficient and harder to implement, especially since the filesystem would
166 * need to calculate it for each directory entry examined during a search.
168 struct fscrypt_digested_name {
171 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
175 * fscrypt_match_name() - test whether the given name matches a directory entry
176 * @fname: the name being searched for
177 * @de_name: the name from the directory entry
178 * @de_name_len: the length of @de_name in bytes
180 * Normally @fname->disk_name will be set, and in that case we simply compare
181 * that to the name stored in the directory entry. The only exception is that
182 * if we don't have the key for an encrypted directory and a filename in it is
183 * very long, then we won't have the full disk_name and we'll instead need to
184 * match against the fscrypt_digested_name.
186 * Return: %true if the name matches, otherwise %false.
188 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
189 const u8 *de_name, u32 de_name_len)
191 if (unlikely(!fname->disk_name.name)) {
192 const struct fscrypt_digested_name *n =
193 (const void *)fname->crypto_buf.name;
194 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
196 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
198 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
199 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
202 if (de_name_len != fname->disk_name.len)
204 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
208 extern void fscrypt_decrypt_bio(struct bio *);
209 extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
211 extern void fscrypt_pullback_bio_page(struct page **, bool);
212 extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
216 extern int fscrypt_file_open(struct inode *inode, struct file *filp);
217 extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir);
218 extern int __fscrypt_prepare_rename(struct inode *old_dir,
219 struct dentry *old_dentry,
220 struct inode *new_dir,
221 struct dentry *new_dentry,
223 extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry);
224 extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
225 unsigned int max_len,
226 struct fscrypt_str *disk_link);
227 extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
229 struct fscrypt_str *disk_link);
230 extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
231 unsigned int max_size,
232 struct delayed_call *done);
233 #else /* !CONFIG_FS_ENCRYPTION */
235 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
240 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
246 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
250 static inline struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *inode,
253 return ERR_PTR(-EOPNOTSUPP);
256 static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
261 static inline struct page *fscrypt_encrypt_page(const struct inode *inode,
265 u64 lblk_num, gfp_t gfp_flags)
267 return ERR_PTR(-EOPNOTSUPP);
270 static inline int fscrypt_decrypt_page(const struct inode *inode,
272 unsigned int len, unsigned int offs,
278 static inline struct page *fscrypt_control_page(struct page *page)
281 return ERR_PTR(-EINVAL);
284 static inline void fscrypt_restore_control_page(struct page *page)
290 static inline int fscrypt_ioctl_set_policy(struct file *filp,
291 const void __user *arg)
296 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
301 static inline int fscrypt_has_permitted_context(struct inode *parent,
307 static inline int fscrypt_inherit_context(struct inode *parent,
309 void *fs_data, bool preload)
315 static inline int fscrypt_get_encryption_info(struct inode *inode)
320 static inline void fscrypt_put_encryption_info(struct inode *inode)
326 static inline int fscrypt_setup_filename(struct inode *dir,
327 const struct qstr *iname,
328 int lookup, struct fscrypt_name *fname)
330 if (IS_ENCRYPTED(dir))
333 memset(fname, 0, sizeof(struct fscrypt_name));
334 fname->usr_fname = iname;
335 fname->disk_name.name = (unsigned char *)iname->name;
336 fname->disk_name.len = iname->len;
340 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
345 static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
346 u32 max_encrypted_len,
347 struct fscrypt_str *crypto_str)
352 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
357 static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
358 u32 hash, u32 minor_hash,
359 const struct fscrypt_str *iname,
360 struct fscrypt_str *oname)
365 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
366 const u8 *de_name, u32 de_name_len)
368 /* Encryption support disabled; use standard comparison */
369 if (de_name_len != fname->disk_name.len)
371 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
375 static inline void fscrypt_decrypt_bio(struct bio *bio)
379 static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
384 static inline void fscrypt_pullback_bio_page(struct page **page, bool restore)
389 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
390 sector_t pblk, unsigned int len)
397 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
399 if (IS_ENCRYPTED(inode))
404 static inline int __fscrypt_prepare_link(struct inode *inode,
410 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
411 struct dentry *old_dentry,
412 struct inode *new_dir,
413 struct dentry *new_dentry,
419 static inline int __fscrypt_prepare_lookup(struct inode *dir,
420 struct dentry *dentry)
425 static inline int __fscrypt_prepare_symlink(struct inode *dir,
427 unsigned int max_len,
428 struct fscrypt_str *disk_link)
434 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
437 struct fscrypt_str *disk_link)
442 static inline const char *fscrypt_get_symlink(struct inode *inode,
444 unsigned int max_size,
445 struct delayed_call *done)
447 return ERR_PTR(-EOPNOTSUPP);
449 #endif /* !CONFIG_FS_ENCRYPTION */
452 * fscrypt_require_key - require an inode's encryption key
453 * @inode: the inode we need the key for
455 * If the inode is encrypted, set up its encryption key if not already done.
456 * Then require that the key be present and return -ENOKEY otherwise.
458 * No locks are needed, and the key will live as long as the struct inode --- so
459 * it won't go away from under you.
461 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
462 * if a problem occurred while setting up the encryption key.
464 static inline int fscrypt_require_key(struct inode *inode)
466 if (IS_ENCRYPTED(inode)) {
467 int err = fscrypt_get_encryption_info(inode);
471 if (!fscrypt_has_encryption_key(inode))
478 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
479 * @old_dentry: an existing dentry for the inode being linked
480 * @dir: the target directory
481 * @dentry: negative dentry for the target filename
483 * A new link can only be added to an encrypted directory if the directory's
484 * encryption key is available --- since otherwise we'd have no way to encrypt
485 * the filename. Therefore, we first set up the directory's encryption key (if
486 * not already done) and return an error if it's unavailable.
488 * We also verify that the link will not violate the constraint that all files
489 * in an encrypted directory tree use the same encryption policy.
491 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
492 * -EXDEV if the link would result in an inconsistent encryption policy, or
493 * another -errno code.
495 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
497 struct dentry *dentry)
499 if (IS_ENCRYPTED(dir))
500 return __fscrypt_prepare_link(d_inode(old_dentry), dir);
505 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
506 * @old_dir: source directory
507 * @old_dentry: dentry for source file
508 * @new_dir: target directory
509 * @new_dentry: dentry for target location (may be negative unless exchanging)
510 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
512 * Prepare for ->rename() where the source and/or target directories may be
513 * encrypted. A new link can only be added to an encrypted directory if the
514 * directory's encryption key is available --- since otherwise we'd have no way
515 * to encrypt the filename. A rename to an existing name, on the other hand,
516 * *is* cryptographically possible without the key. However, we take the more
517 * conservative approach and just forbid all no-key renames.
519 * We also verify that the rename will not violate the constraint that all files
520 * in an encrypted directory tree use the same encryption policy.
522 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
523 * rename would cause inconsistent encryption policies, or another -errno code.
525 static inline int fscrypt_prepare_rename(struct inode *old_dir,
526 struct dentry *old_dentry,
527 struct inode *new_dir,
528 struct dentry *new_dentry,
531 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
532 return __fscrypt_prepare_rename(old_dir, old_dentry,
533 new_dir, new_dentry, flags);
538 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
539 * @dir: directory being searched
540 * @dentry: filename being looked up
541 * @flags: lookup flags
543 * Prepare for ->lookup() in a directory which may be encrypted. Lookups can be
544 * done with or without the directory's encryption key; without the key,
545 * filenames are presented in encrypted form. Therefore, we'll try to set up
546 * the directory's encryption key, but even without it the lookup can continue.
548 * To allow invalidating stale dentries if the directory's encryption key is
549 * added later, we also install a custom ->d_revalidate() method and use the
550 * DCACHE_ENCRYPTED_WITH_KEY flag to indicate whether a given dentry is a
551 * plaintext name (flag set) or a ciphertext name (flag cleared).
553 * Return: 0 on success, -errno if a problem occurred while setting up the
556 static inline int fscrypt_prepare_lookup(struct inode *dir,
557 struct dentry *dentry,
560 if (IS_ENCRYPTED(dir))
561 return __fscrypt_prepare_lookup(dir, dentry);
566 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
567 * @dentry: dentry through which the inode is being changed
568 * @attr: attributes to change
570 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
571 * most attribute changes are allowed even without the encryption key. However,
572 * without the encryption key we do have to forbid truncates. This is needed
573 * because the size being truncated to may not be a multiple of the filesystem
574 * block size, and in that case we'd have to decrypt the final block, zero the
575 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
576 * filesystem block boundary, but it's simpler to just forbid all truncates ---
577 * and we already forbid all other contents modifications without the key.)
579 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
580 * if a problem occurred while setting up the encryption key.
582 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
585 if (attr->ia_valid & ATTR_SIZE)
586 return fscrypt_require_key(d_inode(dentry));
591 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
592 * @dir: directory in which the symlink is being created
593 * @target: plaintext symlink target
594 * @len: length of @target excluding null terminator
595 * @max_len: space the filesystem has available to store the symlink target
596 * @disk_link: (out) the on-disk symlink target being prepared
598 * This function computes the size the symlink target will require on-disk,
599 * stores it in @disk_link->len, and validates it against @max_len. An
600 * encrypted symlink may be longer than the original.
602 * Additionally, @disk_link->name is set to @target if the symlink will be
603 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
604 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
605 * on-disk target later. (The reason for the two-step process is that some
606 * filesystems need to know the size of the symlink target before creating the
607 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
609 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
610 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
611 * occurred while setting up the encryption key.
613 static inline int fscrypt_prepare_symlink(struct inode *dir,
616 unsigned int max_len,
617 struct fscrypt_str *disk_link)
619 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
620 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
622 disk_link->name = (unsigned char *)target;
623 disk_link->len = len + 1;
624 if (disk_link->len > max_len)
625 return -ENAMETOOLONG;
630 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
631 * @inode: symlink inode
632 * @target: plaintext symlink target
633 * @len: length of @target excluding null terminator
634 * @disk_link: (in/out) the on-disk symlink target being prepared
636 * If the symlink target needs to be encrypted, then this function encrypts it
637 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
638 * previously to compute @disk_link->len. If the filesystem did not allocate a
639 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
640 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
642 * Return: 0 on success, -errno on failure
644 static inline int fscrypt_encrypt_symlink(struct inode *inode,
647 struct fscrypt_str *disk_link)
649 if (IS_ENCRYPTED(inode))
650 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
654 #endif /* _LINUX_FSCRYPT_H */