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