1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * fscrypt_supp.h
4  *
5  * Do not include this file directly. Use fscrypt.h instead!
6  */
7 #ifndef _LINUX_FSCRYPT_H
8 #error "Incorrect include of linux/fscrypt_supp.h!"
9 #endif
10 
11 #ifndef _LINUX_FSCRYPT_SUPP_H
12 #define _LINUX_FSCRYPT_SUPP_H
13 
14 #include <linux/mm.h>
15 #include <linux/slab.h>
16 
17 /*
18  * fscrypt superblock flags
19  */
20 #define FS_CFLG_OWN_PAGES (1U << 1)
21 
22 /*
23  * crypto operations for filesystems
24  */
25 struct fscrypt_operations {
26 	unsigned int flags;
27 	const char *key_prefix;
28 	int (*get_context)(struct inode *, void *, size_t);
29 	int (*set_context)(struct inode *, const void *, size_t, void *);
30 	bool (*dummy_context)(struct inode *);
31 	bool (*empty_dir)(struct inode *);
32 	unsigned int max_namelen;
33 };
34 
35 struct fscrypt_ctx {
36 	union {
37 		struct {
38 			struct page *bounce_page;	/* Ciphertext page */
39 			struct page *control_page;	/* Original page  */
40 		} w;
41 		struct {
42 			struct bio *bio;
43 			struct work_struct work;
44 		} r;
45 		struct list_head free_list;	/* Free list */
46 	};
47 	u8 flags;				/* Flags */
48 };
49 
fscrypt_has_encryption_key(const struct inode * inode)50 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
51 {
52 	return (inode->i_crypt_info != NULL);
53 }
54 
fscrypt_dummy_context_enabled(struct inode * inode)55 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
56 {
57 	return inode->i_sb->s_cop->dummy_context &&
58 		inode->i_sb->s_cop->dummy_context(inode);
59 }
60 
61 /**
62  * fscrypt_is_nokey_name() - test whether a dentry is a no-key name
63  * @dentry: the dentry to check
64  *
65  * This returns true if the dentry is a no-key dentry.  A no-key dentry is a
66  * dentry that was created in an encrypted directory that hasn't had its
67  * encryption key added yet.  Such dentries may be either positive or negative.
68  *
69  * When a filesystem is asked to create a new filename in an encrypted directory
70  * and the new filename's dentry is a no-key dentry, it must fail the operation
71  * with ENOKEY.  This includes ->create(), ->mkdir(), ->mknod(), ->symlink(),
72  * ->rename(), and ->link().  (However, ->rename() and ->link() are already
73  * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().)
74  *
75  * This is necessary because creating a filename requires the directory's
76  * encryption key, but just checking for the key on the directory inode during
77  * the final filesystem operation doesn't guarantee that the key was available
78  * during the preceding dentry lookup.  And the key must have already been
79  * available during the dentry lookup in order for it to have been checked
80  * whether the filename already exists in the directory and for the new file's
81  * dentry not to be invalidated due to it incorrectly having the no-key flag.
82  *
83  * Return: %true if the dentry is a no-key name
84  */
fscrypt_is_nokey_name(const struct dentry * dentry)85 static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
86 {
87 	return dentry->d_flags & DCACHE_ENCRYPTED_NAME;
88 }
89 
90 /* crypto.c */
91 extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
92 extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t);
93 extern void fscrypt_release_ctx(struct fscrypt_ctx *);
94 extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
95 						unsigned int, unsigned int,
96 						u64, gfp_t);
97 extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
98 				unsigned int, u64);
99 
fscrypt_control_page(struct page * page)100 static inline struct page *fscrypt_control_page(struct page *page)
101 {
102 	return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
103 }
104 
105 extern void fscrypt_restore_control_page(struct page *);
106 
107 /* policy.c */
108 extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
109 extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
110 extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
111 extern int fscrypt_inherit_context(struct inode *, struct inode *,
112 					void *, bool);
113 /* keyinfo.c */
114 extern int fscrypt_get_encryption_info(struct inode *);
115 extern void fscrypt_put_encryption_info(struct inode *);
116 
117 /* fname.c */
118 extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
119 				int lookup, struct fscrypt_name *);
120 
fscrypt_free_filename(struct fscrypt_name * fname)121 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
122 {
123 	kfree(fname->crypto_buf.name);
124 }
125 
126 extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
127 				struct fscrypt_str *);
128 extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
129 extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
130 			const struct fscrypt_str *, struct fscrypt_str *);
131 
132 #define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE	32
133 
134 /* Extracts the second-to-last ciphertext block; see explanation below */
135 #define FSCRYPT_FNAME_DIGEST(name, len)	\
136 	((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
137 			     FS_CRYPTO_BLOCK_SIZE))
138 
139 #define FSCRYPT_FNAME_DIGEST_SIZE	FS_CRYPTO_BLOCK_SIZE
140 
141 /**
142  * fscrypt_digested_name - alternate identifier for an on-disk filename
143  *
144  * When userspace lists an encrypted directory without access to the key,
145  * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
146  * bytes are shown in this abbreviated form (base64-encoded) rather than as the
147  * full ciphertext (base64-encoded).  This is necessary to allow supporting
148  * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
149  *
150  * To make it possible for filesystems to still find the correct directory entry
151  * despite not knowing the full on-disk name, we encode any filesystem-specific
152  * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
153  * followed by the second-to-last ciphertext block of the filename.  Due to the
154  * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
155  * depends on the full plaintext.  (Note that ciphertext stealing causes the
156  * last two blocks to appear "flipped".)  This makes accidental collisions very
157  * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
158  * share the same filesystem-specific hashes.
159  *
160  * However, this scheme isn't immune to intentional collisions, which can be
161  * created by anyone able to create arbitrary plaintext filenames and view them
162  * without the key.  Making the "digest" be a real cryptographic hash like
163  * SHA-256 over the full ciphertext would prevent this, although it would be
164  * less efficient and harder to implement, especially since the filesystem would
165  * need to calculate it for each directory entry examined during a search.
166  */
167 struct fscrypt_digested_name {
168 	u32 hash;
169 	u32 minor_hash;
170 	u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
171 };
172 
173 /**
174  * fscrypt_match_name() - test whether the given name matches a directory entry
175  * @fname: the name being searched for
176  * @de_name: the name from the directory entry
177  * @de_name_len: the length of @de_name in bytes
178  *
179  * Normally @fname->disk_name will be set, and in that case we simply compare
180  * that to the name stored in the directory entry.  The only exception is that
181  * if we don't have the key for an encrypted directory and a filename in it is
182  * very long, then we won't have the full disk_name and we'll instead need to
183  * match against the fscrypt_digested_name.
184  *
185  * Return: %true if the name matches, otherwise %false.
186  */
fscrypt_match_name(const struct fscrypt_name * fname,const u8 * de_name,u32 de_name_len)187 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
188 				      const u8 *de_name, u32 de_name_len)
189 {
190 	if (unlikely(!fname->disk_name.name)) {
191 		const struct fscrypt_digested_name *n =
192 			(const void *)fname->crypto_buf.name;
193 		if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
194 			return false;
195 		if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
196 			return false;
197 		return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
198 			       n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
199 	}
200 
201 	if (de_name_len != fname->disk_name.len)
202 		return false;
203 	return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
204 }
205 
206 /* bio.c */
207 extern void fscrypt_decrypt_bio(struct bio *);
208 extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
209 					struct bio *bio);
210 extern void fscrypt_pullback_bio_page(struct page **, bool);
211 extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
212 				 unsigned int);
213 
214 /* hooks.c */
215 extern int fscrypt_file_open(struct inode *inode, struct file *filp);
216 extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
217 				  struct dentry *dentry);
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,
222 				    unsigned int flags);
223 extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
224 				    struct fscrypt_name *fname);
225 extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
226 				     unsigned int max_len,
227 				     struct fscrypt_str *disk_link);
228 extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
229 				     unsigned int len,
230 				     struct fscrypt_str *disk_link);
231 extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
232 				       unsigned int max_size,
233 				       struct delayed_call *done);
234 int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat);
235 
236 #endif	/* _LINUX_FSCRYPT_SUPP_H */
237