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