1
2
3
4
5
6
7
8
9
10
11
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
23union fscrypt_policy;
24struct fscrypt_info;
25struct seq_file;
26
27struct fscrypt_str {
28 unsigned char *name;
29 u32 len;
30};
31
32struct fscrypt_name {
33 const struct qstr *usr_fname;
34 struct fscrypt_str disk_name;
35 u32 hash;
36 u32 minor_hash;
37 struct fscrypt_str crypto_buf;
38 bool is_nokey_name;
39};
40
41#define FSTR_INIT(n, l) { .name = n, .len = l }
42#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
43#define fname_name(p) ((p)->disk_name.name)
44#define fname_len(p) ((p)->disk_name.len)
45
46
47#define FSCRYPT_SET_CONTEXT_MAX_SIZE 40
48
49#ifdef CONFIG_FS_ENCRYPTION
50
51
52
53#define FS_CFLG_OWN_PAGES (1U << 1)
54
55
56
57
58struct fscrypt_operations {
59 unsigned int flags;
60 const char *key_prefix;
61 int (*get_context)(struct inode *inode, void *ctx, size_t len);
62 int (*set_context)(struct inode *inode, const void *ctx, size_t len,
63 void *fs_data);
64 const union fscrypt_policy *(*get_dummy_policy)(struct super_block *sb);
65 bool (*empty_dir)(struct inode *inode);
66 unsigned int max_namelen;
67 bool (*has_stable_inodes)(struct super_block *sb);
68 void (*get_ino_and_lblk_bits)(struct super_block *sb,
69 int *ino_bits_ret, int *lblk_bits_ret);
70 int (*get_num_devices)(struct super_block *sb);
71 void (*get_devices)(struct super_block *sb,
72 struct request_queue **devs);
73};
74
75static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
76{
77
78
79
80
81
82
83 return smp_load_acquire(&inode->i_crypt_info);
84}
85
86
87
88
89
90
91
92
93
94
95
96
97static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
98{
99 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
100}
101
102
103
104
105
106
107
108
109static inline void fscrypt_handle_d_move(struct dentry *dentry)
110{
111 dentry->d_flags &= ~DCACHE_NOKEY_NAME;
112}
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
139{
140 return dentry->d_flags & DCACHE_NOKEY_NAME;
141}
142
143
144void fscrypt_enqueue_decrypt_work(struct work_struct *);
145
146struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
147 unsigned int len,
148 unsigned int offs,
149 gfp_t gfp_flags);
150int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
151 unsigned int len, unsigned int offs,
152 u64 lblk_num, gfp_t gfp_flags);
153
154int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
155 unsigned int offs);
156int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
157 unsigned int len, unsigned int offs,
158 u64 lblk_num);
159
160static inline bool fscrypt_is_bounce_page(struct page *page)
161{
162 return page->mapping == NULL;
163}
164
165static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
166{
167 return (struct page *)page_private(bounce_page);
168}
169
170void fscrypt_free_bounce_page(struct page *bounce_page);
171
172
173int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
174int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
175int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
176int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
177int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
178int fscrypt_set_context(struct inode *inode, void *fs_data);
179
180struct fscrypt_dummy_policy {
181 const union fscrypt_policy *policy;
182};
183
184int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
185 struct fscrypt_dummy_policy *dummy_policy);
186void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
187 struct super_block *sb);
188static inline void
189fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
190{
191 kfree(dummy_policy->policy);
192 dummy_policy->policy = NULL;
193}
194
195
196void fscrypt_sb_free(struct super_block *sb);
197int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
198int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
199int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
200int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
201
202
203int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
204 bool *encrypt_ret);
205void fscrypt_put_encryption_info(struct inode *inode);
206void fscrypt_free_inode(struct inode *inode);
207int fscrypt_drop_inode(struct inode *inode);
208
209
210int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
211 int lookup, struct fscrypt_name *fname);
212
213static inline void fscrypt_free_filename(struct fscrypt_name *fname)
214{
215 kfree(fname->crypto_buf.name);
216}
217
218int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
219 struct fscrypt_str *crypto_str);
220void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
221int fscrypt_fname_disk_to_usr(const struct inode *inode,
222 u32 hash, u32 minor_hash,
223 const struct fscrypt_str *iname,
224 struct fscrypt_str *oname);
225bool fscrypt_match_name(const struct fscrypt_name *fname,
226 const u8 *de_name, u32 de_name_len);
227u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
228int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags);
229
230
231void fscrypt_decrypt_bio(struct bio *bio);
232int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
233 sector_t pblk, unsigned int len);
234
235
236int fscrypt_file_open(struct inode *inode, struct file *filp);
237int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
238 struct dentry *dentry);
239int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
240 struct inode *new_dir, struct dentry *new_dentry,
241 unsigned int flags);
242int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
243 struct fscrypt_name *fname);
244int __fscrypt_prepare_readdir(struct inode *dir);
245int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr);
246int fscrypt_prepare_setflags(struct inode *inode,
247 unsigned int oldflags, unsigned int flags);
248int fscrypt_prepare_symlink(struct inode *dir, const char *target,
249 unsigned int len, unsigned int max_len,
250 struct fscrypt_str *disk_link);
251int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
252 unsigned int len, struct fscrypt_str *disk_link);
253const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
254 unsigned int max_size,
255 struct delayed_call *done);
256static inline void fscrypt_set_ops(struct super_block *sb,
257 const struct fscrypt_operations *s_cop)
258{
259 sb->s_cop = s_cop;
260}
261#else
262
263static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
264{
265 return NULL;
266}
267
268static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
269{
270 return false;
271}
272
273static inline void fscrypt_handle_d_move(struct dentry *dentry)
274{
275}
276
277static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
278{
279 return false;
280}
281
282
283static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
284{
285}
286
287static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
288 unsigned int len,
289 unsigned int offs,
290 gfp_t gfp_flags)
291{
292 return ERR_PTR(-EOPNOTSUPP);
293}
294
295static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
296 struct page *page,
297 unsigned int len,
298 unsigned int offs, u64 lblk_num,
299 gfp_t gfp_flags)
300{
301 return -EOPNOTSUPP;
302}
303
304static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
305 unsigned int len,
306 unsigned int offs)
307{
308 return -EOPNOTSUPP;
309}
310
311static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
312 struct page *page,
313 unsigned int len,
314 unsigned int offs, u64 lblk_num)
315{
316 return -EOPNOTSUPP;
317}
318
319static inline bool fscrypt_is_bounce_page(struct page *page)
320{
321 return false;
322}
323
324static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
325{
326 WARN_ON_ONCE(1);
327 return ERR_PTR(-EINVAL);
328}
329
330static inline void fscrypt_free_bounce_page(struct page *bounce_page)
331{
332}
333
334
335static inline int fscrypt_ioctl_set_policy(struct file *filp,
336 const void __user *arg)
337{
338 return -EOPNOTSUPP;
339}
340
341static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
342{
343 return -EOPNOTSUPP;
344}
345
346static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
347 void __user *arg)
348{
349 return -EOPNOTSUPP;
350}
351
352static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
353{
354 return -EOPNOTSUPP;
355}
356
357static inline int fscrypt_has_permitted_context(struct inode *parent,
358 struct inode *child)
359{
360 return 0;
361}
362
363static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
364{
365 return -EOPNOTSUPP;
366}
367
368struct fscrypt_dummy_policy {
369};
370
371static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
372 char sep,
373 struct super_block *sb)
374{
375}
376
377static inline void
378fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
379{
380}
381
382
383static inline void fscrypt_sb_free(struct super_block *sb)
384{
385}
386
387static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
388{
389 return -EOPNOTSUPP;
390}
391
392static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
393{
394 return -EOPNOTSUPP;
395}
396
397static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
398 void __user *arg)
399{
400 return -EOPNOTSUPP;
401}
402
403static inline int fscrypt_ioctl_get_key_status(struct file *filp,
404 void __user *arg)
405{
406 return -EOPNOTSUPP;
407}
408
409
410
411static inline int fscrypt_prepare_new_inode(struct inode *dir,
412 struct inode *inode,
413 bool *encrypt_ret)
414{
415 if (IS_ENCRYPTED(dir))
416 return -EOPNOTSUPP;
417 return 0;
418}
419
420static inline void fscrypt_put_encryption_info(struct inode *inode)
421{
422 return;
423}
424
425static inline void fscrypt_free_inode(struct inode *inode)
426{
427}
428
429static inline int fscrypt_drop_inode(struct inode *inode)
430{
431 return 0;
432}
433
434
435static inline int fscrypt_setup_filename(struct inode *dir,
436 const struct qstr *iname,
437 int lookup, struct fscrypt_name *fname)
438{
439 if (IS_ENCRYPTED(dir))
440 return -EOPNOTSUPP;
441
442 memset(fname, 0, sizeof(*fname));
443 fname->usr_fname = iname;
444 fname->disk_name.name = (unsigned char *)iname->name;
445 fname->disk_name.len = iname->len;
446 return 0;
447}
448
449static inline void fscrypt_free_filename(struct fscrypt_name *fname)
450{
451 return;
452}
453
454static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
455 struct fscrypt_str *crypto_str)
456{
457 return -EOPNOTSUPP;
458}
459
460static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
461{
462 return;
463}
464
465static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
466 u32 hash, u32 minor_hash,
467 const struct fscrypt_str *iname,
468 struct fscrypt_str *oname)
469{
470 return -EOPNOTSUPP;
471}
472
473static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
474 const u8 *de_name, u32 de_name_len)
475{
476
477 if (de_name_len != fname->disk_name.len)
478 return false;
479 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
480}
481
482static inline u64 fscrypt_fname_siphash(const struct inode *dir,
483 const struct qstr *name)
484{
485 WARN_ON_ONCE(1);
486 return 0;
487}
488
489static inline int fscrypt_d_revalidate(struct dentry *dentry,
490 unsigned int flags)
491{
492 return 1;
493}
494
495
496static inline void fscrypt_decrypt_bio(struct bio *bio)
497{
498}
499
500static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
501 sector_t pblk, unsigned int len)
502{
503 return -EOPNOTSUPP;
504}
505
506
507
508static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
509{
510 if (IS_ENCRYPTED(inode))
511 return -EOPNOTSUPP;
512 return 0;
513}
514
515static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
516 struct dentry *dentry)
517{
518 return -EOPNOTSUPP;
519}
520
521static inline int __fscrypt_prepare_rename(struct inode *old_dir,
522 struct dentry *old_dentry,
523 struct inode *new_dir,
524 struct dentry *new_dentry,
525 unsigned int flags)
526{
527 return -EOPNOTSUPP;
528}
529
530static inline int __fscrypt_prepare_lookup(struct inode *dir,
531 struct dentry *dentry,
532 struct fscrypt_name *fname)
533{
534 return -EOPNOTSUPP;
535}
536
537static inline int __fscrypt_prepare_readdir(struct inode *dir)
538{
539 return -EOPNOTSUPP;
540}
541
542static inline int __fscrypt_prepare_setattr(struct dentry *dentry,
543 struct iattr *attr)
544{
545 return -EOPNOTSUPP;
546}
547
548static inline int fscrypt_prepare_setflags(struct inode *inode,
549 unsigned int oldflags,
550 unsigned int flags)
551{
552 return 0;
553}
554
555static inline int fscrypt_prepare_symlink(struct inode *dir,
556 const char *target,
557 unsigned int len,
558 unsigned int max_len,
559 struct fscrypt_str *disk_link)
560{
561 if (IS_ENCRYPTED(dir))
562 return -EOPNOTSUPP;
563 disk_link->name = (unsigned char *)target;
564 disk_link->len = len + 1;
565 if (disk_link->len > max_len)
566 return -ENAMETOOLONG;
567 return 0;
568}
569
570static inline int __fscrypt_encrypt_symlink(struct inode *inode,
571 const char *target,
572 unsigned int len,
573 struct fscrypt_str *disk_link)
574{
575 return -EOPNOTSUPP;
576}
577
578static inline const char *fscrypt_get_symlink(struct inode *inode,
579 const void *caddr,
580 unsigned int max_size,
581 struct delayed_call *done)
582{
583 return ERR_PTR(-EOPNOTSUPP);
584}
585
586static inline void fscrypt_set_ops(struct super_block *sb,
587 const struct fscrypt_operations *s_cop)
588{
589}
590
591#endif
592
593
594#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
595
596bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
597
598void fscrypt_set_bio_crypt_ctx(struct bio *bio,
599 const struct inode *inode, u64 first_lblk,
600 gfp_t gfp_mask);
601
602void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
603 const struct buffer_head *first_bh,
604 gfp_t gfp_mask);
605
606bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
607 u64 next_lblk);
608
609bool fscrypt_mergeable_bio_bh(struct bio *bio,
610 const struct buffer_head *next_bh);
611
612#else
613
614static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
615{
616 return false;
617}
618
619static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
620 const struct inode *inode,
621 u64 first_lblk, gfp_t gfp_mask) { }
622
623static inline void fscrypt_set_bio_crypt_ctx_bh(
624 struct bio *bio,
625 const struct buffer_head *first_bh,
626 gfp_t gfp_mask) { }
627
628static inline bool fscrypt_mergeable_bio(struct bio *bio,
629 const struct inode *inode,
630 u64 next_lblk)
631{
632 return true;
633}
634
635static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
636 const struct buffer_head *next_bh)
637{
638 return true;
639}
640#endif
641
642
643
644
645
646
647
648
649
650
651static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
652{
653 return fscrypt_needs_contents_encryption(inode) &&
654 __fscrypt_inode_uses_inline_crypto(inode);
655}
656
657
658
659
660
661
662
663
664
665
666static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
667{
668 return fscrypt_needs_contents_encryption(inode) &&
669 !__fscrypt_inode_uses_inline_crypto(inode);
670}
671
672
673
674
675
676
677
678
679
680
681static inline bool fscrypt_has_encryption_key(const struct inode *inode)
682{
683 return fscrypt_get_info(inode) != NULL;
684}
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704static inline int fscrypt_prepare_link(struct dentry *old_dentry,
705 struct inode *dir,
706 struct dentry *dentry)
707{
708 if (IS_ENCRYPTED(dir))
709 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
710 return 0;
711}
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735static inline int fscrypt_prepare_rename(struct inode *old_dir,
736 struct dentry *old_dentry,
737 struct inode *new_dir,
738 struct dentry *new_dentry,
739 unsigned int flags)
740{
741 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
742 return __fscrypt_prepare_rename(old_dir, old_dentry,
743 new_dir, new_dentry, flags);
744 return 0;
745}
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770static inline int fscrypt_prepare_lookup(struct inode *dir,
771 struct dentry *dentry,
772 struct fscrypt_name *fname)
773{
774 if (IS_ENCRYPTED(dir))
775 return __fscrypt_prepare_lookup(dir, dentry, fname);
776
777 memset(fname, 0, sizeof(*fname));
778 fname->usr_fname = &dentry->d_name;
779 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
780 fname->disk_name.len = dentry->d_name.len;
781 return 0;
782}
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797static inline int fscrypt_prepare_readdir(struct inode *dir)
798{
799 if (IS_ENCRYPTED(dir))
800 return __fscrypt_prepare_readdir(dir);
801 return 0;
802}
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822static inline int fscrypt_prepare_setattr(struct dentry *dentry,
823 struct iattr *attr)
824{
825 if (IS_ENCRYPTED(d_inode(dentry)))
826 return __fscrypt_prepare_setattr(dentry, attr);
827 return 0;
828}
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845static inline int fscrypt_encrypt_symlink(struct inode *inode,
846 const char *target,
847 unsigned int len,
848 struct fscrypt_str *disk_link)
849{
850 if (IS_ENCRYPTED(inode))
851 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
852 return 0;
853}
854
855
856static inline void fscrypt_finalize_bounce_page(struct page **pagep)
857{
858 struct page *page = *pagep;
859
860 if (fscrypt_is_bounce_page(page)) {
861 *pagep = fscrypt_pagecache_page(page);
862 fscrypt_free_bounce_page(page);
863 }
864}
865
866#endif
867