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