• 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/parser.h>
19 #include <linux/slab.h>
20 #include <uapi/linux/fscrypt.h>
21 
22 #define FS_CRYPTO_BLOCK_SIZE		16
23 
24 union fscrypt_context;
25 struct fscrypt_info;
26 struct seq_file;
27 
28 struct fscrypt_str {
29 	unsigned char *name;
30 	u32 len;
31 };
32 
33 struct fscrypt_name {
34 	const struct qstr *usr_fname;
35 	struct fscrypt_str disk_name;
36 	u32 hash;
37 	u32 minor_hash;
38 	struct fscrypt_str crypto_buf;
39 	bool is_ciphertext_name;
40 };
41 
42 #define FSTR_INIT(n, l)		{ .name = n, .len = l }
43 #define FSTR_TO_QSTR(f)		QSTR_INIT((f)->name, (f)->len)
44 #define fname_name(p)		((p)->disk_name.name)
45 #define fname_len(p)		((p)->disk_name.len)
46 
47 /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
48 #define FSCRYPT_SET_CONTEXT_MAX_SIZE	40
49 
50 #ifdef CONFIG_FS_ENCRYPTION
51 /*
52  * fscrypt superblock flags
53  */
54 #define FS_CFLG_OWN_PAGES (1U << 1)
55 
56 #ifdef FSCRYPT_NEED_OPS
57 /*
58  * crypto operations for filesystems
59  */
60 struct fscrypt_operations {
61 	unsigned int flags;
62 	const char *key_prefix;
63 	int (*get_context)(struct inode *inode, void *ctx, size_t len);
64 	int (*set_context)(struct inode *inode, const void *ctx, size_t len,
65 			   void *fs_data);
66 	const union fscrypt_context *(*get_dummy_context)(
67 		struct super_block *sb);
68 	bool (*empty_dir)(struct inode *inode);
69 	unsigned int max_namelen;
70 	bool (*has_stable_inodes)(struct super_block *sb);
71 	void (*get_ino_and_lblk_bits)(struct super_block *sb,
72 				      int *ino_bits_ret, int *lblk_bits_ret);
73 	bool (*inline_crypt_enabled)(struct super_block *sb);
74 	int (*get_num_devices)(struct super_block *sb);
75 	void (*get_devices)(struct super_block *sb,
76 			    struct request_queue **devs);
77 };
78 #endif
79 
fscrypt_has_encryption_key(const struct inode * inode)80 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
81 {
82 	/* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
83 	return READ_ONCE(inode->i_crypt_info) != NULL;
84 }
85 
86 /**
87  * fscrypt_needs_contents_encryption() - check whether an inode needs
88  *					 contents encryption
89  * @inode: the inode to check
90  *
91  * Return: %true iff the inode is an encrypted regular file and the kernel was
92  * built with fscrypt support.
93  *
94  * If you need to know whether the encrypt bit is set even when the kernel was
95  * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
96  */
fscrypt_needs_contents_encryption(const struct inode * inode)97 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
98 {
99 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
100 }
101 
102 #ifdef FSCRYPT_NEED_OPS
103 static inline const union fscrypt_context *
fscrypt_get_dummy_context(struct super_block * sb)104 fscrypt_get_dummy_context(struct super_block *sb)
105 {
106 	if (!sb->s_cop->get_dummy_context)
107 		return NULL;
108 	return sb->s_cop->get_dummy_context(sb);
109 }
110 #endif
111 
112 /*
113  * When d_splice_alias() moves a directory's encrypted alias to its decrypted
114  * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
115  * must be cleared.  Note that we don't have to support arbitrary moves of this
116  * flag because fscrypt doesn't allow encrypted aliases to be the source or
117  * target of a rename().
118  */
fscrypt_handle_d_move(struct dentry * dentry)119 static inline void fscrypt_handle_d_move(struct dentry *dentry)
120 {
121 	dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
122 }
123 
124 /**
125  * fscrypt_is_nokey_name() - test whether a dentry is a no-key name
126  * @dentry: the dentry to check
127  *
128  * This returns true if the dentry is a no-key dentry.  A no-key dentry is a
129  * dentry that was created in an encrypted directory that hasn't had its
130  * encryption key added yet.  Such dentries may be either positive or negative.
131  *
132  * When a filesystem is asked to create a new filename in an encrypted directory
133  * and the new filename's dentry is a no-key dentry, it must fail the operation
134  * with ENOKEY.  This includes ->create(), ->mkdir(), ->mknod(), ->symlink(),
135  * ->rename(), and ->link().  (However, ->rename() and ->link() are already
136  * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().)
137  *
138  * This is necessary because creating a filename requires the directory's
139  * encryption key, but just checking for the key on the directory inode during
140  * the final filesystem operation doesn't guarantee that the key was available
141  * during the preceding dentry lookup.  And the key must have already been
142  * available during the dentry lookup in order for it to have been checked
143  * whether the filename already exists in the directory and for the new file's
144  * dentry not to be invalidated due to it incorrectly having the no-key flag.
145  *
146  * Return: %true if the dentry is a no-key name
147  */
fscrypt_is_nokey_name(const struct dentry * dentry)148 static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
149 {
150 	return dentry->d_flags & DCACHE_ENCRYPTED_NAME;
151 }
152 
153 /* crypto.c */
154 void fscrypt_enqueue_decrypt_work(struct work_struct *);
155 
156 struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
157 					      unsigned int len,
158 					      unsigned int offs,
159 					      gfp_t gfp_flags);
160 int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
161 				  unsigned int len, unsigned int offs,
162 				  u64 lblk_num, gfp_t gfp_flags);
163 
164 int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
165 				     unsigned int offs);
166 int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
167 				  unsigned int len, unsigned int offs,
168 				  u64 lblk_num);
169 
fscrypt_is_bounce_page(struct page * page)170 static inline bool fscrypt_is_bounce_page(struct page *page)
171 {
172 	return page->mapping == NULL;
173 }
174 
fscrypt_pagecache_page(struct page * bounce_page)175 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
176 {
177 	return (struct page *)page_private(bounce_page);
178 }
179 
180 void fscrypt_free_bounce_page(struct page *bounce_page);
181 int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags);
182 
183 /* policy.c */
184 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
185 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
186 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
187 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
188 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
189 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
190 			    void *fs_data, bool preload);
191 
192 struct fscrypt_dummy_context {
193 	const union fscrypt_context *ctx;
194 };
195 
196 int fscrypt_set_test_dummy_encryption(struct super_block *sb,
197 				      const substring_t *arg,
198 				      struct fscrypt_dummy_context *dummy_ctx);
199 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
200 					struct super_block *sb);
201 static inline void
fscrypt_free_dummy_context(struct fscrypt_dummy_context * dummy_ctx)202 fscrypt_free_dummy_context(struct fscrypt_dummy_context *dummy_ctx)
203 {
204 	kfree(dummy_ctx->ctx);
205 	dummy_ctx->ctx = NULL;
206 }
207 
208 /* keyring.c */
209 void fscrypt_sb_free(struct super_block *sb);
210 int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
211 int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
212 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
213 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
214 
215 /* keysetup.c */
216 int fscrypt_get_encryption_info(struct inode *inode);
217 void fscrypt_put_encryption_info(struct inode *inode);
218 void fscrypt_free_inode(struct inode *inode);
219 int fscrypt_drop_inode(struct inode *inode);
220 
221 /* fname.c */
222 int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
223 			   int lookup, struct fscrypt_name *fname);
224 
fscrypt_free_filename(struct fscrypt_name * fname)225 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
226 {
227 	kfree(fname->crypto_buf.name);
228 }
229 
230 int fscrypt_fname_alloc_buffer(const struct inode *inode, u32 max_encrypted_len,
231 			       struct fscrypt_str *crypto_str);
232 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
233 int fscrypt_fname_disk_to_usr(const struct inode *inode,
234 			      u32 hash, u32 minor_hash,
235 			      const struct fscrypt_str *iname,
236 			      struct fscrypt_str *oname);
237 bool fscrypt_match_name(const struct fscrypt_name *fname,
238 			const u8 *de_name, u32 de_name_len);
239 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
240 
241 /* bio.c */
242 void fscrypt_decrypt_bio(struct bio *bio);
243 int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
244 			  sector_t pblk, unsigned int len);
245 
246 /* hooks.c */
247 int fscrypt_file_open(struct inode *inode, struct file *filp);
248 int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
249 			   struct dentry *dentry);
250 int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
251 			     struct inode *new_dir, struct dentry *new_dentry,
252 			     unsigned int flags);
253 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
254 			     struct fscrypt_name *fname);
255 int fscrypt_prepare_setflags(struct inode *inode,
256 			     unsigned int oldflags, unsigned int flags);
257 int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
258 			      unsigned int max_len,
259 			      struct fscrypt_str *disk_link);
260 int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
261 			      unsigned int len, struct fscrypt_str *disk_link);
262 const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
263 				unsigned int max_size,
264 				struct delayed_call *done);
265 int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat);
fscrypt_set_ops(struct super_block * sb,const struct fscrypt_operations * s_cop)266 static inline void fscrypt_set_ops(struct super_block *sb,
267 				   const struct fscrypt_operations *s_cop)
268 {
269 	sb->s_cop = s_cop;
270 }
271 #else  /* !CONFIG_FS_ENCRYPTION */
272 
fscrypt_has_encryption_key(const struct inode * inode)273 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
274 {
275 	return false;
276 }
277 
fscrypt_needs_contents_encryption(const struct inode * inode)278 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
279 {
280 	return false;
281 }
282 
283 static inline const union fscrypt_context *
fscrypt_get_dummy_context(struct super_block * sb)284 fscrypt_get_dummy_context(struct super_block *sb)
285 {
286 	return NULL;
287 }
288 
fscrypt_handle_d_move(struct dentry * dentry)289 static inline void fscrypt_handle_d_move(struct dentry *dentry)
290 {
291 }
292 
fscrypt_is_nokey_name(const struct dentry * dentry)293 static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
294 {
295 	return false;
296 }
297 
298 /* crypto.c */
fscrypt_enqueue_decrypt_work(struct work_struct * work)299 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
300 {
301 }
302 
fscrypt_encrypt_pagecache_blocks(struct page * page,unsigned int len,unsigned int offs,gfp_t gfp_flags)303 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
304 							    unsigned int len,
305 							    unsigned int offs,
306 							    gfp_t gfp_flags)
307 {
308 	return ERR_PTR(-EOPNOTSUPP);
309 }
310 
fscrypt_encrypt_block_inplace(const struct inode * inode,struct page * page,unsigned int len,unsigned int offs,u64 lblk_num,gfp_t gfp_flags)311 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
312 						struct page *page,
313 						unsigned int len,
314 						unsigned int offs, u64 lblk_num,
315 						gfp_t gfp_flags)
316 {
317 	return -EOPNOTSUPP;
318 }
319 
fscrypt_decrypt_pagecache_blocks(struct page * page,unsigned int len,unsigned int offs)320 static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
321 						   unsigned int len,
322 						   unsigned int offs)
323 {
324 	return -EOPNOTSUPP;
325 }
326 
fscrypt_decrypt_block_inplace(const struct inode * inode,struct page * page,unsigned int len,unsigned int offs,u64 lblk_num)327 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
328 						struct page *page,
329 						unsigned int len,
330 						unsigned int offs, u64 lblk_num)
331 {
332 	return -EOPNOTSUPP;
333 }
334 
fscrypt_is_bounce_page(struct page * page)335 static inline bool fscrypt_is_bounce_page(struct page *page)
336 {
337 	return false;
338 }
339 
fscrypt_pagecache_page(struct page * bounce_page)340 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
341 {
342 	WARN_ON_ONCE(1);
343 	return ERR_PTR(-EINVAL);
344 }
345 
fscrypt_free_bounce_page(struct page * bounce_page)346 static inline void fscrypt_free_bounce_page(struct page *bounce_page)
347 {
348 }
349 
350 /* policy.c */
fscrypt_ioctl_set_policy(struct file * filp,const void __user * arg)351 static inline int fscrypt_ioctl_set_policy(struct file *filp,
352 					   const void __user *arg)
353 {
354 	return -EOPNOTSUPP;
355 }
356 
fscrypt_ioctl_get_policy(struct file * filp,void __user * arg)357 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
358 {
359 	return -EOPNOTSUPP;
360 }
361 
fscrypt_ioctl_get_policy_ex(struct file * filp,void __user * arg)362 static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
363 					      void __user *arg)
364 {
365 	return -EOPNOTSUPP;
366 }
367 
fscrypt_ioctl_get_nonce(struct file * filp,void __user * arg)368 static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
369 {
370 	return -EOPNOTSUPP;
371 }
372 
fscrypt_has_permitted_context(struct inode * parent,struct inode * child)373 static inline int fscrypt_has_permitted_context(struct inode *parent,
374 						struct inode *child)
375 {
376 	return 0;
377 }
378 
fscrypt_inherit_context(struct inode * parent,struct inode * child,void * fs_data,bool preload)379 static inline int fscrypt_inherit_context(struct inode *parent,
380 					  struct inode *child,
381 					  void *fs_data, bool preload)
382 {
383 	return -EOPNOTSUPP;
384 }
385 
386 struct fscrypt_dummy_context {
387 };
388 
fscrypt_show_test_dummy_encryption(struct seq_file * seq,char sep,struct super_block * sb)389 static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
390 						      char sep,
391 						      struct super_block *sb)
392 {
393 }
394 
395 static inline void
fscrypt_free_dummy_context(struct fscrypt_dummy_context * dummy_ctx)396 fscrypt_free_dummy_context(struct fscrypt_dummy_context *dummy_ctx)
397 {
398 }
399 
400 /* keyring.c */
fscrypt_sb_free(struct super_block * sb)401 static inline void fscrypt_sb_free(struct super_block *sb)
402 {
403 }
404 
fscrypt_ioctl_add_key(struct file * filp,void __user * arg)405 static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
406 {
407 	return -EOPNOTSUPP;
408 }
409 
fscrypt_ioctl_remove_key(struct file * filp,void __user * arg)410 static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
411 {
412 	return -EOPNOTSUPP;
413 }
414 
fscrypt_ioctl_remove_key_all_users(struct file * filp,void __user * arg)415 static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
416 						     void __user *arg)
417 {
418 	return -EOPNOTSUPP;
419 }
420 
fscrypt_ioctl_get_key_status(struct file * filp,void __user * arg)421 static inline int fscrypt_ioctl_get_key_status(struct file *filp,
422 					       void __user *arg)
423 {
424 	return -EOPNOTSUPP;
425 }
426 
427 /* keysetup.c */
fscrypt_get_encryption_info(struct inode * inode)428 static inline int fscrypt_get_encryption_info(struct inode *inode)
429 {
430 	return -EOPNOTSUPP;
431 }
432 
fscrypt_put_encryption_info(struct inode * inode)433 static inline void fscrypt_put_encryption_info(struct inode *inode)
434 {
435 	return;
436 }
437 
fscrypt_free_inode(struct inode * inode)438 static inline void fscrypt_free_inode(struct inode *inode)
439 {
440 }
441 
fscrypt_drop_inode(struct inode * inode)442 static inline int fscrypt_drop_inode(struct inode *inode)
443 {
444 	return 0;
445 }
446 
447  /* fname.c */
fscrypt_setup_filename(struct inode * dir,const struct qstr * iname,int lookup,struct fscrypt_name * fname)448 static inline int fscrypt_setup_filename(struct inode *dir,
449 					 const struct qstr *iname,
450 					 int lookup, struct fscrypt_name *fname)
451 {
452 	if (IS_ENCRYPTED(dir))
453 		return -EOPNOTSUPP;
454 
455 	memset(fname, 0, sizeof(*fname));
456 	fname->usr_fname = iname;
457 	fname->disk_name.name = (unsigned char *)iname->name;
458 	fname->disk_name.len = iname->len;
459 	return 0;
460 }
461 
fscrypt_free_filename(struct fscrypt_name * fname)462 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
463 {
464 	return;
465 }
466 
fscrypt_fname_alloc_buffer(const struct inode * inode,u32 max_encrypted_len,struct fscrypt_str * crypto_str)467 static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
468 					     u32 max_encrypted_len,
469 					     struct fscrypt_str *crypto_str)
470 {
471 	return -EOPNOTSUPP;
472 }
473 
fscrypt_fname_free_buffer(struct fscrypt_str * crypto_str)474 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
475 {
476 	return;
477 }
478 
fscrypt_fname_disk_to_usr(const struct inode * inode,u32 hash,u32 minor_hash,const struct fscrypt_str * iname,struct fscrypt_str * oname)479 static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
480 					    u32 hash, u32 minor_hash,
481 					    const struct fscrypt_str *iname,
482 					    struct fscrypt_str *oname)
483 {
484 	return -EOPNOTSUPP;
485 }
486 
fscrypt_match_name(const struct fscrypt_name * fname,const u8 * de_name,u32 de_name_len)487 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
488 				      const u8 *de_name, u32 de_name_len)
489 {
490 	/* Encryption support disabled; use standard comparison */
491 	if (de_name_len != fname->disk_name.len)
492 		return false;
493 	return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
494 }
495 
fscrypt_fname_siphash(const struct inode * dir,const struct qstr * name)496 static inline u64 fscrypt_fname_siphash(const struct inode *dir,
497 					const struct qstr *name)
498 {
499 	WARN_ON_ONCE(1);
500 	return 0;
501 }
502 
503 /* bio.c */
fscrypt_decrypt_bio(struct bio * bio)504 static inline void fscrypt_decrypt_bio(struct bio *bio)
505 {
506 }
507 
fscrypt_zeroout_range(const struct inode * inode,pgoff_t lblk,sector_t pblk,unsigned int len)508 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
509 					sector_t pblk, unsigned int len)
510 {
511 	return -EOPNOTSUPP;
512 }
513 
514 /* hooks.c */
515 
fscrypt_file_open(struct inode * inode,struct file * filp)516 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
517 {
518 	if (IS_ENCRYPTED(inode))
519 		return -EOPNOTSUPP;
520 	return 0;
521 }
522 
__fscrypt_prepare_link(struct inode * inode,struct inode * dir,struct dentry * dentry)523 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
524 					 struct dentry *dentry)
525 {
526 	return -EOPNOTSUPP;
527 }
528 
__fscrypt_prepare_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)529 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
530 					   struct dentry *old_dentry,
531 					   struct inode *new_dir,
532 					   struct dentry *new_dentry,
533 					   unsigned int flags)
534 {
535 	return -EOPNOTSUPP;
536 }
537 
__fscrypt_prepare_lookup(struct inode * dir,struct dentry * dentry,struct fscrypt_name * fname)538 static inline int __fscrypt_prepare_lookup(struct inode *dir,
539 					   struct dentry *dentry,
540 					   struct fscrypt_name *fname)
541 {
542 	return -EOPNOTSUPP;
543 }
544 
fscrypt_prepare_setflags(struct inode * inode,unsigned int oldflags,unsigned int flags)545 static inline int fscrypt_prepare_setflags(struct inode *inode,
546 					   unsigned int oldflags,
547 					   unsigned int flags)
548 {
549 	return 0;
550 }
551 
__fscrypt_prepare_symlink(struct inode * dir,unsigned int len,unsigned int max_len,struct fscrypt_str * disk_link)552 static inline int __fscrypt_prepare_symlink(struct inode *dir,
553 					    unsigned int len,
554 					    unsigned int max_len,
555 					    struct fscrypt_str *disk_link)
556 {
557 	return -EOPNOTSUPP;
558 }
559 
560 
__fscrypt_encrypt_symlink(struct inode * inode,const char * target,unsigned int len,struct fscrypt_str * disk_link)561 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
562 					    const char *target,
563 					    unsigned int len,
564 					    struct fscrypt_str *disk_link)
565 {
566 	return -EOPNOTSUPP;
567 }
568 
fscrypt_get_symlink(struct inode * inode,const void * caddr,unsigned int max_size,struct delayed_call * done)569 static inline const char *fscrypt_get_symlink(struct inode *inode,
570 					      const void *caddr,
571 					      unsigned int max_size,
572 					      struct delayed_call *done)
573 {
574 	return ERR_PTR(-EOPNOTSUPP);
575 }
576 
fscrypt_symlink_getattr(const struct path * path,struct kstat * stat)577 static inline int fscrypt_symlink_getattr(const struct path *path,
578 					  struct kstat *stat)
579 {
580 	return -EOPNOTSUPP;
581 }
582 
fscrypt_set_ops(struct super_block * sb,const struct fscrypt_operations * s_cop)583 static inline void fscrypt_set_ops(struct super_block *sb,
584 				   const struct fscrypt_operations *s_cop)
585 {
586 }
587 
588 #endif	/* !CONFIG_FS_ENCRYPTION */
589 
590 /* inline_crypt.c */
591 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
592 extern bool fscrypt_inode_uses_inline_crypto(const struct inode *inode);
593 
594 extern bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode);
595 
596 extern void fscrypt_set_bio_crypt_ctx(struct bio *bio,
597 				      const struct inode *inode,
598 				      u64 first_lblk, gfp_t gfp_mask);
599 
600 extern void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
601 					 const struct buffer_head *first_bh,
602 					 gfp_t gfp_mask);
603 
604 extern bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
605 				  u64 next_lblk);
606 
607 extern bool fscrypt_mergeable_bio_bh(struct bio *bio,
608 				     const struct buffer_head *next_bh);
609 
610 bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter);
611 
612 int fscrypt_limit_dio_pages(const struct inode *inode, loff_t pos,
613 			    int nr_pages);
614 
615 #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
fscrypt_inode_uses_inline_crypto(const struct inode * inode)616 static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
617 {
618 	return false;
619 }
620 
fscrypt_inode_uses_fs_layer_crypto(const struct inode * inode)621 static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
622 {
623 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
624 }
625 
fscrypt_set_bio_crypt_ctx(struct bio * bio,const struct inode * inode,u64 first_lblk,gfp_t gfp_mask)626 static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
627 					     const struct inode *inode,
628 					     u64 first_lblk, gfp_t gfp_mask) { }
629 
fscrypt_set_bio_crypt_ctx_bh(struct bio * bio,const struct buffer_head * first_bh,gfp_t gfp_mask)630 static inline void fscrypt_set_bio_crypt_ctx_bh(
631 					 struct bio *bio,
632 					 const struct buffer_head *first_bh,
633 					 gfp_t gfp_mask) { }
634 
fscrypt_mergeable_bio(struct bio * bio,const struct inode * inode,u64 next_lblk)635 static inline bool fscrypt_mergeable_bio(struct bio *bio,
636 					 const struct inode *inode,
637 					 u64 next_lblk)
638 {
639 	return true;
640 }
641 
fscrypt_mergeable_bio_bh(struct bio * bio,const struct buffer_head * next_bh)642 static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
643 					    const struct buffer_head *next_bh)
644 {
645 	return true;
646 }
647 
fscrypt_dio_supported(struct kiocb * iocb,struct iov_iter * iter)648 static inline bool fscrypt_dio_supported(struct kiocb *iocb,
649 					 struct iov_iter *iter)
650 {
651 	const struct inode *inode = file_inode(iocb->ki_filp);
652 
653 	return !fscrypt_needs_contents_encryption(inode);
654 }
655 
fscrypt_limit_dio_pages(const struct inode * inode,loff_t pos,int nr_pages)656 static inline int fscrypt_limit_dio_pages(const struct inode *inode, loff_t pos,
657 					  int nr_pages)
658 {
659 	return nr_pages;
660 }
661 #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
662 
663 #if IS_ENABLED(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_DM_DEFAULT_KEY)
664 static inline bool
fscrypt_inode_should_skip_dm_default_key(const struct inode * inode)665 fscrypt_inode_should_skip_dm_default_key(const struct inode *inode)
666 {
667 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
668 }
669 #else
670 static inline bool
fscrypt_inode_should_skip_dm_default_key(const struct inode * inode)671 fscrypt_inode_should_skip_dm_default_key(const struct inode *inode)
672 {
673 	return false;
674 }
675 #endif
676 
677 /**
678  * fscrypt_require_key() - require an inode's encryption key
679  * @inode: the inode we need the key for
680  *
681  * If the inode is encrypted, set up its encryption key if not already done.
682  * Then require that the key be present and return -ENOKEY otherwise.
683  *
684  * No locks are needed, and the key will live as long as the struct inode --- so
685  * it won't go away from under you.
686  *
687  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
688  * if a problem occurred while setting up the encryption key.
689  */
fscrypt_require_key(struct inode * inode)690 static inline int fscrypt_require_key(struct inode *inode)
691 {
692 	if (IS_ENCRYPTED(inode)) {
693 		int err = fscrypt_get_encryption_info(inode);
694 
695 		if (err)
696 			return err;
697 		if (!fscrypt_has_encryption_key(inode))
698 			return -ENOKEY;
699 	}
700 	return 0;
701 }
702 
703 /**
704  * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
705  *			    directory
706  * @old_dentry: an existing dentry for the inode being linked
707  * @dir: the target directory
708  * @dentry: negative dentry for the target filename
709  *
710  * A new link can only be added to an encrypted directory if the directory's
711  * encryption key is available --- since otherwise we'd have no way to encrypt
712  * the filename.  Therefore, we first set up the directory's encryption key (if
713  * not already done) and return an error if it's unavailable.
714  *
715  * We also verify that the link will not violate the constraint that all files
716  * in an encrypted directory tree use the same encryption policy.
717  *
718  * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
719  * -EXDEV if the link would result in an inconsistent encryption policy, or
720  * another -errno code.
721  */
fscrypt_prepare_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)722 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
723 				       struct inode *dir,
724 				       struct dentry *dentry)
725 {
726 	if (IS_ENCRYPTED(dir))
727 		return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
728 	return 0;
729 }
730 
731 /**
732  * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
733  *			      directories
734  * @old_dir: source directory
735  * @old_dentry: dentry for source file
736  * @new_dir: target directory
737  * @new_dentry: dentry for target location (may be negative unless exchanging)
738  * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
739  *
740  * Prepare for ->rename() where the source and/or target directories may be
741  * encrypted.  A new link can only be added to an encrypted directory if the
742  * directory's encryption key is available --- since otherwise we'd have no way
743  * to encrypt the filename.  A rename to an existing name, on the other hand,
744  * *is* cryptographically possible without the key.  However, we take the more
745  * conservative approach and just forbid all no-key renames.
746  *
747  * We also verify that the rename will not violate the constraint that all files
748  * in an encrypted directory tree use the same encryption policy.
749  *
750  * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
751  * rename would cause inconsistent encryption policies, or another -errno code.
752  */
fscrypt_prepare_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)753 static inline int fscrypt_prepare_rename(struct inode *old_dir,
754 					 struct dentry *old_dentry,
755 					 struct inode *new_dir,
756 					 struct dentry *new_dentry,
757 					 unsigned int flags)
758 {
759 	if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
760 		return __fscrypt_prepare_rename(old_dir, old_dentry,
761 						new_dir, new_dentry, flags);
762 	return 0;
763 }
764 
765 /**
766  * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
767  *			      directory
768  * @dir: directory being searched
769  * @dentry: filename being looked up
770  * @fname: (output) the name to use to search the on-disk directory
771  *
772  * Prepare for ->lookup() in a directory which may be encrypted by determining
773  * the name that will actually be used to search the directory on-disk.  Lookups
774  * can be done with or without the directory's encryption key; without the key,
775  * filenames are presented in encrypted form.  Therefore, we'll try to set up
776  * the directory's encryption key, but even without it the lookup can continue.
777  *
778  * After calling this function, a filesystem should ensure that it's dentry
779  * operations contain fscrypt_d_revalidate if DCACHE_ENCRYPTED_NAME was set,
780  * so that the dentry can be invalidated if the key is later added.
781  *
782  * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
783  * correctly formed encoded ciphertext name, so a negative dentry should be
784  * created; or another -errno code.
785  */
fscrypt_prepare_lookup(struct inode * dir,struct dentry * dentry,struct fscrypt_name * fname)786 static inline int fscrypt_prepare_lookup(struct inode *dir,
787 					 struct dentry *dentry,
788 					 struct fscrypt_name *fname)
789 {
790 	if (IS_ENCRYPTED(dir))
791 		return __fscrypt_prepare_lookup(dir, dentry, fname);
792 
793 	memset(fname, 0, sizeof(*fname));
794 	fname->usr_fname = &dentry->d_name;
795 	fname->disk_name.name = (unsigned char *)dentry->d_name.name;
796 	fname->disk_name.len = dentry->d_name.len;
797 	return 0;
798 }
799 
800 /**
801  * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
802  *			       attributes
803  * @dentry: dentry through which the inode is being changed
804  * @attr: attributes to change
805  *
806  * Prepare for ->setattr() on a possibly-encrypted inode.  On an encrypted file,
807  * most attribute changes are allowed even without the encryption key.  However,
808  * without the encryption key we do have to forbid truncates.  This is needed
809  * because the size being truncated to may not be a multiple of the filesystem
810  * block size, and in that case we'd have to decrypt the final block, zero the
811  * portion past i_size, and re-encrypt it.  (We *could* allow truncating to a
812  * filesystem block boundary, but it's simpler to just forbid all truncates ---
813  * and we already forbid all other contents modifications without the key.)
814  *
815  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
816  * if a problem occurred while setting up the encryption key.
817  */
fscrypt_prepare_setattr(struct dentry * dentry,struct iattr * attr)818 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
819 					  struct iattr *attr)
820 {
821 	if (attr->ia_valid & ATTR_SIZE)
822 		return fscrypt_require_key(d_inode(dentry));
823 	return 0;
824 }
825 
826 /**
827  * fscrypt_prepare_symlink() - prepare to create a possibly-encrypted symlink
828  * @dir: directory in which the symlink is being created
829  * @target: plaintext symlink target
830  * @len: length of @target excluding null terminator
831  * @max_len: space the filesystem has available to store the symlink target
832  * @disk_link: (out) the on-disk symlink target being prepared
833  *
834  * This function computes the size the symlink target will require on-disk,
835  * stores it in @disk_link->len, and validates it against @max_len.  An
836  * encrypted symlink may be longer than the original.
837  *
838  * Additionally, @disk_link->name is set to @target if the symlink will be
839  * unencrypted, but left NULL if the symlink will be encrypted.  For encrypted
840  * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
841  * on-disk target later.  (The reason for the two-step process is that some
842  * filesystems need to know the size of the symlink target before creating the
843  * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
844  *
845  * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
846  * -ENOKEY if the encryption key is missing, or another -errno code if a problem
847  * occurred while setting up the encryption key.
848  */
849 #ifdef FSCRYPT_NEED_OPS
fscrypt_prepare_symlink(struct inode * dir,const char * target,unsigned int len,unsigned int max_len,struct fscrypt_str * disk_link)850 static inline int fscrypt_prepare_symlink(struct inode *dir,
851 					  const char *target,
852 					  unsigned int len,
853 					  unsigned int max_len,
854 					  struct fscrypt_str *disk_link)
855 {
856 	if (IS_ENCRYPTED(dir) || fscrypt_get_dummy_context(dir->i_sb) != NULL)
857 		return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
858 
859 	disk_link->name = (unsigned char *)target;
860 	disk_link->len = len + 1;
861 	if (disk_link->len > max_len)
862 		return -ENAMETOOLONG;
863 	return 0;
864 }
865 #endif
866 
867 /**
868  * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
869  * @inode: symlink inode
870  * @target: plaintext symlink target
871  * @len: length of @target excluding null terminator
872  * @disk_link: (in/out) the on-disk symlink target being prepared
873  *
874  * If the symlink target needs to be encrypted, then this function encrypts it
875  * into @disk_link->name.  fscrypt_prepare_symlink() must have been called
876  * previously to compute @disk_link->len.  If the filesystem did not allocate a
877  * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
878  * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
879  *
880  * Return: 0 on success, -errno on failure
881  */
fscrypt_encrypt_symlink(struct inode * inode,const char * target,unsigned int len,struct fscrypt_str * disk_link)882 static inline int fscrypt_encrypt_symlink(struct inode *inode,
883 					  const char *target,
884 					  unsigned int len,
885 					  struct fscrypt_str *disk_link)
886 {
887 	if (IS_ENCRYPTED(inode))
888 		return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
889 	return 0;
890 }
891 
892 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
fscrypt_finalize_bounce_page(struct page ** pagep)893 static inline void fscrypt_finalize_bounce_page(struct page **pagep)
894 {
895 	struct page *page = *pagep;
896 
897 	if (fscrypt_is_bounce_page(page)) {
898 		*pagep = fscrypt_pagecache_page(page);
899 		fscrypt_free_bounce_page(page);
900 	}
901 }
902 
903 #endif	/* _LINUX_FSCRYPT_H */
904