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