1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * fscrypt.h: declarations for per-file encryption
4  *
5  * Filesystems that implement per-file encryption must include this header
6  * file.
7  *
8  * Copyright (C) 2015, Google, Inc.
9  *
10  * Written by Michael Halcrow, 2015.
11  * Modified by Jaegeuk Kim, 2015.
12  */
13 #ifndef _LINUX_FSCRYPT_H
14 #define _LINUX_FSCRYPT_H
15 
16 #include <linux/fs.h>
17 #include <linux/mm.h>
18 #include <linux/slab.h>
19 #include <uapi/linux/fscrypt.h>
20 
21 /*
22  * The lengths of all file contents blocks must be divisible by this value.
23  * This is needed to ensure that all contents encryption modes will work, as
24  * some of the supported modes don't support arbitrarily byte-aligned messages.
25  *
26  * Since the needed alignment is 16 bytes, most filesystems will meet this
27  * requirement naturally, as typical block sizes are powers of 2.  However, if a
28  * filesystem can generate arbitrarily byte-aligned block lengths (e.g., via
29  * compression), then it will need to pad to this alignment before encryption.
30  */
31 #define FSCRYPT_CONTENTS_ALIGNMENT 16
32 
33 union fscrypt_policy;
34 struct fscrypt_info;
35 struct fs_parameter;
36 struct seq_file;
37 
38 struct fscrypt_str {
39 	unsigned char *name;
40 	u32 len;
41 };
42 
43 struct fscrypt_name {
44 	const struct qstr *usr_fname;
45 	struct fscrypt_str disk_name;
46 	u32 hash;
47 	u32 minor_hash;
48 	struct fscrypt_str crypto_buf;
49 	bool is_nokey_name;
50 };
51 
52 #define FSTR_INIT(n, l)		{ .name = n, .len = l }
53 #define FSTR_TO_QSTR(f)		QSTR_INIT((f)->name, (f)->len)
54 #define fname_name(p)		((p)->disk_name.name)
55 #define fname_len(p)		((p)->disk_name.len)
56 
57 /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
58 #define FSCRYPT_SET_CONTEXT_MAX_SIZE	40
59 
60 #ifdef CONFIG_FS_ENCRYPTION
61 
62 /*
63  * If set, the fscrypt bounce page pool won't be allocated (unless another
64  * filesystem needs it).  Set this if the filesystem always uses its own bounce
65  * pages for writes and therefore won't need the fscrypt bounce page pool.
66  */
67 #define FS_CFLG_OWN_PAGES (1U << 1)
68 
69 /* Crypto operations for filesystems */
70 struct fscrypt_operations {
71 
72 	/* Set of optional flags; see above for allowed flags */
73 	unsigned int flags;
74 
75 	/*
76 	 * If set, this is a filesystem-specific key description prefix that
77 	 * will be accepted for "logon" keys for v1 fscrypt policies, in
78 	 * addition to the generic prefix "fscrypt:".  This functionality is
79 	 * deprecated, so new filesystems shouldn't set this field.
80 	 */
81 	const char *key_prefix;
82 
83 	/*
84 	 * Get the fscrypt context of the given inode.
85 	 *
86 	 * @inode: the inode whose context to get
87 	 * @ctx: the buffer into which to get the context
88 	 * @len: length of the @ctx buffer in bytes
89 	 *
90 	 * Return: On success, returns the length of the context in bytes; this
91 	 *	   may be less than @len.  On failure, returns -ENODATA if the
92 	 *	   inode doesn't have a context, -ERANGE if the context is
93 	 *	   longer than @len, or another -errno code.
94 	 */
95 	int (*get_context)(struct inode *inode, void *ctx, size_t len);
96 
97 	/*
98 	 * Set an fscrypt context on the given inode.
99 	 *
100 	 * @inode: the inode whose context to set.  The inode won't already have
101 	 *	   an fscrypt context.
102 	 * @ctx: the context to set
103 	 * @len: length of @ctx in bytes (at most FSCRYPT_SET_CONTEXT_MAX_SIZE)
104 	 * @fs_data: If called from fscrypt_set_context(), this will be the
105 	 *	     value the filesystem passed to fscrypt_set_context().
106 	 *	     Otherwise (i.e. when called from
107 	 *	     FS_IOC_SET_ENCRYPTION_POLICY) this will be NULL.
108 	 *
109 	 * i_rwsem will be held for write.
110 	 *
111 	 * Return: 0 on success, -errno on failure.
112 	 */
113 	int (*set_context)(struct inode *inode, const void *ctx, size_t len,
114 			   void *fs_data);
115 
116 	/*
117 	 * Get the dummy fscrypt policy in use on the filesystem (if any).
118 	 *
119 	 * Filesystems only need to implement this function if they support the
120 	 * test_dummy_encryption mount option.
121 	 *
122 	 * Return: A pointer to the dummy fscrypt policy, if the filesystem is
123 	 *	   mounted with test_dummy_encryption; otherwise NULL.
124 	 */
125 	const union fscrypt_policy *(*get_dummy_policy)(struct super_block *sb);
126 
127 	/*
128 	 * Check whether a directory is empty.  i_rwsem will be held for write.
129 	 */
130 	bool (*empty_dir)(struct inode *inode);
131 
132 	/*
133 	 * Check whether the filesystem's inode numbers and UUID are stable,
134 	 * meaning that they will never be changed even by offline operations
135 	 * such as filesystem shrinking and therefore can be used in the
136 	 * encryption without the possibility of files becoming unreadable.
137 	 *
138 	 * Filesystems only need to implement this function if they want to
139 	 * support the FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64} flags.  These
140 	 * flags are designed to work around the limitations of UFS and eMMC
141 	 * inline crypto hardware, and they shouldn't be used in scenarios where
142 	 * such hardware isn't being used.
143 	 *
144 	 * Leaving this NULL is equivalent to always returning false.
145 	 */
146 	bool (*has_stable_inodes)(struct super_block *sb);
147 
148 	/*
149 	 * Get the number of bits that the filesystem uses to represent inode
150 	 * numbers and file logical block numbers.
151 	 *
152 	 * By default, both of these are assumed to be 64-bit.  This function
153 	 * can be implemented to declare that either or both of these numbers is
154 	 * shorter, which may allow the use of the
155 	 * FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64} flags and/or the use of
156 	 * inline crypto hardware whose maximum DUN length is less than 64 bits
157 	 * (e.g., eMMC v5.2 spec compliant hardware).  This function only needs
158 	 * to be implemented if support for one of these features is needed.
159 	 */
160 	void (*get_ino_and_lblk_bits)(struct super_block *sb,
161 				      int *ino_bits_ret, int *lblk_bits_ret);
162 
163 	/*
164 	 * Return an array of pointers to the block devices to which the
165 	 * filesystem may write encrypted file contents, NULL if the filesystem
166 	 * only has a single such block device, or an ERR_PTR() on error.
167 	 *
168 	 * On successful non-NULL return, *num_devs is set to the number of
169 	 * devices in the returned array.  The caller must free the returned
170 	 * array using kfree().
171 	 *
172 	 * If the filesystem can use multiple block devices (other than block
173 	 * devices that aren't used for encrypted file contents, such as
174 	 * external journal devices), and wants to support inline encryption,
175 	 * then it must implement this function.  Otherwise it's not needed.
176 	 */
177 	struct block_device **(*get_devices)(struct super_block *sb,
178 					     unsigned int *num_devs);
179 };
180 
fscrypt_get_info(const struct inode * inode)181 static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
182 {
183 	/*
184 	 * Pairs with the cmpxchg_release() in fscrypt_setup_encryption_info().
185 	 * I.e., another task may publish ->i_crypt_info concurrently, executing
186 	 * a RELEASE barrier.  We need to use smp_load_acquire() here to safely
187 	 * ACQUIRE the memory the other task published.
188 	 */
189 	return smp_load_acquire(&inode->i_crypt_info);
190 }
191 
192 /**
193  * fscrypt_needs_contents_encryption() - check whether an inode needs
194  *					 contents encryption
195  * @inode: the inode to check
196  *
197  * Return: %true iff the inode is an encrypted regular file and the kernel was
198  * built with fscrypt support.
199  *
200  * If you need to know whether the encrypt bit is set even when the kernel was
201  * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
202  */
fscrypt_needs_contents_encryption(const struct inode * inode)203 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
204 {
205 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
206 }
207 
208 /*
209  * When d_splice_alias() moves a directory's no-key alias to its plaintext alias
210  * as a result of the encryption key being added, DCACHE_NOKEY_NAME must be
211  * cleared.  Note that we don't have to support arbitrary moves of this flag
212  * because fscrypt doesn't allow no-key names to be the source or target of a
213  * rename().
214  */
fscrypt_handle_d_move(struct dentry * dentry)215 static inline void fscrypt_handle_d_move(struct dentry *dentry)
216 {
217 	dentry->d_flags &= ~DCACHE_NOKEY_NAME;
218 }
219 
220 /**
221  * fscrypt_is_nokey_name() - test whether a dentry is a no-key name
222  * @dentry: the dentry to check
223  *
224  * This returns true if the dentry is a no-key dentry.  A no-key dentry is a
225  * dentry that was created in an encrypted directory that hasn't had its
226  * encryption key added yet.  Such dentries may be either positive or negative.
227  *
228  * When a filesystem is asked to create a new filename in an encrypted directory
229  * and the new filename's dentry is a no-key dentry, it must fail the operation
230  * with ENOKEY.  This includes ->create(), ->mkdir(), ->mknod(), ->symlink(),
231  * ->rename(), and ->link().  (However, ->rename() and ->link() are already
232  * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().)
233  *
234  * This is necessary because creating a filename requires the directory's
235  * encryption key, but just checking for the key on the directory inode during
236  * the final filesystem operation doesn't guarantee that the key was available
237  * during the preceding dentry lookup.  And the key must have already been
238  * available during the dentry lookup in order for it to have been checked
239  * whether the filename already exists in the directory and for the new file's
240  * dentry not to be invalidated due to it incorrectly having the no-key flag.
241  *
242  * Return: %true if the dentry is a no-key name
243  */
fscrypt_is_nokey_name(const struct dentry * dentry)244 static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
245 {
246 	return dentry->d_flags & DCACHE_NOKEY_NAME;
247 }
248 
249 /* crypto.c */
250 void fscrypt_enqueue_decrypt_work(struct work_struct *);
251 
252 struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
253 					      unsigned int len,
254 					      unsigned int offs,
255 					      gfp_t gfp_flags);
256 int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
257 				  unsigned int len, unsigned int offs,
258 				  u64 lblk_num, gfp_t gfp_flags);
259 
260 int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
261 				     unsigned int offs);
262 int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
263 				  unsigned int len, unsigned int offs,
264 				  u64 lblk_num);
265 
fscrypt_is_bounce_page(struct page * page)266 static inline bool fscrypt_is_bounce_page(struct page *page)
267 {
268 	return page->mapping == NULL;
269 }
270 
fscrypt_pagecache_page(struct page * bounce_page)271 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
272 {
273 	return (struct page *)page_private(bounce_page);
274 }
275 
276 void fscrypt_free_bounce_page(struct page *bounce_page);
277 
278 /* policy.c */
279 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
280 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
281 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
282 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
283 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
284 int fscrypt_context_for_new_inode(void *ctx, struct inode *inode);
285 int fscrypt_set_context(struct inode *inode, void *fs_data);
286 
287 struct fscrypt_dummy_policy {
288 	const union fscrypt_policy *policy;
289 };
290 
291 int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
292 				    struct fscrypt_dummy_policy *dummy_policy);
293 bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
294 				  const struct fscrypt_dummy_policy *p2);
295 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
296 					struct super_block *sb);
297 static inline bool
fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy * dummy_policy)298 fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
299 {
300 	return dummy_policy->policy != NULL;
301 }
302 static inline void
fscrypt_free_dummy_policy(struct fscrypt_dummy_policy * dummy_policy)303 fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
304 {
305 	kfree(dummy_policy->policy);
306 	dummy_policy->policy = NULL;
307 }
308 
309 /* keyring.c */
310 void fscrypt_destroy_keyring(struct super_block *sb);
311 int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
312 int fscrypt_add_test_dummy_key(struct super_block *sb,
313 			       const struct fscrypt_dummy_policy *dummy_policy);
314 int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
315 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
316 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
317 
318 /* keysetup.c */
319 int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
320 			      bool *encrypt_ret);
321 void fscrypt_put_encryption_info(struct inode *inode);
322 void fscrypt_free_inode(struct inode *inode);
323 int fscrypt_drop_inode(struct inode *inode);
324 
325 /* fname.c */
326 int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
327 			  u8 *out, unsigned int olen);
328 bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
329 				  u32 max_len, u32 *encrypted_len_ret);
330 int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
331 			   int lookup, struct fscrypt_name *fname);
332 
fscrypt_free_filename(struct fscrypt_name * fname)333 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
334 {
335 	kfree(fname->crypto_buf.name);
336 }
337 
338 int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
339 			       struct fscrypt_str *crypto_str);
340 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
341 int fscrypt_fname_disk_to_usr(const struct inode *inode,
342 			      u32 hash, u32 minor_hash,
343 			      const struct fscrypt_str *iname,
344 			      struct fscrypt_str *oname);
345 bool fscrypt_match_name(const struct fscrypt_name *fname,
346 			const u8 *de_name, u32 de_name_len);
347 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
348 int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags);
349 
350 /* bio.c */
351 bool fscrypt_decrypt_bio(struct bio *bio);
352 int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
353 			  sector_t pblk, unsigned int len);
354 
355 /* hooks.c */
356 int fscrypt_file_open(struct inode *inode, struct file *filp);
357 int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
358 			   struct dentry *dentry);
359 int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
360 			     struct inode *new_dir, struct dentry *new_dentry,
361 			     unsigned int flags);
362 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
363 			     struct fscrypt_name *fname);
364 int __fscrypt_prepare_readdir(struct inode *dir);
365 int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr);
366 int fscrypt_prepare_setflags(struct inode *inode,
367 			     unsigned int oldflags, unsigned int flags);
368 int fscrypt_prepare_symlink(struct inode *dir, const char *target,
369 			    unsigned int len, unsigned int max_len,
370 			    struct fscrypt_str *disk_link);
371 int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
372 			      unsigned int len, struct fscrypt_str *disk_link);
373 const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
374 				unsigned int max_size,
375 				struct delayed_call *done);
376 int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat);
fscrypt_set_ops(struct super_block * sb,const struct fscrypt_operations * s_cop)377 static inline void fscrypt_set_ops(struct super_block *sb,
378 				   const struct fscrypt_operations *s_cop)
379 {
380 	sb->s_cop = s_cop;
381 }
382 #else  /* !CONFIG_FS_ENCRYPTION */
383 
fscrypt_get_info(const struct inode * inode)384 static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
385 {
386 	return NULL;
387 }
388 
fscrypt_needs_contents_encryption(const struct inode * inode)389 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
390 {
391 	return false;
392 }
393 
fscrypt_handle_d_move(struct dentry * dentry)394 static inline void fscrypt_handle_d_move(struct dentry *dentry)
395 {
396 }
397 
fscrypt_is_nokey_name(const struct dentry * dentry)398 static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
399 {
400 	return false;
401 }
402 
403 /* crypto.c */
fscrypt_enqueue_decrypt_work(struct work_struct * work)404 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
405 {
406 }
407 
fscrypt_encrypt_pagecache_blocks(struct page * page,unsigned int len,unsigned int offs,gfp_t gfp_flags)408 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
409 							    unsigned int len,
410 							    unsigned int offs,
411 							    gfp_t gfp_flags)
412 {
413 	return ERR_PTR(-EOPNOTSUPP);
414 }
415 
fscrypt_encrypt_block_inplace(const struct inode * inode,struct page * page,unsigned int len,unsigned int offs,u64 lblk_num,gfp_t gfp_flags)416 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
417 						struct page *page,
418 						unsigned int len,
419 						unsigned int offs, u64 lblk_num,
420 						gfp_t gfp_flags)
421 {
422 	return -EOPNOTSUPP;
423 }
424 
fscrypt_decrypt_pagecache_blocks(struct page * page,unsigned int len,unsigned int offs)425 static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
426 						   unsigned int len,
427 						   unsigned int offs)
428 {
429 	return -EOPNOTSUPP;
430 }
431 
fscrypt_decrypt_block_inplace(const struct inode * inode,struct page * page,unsigned int len,unsigned int offs,u64 lblk_num)432 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
433 						struct page *page,
434 						unsigned int len,
435 						unsigned int offs, u64 lblk_num)
436 {
437 	return -EOPNOTSUPP;
438 }
439 
fscrypt_is_bounce_page(struct page * page)440 static inline bool fscrypt_is_bounce_page(struct page *page)
441 {
442 	return false;
443 }
444 
fscrypt_pagecache_page(struct page * bounce_page)445 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
446 {
447 	WARN_ON_ONCE(1);
448 	return ERR_PTR(-EINVAL);
449 }
450 
fscrypt_free_bounce_page(struct page * bounce_page)451 static inline void fscrypt_free_bounce_page(struct page *bounce_page)
452 {
453 }
454 
455 /* policy.c */
fscrypt_ioctl_set_policy(struct file * filp,const void __user * arg)456 static inline int fscrypt_ioctl_set_policy(struct file *filp,
457 					   const void __user *arg)
458 {
459 	return -EOPNOTSUPP;
460 }
461 
fscrypt_ioctl_get_policy(struct file * filp,void __user * arg)462 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
463 {
464 	return -EOPNOTSUPP;
465 }
466 
fscrypt_ioctl_get_policy_ex(struct file * filp,void __user * arg)467 static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
468 					      void __user *arg)
469 {
470 	return -EOPNOTSUPP;
471 }
472 
fscrypt_ioctl_get_nonce(struct file * filp,void __user * arg)473 static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
474 {
475 	return -EOPNOTSUPP;
476 }
477 
fscrypt_has_permitted_context(struct inode * parent,struct inode * child)478 static inline int fscrypt_has_permitted_context(struct inode *parent,
479 						struct inode *child)
480 {
481 	return 0;
482 }
483 
fscrypt_set_context(struct inode * inode,void * fs_data)484 static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
485 {
486 	return -EOPNOTSUPP;
487 }
488 
489 struct fscrypt_dummy_policy {
490 };
491 
492 static inline int
fscrypt_parse_test_dummy_encryption(const struct fs_parameter * param,struct fscrypt_dummy_policy * dummy_policy)493 fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
494 				    struct fscrypt_dummy_policy *dummy_policy)
495 {
496 	return -EINVAL;
497 }
498 
499 static inline bool
fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy * p1,const struct fscrypt_dummy_policy * p2)500 fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
501 			     const struct fscrypt_dummy_policy *p2)
502 {
503 	return true;
504 }
505 
fscrypt_show_test_dummy_encryption(struct seq_file * seq,char sep,struct super_block * sb)506 static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
507 						      char sep,
508 						      struct super_block *sb)
509 {
510 }
511 
512 static inline bool
fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy * dummy_policy)513 fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
514 {
515 	return false;
516 }
517 
518 static inline void
fscrypt_free_dummy_policy(struct fscrypt_dummy_policy * dummy_policy)519 fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
520 {
521 }
522 
523 /* keyring.c */
fscrypt_destroy_keyring(struct super_block * sb)524 static inline void fscrypt_destroy_keyring(struct super_block *sb)
525 {
526 }
527 
fscrypt_ioctl_add_key(struct file * filp,void __user * arg)528 static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
529 {
530 	return -EOPNOTSUPP;
531 }
532 
533 static inline int
fscrypt_add_test_dummy_key(struct super_block * sb,const struct fscrypt_dummy_policy * dummy_policy)534 fscrypt_add_test_dummy_key(struct super_block *sb,
535 			   const struct fscrypt_dummy_policy *dummy_policy)
536 {
537 	return 0;
538 }
539 
fscrypt_ioctl_remove_key(struct file * filp,void __user * arg)540 static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
541 {
542 	return -EOPNOTSUPP;
543 }
544 
fscrypt_ioctl_remove_key_all_users(struct file * filp,void __user * arg)545 static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
546 						     void __user *arg)
547 {
548 	return -EOPNOTSUPP;
549 }
550 
fscrypt_ioctl_get_key_status(struct file * filp,void __user * arg)551 static inline int fscrypt_ioctl_get_key_status(struct file *filp,
552 					       void __user *arg)
553 {
554 	return -EOPNOTSUPP;
555 }
556 
557 /* keysetup.c */
558 
fscrypt_prepare_new_inode(struct inode * dir,struct inode * inode,bool * encrypt_ret)559 static inline int fscrypt_prepare_new_inode(struct inode *dir,
560 					    struct inode *inode,
561 					    bool *encrypt_ret)
562 {
563 	if (IS_ENCRYPTED(dir))
564 		return -EOPNOTSUPP;
565 	return 0;
566 }
567 
fscrypt_put_encryption_info(struct inode * inode)568 static inline void fscrypt_put_encryption_info(struct inode *inode)
569 {
570 	return;
571 }
572 
fscrypt_free_inode(struct inode * inode)573 static inline void fscrypt_free_inode(struct inode *inode)
574 {
575 }
576 
fscrypt_drop_inode(struct inode * inode)577 static inline int fscrypt_drop_inode(struct inode *inode)
578 {
579 	return 0;
580 }
581 
582  /* fname.c */
fscrypt_setup_filename(struct inode * dir,const struct qstr * iname,int lookup,struct fscrypt_name * fname)583 static inline int fscrypt_setup_filename(struct inode *dir,
584 					 const struct qstr *iname,
585 					 int lookup, struct fscrypt_name *fname)
586 {
587 	if (IS_ENCRYPTED(dir))
588 		return -EOPNOTSUPP;
589 
590 	memset(fname, 0, sizeof(*fname));
591 	fname->usr_fname = iname;
592 	fname->disk_name.name = (unsigned char *)iname->name;
593 	fname->disk_name.len = iname->len;
594 	return 0;
595 }
596 
fscrypt_free_filename(struct fscrypt_name * fname)597 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
598 {
599 	return;
600 }
601 
fscrypt_fname_alloc_buffer(u32 max_encrypted_len,struct fscrypt_str * crypto_str)602 static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
603 					     struct fscrypt_str *crypto_str)
604 {
605 	return -EOPNOTSUPP;
606 }
607 
fscrypt_fname_free_buffer(struct fscrypt_str * crypto_str)608 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
609 {
610 	return;
611 }
612 
fscrypt_fname_disk_to_usr(const struct inode * inode,u32 hash,u32 minor_hash,const struct fscrypt_str * iname,struct fscrypt_str * oname)613 static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
614 					    u32 hash, u32 minor_hash,
615 					    const struct fscrypt_str *iname,
616 					    struct fscrypt_str *oname)
617 {
618 	return -EOPNOTSUPP;
619 }
620 
fscrypt_match_name(const struct fscrypt_name * fname,const u8 * de_name,u32 de_name_len)621 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
622 				      const u8 *de_name, u32 de_name_len)
623 {
624 	/* Encryption support disabled; use standard comparison */
625 	if (de_name_len != fname->disk_name.len)
626 		return false;
627 	return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
628 }
629 
fscrypt_fname_siphash(const struct inode * dir,const struct qstr * name)630 static inline u64 fscrypt_fname_siphash(const struct inode *dir,
631 					const struct qstr *name)
632 {
633 	WARN_ON_ONCE(1);
634 	return 0;
635 }
636 
fscrypt_d_revalidate(struct dentry * dentry,unsigned int flags)637 static inline int fscrypt_d_revalidate(struct dentry *dentry,
638 				       unsigned int flags)
639 {
640 	return 1;
641 }
642 
643 /* bio.c */
fscrypt_decrypt_bio(struct bio * bio)644 static inline bool fscrypt_decrypt_bio(struct bio *bio)
645 {
646 	return true;
647 }
648 
fscrypt_zeroout_range(const struct inode * inode,pgoff_t lblk,sector_t pblk,unsigned int len)649 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
650 					sector_t pblk, unsigned int len)
651 {
652 	return -EOPNOTSUPP;
653 }
654 
655 /* hooks.c */
656 
fscrypt_file_open(struct inode * inode,struct file * filp)657 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
658 {
659 	if (IS_ENCRYPTED(inode))
660 		return -EOPNOTSUPP;
661 	return 0;
662 }
663 
__fscrypt_prepare_link(struct inode * inode,struct inode * dir,struct dentry * dentry)664 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
665 					 struct dentry *dentry)
666 {
667 	return -EOPNOTSUPP;
668 }
669 
__fscrypt_prepare_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)670 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
671 					   struct dentry *old_dentry,
672 					   struct inode *new_dir,
673 					   struct dentry *new_dentry,
674 					   unsigned int flags)
675 {
676 	return -EOPNOTSUPP;
677 }
678 
__fscrypt_prepare_lookup(struct inode * dir,struct dentry * dentry,struct fscrypt_name * fname)679 static inline int __fscrypt_prepare_lookup(struct inode *dir,
680 					   struct dentry *dentry,
681 					   struct fscrypt_name *fname)
682 {
683 	return -EOPNOTSUPP;
684 }
685 
__fscrypt_prepare_readdir(struct inode * dir)686 static inline int __fscrypt_prepare_readdir(struct inode *dir)
687 {
688 	return -EOPNOTSUPP;
689 }
690 
__fscrypt_prepare_setattr(struct dentry * dentry,struct iattr * attr)691 static inline int __fscrypt_prepare_setattr(struct dentry *dentry,
692 					    struct iattr *attr)
693 {
694 	return -EOPNOTSUPP;
695 }
696 
fscrypt_prepare_setflags(struct inode * inode,unsigned int oldflags,unsigned int flags)697 static inline int fscrypt_prepare_setflags(struct inode *inode,
698 					   unsigned int oldflags,
699 					   unsigned int flags)
700 {
701 	return 0;
702 }
703 
fscrypt_prepare_symlink(struct inode * dir,const char * target,unsigned int len,unsigned int max_len,struct fscrypt_str * disk_link)704 static inline int fscrypt_prepare_symlink(struct inode *dir,
705 					  const char *target,
706 					  unsigned int len,
707 					  unsigned int max_len,
708 					  struct fscrypt_str *disk_link)
709 {
710 	if (IS_ENCRYPTED(dir))
711 		return -EOPNOTSUPP;
712 	disk_link->name = (unsigned char *)target;
713 	disk_link->len = len + 1;
714 	if (disk_link->len > max_len)
715 		return -ENAMETOOLONG;
716 	return 0;
717 }
718 
__fscrypt_encrypt_symlink(struct inode * inode,const char * target,unsigned int len,struct fscrypt_str * disk_link)719 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
720 					    const char *target,
721 					    unsigned int len,
722 					    struct fscrypt_str *disk_link)
723 {
724 	return -EOPNOTSUPP;
725 }
726 
fscrypt_get_symlink(struct inode * inode,const void * caddr,unsigned int max_size,struct delayed_call * done)727 static inline const char *fscrypt_get_symlink(struct inode *inode,
728 					      const void *caddr,
729 					      unsigned int max_size,
730 					      struct delayed_call *done)
731 {
732 	return ERR_PTR(-EOPNOTSUPP);
733 }
734 
fscrypt_symlink_getattr(const struct path * path,struct kstat * stat)735 static inline int fscrypt_symlink_getattr(const struct path *path,
736 					  struct kstat *stat)
737 {
738 	return -EOPNOTSUPP;
739 }
740 
fscrypt_set_ops(struct super_block * sb,const struct fscrypt_operations * s_cop)741 static inline void fscrypt_set_ops(struct super_block *sb,
742 				   const struct fscrypt_operations *s_cop)
743 {
744 }
745 
746 #endif	/* !CONFIG_FS_ENCRYPTION */
747 
748 /* inline_crypt.c */
749 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
750 
751 bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
752 
753 void fscrypt_set_bio_crypt_ctx(struct bio *bio,
754 			       const struct inode *inode, u64 first_lblk,
755 			       gfp_t gfp_mask);
756 
757 void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
758 				  const struct buffer_head *first_bh,
759 				  gfp_t gfp_mask);
760 
761 bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
762 			   u64 next_lblk);
763 
764 bool fscrypt_mergeable_bio_bh(struct bio *bio,
765 			      const struct buffer_head *next_bh);
766 
767 bool fscrypt_dio_supported(struct inode *inode);
768 
769 u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks);
770 
771 #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
772 
__fscrypt_inode_uses_inline_crypto(const struct inode * inode)773 static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
774 {
775 	return false;
776 }
777 
fscrypt_set_bio_crypt_ctx(struct bio * bio,const struct inode * inode,u64 first_lblk,gfp_t gfp_mask)778 static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
779 					     const struct inode *inode,
780 					     u64 first_lblk, gfp_t gfp_mask) { }
781 
fscrypt_set_bio_crypt_ctx_bh(struct bio * bio,const struct buffer_head * first_bh,gfp_t gfp_mask)782 static inline void fscrypt_set_bio_crypt_ctx_bh(
783 					 struct bio *bio,
784 					 const struct buffer_head *first_bh,
785 					 gfp_t gfp_mask) { }
786 
fscrypt_mergeable_bio(struct bio * bio,const struct inode * inode,u64 next_lblk)787 static inline bool fscrypt_mergeable_bio(struct bio *bio,
788 					 const struct inode *inode,
789 					 u64 next_lblk)
790 {
791 	return true;
792 }
793 
fscrypt_mergeable_bio_bh(struct bio * bio,const struct buffer_head * next_bh)794 static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
795 					    const struct buffer_head *next_bh)
796 {
797 	return true;
798 }
799 
fscrypt_dio_supported(struct inode * inode)800 static inline bool fscrypt_dio_supported(struct inode *inode)
801 {
802 	return !fscrypt_needs_contents_encryption(inode);
803 }
804 
fscrypt_limit_io_blocks(const struct inode * inode,u64 lblk,u64 nr_blocks)805 static inline u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk,
806 					  u64 nr_blocks)
807 {
808 	return nr_blocks;
809 }
810 #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
811 
812 /**
813  * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
814  *					encryption
815  * @inode: an inode. If encrypted, its key must be set up.
816  *
817  * Return: true if the inode requires file contents encryption and if the
818  *	   encryption should be done in the block layer via blk-crypto rather
819  *	   than in the filesystem layer.
820  */
fscrypt_inode_uses_inline_crypto(const struct inode * inode)821 static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
822 {
823 	return fscrypt_needs_contents_encryption(inode) &&
824 	       __fscrypt_inode_uses_inline_crypto(inode);
825 }
826 
827 /**
828  * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
829  *					  encryption
830  * @inode: an inode. If encrypted, its key must be set up.
831  *
832  * Return: true if the inode requires file contents encryption and if the
833  *	   encryption should be done in the filesystem layer rather than in the
834  *	   block layer via blk-crypto.
835  */
fscrypt_inode_uses_fs_layer_crypto(const struct inode * inode)836 static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
837 {
838 	return fscrypt_needs_contents_encryption(inode) &&
839 	       !__fscrypt_inode_uses_inline_crypto(inode);
840 }
841 
842 /**
843  * fscrypt_has_encryption_key() - check whether an inode has had its key set up
844  * @inode: the inode to check
845  *
846  * Return: %true if the inode has had its encryption key set up, else %false.
847  *
848  * Usually this should be preceded by fscrypt_get_encryption_info() to try to
849  * set up the key first.
850  */
fscrypt_has_encryption_key(const struct inode * inode)851 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
852 {
853 	return fscrypt_get_info(inode) != NULL;
854 }
855 
856 /**
857  * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
858  *			    directory
859  * @old_dentry: an existing dentry for the inode being linked
860  * @dir: the target directory
861  * @dentry: negative dentry for the target filename
862  *
863  * A new link can only be added to an encrypted directory if the directory's
864  * encryption key is available --- since otherwise we'd have no way to encrypt
865  * the filename.
866  *
867  * We also verify that the link will not violate the constraint that all files
868  * in an encrypted directory tree use the same encryption policy.
869  *
870  * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
871  * -EXDEV if the link would result in an inconsistent encryption policy, or
872  * another -errno code.
873  */
fscrypt_prepare_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)874 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
875 				       struct inode *dir,
876 				       struct dentry *dentry)
877 {
878 	if (IS_ENCRYPTED(dir))
879 		return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
880 	return 0;
881 }
882 
883 /**
884  * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
885  *			      directories
886  * @old_dir: source directory
887  * @old_dentry: dentry for source file
888  * @new_dir: target directory
889  * @new_dentry: dentry for target location (may be negative unless exchanging)
890  * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
891  *
892  * Prepare for ->rename() where the source and/or target directories may be
893  * encrypted.  A new link can only be added to an encrypted directory if the
894  * directory's encryption key is available --- since otherwise we'd have no way
895  * to encrypt the filename.  A rename to an existing name, on the other hand,
896  * *is* cryptographically possible without the key.  However, we take the more
897  * conservative approach and just forbid all no-key renames.
898  *
899  * We also verify that the rename will not violate the constraint that all files
900  * in an encrypted directory tree use the same encryption policy.
901  *
902  * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
903  * rename would cause inconsistent encryption policies, or another -errno code.
904  */
fscrypt_prepare_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)905 static inline int fscrypt_prepare_rename(struct inode *old_dir,
906 					 struct dentry *old_dentry,
907 					 struct inode *new_dir,
908 					 struct dentry *new_dentry,
909 					 unsigned int flags)
910 {
911 	if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
912 		return __fscrypt_prepare_rename(old_dir, old_dentry,
913 						new_dir, new_dentry, flags);
914 	return 0;
915 }
916 
917 /**
918  * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
919  *			      directory
920  * @dir: directory being searched
921  * @dentry: filename being looked up
922  * @fname: (output) the name to use to search the on-disk directory
923  *
924  * Prepare for ->lookup() in a directory which may be encrypted by determining
925  * the name that will actually be used to search the directory on-disk.  If the
926  * directory's encryption policy is supported by this kernel and its encryption
927  * key is available, then the lookup is assumed to be by plaintext name;
928  * otherwise, it is assumed to be by no-key name.
929  *
930  * This will set DCACHE_NOKEY_NAME on the dentry if the lookup is by no-key
931  * name.  In this case the filesystem must assign the dentry a dentry_operations
932  * which contains fscrypt_d_revalidate (or contains a d_revalidate method that
933  * calls fscrypt_d_revalidate), so that the dentry will be invalidated if the
934  * directory's encryption key is later added.
935  *
936  * Return: 0 on success; -ENOENT if the directory's key is unavailable but the
937  * filename isn't a valid no-key name, so a negative dentry should be created;
938  * or another -errno code.
939  */
fscrypt_prepare_lookup(struct inode * dir,struct dentry * dentry,struct fscrypt_name * fname)940 static inline int fscrypt_prepare_lookup(struct inode *dir,
941 					 struct dentry *dentry,
942 					 struct fscrypt_name *fname)
943 {
944 	if (IS_ENCRYPTED(dir))
945 		return __fscrypt_prepare_lookup(dir, dentry, fname);
946 
947 	memset(fname, 0, sizeof(*fname));
948 	fname->usr_fname = &dentry->d_name;
949 	fname->disk_name.name = (unsigned char *)dentry->d_name.name;
950 	fname->disk_name.len = dentry->d_name.len;
951 	return 0;
952 }
953 
954 /**
955  * fscrypt_prepare_readdir() - prepare to read a possibly-encrypted directory
956  * @dir: the directory inode
957  *
958  * If the directory is encrypted and it doesn't already have its encryption key
959  * set up, try to set it up so that the filenames will be listed in plaintext
960  * form rather than in no-key form.
961  *
962  * Return: 0 on success; -errno on error.  Note that the encryption key being
963  *	   unavailable is not considered an error.  It is also not an error if
964  *	   the encryption policy is unsupported by this kernel; that is treated
965  *	   like the key being unavailable, so that files can still be deleted.
966  */
fscrypt_prepare_readdir(struct inode * dir)967 static inline int fscrypt_prepare_readdir(struct inode *dir)
968 {
969 	if (IS_ENCRYPTED(dir))
970 		return __fscrypt_prepare_readdir(dir);
971 	return 0;
972 }
973 
974 /**
975  * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
976  *			       attributes
977  * @dentry: dentry through which the inode is being changed
978  * @attr: attributes to change
979  *
980  * Prepare for ->setattr() on a possibly-encrypted inode.  On an encrypted file,
981  * most attribute changes are allowed even without the encryption key.  However,
982  * without the encryption key we do have to forbid truncates.  This is needed
983  * because the size being truncated to may not be a multiple of the filesystem
984  * block size, and in that case we'd have to decrypt the final block, zero the
985  * portion past i_size, and re-encrypt it.  (We *could* allow truncating to a
986  * filesystem block boundary, but it's simpler to just forbid all truncates ---
987  * and we already forbid all other contents modifications without the key.)
988  *
989  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
990  * if a problem occurred while setting up the encryption key.
991  */
fscrypt_prepare_setattr(struct dentry * dentry,struct iattr * attr)992 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
993 					  struct iattr *attr)
994 {
995 	if (IS_ENCRYPTED(d_inode(dentry)))
996 		return __fscrypt_prepare_setattr(dentry, attr);
997 	return 0;
998 }
999 
1000 /**
1001  * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
1002  * @inode: symlink inode
1003  * @target: plaintext symlink target
1004  * @len: length of @target excluding null terminator
1005  * @disk_link: (in/out) the on-disk symlink target being prepared
1006  *
1007  * If the symlink target needs to be encrypted, then this function encrypts it
1008  * into @disk_link->name.  fscrypt_prepare_symlink() must have been called
1009  * previously to compute @disk_link->len.  If the filesystem did not allocate a
1010  * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
1011  * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
1012  *
1013  * Return: 0 on success, -errno on failure
1014  */
fscrypt_encrypt_symlink(struct inode * inode,const char * target,unsigned int len,struct fscrypt_str * disk_link)1015 static inline int fscrypt_encrypt_symlink(struct inode *inode,
1016 					  const char *target,
1017 					  unsigned int len,
1018 					  struct fscrypt_str *disk_link)
1019 {
1020 	if (IS_ENCRYPTED(inode))
1021 		return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
1022 	return 0;
1023 }
1024 
1025 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
fscrypt_finalize_bounce_page(struct page ** pagep)1026 static inline void fscrypt_finalize_bounce_page(struct page **pagep)
1027 {
1028 	struct page *page = *pagep;
1029 
1030 	if (fscrypt_is_bounce_page(page)) {
1031 		*pagep = fscrypt_pagecache_page(page);
1032 		fscrypt_free_bounce_page(page);
1033 	}
1034 }
1035 
1036 #endif	/* _LINUX_FSCRYPT_H */
1037